导出样式
This commit is contained in:
@@ -14,22 +14,40 @@ use Maatwebsite\Excel\Concerns\FromCollection;
|
||||
use Maatwebsite\Excel\Concerns\WithStrictNullComparison;
|
||||
use Maatwebsite\Excel\Concerns\WithStyles;
|
||||
use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
|
||||
use PhpOffice\PhpSpreadsheet\Style\Alignment;
|
||||
use PhpOffice\PhpSpreadsheet\Style\Border;
|
||||
use PhpOffice\PhpSpreadsheet\Style\Fill;
|
||||
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
|
||||
|
||||
/**
|
||||
* 采购单商品明细导出:系统全部商品行(含本采购单无订货的商品,数量 0),
|
||||
* 支持按供应商筛选;行尾合计 + 门店列合计;门店列整列填充突出颜色
|
||||
* 支持按供应商筛选;行尾合计 + 门店列合计;数量/金额/门店数量按值条件填色
|
||||
*/
|
||||
class PurchaseOrderExport implements FromCollection, WithStrictNullComparison, WithStyles
|
||||
{
|
||||
/** 门店列填充色(浅橙,突出显示) */
|
||||
private const string STORE_FILL = 'FFFFF7E6';
|
||||
/** 数量列填充色(浅黄) */
|
||||
private const string QUANTITY_FILL = 'FFFFF2CC';
|
||||
|
||||
/** 门店数量列填充色(浅蓝) */
|
||||
private const string STORE_FILL = 'FFDDEBF7';
|
||||
|
||||
/** 金额列填充色(浅红) */
|
||||
private const string AMOUNT_FILL = 'FFFFCCCC';
|
||||
|
||||
/** 金额格式:¥ + 两位小数 */
|
||||
private const string CURRENCY_FORMAT = '"¥"#,##0.00';
|
||||
|
||||
private ?Collection $rows = null;
|
||||
|
||||
/** @var array<int, string> 特殊行索引(1 起)=> 类型(title/scope/header/summary) */
|
||||
private array $specialRows = [];
|
||||
|
||||
/** @var array<int, array{quantity: float, amount: float, store_quantities: array<int, int>}> 明细行索引 => 填色判断数据 */
|
||||
private array $rowData = [];
|
||||
|
||||
/** @var array{quantity: float, amount: float, stores: array<int, int>} 合计行填色判断数据 */
|
||||
private array $summaryTotals = ['quantity' => 0.0, 'amount' => 0.0, 'stores' => []];
|
||||
|
||||
/** 列头所在行索引 */
|
||||
private int $headerRow = 1;
|
||||
|
||||
@@ -130,10 +148,10 @@ class PurchaseOrderExport implements FromCollection, WithStrictNullComparison, W
|
||||
'product_spec' => $spec,
|
||||
'unit' => (string) ($snapshot->unit ?? $product->unit),
|
||||
'cost_price' => (float) ($snapshot->cost_price ?? $product->cost_price),
|
||||
// 单价 = 加权平均售价 ÷ 包规数值(无订货行无售价数据,留空)
|
||||
// 单价 = 加权平均售价 ÷ 包规数值(无订货时按成本价换算,保证始终有单价)
|
||||
'retail_price' => $group !== null && (float) $quantity > 0
|
||||
? $this->unitRefPrice((float) bcdiv($amount, $quantity, 4), $spec)
|
||||
: null,
|
||||
: $this->unitRefPrice((float) ($snapshot->cost_price ?? $product->cost_price), $spec),
|
||||
'quantity' => (float) $quantity,
|
||||
'weight' => (float) ($group['weight'] ?? '0'),
|
||||
'amount' => (float) $amount,
|
||||
@@ -185,6 +203,11 @@ class PurchaseOrderExport implements FromCollection, WithStrictNullComparison, W
|
||||
$totalAmount = '0';
|
||||
$storeTotals = array_fill_keys(array_keys($this->storeNames), 0);
|
||||
foreach (array_values($items) as $sort => $item) {
|
||||
$this->rowData[++$rowIndex] = [
|
||||
'quantity' => $item['quantity'],
|
||||
'amount' => $item['amount'],
|
||||
'store_quantities' => $item['store_quantities'],
|
||||
];
|
||||
$rows[] = array_merge([
|
||||
$sort + 1,
|
||||
$item['category'],
|
||||
@@ -194,12 +217,11 @@ class PurchaseOrderExport implements FromCollection, WithStrictNullComparison, W
|
||||
$item['product_spec'],
|
||||
$item['unit'],
|
||||
$item['cost_price'],
|
||||
$item['retail_price'] !== null ? $item['retail_price'] : '',
|
||||
$item['retail_price'],
|
||||
$item['quantity'],
|
||||
$item['weight'],
|
||||
$item['amount'],
|
||||
], array_values($item['store_quantities']));
|
||||
$rowIndex++;
|
||||
$totalQuantity = bcadd($totalQuantity, (string) $item['quantity'], 2);
|
||||
$totalWeight = bcadd($totalWeight, (string) $item['weight'], 3);
|
||||
$totalAmount = bcadd($totalAmount, (string) $item['amount'], 2);
|
||||
@@ -215,12 +237,18 @@ class PurchaseOrderExport implements FromCollection, WithStrictNullComparison, W
|
||||
);
|
||||
$this->specialRows[++$rowIndex] = 'summary';
|
||||
$this->lastRow = $rowIndex;
|
||||
$this->summaryTotals = [
|
||||
'quantity' => (float) $totalQuantity,
|
||||
'amount' => (float) $totalAmount,
|
||||
'stores' => $storeTotals,
|
||||
];
|
||||
|
||||
return $this->rows = collect($rows);
|
||||
}
|
||||
|
||||
/**
|
||||
* 标题/列头/合计加粗,门店列整列填充突出颜色,冻结列头
|
||||
* 标题/列头/合计加粗;全表居中 + 全边框;金额类列货币格式;
|
||||
* 数量(浅黄)/门店数量(浅蓝)/金额(浅红)按值条件填色(0 不填、列头固定填色),冻结列头
|
||||
*/
|
||||
public function styles(Worksheet $sheet): array
|
||||
{
|
||||
@@ -232,16 +260,66 @@ class PurchaseOrderExport implements FromCollection, WithStrictNullComparison, W
|
||||
$sheet->getColumnDimension(Coordinate::stringFromColumnIndex($index + 1))->setWidth($width);
|
||||
}
|
||||
|
||||
// 门店列整列(列头 → 合计行)填充突出颜色
|
||||
$storeCount = count($this->storeNames);
|
||||
for ($i = 0; $i < $storeCount; $i++) {
|
||||
$storeIds = array_keys($this->storeNames);
|
||||
$storeCount = count($storeIds);
|
||||
$lastColumn = Coordinate::stringFromColumnIndex(12 + max($storeCount, 1));
|
||||
foreach ($storeIds as $i => $storeId) {
|
||||
$sheet->getColumnDimension(Coordinate::stringFromColumnIndex(13 + $i))->setWidth(12);
|
||||
}
|
||||
|
||||
// 全部单元格水平/垂直居中
|
||||
$sheet->getStyle('A1:' . $lastColumn . $this->lastRow)
|
||||
->getAlignment()
|
||||
->setHorizontal(Alignment::HORIZONTAL_CENTER)
|
||||
->setVertical(Alignment::VERTICAL_CENTER);
|
||||
|
||||
// 表格区域(列头 → 合计行)添加所有边框
|
||||
$sheet->getStyle('A' . $this->headerRow . ':' . $lastColumn . $this->lastRow)
|
||||
->getBorders()
|
||||
->getAllBorders()
|
||||
->setBorderStyle(Border::BORDER_THIN);
|
||||
|
||||
// 金额类列(成本/单价/金额)货币格式:¥ + 两位小数
|
||||
foreach (['H', 'I', 'L'] as $column) {
|
||||
$sheet->getStyle($column . ($this->headerRow + 1) . ':' . $column . $this->lastRow)
|
||||
->getNumberFormat()
|
||||
->setFormatCode(self::CURRENCY_FORMAT);
|
||||
}
|
||||
|
||||
// 数量列(J):有数据浅黄,0 无背景,列头固定浅黄
|
||||
$this->fillCell($sheet, 'J' . $this->headerRow, self::QUANTITY_FILL);
|
||||
foreach ($this->rowData as $row => $data) {
|
||||
if ($data['quantity'] > 0) {
|
||||
$this->fillCell($sheet, 'J' . $row, self::QUANTITY_FILL);
|
||||
}
|
||||
}
|
||||
if ($this->summaryTotals['quantity'] > 0) {
|
||||
$this->fillCell($sheet, 'J' . $this->lastRow, self::QUANTITY_FILL);
|
||||
}
|
||||
|
||||
// 金额列(L):有数据浅红,0 无背景,列头固定浅红
|
||||
$this->fillCell($sheet, 'L' . $this->headerRow, self::AMOUNT_FILL);
|
||||
foreach ($this->rowData as $row => $data) {
|
||||
if ($data['amount'] > 0) {
|
||||
$this->fillCell($sheet, 'L' . $row, self::AMOUNT_FILL);
|
||||
}
|
||||
}
|
||||
if ($this->summaryTotals['amount'] > 0) {
|
||||
$this->fillCell($sheet, 'L' . $this->lastRow, self::AMOUNT_FILL);
|
||||
}
|
||||
|
||||
// 门店数量列:有数据浅蓝,0 无背景,列头固定浅蓝
|
||||
foreach ($storeIds as $i => $storeId) {
|
||||
$column = Coordinate::stringFromColumnIndex(13 + $i);
|
||||
$sheet->getColumnDimension($column)->setWidth(12);
|
||||
$sheet->getStyle($column . $this->headerRow . ':' . $column . $this->lastRow)
|
||||
->getFill()
|
||||
->setFillType(\PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID)
|
||||
->getStartColor()
|
||||
->setARGB(self::STORE_FILL);
|
||||
$this->fillCell($sheet, $column . $this->headerRow, self::STORE_FILL);
|
||||
foreach ($this->rowData as $row => $data) {
|
||||
if (($data['store_quantities'][$storeId] ?? 0) > 0) {
|
||||
$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 = [];
|
||||
@@ -259,6 +337,18 @@ class PurchaseOrderExport implements FromCollection, WithStrictNullComparison, W
|
||||
return $styles;
|
||||
}
|
||||
|
||||
/**
|
||||
* 单元格纯色填充
|
||||
*/
|
||||
private function fillCell(Worksheet $sheet, string $coordinate, string $argb): void
|
||||
{
|
||||
$sheet->getStyle($coordinate)
|
||||
->getFill()
|
||||
->setFillType(Fill::FILL_SOLID)
|
||||
->getStartColor()
|
||||
->setARGB($argb);
|
||||
}
|
||||
|
||||
/**
|
||||
* 每单位参考价 = 整单价 ÷ 包规数值(包规解析不出正数时按 1 处理;仅展示参考)
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user