导出优化
This commit is contained in:
@@ -30,7 +30,7 @@ class PurchaseSupplierSheet implements FromCollection, WithStrictNullComparison,
|
||||
|
||||
private ?Collection $rows = null;
|
||||
|
||||
/** @var array<int, string> 特殊行索引(1 起)=> 类型(title/header/summary) */
|
||||
/** @var array<int, string> 特殊行索引(1 起)=> 类型(title/header) */
|
||||
private array $specialRows = [];
|
||||
|
||||
/** 列头所在行索引 */
|
||||
@@ -42,9 +42,6 @@ class PurchaseSupplierSheet implements FromCollection, WithStrictNullComparison,
|
||||
/** @var array<int, array{quantity: int, store_quantities: array<int, int>}> 明细行索引 => 填色判断数据 */
|
||||
private array $rowData = [];
|
||||
|
||||
/** @var array{quantity: int, stores: array<int, int>} 合计行填色判断数据 */
|
||||
private array $summaryTotals = ['quantity' => 0, 'stores' => []];
|
||||
|
||||
/**
|
||||
* @param PurchaseOrderModel $purchase 采购单
|
||||
* @param SupplierModel $supplier 供应商
|
||||
@@ -64,7 +61,7 @@ class PurchaseSupplierSheet implements FromCollection, WithStrictNullComparison,
|
||||
}
|
||||
|
||||
/**
|
||||
* 导出行:标题/空行/列头(品名、汇总、市场、各门店)/明细/合计
|
||||
* 导出行:标题/空行/列头(品名、汇总、市场、各门店)/明细
|
||||
*/
|
||||
public function collection(): Collection
|
||||
{
|
||||
@@ -91,8 +88,6 @@ class PurchaseSupplierSheet implements FromCollection, WithStrictNullComparison,
|
||||
$this->headerRow = $rowIndex;
|
||||
|
||||
// 明细行(0 数量的门店格留空不填色)
|
||||
$totalQuantity = 0;
|
||||
$storeTotals = array_fill_keys($storeIds, 0);
|
||||
foreach ($this->productRows as $productRow) {
|
||||
$line = [
|
||||
$productRow['product_name'],
|
||||
@@ -104,24 +99,14 @@ class PurchaseSupplierSheet implements FromCollection, WithStrictNullComparison,
|
||||
$quantity = (int) ($productRow['store_quantities'][$storeId] ?? 0);
|
||||
$line[] = $quantity > 0 ? $quantity : '';
|
||||
$storeQuantities[$storeId] = $quantity;
|
||||
$storeTotals[$storeId] += $quantity;
|
||||
}
|
||||
$rows[] = $line;
|
||||
$this->rowData[++$rowIndex] = [
|
||||
'quantity' => (int) $productRow['quantity'],
|
||||
'store_quantities' => $storeQuantities,
|
||||
];
|
||||
$totalQuantity += (int) $productRow['quantity'];
|
||||
}
|
||||
|
||||
// 合计行
|
||||
$rows[] = array_merge(
|
||||
['合计', $totalQuantity, ''],
|
||||
array_map(static fn (int $storeId): int => $storeTotals[$storeId], $storeIds),
|
||||
);
|
||||
$this->specialRows[++$rowIndex] = 'summary';
|
||||
$this->lastRow = $rowIndex;
|
||||
$this->summaryTotals = ['quantity' => $totalQuantity, 'stores' => $storeTotals];
|
||||
|
||||
return $this->rows = collect($rows);
|
||||
}
|
||||
@@ -132,7 +117,7 @@ class PurchaseSupplierSheet implements FromCollection, WithStrictNullComparison,
|
||||
}
|
||||
|
||||
/**
|
||||
* 标题/列头/合计加粗;全表居中 + 全边框;
|
||||
* 标题/列头加粗;全表居中 + 全边框;
|
||||
* 汇总列(浅黄)/门店数量列(浅蓝)按值条件填色(0 不填、列头固定填色),冻结列头
|
||||
*/
|
||||
public function styles(Worksheet $sheet): array
|
||||
@@ -157,7 +142,7 @@ class PurchaseSupplierSheet implements FromCollection, WithStrictNullComparison,
|
||||
->setHorizontal(Alignment::HORIZONTAL_CENTER)
|
||||
->setVertical(Alignment::VERTICAL_CENTER);
|
||||
|
||||
// 表格区域(列头 → 合计行)添加所有边框
|
||||
// 表格区域(列头 → 数据末行)添加所有边框
|
||||
$sheet->getStyle('A' . $this->headerRow . ':' . $lastColumn . $this->lastRow)
|
||||
->getBorders()
|
||||
->getAllBorders()
|
||||
@@ -170,9 +155,6 @@ class PurchaseSupplierSheet implements FromCollection, WithStrictNullComparison,
|
||||
$this->fillCell($sheet, 'B' . $row, self::QUANTITY_FILL);
|
||||
}
|
||||
}
|
||||
if ($this->summaryTotals['quantity'] > 0) {
|
||||
$this->fillCell($sheet, 'B' . $this->lastRow, self::QUANTITY_FILL);
|
||||
}
|
||||
|
||||
// 门店数量列(D 起):有数据浅蓝,0 无背景,列头固定浅蓝
|
||||
foreach ($storeIds as $i => $storeId) {
|
||||
@@ -183,16 +165,13 @@ class PurchaseSupplierSheet implements FromCollection, WithStrictNullComparison,
|
||||
$this->fillCell($sheet, $column . $row, self::STORE_FILL);
|
||||
}
|
||||
}
|
||||
if (($this->summaryTotals['stores'][$storeId] ?? 0) > 0) {
|
||||
$this->fillCell($sheet, $column . $this->lastRow, self::STORE_FILL);
|
||||
}
|
||||
}
|
||||
|
||||
$styles = [];
|
||||
foreach ($this->specialRows as $row => $type) {
|
||||
$style = match ($type) {
|
||||
'title' => ['font' => ['bold' => true, 'size' => 14]],
|
||||
'header', 'summary' => ['font' => ['bold' => true]],
|
||||
'header' => ['font' => ['bold' => true]],
|
||||
default => [],
|
||||
};
|
||||
if ($style !== []) {
|
||||
|
||||
Reference in New Issue
Block a user