导出优化

This commit is contained in:
liu
2026-09-05 14:03:23 +08:00
parent 17c1281073
commit 5c7a5829af
4 changed files with 64 additions and 35 deletions
+10 -4
View File
@@ -20,8 +20,9 @@ use PhpOffice\PhpSpreadsheet\Style\Fill;
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
/**
* 采购单商品明细导出:系统全部商品行(含本采购单无订货的商品,数量 0),
* 支持按供应商筛选;行尾合计 + 门店列合计;数量/金额/门店数量按值条件填色
* 采购单商品明细导出:系统全部未删除商品行(含本采购单无订货的商品,数量 0),
* 支持按供应商筛选;行尾合计 + 门店列合计;数量/金额/门店数量按值条件填色
* 序号/分类/包规/单位/成本/单价/实际称重/金额列在表格中默认隐藏(数据照常导出,Excel 中可取消隐藏)
*/
class PurchaseOrderExport implements FromCollection, WithStrictNullComparison, WithStyles
{
@@ -114,10 +115,10 @@ class PurchaseOrderExport implements FromCollection, WithStrictNullComparison, W
];
}
// 导出范围:系统全部上架商品 本采购单有订货的商品(含已删/下架
// 导出范围:系统全部未删除商品(含下架) 本采购单有订货的商品(含已删)
$products = ProductModel::withTrashed()
->with('category:id,sort')
->where('status', ProductModel::STATUS_ON)
->whereNull('deleted_at')
->orWhereIn('id', array_keys($itemGroups))
->get()
->keyBy('id');
@@ -267,6 +268,11 @@ class PurchaseOrderExport implements FromCollection, WithStrictNullComparison, W
$sheet->getColumnDimension(Coordinate::stringFromColumnIndex(13 + $i))->setWidth(12);
}
// 默认隐藏列:序号/分类/包规/单位/成本/单价/实际称重/金额(数据照常导出,Excel 中可取消隐藏)
foreach ([1, 2, 6, 7, 8, 9, 11, 12] as $hiddenIndex) {
$sheet->getColumnDimension(Coordinate::stringFromColumnIndex($hiddenIndex))->setVisible(false);
}
// 全部单元格水平/垂直居中
$sheet->getStyle('A1:' . $lastColumn . $this->lastRow)
->getAlignment()
+5 -26
View File
@@ -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 !== []) {