导出样式
This commit is contained in:
File diff suppressed because one or more lines are too long
@@ -10,6 +10,9 @@ use App\Services\BillDetailService;
|
|||||||
use Illuminate\Support\Collection;
|
use Illuminate\Support\Collection;
|
||||||
use Maatwebsite\Excel\Concerns\FromCollection;
|
use Maatwebsite\Excel\Concerns\FromCollection;
|
||||||
use Maatwebsite\Excel\Concerns\WithStyles;
|
use Maatwebsite\Excel\Concerns\WithStyles;
|
||||||
|
use PhpOffice\PhpSpreadsheet\Style\Alignment;
|
||||||
|
use PhpOffice\PhpSpreadsheet\Style\Border;
|
||||||
|
use PhpOffice\PhpSpreadsheet\Style\Fill;
|
||||||
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
|
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -18,6 +21,15 @@ use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
|
|||||||
*/
|
*/
|
||||||
class BillExport implements FromCollection, WithStyles
|
class BillExport implements FromCollection, WithStyles
|
||||||
{
|
{
|
||||||
|
/** 数量列填充色(浅黄) */
|
||||||
|
private const string QUANTITY_FILL = 'FFFFF2CC';
|
||||||
|
|
||||||
|
/** 金额列填充色(浅红) */
|
||||||
|
private const string AMOUNT_FILL = 'FFFFCCCC';
|
||||||
|
|
||||||
|
/** 金额格式:¥ + 两位小数 */
|
||||||
|
private const string CURRENCY_FORMAT = '"¥"#,##0.00';
|
||||||
|
|
||||||
private ?Collection $rows = null;
|
private ?Collection $rows = null;
|
||||||
|
|
||||||
/** @var array<int, string> 特殊行索引(1 起)=> 类型(title/scope/header/summary/grand) */
|
/** @var array<int, string> 特殊行索引(1 起)=> 类型(title/scope/header/summary/grand) */
|
||||||
@@ -26,9 +38,15 @@ class BillExport implements FromCollection, WithStyles
|
|||||||
/** @var array<int, int> 标签需左合并 A:G 的合计行索引 */
|
/** @var array<int, int> 标签需左合并 A:G 的合计行索引 */
|
||||||
private array $mergeRows = [];
|
private array $mergeRows = [];
|
||||||
|
|
||||||
|
/** @var array<int, array{row: int, column: string, value: float}> 条件填色单元格(F=数量浅黄,H=金额浅红,非 0 填色) */
|
||||||
|
private array $fillCells = [];
|
||||||
|
|
||||||
/** 列头所在行索引 */
|
/** 列头所在行索引 */
|
||||||
private int $headerRow = 1;
|
private int $headerRow = 1;
|
||||||
|
|
||||||
|
/** 数据末行索引 */
|
||||||
|
private int $lastRow = 1;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param Collection<int, BillModel> $bills 勾选账单(已按 bill_date/id 排序,with store)
|
* @param Collection<int, BillModel> $bills 勾选账单(已按 bill_date/id 排序,with store)
|
||||||
* @param int $categoryId 一级分类ID(0=全部)
|
* @param int $categoryId 一级分类ID(0=全部)
|
||||||
@@ -104,6 +122,8 @@ class BillExport implements FromCollection, WithStyles
|
|||||||
(float) $item['amount'],
|
(float) $item['amount'],
|
||||||
];
|
];
|
||||||
$rowIndex++;
|
$rowIndex++;
|
||||||
|
$this->fillCells[] = ['row' => $rowIndex, 'column' => 'F', 'value' => (float) $item['quantity']];
|
||||||
|
$this->fillCells[] = ['row' => $rowIndex, 'column' => 'H', 'value' => (float) $item['amount']];
|
||||||
$totalQuantity += (int) $item['quantity'];
|
$totalQuantity += (int) $item['quantity'];
|
||||||
$totalWeight = bcadd($totalWeight, $item['weight'], 3);
|
$totalWeight = bcadd($totalWeight, $item['weight'], 3);
|
||||||
$totalAmount = bcadd($totalAmount, $item['amount'], 2);
|
$totalAmount = bcadd($totalAmount, $item['amount'], 2);
|
||||||
@@ -112,6 +132,8 @@ class BillExport implements FromCollection, WithStyles
|
|||||||
// 商品合计行
|
// 商品合计行
|
||||||
$rows[] = ['', '合计', '', '', '', $totalQuantity, (float) $totalWeight, (float) $totalAmount];
|
$rows[] = ['', '合计', '', '', '', $totalQuantity, (float) $totalWeight, (float) $totalAmount];
|
||||||
$this->specialRows[++$rowIndex] = 'summary';
|
$this->specialRows[++$rowIndex] = 'summary';
|
||||||
|
$this->fillCells[] = ['row' => $rowIndex, 'column' => 'F', 'value' => (float) $totalQuantity];
|
||||||
|
$this->fillCells[] = ['row' => $rowIndex, 'column' => 'H', 'value' => (float) $totalAmount];
|
||||||
|
|
||||||
// 配送费/附加金额/售后金额/总金额(账单级费用全额汇总,不受分类过滤影响)
|
// 配送费/附加金额/售后金额/总金额(账单级费用全额汇总,不受分类过滤影响)
|
||||||
$deliveryTotal = '0';
|
$deliveryTotal = '0';
|
||||||
@@ -131,24 +153,30 @@ class BillExport implements FromCollection, WithStyles
|
|||||||
$rows[] = ['配送费合计', '', '', '', '', '', '', (float) $deliveryTotal];
|
$rows[] = ['配送费合计', '', '', '', '', '', '', (float) $deliveryTotal];
|
||||||
$this->specialRows[++$rowIndex] = 'summary';
|
$this->specialRows[++$rowIndex] = 'summary';
|
||||||
$this->mergeRows[] = $rowIndex;
|
$this->mergeRows[] = $rowIndex;
|
||||||
|
$this->fillCells[] = ['row' => $rowIndex, 'column' => 'H', 'value' => (float) $deliveryTotal];
|
||||||
|
|
||||||
$rows[] = ['附加金额合计(周转筐 ' . $boxNum . ' 个 / 周转托盘 ' . $trayNum . ' 个)', '', '', '', '', '', '', (float) $addedTotal];
|
$rows[] = ['附加金额合计(周转筐 ' . $boxNum . ' 个 / 周转托盘 ' . $trayNum . ' 个)', '', '', '', '', '', '', (float) $addedTotal];
|
||||||
$this->specialRows[++$rowIndex] = 'summary';
|
$this->specialRows[++$rowIndex] = 'summary';
|
||||||
$this->mergeRows[] = $rowIndex;
|
$this->mergeRows[] = $rowIndex;
|
||||||
|
$this->fillCells[] = ['row' => $rowIndex, 'column' => 'H', 'value' => (float) $addedTotal];
|
||||||
|
|
||||||
$rows[] = ['售后金额合计(可正负)', '', '', '', '', '', '', (float) $afterSaleTotal];
|
$rows[] = ['售后金额合计(可正负)', '', '', '', '', '', '', (float) $afterSaleTotal];
|
||||||
$this->specialRows[++$rowIndex] = 'summary';
|
$this->specialRows[++$rowIndex] = 'summary';
|
||||||
$this->mergeRows[] = $rowIndex;
|
$this->mergeRows[] = $rowIndex;
|
||||||
|
$this->fillCells[] = ['row' => $rowIndex, 'column' => 'H', 'value' => (float) $afterSaleTotal];
|
||||||
|
|
||||||
$rows[] = ['总金额(商品金额+配送费+附加金额+售后金额)', '', '', '', '', '', '', (float) $grandTotal];
|
$rows[] = ['总金额(商品金额+配送费+附加金额+售后金额)', '', '', '', '', '', '', (float) $grandTotal];
|
||||||
$this->specialRows[++$rowIndex] = 'grand';
|
$this->specialRows[++$rowIndex] = 'grand';
|
||||||
$this->mergeRows[] = $rowIndex;
|
$this->mergeRows[] = $rowIndex;
|
||||||
|
$this->fillCells[] = ['row' => $rowIndex, 'column' => 'H', 'value' => (float) $grandTotal];
|
||||||
|
$this->lastRow = $rowIndex;
|
||||||
|
|
||||||
return $this->rows = collect($rows);
|
return $this->rows = collect($rows);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 标题/列头/合计行加粗,合计行标签左合并 A:G,冻结列头
|
* 标题/列头/合计行加粗,合计行标签左合并 A:G;全表居中 + 全边框;金额类列货币格式;
|
||||||
|
* 数量(浅黄)/金额(浅红)非 0 条件填色(0 不填、列头固定填色),冻结列头
|
||||||
*/
|
*/
|
||||||
public function styles(Worksheet $sheet): array
|
public function styles(Worksheet $sheet): array
|
||||||
{
|
{
|
||||||
@@ -168,6 +196,43 @@ class BillExport implements FromCollection, WithStyles
|
|||||||
$sheet->mergeCells('A' . $row . ':G' . $row);
|
$sheet->mergeCells('A' . $row . ':G' . $row);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 全部单元格水平/垂直居中(合计行标签保持左对齐)
|
||||||
|
$sheet->getStyle('A1:H' . $this->lastRow)
|
||||||
|
->getAlignment()
|
||||||
|
->setHorizontal(Alignment::HORIZONTAL_CENTER)
|
||||||
|
->setVertical(Alignment::VERTICAL_CENTER);
|
||||||
|
foreach ($this->mergeRows as $row) {
|
||||||
|
$sheet->getStyle('A' . $row)->getAlignment()->setHorizontal(Alignment::HORIZONTAL_LEFT);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 表格区域(列头 → 末行)添加所有边框
|
||||||
|
$sheet->getStyle('A' . $this->headerRow . ':H' . $this->lastRow)
|
||||||
|
->getBorders()
|
||||||
|
->getAllBorders()
|
||||||
|
->setBorderStyle(Border::BORDER_THIN);
|
||||||
|
|
||||||
|
// 金额类列(单价/金额)货币格式:¥ + 两位小数
|
||||||
|
foreach (['E', 'H'] as $column) {
|
||||||
|
$sheet->getStyle($column . ($this->headerRow + 1) . ':' . $column . $this->lastRow)
|
||||||
|
->getNumberFormat()
|
||||||
|
->setFormatCode(self::CURRENCY_FORMAT);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 列头固定填色:数量列(F)浅黄、金额列(H)浅红
|
||||||
|
$this->fillCell($sheet, 'F' . $this->headerRow, self::QUANTITY_FILL);
|
||||||
|
$this->fillCell($sheet, 'H' . $this->headerRow, self::AMOUNT_FILL);
|
||||||
|
|
||||||
|
// 数据区条件填色:非 0 填色,0 无背景(金额含负值)
|
||||||
|
foreach ($this->fillCells as $cell) {
|
||||||
|
if ((float) $cell['value'] !== 0.0) {
|
||||||
|
$this->fillCell(
|
||||||
|
$sheet,
|
||||||
|
$cell['column'] . $cell['row'],
|
||||||
|
$cell['column'] === 'F' ? self::QUANTITY_FILL : self::AMOUNT_FILL
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
$styles = [];
|
$styles = [];
|
||||||
foreach ($this->specialRows as $row => $type) {
|
foreach ($this->specialRows as $row => $type) {
|
||||||
$style = match ($type) {
|
$style = match ($type) {
|
||||||
@@ -184,6 +249,18 @@ class BillExport implements FromCollection, WithStyles
|
|||||||
return $styles;
|
return $styles;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 单元格纯色填充
|
||||||
|
*/
|
||||||
|
private function fillCell(Worksheet $sheet, string $coordinate, string $argb): void
|
||||||
|
{
|
||||||
|
$sheet->getStyle($coordinate)
|
||||||
|
->getFill()
|
||||||
|
->setFillType(Fill::FILL_SOLID)
|
||||||
|
->getStartColor()
|
||||||
|
->setARGB($argb);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 按一级分类过滤明细:沿 parent_id 上溯取根分类(商品含软删除,保证历史账单可导出),
|
* 按一级分类过滤明细:沿 parent_id 上溯取根分类(商品含软删除,保证历史账单可导出),
|
||||||
* 仅保留根分类为所选分类的行
|
* 仅保留根分类为所选分类的行
|
||||||
|
|||||||
@@ -8,6 +8,10 @@ use Illuminate\Support\Collection;
|
|||||||
use Maatwebsite\Excel\Concerns\FromCollection;
|
use Maatwebsite\Excel\Concerns\FromCollection;
|
||||||
use Maatwebsite\Excel\Concerns\WithStrictNullComparison;
|
use Maatwebsite\Excel\Concerns\WithStrictNullComparison;
|
||||||
use Maatwebsite\Excel\Concerns\WithStyles;
|
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;
|
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -20,14 +24,32 @@ use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
|
|||||||
*/
|
*/
|
||||||
class ContainerReturnExport implements FromCollection, WithStyles, WithStrictNullComparison
|
class ContainerReturnExport implements FromCollection, WithStyles, WithStrictNullComparison
|
||||||
{
|
{
|
||||||
|
/** 门店列填充色(浅蓝) */
|
||||||
|
private const string STORE_FILL = 'FFDDEBF7';
|
||||||
|
|
||||||
|
/** 合计列填充色(浅红) */
|
||||||
|
private const string AMOUNT_FILL = 'FFFFCCCC';
|
||||||
|
|
||||||
|
/** 金额格式:¥ + 两位小数 */
|
||||||
|
private const string CURRENCY_FORMAT = '"¥"#,##0.00';
|
||||||
|
|
||||||
private ?Collection $rows = null;
|
private ?Collection $rows = null;
|
||||||
|
|
||||||
/** @var array<int, string> 特殊行索引(1 起)=> 类型(title/scope/header/total) */
|
/** @var array<int, string> 特殊行索引(1 起)=> 类型(title/scope/header/total) */
|
||||||
private array $specialRows = [];
|
private array $specialRows = [];
|
||||||
|
|
||||||
|
/** @var array<int, array{stores: array<int, float>, total: float}> 日期行索引 => 填色判断数据 */
|
||||||
|
private array $rowData = [];
|
||||||
|
|
||||||
|
/** @var array{stores: array<int, float>, total: float} 合计行填色判断数据 */
|
||||||
|
private array $summaryTotals = ['stores' => [], 'total' => 0.0];
|
||||||
|
|
||||||
/** 列头所在行索引 */
|
/** 列头所在行索引 */
|
||||||
private int $headerRow = 1;
|
private int $headerRow = 1;
|
||||||
|
|
||||||
|
/** 数据末行索引 */
|
||||||
|
private int $lastRow = 1;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param Collection<int, ContainerReturnModel> $records 区间内压回筐记录
|
* @param Collection<int, ContainerReturnModel> $records 区间内压回筐记录
|
||||||
* @param array<int, string> $storeNames 门店ID => 名称(导出列,升序)
|
* @param array<int, string> $storeNames 门店ID => 名称(导出列,升序)
|
||||||
@@ -90,16 +112,18 @@ class ContainerReturnExport implements FromCollection, WithStyles, WithStrictNul
|
|||||||
$dateKey = $date->format('Y-m-d');
|
$dateKey = $date->format('Y-m-d');
|
||||||
$row = [$dateKey];
|
$row = [$dateKey];
|
||||||
$rowTotal = '0';
|
$rowTotal = '0';
|
||||||
|
$storeValues = [];
|
||||||
foreach ($storeIds as $storeId) {
|
foreach ($storeIds as $storeId) {
|
||||||
$amount = $amounts[$dateKey][$storeId] ?? '0.00';
|
$amount = $amounts[$dateKey][$storeId] ?? '0.00';
|
||||||
$row[] = (float) $amount;
|
$row[] = (float) $amount;
|
||||||
|
$storeValues[$storeId] = (float) $amount;
|
||||||
$rowTotal = bcadd($rowTotal, $amount, 2);
|
$rowTotal = bcadd($rowTotal, $amount, 2);
|
||||||
$columnTotals[$storeId] = bcadd($columnTotals[$storeId], $amount, 2);
|
$columnTotals[$storeId] = bcadd($columnTotals[$storeId], $amount, 2);
|
||||||
}
|
}
|
||||||
$grandTotal = bcadd($grandTotal, $rowTotal, 2);
|
$grandTotal = bcadd($grandTotal, $rowTotal, 2);
|
||||||
$row[] = (float) $rowTotal;
|
$row[] = (float) $rowTotal;
|
||||||
$rows[] = $row;
|
$rows[] = $row;
|
||||||
$rowIndex++;
|
$this->rowData[++$rowIndex] = ['stores' => $storeValues, 'total' => (float) $rowTotal];
|
||||||
}
|
}
|
||||||
|
|
||||||
// 合计行:各门店列合计 + 总计
|
// 合计行:各门店列合计 + 总计
|
||||||
@@ -110,12 +134,18 @@ class ContainerReturnExport implements FromCollection, WithStyles, WithStrictNul
|
|||||||
$totalRow[] = (float) $grandTotal;
|
$totalRow[] = (float) $grandTotal;
|
||||||
$rows[] = $totalRow;
|
$rows[] = $totalRow;
|
||||||
$this->specialRows[++$rowIndex] = 'total';
|
$this->specialRows[++$rowIndex] = 'total';
|
||||||
|
$this->lastRow = $rowIndex;
|
||||||
|
$this->summaryTotals = [
|
||||||
|
'stores' => array_map(floatval(...), $columnTotals),
|
||||||
|
'total' => (float) $grandTotal,
|
||||||
|
];
|
||||||
|
|
||||||
return $this->rows = collect($rows);
|
return $this->rows = collect($rows);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 标题/列头/合计行加粗,冻结列头行
|
* 标题/列头/合计行加粗;全表居中 + 全边框;金额区货币格式;
|
||||||
|
* 门店列(浅蓝)/合计列(浅红)非 0 条件填色(0 不填、列头固定填色),冻结列头行
|
||||||
*/
|
*/
|
||||||
public function styles(Worksheet $sheet): array
|
public function styles(Worksheet $sheet): array
|
||||||
{
|
{
|
||||||
@@ -128,6 +158,51 @@ class ContainerReturnExport implements FromCollection, WithStyles, WithStrictNul
|
|||||||
$sheet->getColumnDimensionByColumn($column)->setWidth(12);
|
$sheet->getColumnDimensionByColumn($column)->setWidth(12);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$storeIds = array_map(intval(...), array_keys($this->storeNames));
|
||||||
|
$lastColumn = Coordinate::stringFromColumnIndex($columnCount);
|
||||||
|
|
||||||
|
// 全部单元格水平/垂直居中
|
||||||
|
$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);
|
||||||
|
|
||||||
|
// 金额区(门店列 + 合计列)货币格式:¥ + 两位小数
|
||||||
|
$sheet->getStyle('B' . ($this->headerRow + 1) . ':' . $lastColumn . $this->lastRow)
|
||||||
|
->getNumberFormat()
|
||||||
|
->setFormatCode(self::CURRENCY_FORMAT);
|
||||||
|
|
||||||
|
// 门店列(B 起):非 0 浅蓝,0 无背景,列头固定浅蓝
|
||||||
|
foreach ($storeIds as $i => $storeId) {
|
||||||
|
$column = Coordinate::stringFromColumnIndex(2 + $i);
|
||||||
|
$this->fillCell($sheet, $column . $this->headerRow, self::STORE_FILL);
|
||||||
|
foreach ($this->rowData as $row => $data) {
|
||||||
|
if (($data['stores'][$storeId] ?? 0.0) !== 0.0) {
|
||||||
|
$this->fillCell($sheet, $column . $row, self::STORE_FILL);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (($this->summaryTotals['stores'][$storeId] ?? 0.0) !== 0.0) {
|
||||||
|
$this->fillCell($sheet, $column . $this->lastRow, self::STORE_FILL);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 合计列(末列):非 0 浅红,0 无背景,列头固定浅红
|
||||||
|
$this->fillCell($sheet, $lastColumn . $this->headerRow, self::AMOUNT_FILL);
|
||||||
|
foreach ($this->rowData as $row => $data) {
|
||||||
|
if ($data['total'] !== 0.0) {
|
||||||
|
$this->fillCell($sheet, $lastColumn . $row, self::AMOUNT_FILL);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if ($this->summaryTotals['total'] !== 0.0) {
|
||||||
|
$this->fillCell($sheet, $lastColumn . $this->lastRow, self::AMOUNT_FILL);
|
||||||
|
}
|
||||||
|
|
||||||
$styles = [];
|
$styles = [];
|
||||||
foreach ($this->specialRows as $row => $type) {
|
foreach ($this->specialRows as $row => $type) {
|
||||||
$style = match ($type) {
|
$style = match ($type) {
|
||||||
@@ -142,4 +217,16 @@ class ContainerReturnExport implements FromCollection, WithStyles, WithStrictNul
|
|||||||
|
|
||||||
return $styles;
|
return $styles;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 单元格纯色填充
|
||||||
|
*/
|
||||||
|
private function fillCell(Worksheet $sheet, string $coordinate, string $argb): void
|
||||||
|
{
|
||||||
|
$sheet->getStyle($coordinate)
|
||||||
|
->getFill()
|
||||||
|
->setFillType(Fill::FILL_SOLID)
|
||||||
|
->getStartColor()
|
||||||
|
->setARGB($argb);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,22 +14,40 @@ use Maatwebsite\Excel\Concerns\FromCollection;
|
|||||||
use Maatwebsite\Excel\Concerns\WithStrictNullComparison;
|
use Maatwebsite\Excel\Concerns\WithStrictNullComparison;
|
||||||
use Maatwebsite\Excel\Concerns\WithStyles;
|
use Maatwebsite\Excel\Concerns\WithStyles;
|
||||||
use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
|
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;
|
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 采购单商品明细导出:系统全部商品行(含本采购单无订货的商品,数量 0),
|
* 采购单商品明细导出:系统全部商品行(含本采购单无订货的商品,数量 0),
|
||||||
* 支持按供应商筛选;行尾合计 + 门店列合计;门店列整列填充突出颜色
|
* 支持按供应商筛选;行尾合计 + 门店列合计;数量/金额/门店数量按值条件填色
|
||||||
*/
|
*/
|
||||||
class PurchaseOrderExport implements FromCollection, WithStrictNullComparison, WithStyles
|
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;
|
private ?Collection $rows = null;
|
||||||
|
|
||||||
/** @var array<int, string> 特殊行索引(1 起)=> 类型(title/scope/header/summary) */
|
/** @var array<int, string> 特殊行索引(1 起)=> 类型(title/scope/header/summary) */
|
||||||
private array $specialRows = [];
|
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;
|
private int $headerRow = 1;
|
||||||
|
|
||||||
@@ -130,10 +148,10 @@ class PurchaseOrderExport implements FromCollection, WithStrictNullComparison, W
|
|||||||
'product_spec' => $spec,
|
'product_spec' => $spec,
|
||||||
'unit' => (string) ($snapshot->unit ?? $product->unit),
|
'unit' => (string) ($snapshot->unit ?? $product->unit),
|
||||||
'cost_price' => (float) ($snapshot->cost_price ?? $product->cost_price),
|
'cost_price' => (float) ($snapshot->cost_price ?? $product->cost_price),
|
||||||
// 单价 = 加权平均售价 ÷ 包规数值(无订货行无售价数据,留空)
|
// 单价 = 加权平均售价 ÷ 包规数值(无订货时按成本价换算,保证始终有单价)
|
||||||
'retail_price' => $group !== null && (float) $quantity > 0
|
'retail_price' => $group !== null && (float) $quantity > 0
|
||||||
? $this->unitRefPrice((float) bcdiv($amount, $quantity, 4), $spec)
|
? $this->unitRefPrice((float) bcdiv($amount, $quantity, 4), $spec)
|
||||||
: null,
|
: $this->unitRefPrice((float) ($snapshot->cost_price ?? $product->cost_price), $spec),
|
||||||
'quantity' => (float) $quantity,
|
'quantity' => (float) $quantity,
|
||||||
'weight' => (float) ($group['weight'] ?? '0'),
|
'weight' => (float) ($group['weight'] ?? '0'),
|
||||||
'amount' => (float) $amount,
|
'amount' => (float) $amount,
|
||||||
@@ -185,6 +203,11 @@ class PurchaseOrderExport implements FromCollection, WithStrictNullComparison, W
|
|||||||
$totalAmount = '0';
|
$totalAmount = '0';
|
||||||
$storeTotals = array_fill_keys(array_keys($this->storeNames), 0);
|
$storeTotals = array_fill_keys(array_keys($this->storeNames), 0);
|
||||||
foreach (array_values($items) as $sort => $item) {
|
foreach (array_values($items) as $sort => $item) {
|
||||||
|
$this->rowData[++$rowIndex] = [
|
||||||
|
'quantity' => $item['quantity'],
|
||||||
|
'amount' => $item['amount'],
|
||||||
|
'store_quantities' => $item['store_quantities'],
|
||||||
|
];
|
||||||
$rows[] = array_merge([
|
$rows[] = array_merge([
|
||||||
$sort + 1,
|
$sort + 1,
|
||||||
$item['category'],
|
$item['category'],
|
||||||
@@ -194,12 +217,11 @@ class PurchaseOrderExport implements FromCollection, WithStrictNullComparison, W
|
|||||||
$item['product_spec'],
|
$item['product_spec'],
|
||||||
$item['unit'],
|
$item['unit'],
|
||||||
$item['cost_price'],
|
$item['cost_price'],
|
||||||
$item['retail_price'] !== null ? $item['retail_price'] : '',
|
$item['retail_price'],
|
||||||
$item['quantity'],
|
$item['quantity'],
|
||||||
$item['weight'],
|
$item['weight'],
|
||||||
$item['amount'],
|
$item['amount'],
|
||||||
], array_values($item['store_quantities']));
|
], array_values($item['store_quantities']));
|
||||||
$rowIndex++;
|
|
||||||
$totalQuantity = bcadd($totalQuantity, (string) $item['quantity'], 2);
|
$totalQuantity = bcadd($totalQuantity, (string) $item['quantity'], 2);
|
||||||
$totalWeight = bcadd($totalWeight, (string) $item['weight'], 3);
|
$totalWeight = bcadd($totalWeight, (string) $item['weight'], 3);
|
||||||
$totalAmount = bcadd($totalAmount, (string) $item['amount'], 2);
|
$totalAmount = bcadd($totalAmount, (string) $item['amount'], 2);
|
||||||
@@ -215,12 +237,18 @@ class PurchaseOrderExport implements FromCollection, WithStrictNullComparison, W
|
|||||||
);
|
);
|
||||||
$this->specialRows[++$rowIndex] = 'summary';
|
$this->specialRows[++$rowIndex] = 'summary';
|
||||||
$this->lastRow = $rowIndex;
|
$this->lastRow = $rowIndex;
|
||||||
|
$this->summaryTotals = [
|
||||||
|
'quantity' => (float) $totalQuantity,
|
||||||
|
'amount' => (float) $totalAmount,
|
||||||
|
'stores' => $storeTotals,
|
||||||
|
];
|
||||||
|
|
||||||
return $this->rows = collect($rows);
|
return $this->rows = collect($rows);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 标题/列头/合计加粗,门店列整列填充突出颜色,冻结列头
|
* 标题/列头/合计加粗;全表居中 + 全边框;金额类列货币格式;
|
||||||
|
* 数量(浅黄)/门店数量(浅蓝)/金额(浅红)按值条件填色(0 不填、列头固定填色),冻结列头
|
||||||
*/
|
*/
|
||||||
public function styles(Worksheet $sheet): array
|
public function styles(Worksheet $sheet): array
|
||||||
{
|
{
|
||||||
@@ -232,16 +260,66 @@ class PurchaseOrderExport implements FromCollection, WithStrictNullComparison, W
|
|||||||
$sheet->getColumnDimension(Coordinate::stringFromColumnIndex($index + 1))->setWidth($width);
|
$sheet->getColumnDimension(Coordinate::stringFromColumnIndex($index + 1))->setWidth($width);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 门店列整列(列头 → 合计行)填充突出颜色
|
$storeIds = array_keys($this->storeNames);
|
||||||
$storeCount = count($this->storeNames);
|
$storeCount = count($storeIds);
|
||||||
for ($i = 0; $i < $storeCount; $i++) {
|
$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);
|
$column = Coordinate::stringFromColumnIndex(13 + $i);
|
||||||
$sheet->getColumnDimension($column)->setWidth(12);
|
$this->fillCell($sheet, $column . $this->headerRow, self::STORE_FILL);
|
||||||
$sheet->getStyle($column . $this->headerRow . ':' . $column . $this->lastRow)
|
foreach ($this->rowData as $row => $data) {
|
||||||
->getFill()
|
if (($data['store_quantities'][$storeId] ?? 0) > 0) {
|
||||||
->setFillType(\PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID)
|
$this->fillCell($sheet, $column . $row, self::STORE_FILL);
|
||||||
->getStartColor()
|
}
|
||||||
->setARGB(self::STORE_FILL);
|
}
|
||||||
|
if (($this->summaryTotals['stores'][$storeId] ?? 0) > 0) {
|
||||||
|
$this->fillCell($sheet, $column . $this->lastRow, self::STORE_FILL);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$styles = [];
|
$styles = [];
|
||||||
@@ -259,6 +337,18 @@ class PurchaseOrderExport implements FromCollection, WithStrictNullComparison, W
|
|||||||
return $styles;
|
return $styles;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 单元格纯色填充
|
||||||
|
*/
|
||||||
|
private function fillCell(Worksheet $sheet, string $coordinate, string $argb): void
|
||||||
|
{
|
||||||
|
$sheet->getStyle($coordinate)
|
||||||
|
->getFill()
|
||||||
|
->setFillType(Fill::FILL_SOLID)
|
||||||
|
->getStartColor()
|
||||||
|
->setARGB($argb);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 每单位参考价 = 整单价 ÷ 包规数值(包规解析不出正数时按 1 处理;仅展示参考)
|
* 每单位参考价 = 整单价 ÷ 包规数值(包规解析不出正数时按 1 处理;仅展示参考)
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -10,6 +10,9 @@ use Maatwebsite\Excel\Concerns\FromCollection;
|
|||||||
use Maatwebsite\Excel\Concerns\WithStrictNullComparison;
|
use Maatwebsite\Excel\Concerns\WithStrictNullComparison;
|
||||||
use Maatwebsite\Excel\Concerns\WithStyles;
|
use Maatwebsite\Excel\Concerns\WithStyles;
|
||||||
use Maatwebsite\Excel\Concerns\WithTitle;
|
use Maatwebsite\Excel\Concerns\WithTitle;
|
||||||
|
use PhpOffice\PhpSpreadsheet\Style\Alignment;
|
||||||
|
use PhpOffice\PhpSpreadsheet\Style\Border;
|
||||||
|
use PhpOffice\PhpSpreadsheet\Style\Fill;
|
||||||
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
|
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -17,14 +20,32 @@ use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
|
|||||||
*/
|
*/
|
||||||
class PurchaseStoreSheet implements FromCollection, WithStrictNullComparison, WithStyles, WithTitle
|
class PurchaseStoreSheet implements FromCollection, WithStrictNullComparison, WithStyles, WithTitle
|
||||||
{
|
{
|
||||||
|
/** 数量列填充色(浅黄) */
|
||||||
|
private const string QUANTITY_FILL = 'FFFFF2CC';
|
||||||
|
|
||||||
|
/** 金额列填充色(浅红) */
|
||||||
|
private const string AMOUNT_FILL = 'FFFFCCCC';
|
||||||
|
|
||||||
|
/** 金额格式:¥ + 两位小数 */
|
||||||
|
private const string CURRENCY_FORMAT = '"¥"#,##0.00';
|
||||||
|
|
||||||
private ?Collection $rows = null;
|
private ?Collection $rows = null;
|
||||||
|
|
||||||
/** @var array<int, string> 特殊行索引(1 起)=> 类型(title/header/summary) */
|
/** @var array<int, string> 特殊行索引(1 起)=> 类型(title/header/summary) */
|
||||||
private array $specialRows = [];
|
private array $specialRows = [];
|
||||||
|
|
||||||
|
/** @var array<int, array{quantity: int, amount: float}> 明细行索引 => 填色判断数据 */
|
||||||
|
private array $rowData = [];
|
||||||
|
|
||||||
|
/** @var array{quantity: int, amount: float} 合计行填色判断数据 */
|
||||||
|
private array $summaryTotals = ['quantity' => 0, 'amount' => 0.0];
|
||||||
|
|
||||||
/** 列头所在行索引 */
|
/** 列头所在行索引 */
|
||||||
private int $headerRow = 1;
|
private int $headerRow = 1;
|
||||||
|
|
||||||
|
/** 数据末行索引 */
|
||||||
|
private int $lastRow = 1;
|
||||||
|
|
||||||
public function __construct(
|
public function __construct(
|
||||||
private readonly PurchaseOrderModel $purchase,
|
private readonly PurchaseOrderModel $purchase,
|
||||||
private readonly StoreModel $store,
|
private readonly StoreModel $store,
|
||||||
@@ -64,6 +85,10 @@ class PurchaseStoreSheet implements FromCollection, WithStrictNullComparison, Wi
|
|||||||
$totalWeight = '0';
|
$totalWeight = '0';
|
||||||
$totalAmount = '0';
|
$totalAmount = '0';
|
||||||
foreach (array_values($items) as $sort => $item) {
|
foreach (array_values($items) as $sort => $item) {
|
||||||
|
$this->rowData[++$rowIndex] = [
|
||||||
|
'quantity' => (int) $item['quantity'],
|
||||||
|
'amount' => (float) $item['amount'],
|
||||||
|
];
|
||||||
$rows[] = [
|
$rows[] = [
|
||||||
$sort + 1,
|
$sort + 1,
|
||||||
$item['product_name'],
|
$item['product_name'],
|
||||||
@@ -75,7 +100,6 @@ class PurchaseStoreSheet implements FromCollection, WithStrictNullComparison, Wi
|
|||||||
(float) $item['weight'],
|
(float) $item['weight'],
|
||||||
(float) $item['amount'],
|
(float) $item['amount'],
|
||||||
];
|
];
|
||||||
$rowIndex++;
|
|
||||||
$totalQuantity += (int) $item['quantity'];
|
$totalQuantity += (int) $item['quantity'];
|
||||||
$totalWeight = bcadd($totalWeight, (string) $item['weight'], 3);
|
$totalWeight = bcadd($totalWeight, (string) $item['weight'], 3);
|
||||||
$totalAmount = bcadd($totalAmount, (string) $item['amount'], 2);
|
$totalAmount = bcadd($totalAmount, (string) $item['amount'], 2);
|
||||||
@@ -84,6 +108,8 @@ class PurchaseStoreSheet implements FromCollection, WithStrictNullComparison, Wi
|
|||||||
// 合计行
|
// 合计行
|
||||||
$rows[] = ['', '合计', '', '', '', '', $totalQuantity, (float) $totalWeight, (float) $totalAmount];
|
$rows[] = ['', '合计', '', '', '', '', $totalQuantity, (float) $totalWeight, (float) $totalAmount];
|
||||||
$this->specialRows[++$rowIndex] = 'summary';
|
$this->specialRows[++$rowIndex] = 'summary';
|
||||||
|
$this->lastRow = $rowIndex;
|
||||||
|
$this->summaryTotals = ['quantity' => $totalQuantity, 'amount' => (float) $totalAmount];
|
||||||
|
|
||||||
return $this->rows = collect($rows);
|
return $this->rows = collect($rows);
|
||||||
}
|
}
|
||||||
@@ -94,7 +120,8 @@ class PurchaseStoreSheet implements FromCollection, WithStrictNullComparison, Wi
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 标题/列头/合计加粗,冻结列头
|
* 标题/列头/合计加粗;全表居中 + 全边框;金额类列货币格式;
|
||||||
|
* 数量(浅黄)/金额(浅红)按值条件填色(0 不填、列头固定填色),冻结列头
|
||||||
*/
|
*/
|
||||||
public function styles(Worksheet $sheet): array
|
public function styles(Worksheet $sheet): array
|
||||||
{
|
{
|
||||||
@@ -107,6 +134,47 @@ class PurchaseStoreSheet implements FromCollection, WithStrictNullComparison, Wi
|
|||||||
->setWidth($width);
|
->setWidth($width);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 全部单元格水平/垂直居中
|
||||||
|
$sheet->getStyle('A1:I' . $this->lastRow)
|
||||||
|
->getAlignment()
|
||||||
|
->setHorizontal(Alignment::HORIZONTAL_CENTER)
|
||||||
|
->setVertical(Alignment::VERTICAL_CENTER);
|
||||||
|
|
||||||
|
// 表格区域(列头 → 合计行)添加所有边框
|
||||||
|
$sheet->getStyle('A' . $this->headerRow . ':I' . $this->lastRow)
|
||||||
|
->getBorders()
|
||||||
|
->getAllBorders()
|
||||||
|
->setBorderStyle(Border::BORDER_THIN);
|
||||||
|
|
||||||
|
// 金额类列(单价/预计金额)货币格式:¥ + 两位小数
|
||||||
|
foreach (['F', 'I'] as $column) {
|
||||||
|
$sheet->getStyle($column . ($this->headerRow + 1) . ':' . $column . $this->lastRow)
|
||||||
|
->getNumberFormat()
|
||||||
|
->setFormatCode(self::CURRENCY_FORMAT);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 数量列(G):有数据浅黄,0 无背景,列头固定浅黄
|
||||||
|
$this->fillCell($sheet, 'G' . $this->headerRow, self::QUANTITY_FILL);
|
||||||
|
foreach ($this->rowData as $row => $data) {
|
||||||
|
if ($data['quantity'] > 0) {
|
||||||
|
$this->fillCell($sheet, 'G' . $row, self::QUANTITY_FILL);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if ($this->summaryTotals['quantity'] > 0) {
|
||||||
|
$this->fillCell($sheet, 'G' . $this->lastRow, self::QUANTITY_FILL);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 金额列(I):有数据浅红,0 无背景,列头固定浅红
|
||||||
|
$this->fillCell($sheet, 'I' . $this->headerRow, self::AMOUNT_FILL);
|
||||||
|
foreach ($this->rowData as $row => $data) {
|
||||||
|
if ($data['amount'] > 0) {
|
||||||
|
$this->fillCell($sheet, 'I' . $row, self::AMOUNT_FILL);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if ($this->summaryTotals['amount'] > 0) {
|
||||||
|
$this->fillCell($sheet, 'I' . $this->lastRow, self::AMOUNT_FILL);
|
||||||
|
}
|
||||||
|
|
||||||
$styles = [];
|
$styles = [];
|
||||||
foreach ($this->specialRows as $row => $type) {
|
foreach ($this->specialRows as $row => $type) {
|
||||||
$style = match ($type) {
|
$style = match ($type) {
|
||||||
@@ -121,4 +189,16 @@ class PurchaseStoreSheet implements FromCollection, WithStrictNullComparison, Wi
|
|||||||
|
|
||||||
return $styles;
|
return $styles;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 单元格纯色填充
|
||||||
|
*/
|
||||||
|
private function fillCell(Worksheet $sheet, string $coordinate, string $argb): void
|
||||||
|
{
|
||||||
|
$sheet->getStyle($coordinate)
|
||||||
|
->getFill()
|
||||||
|
->setFillType(Fill::FILL_SOLID)
|
||||||
|
->getStartColor()
|
||||||
|
->setARGB($argb);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,18 +10,23 @@ use Maatwebsite\Excel\Concerns\WithStrictNullComparison;
|
|||||||
use Maatwebsite\Excel\Concerns\WithStyles;
|
use Maatwebsite\Excel\Concerns\WithStyles;
|
||||||
use Maatwebsite\Excel\Concerns\WithTitle;
|
use Maatwebsite\Excel\Concerns\WithTitle;
|
||||||
use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
|
use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
|
||||||
|
use PhpOffice\PhpSpreadsheet\Style\Alignment;
|
||||||
|
use PhpOffice\PhpSpreadsheet\Style\Border;
|
||||||
use PhpOffice\PhpSpreadsheet\Style\Fill;
|
use PhpOffice\PhpSpreadsheet\Style\Fill;
|
||||||
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
|
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 供应商采购明细导出 · 单市场工作表
|
* 供应商采购明细导出 · 单市场工作表
|
||||||
* 模板列序:品名、汇总、市场、各门店明细(门店列=该供应商有明细的门店);
|
* 模板列序:品名、汇总、市场、各门店明细(门店列=该供应商有明细的门店);
|
||||||
* 有数据的单元格(品名/汇总/门店数量)填充突出颜色并加粗
|
* 汇总列(浅黄)/门店数量列(浅蓝)按值条件填色(0 不填、列头固定填色)
|
||||||
*/
|
*/
|
||||||
class PurchaseSupplierSheet implements FromCollection, WithStrictNullComparison, WithStyles, WithTitle
|
class PurchaseSupplierSheet implements FromCollection, WithStrictNullComparison, WithStyles, WithTitle
|
||||||
{
|
{
|
||||||
/** 有数据单元格填充色(浅黄,突出显示) */
|
/** 汇总列填充色(浅黄) */
|
||||||
private const string DATA_FILL = 'FFFFFF99';
|
private const string QUANTITY_FILL = 'FFFFF2CC';
|
||||||
|
|
||||||
|
/** 门店数量列填充色(浅蓝) */
|
||||||
|
private const string STORE_FILL = 'FFDDEBF7';
|
||||||
|
|
||||||
private ?Collection $rows = null;
|
private ?Collection $rows = null;
|
||||||
|
|
||||||
@@ -31,8 +36,14 @@ class PurchaseSupplierSheet implements FromCollection, WithStrictNullComparison,
|
|||||||
/** 列头所在行索引 */
|
/** 列头所在行索引 */
|
||||||
private int $headerRow = 1;
|
private int $headerRow = 1;
|
||||||
|
|
||||||
/** @var array<int, array<int, int>> 有数据的单元格:行索引(1 起)=> 列索引(1 起)列表 */
|
/** 数据末行索引 */
|
||||||
private array $dataCells = [];
|
private int $lastRow = 1;
|
||||||
|
|
||||||
|
/** @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 PurchaseOrderModel $purchase 采购单
|
||||||
@@ -79,7 +90,7 @@ class PurchaseSupplierSheet implements FromCollection, WithStrictNullComparison,
|
|||||||
$this->specialRows[++$rowIndex] = 'header';
|
$this->specialRows[++$rowIndex] = 'header';
|
||||||
$this->headerRow = $rowIndex;
|
$this->headerRow = $rowIndex;
|
||||||
|
|
||||||
// 明细行(有数据的单元格记录到 dataCells:品名/汇总/门店数量)
|
// 明细行(0 数量的门店格留空不填色)
|
||||||
$totalQuantity = 0;
|
$totalQuantity = 0;
|
||||||
$storeTotals = array_fill_keys($storeIds, 0);
|
$storeTotals = array_fill_keys($storeIds, 0);
|
||||||
foreach ($this->productRows as $productRow) {
|
foreach ($this->productRows as $productRow) {
|
||||||
@@ -88,17 +99,18 @@ class PurchaseSupplierSheet implements FromCollection, WithStrictNullComparison,
|
|||||||
(int) $productRow['quantity'],
|
(int) $productRow['quantity'],
|
||||||
$this->marketLabel,
|
$this->marketLabel,
|
||||||
];
|
];
|
||||||
$filled = [1, 2, 3];
|
$storeQuantities = [];
|
||||||
foreach ($storeIds as $position => $storeId) {
|
foreach ($storeIds as $storeId) {
|
||||||
$quantity = (int) ($productRow['store_quantities'][$storeId] ?? 0);
|
$quantity = (int) ($productRow['store_quantities'][$storeId] ?? 0);
|
||||||
$line[] = $quantity > 0 ? $quantity : '';
|
$line[] = $quantity > 0 ? $quantity : '';
|
||||||
if ($quantity > 0) {
|
$storeQuantities[$storeId] = $quantity;
|
||||||
$filled[] = 4 + $position;
|
$storeTotals[$storeId] += $quantity;
|
||||||
$storeTotals[$storeId] += $quantity;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
$rows[] = $line;
|
$rows[] = $line;
|
||||||
$this->dataCells[++$rowIndex] = $filled;
|
$this->rowData[++$rowIndex] = [
|
||||||
|
'quantity' => (int) $productRow['quantity'],
|
||||||
|
'store_quantities' => $storeQuantities,
|
||||||
|
];
|
||||||
$totalQuantity += (int) $productRow['quantity'];
|
$totalQuantity += (int) $productRow['quantity'];
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -108,6 +120,8 @@ class PurchaseSupplierSheet implements FromCollection, WithStrictNullComparison,
|
|||||||
array_map(static fn (int $storeId): int => $storeTotals[$storeId], $storeIds),
|
array_map(static fn (int $storeId): int => $storeTotals[$storeId], $storeIds),
|
||||||
);
|
);
|
||||||
$this->specialRows[++$rowIndex] = 'summary';
|
$this->specialRows[++$rowIndex] = 'summary';
|
||||||
|
$this->lastRow = $rowIndex;
|
||||||
|
$this->summaryTotals = ['quantity' => $totalQuantity, 'stores' => $storeTotals];
|
||||||
|
|
||||||
return $this->rows = collect($rows);
|
return $this->rows = collect($rows);
|
||||||
}
|
}
|
||||||
@@ -118,7 +132,8 @@ class PurchaseSupplierSheet implements FromCollection, WithStrictNullComparison,
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 标题/列头/合计加粗,有数据的单元格填充突出颜色,冻结列头
|
* 标题/列头/合计加粗;全表居中 + 全边框;
|
||||||
|
* 汇总列(浅黄)/门店数量列(浅蓝)按值条件填色(0 不填、列头固定填色),冻结列头
|
||||||
*/
|
*/
|
||||||
public function styles(Worksheet $sheet): array
|
public function styles(Worksheet $sheet): array
|
||||||
{
|
{
|
||||||
@@ -130,19 +145,46 @@ class PurchaseSupplierSheet implements FromCollection, WithStrictNullComparison,
|
|||||||
foreach ($widths as $index => $width) {
|
foreach ($widths as $index => $width) {
|
||||||
$sheet->getColumnDimension(Coordinate::stringFromColumnIndex($index + 1))->setWidth($width);
|
$sheet->getColumnDimension(Coordinate::stringFromColumnIndex($index + 1))->setWidth($width);
|
||||||
}
|
}
|
||||||
$storeCount = count($this->stores);
|
$storeIds = array_map('intval', array_keys($this->stores));
|
||||||
for ($i = 0; $i < $storeCount; $i++) {
|
$lastColumn = Coordinate::stringFromColumnIndex(3 + max(count($storeIds), 1));
|
||||||
|
foreach ($storeIds as $i => $storeId) {
|
||||||
$sheet->getColumnDimension(Coordinate::stringFromColumnIndex(4 + $i))->setWidth(12);
|
$sheet->getColumnDimension(Coordinate::stringFromColumnIndex(4 + $i))->setWidth(12);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 有数据的单元格:填充突出颜色并加粗(含品名/汇总/门店数量)
|
// 全部单元格水平/垂直居中
|
||||||
foreach ($this->dataCells as $rowIndex => $columns) {
|
$sheet->getStyle('A1:' . $lastColumn . $this->lastRow)
|
||||||
foreach ($columns as $columnIndex) {
|
->getAlignment()
|
||||||
$style = $sheet->getStyle(Coordinate::stringFromColumnIndex($columnIndex) . $rowIndex);
|
->setHorizontal(Alignment::HORIZONTAL_CENTER)
|
||||||
$style->getFill()
|
->setVertical(Alignment::VERTICAL_CENTER);
|
||||||
->setFillType(Fill::FILL_SOLID)
|
|
||||||
->getStartColor()->setARGB(self::DATA_FILL);
|
// 表格区域(列头 → 合计行)添加所有边框
|
||||||
$style->getFont()->setBold(true);
|
$sheet->getStyle('A' . $this->headerRow . ':' . $lastColumn . $this->lastRow)
|
||||||
|
->getBorders()
|
||||||
|
->getAllBorders()
|
||||||
|
->setBorderStyle(Border::BORDER_THIN);
|
||||||
|
|
||||||
|
// 汇总列(B):有数据浅黄,0 无背景,列头固定浅黄
|
||||||
|
$this->fillCell($sheet, 'B' . $this->headerRow, self::QUANTITY_FILL);
|
||||||
|
foreach ($this->rowData as $row => $data) {
|
||||||
|
if ($data['quantity'] > 0) {
|
||||||
|
$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) {
|
||||||
|
$column = Coordinate::stringFromColumnIndex(4 + $i);
|
||||||
|
$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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -160,4 +202,16 @@ class PurchaseSupplierSheet implements FromCollection, WithStrictNullComparison,
|
|||||||
|
|
||||||
return $styles;
|
return $styles;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 单元格纯色填充
|
||||||
|
*/
|
||||||
|
private function fillCell(Worksheet $sheet, string $coordinate, string $argb): void
|
||||||
|
{
|
||||||
|
$sheet->getStyle($coordinate)
|
||||||
|
->getFill()
|
||||||
|
->setFillType(Fill::FILL_SOLID)
|
||||||
|
->getStartColor()
|
||||||
|
->setARGB($argb);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -142,7 +142,7 @@ class ExportTest extends ProcurementTestCase
|
|||||||
{
|
{
|
||||||
[$purchase, $veg, $meat, $storeA, $storeB] = $this->buildPurchaseWithSuppliers();
|
[$purchase, $veg, $meat, $storeA, $storeB] = $this->buildPurchaseWithSuppliers();
|
||||||
// 无订货的上架商品也应出现在导出中
|
// 无订货的上架商品也应出现在导出中
|
||||||
$extra = ProductModel::factory()->create(['status' => ProductModel::STATUS_ON, 'cost_price' => '7.00']);
|
$extra = ProductModel::factory()->create(['status' => ProductModel::STATUS_ON, 'cost_price' => '7.00', 'spec' => '10斤/箱']);
|
||||||
|
|
||||||
Excel::fake();
|
Excel::fake();
|
||||||
$this->actingAsSysUser();
|
$this->actingAsSysUser();
|
||||||
@@ -170,10 +170,10 @@ class ExportTest extends ProcurementTestCase
|
|||||||
|| (float) $vegRow[12] !== 2.0 || (float) $vegRow[13] !== 3.0) {
|
|| (float) $vegRow[12] !== 2.0 || (float) $vegRow[13] !== 3.0) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
// 无订货商品行:数量 0、单价留空、门店列 0
|
// 无订货商品行:数量 0、单价按成本价÷包规显示(7÷10=0.70)、门店列 0
|
||||||
$extraRow = $rows->firstWhere(2, $extra->name);
|
$extraRow = $rows->firstWhere(2, $extra->name);
|
||||||
if ($extraRow === null
|
if ($extraRow === null
|
||||||
|| (float) $extraRow[9] !== 0.0 || $extraRow[8] !== ''
|
|| (float) $extraRow[9] !== 0.0 || (float) $extraRow[8] !== 0.7
|
||||||
|| (float) $extraRow[12] !== 0.0) {
|
|| (float) $extraRow[12] !== 0.0) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user