账单导出
This commit is contained in:
+51
-38
@@ -7,6 +7,7 @@ use App\Models\BillModel;
|
||||
use App\Models\ProductCategoryModel;
|
||||
use App\Models\ProductModel;
|
||||
use App\Services\BillDetailService;
|
||||
use App\Services\WeightEstimator;
|
||||
use Illuminate\Support\Collection;
|
||||
use Maatwebsite\Excel\Concerns\FromCollection;
|
||||
use Maatwebsite\Excel\Concerns\WithStyles;
|
||||
@@ -35,10 +36,10 @@ class BillExport implements FromCollection, WithStyles
|
||||
/** @var array<int, string> 特殊行索引(1 起)=> 类型(title/scope/header/summary/grand) */
|
||||
private array $specialRows = [];
|
||||
|
||||
/** @var array<int, int> 标签需左合并 A:G 的合计行索引 */
|
||||
/** @var array<int, int> 标签需左合并 A:F 的合计行索引 */
|
||||
private array $mergeRows = [];
|
||||
|
||||
/** @var array<int, array{row: int, column: string, value: float}> 条件填色单元格(F=数量浅黄,H=金额浅红,非 0 填色) */
|
||||
/** @var array<int, array{row: int, column: string, value: float}> 条件填色单元格(E=数量浅黄,G=金额浅红,非 0 填色) */
|
||||
private array $fillCells = [];
|
||||
|
||||
/** 列头所在行索引 */
|
||||
@@ -95,12 +96,12 @@ class BillExport implements FromCollection, WithStyles
|
||||
$rows[] = [''];
|
||||
$rowIndex++;
|
||||
|
||||
// 列头
|
||||
$rows[] = ['序号', '品名', '包规', '单位', '单价', '数量', '重量(斤)', '金额'];
|
||||
// 列头(包规=包规+单位合并;单价=每件零售价+单价单位)
|
||||
$rows[] = ['序号', '品名', '包规', '单价', '数量', '重量(斤)', '金额'];
|
||||
$this->specialRows[++$rowIndex] = 'header';
|
||||
$this->headerRow = $rowIndex;
|
||||
|
||||
// 明细行(账单内同商品已合并,单价为加权平均)
|
||||
// 明细行(账单内同商品已合并,单价为每件价折算的零售价)
|
||||
$totalQuantity = 0;
|
||||
$totalWeight = '0';
|
||||
$totalAmount = '0';
|
||||
@@ -108,26 +109,25 @@ class BillExport implements FromCollection, WithStyles
|
||||
$rows[] = [
|
||||
$sort + 1,
|
||||
$item['product_name'],
|
||||
$item['product_spec'],
|
||||
$item['unit'],
|
||||
(float) $item['price'],
|
||||
$item['product_spec'] . $item['unit'],
|
||||
self::retailPriceLabel($item),
|
||||
(int) $item['quantity'],
|
||||
(float) $item['weight'],
|
||||
(float) $item['amount'],
|
||||
];
|
||||
$rowIndex++;
|
||||
$this->fillCells[] = ['row' => $rowIndex, 'column' => 'F', 'value' => (float) $item['quantity']];
|
||||
$this->fillCells[] = ['row' => $rowIndex, 'column' => 'H', 'value' => (float) $item['amount']];
|
||||
$this->fillCells[] = ['row' => $rowIndex, 'column' => 'E', 'value' => (float) $item['quantity']];
|
||||
$this->fillCells[] = ['row' => $rowIndex, 'column' => 'G', 'value' => (float) $item['amount']];
|
||||
$totalQuantity += (int) $item['quantity'];
|
||||
$totalWeight = bcadd($totalWeight, $item['weight'], 3);
|
||||
$totalAmount = bcadd($totalAmount, $item['amount'], 2);
|
||||
}
|
||||
|
||||
// 商品合计行
|
||||
$rows[] = ['', '合计', '', '', '', $totalQuantity, (float) $totalWeight, (float) $totalAmount];
|
||||
$rows[] = ['', '合计', '', '', $totalQuantity, (float) $totalWeight, (float) $totalAmount];
|
||||
$this->specialRows[++$rowIndex] = 'summary';
|
||||
$this->fillCells[] = ['row' => $rowIndex, 'column' => 'F', 'value' => (float) $totalQuantity];
|
||||
$this->fillCells[] = ['row' => $rowIndex, 'column' => 'H', 'value' => (float) $totalAmount];
|
||||
$this->fillCells[] = ['row' => $rowIndex, 'column' => 'E', 'value' => (float) $totalQuantity];
|
||||
$this->fillCells[] = ['row' => $rowIndex, 'column' => 'G', 'value' => (float) $totalAmount];
|
||||
|
||||
// 配送费/附加金额/售后金额/总金额(账单级费用全额计入,不受分类过滤影响)
|
||||
$deliveryTotal = (string) $bill->delivery_fee;
|
||||
@@ -135,32 +135,32 @@ class BillExport implements FromCollection, WithStyles
|
||||
$afterSaleTotal = (string) $bill->after_sale;
|
||||
$grandTotal = bcadd(bcadd(bcadd($totalAmount, $deliveryTotal, 2), $addedTotal, 2), $afterSaleTotal, 2);
|
||||
|
||||
$rows[] = ['配送费', '', '', '', '', '', '', (float) $deliveryTotal];
|
||||
$rows[] = ['配送费', '', '', '', '', '', (float) $deliveryTotal];
|
||||
$this->specialRows[++$rowIndex] = 'summary';
|
||||
$this->mergeRows[] = $rowIndex;
|
||||
$this->fillCells[] = ['row' => $rowIndex, 'column' => 'H', 'value' => (float) $deliveryTotal];
|
||||
$this->fillCells[] = ['row' => $rowIndex, 'column' => 'G', 'value' => (float) $deliveryTotal];
|
||||
|
||||
$rows[] = ['附加金额(周转筐 ' . (int) $bill->box_num . ' 个 / 周转托盘 ' . (int) $bill->tray_num . ' 个)', '', '', '', '', '', '', (float) $addedTotal];
|
||||
$rows[] = ['附加金额(周转筐 ' . (int) $bill->box_num . ' 个 / 周转托盘 ' . (int) $bill->tray_num . ' 个)', '', '', '', '', '', (float) $addedTotal];
|
||||
$this->specialRows[++$rowIndex] = 'summary';
|
||||
$this->mergeRows[] = $rowIndex;
|
||||
$this->fillCells[] = ['row' => $rowIndex, 'column' => 'H', 'value' => (float) $addedTotal];
|
||||
$this->fillCells[] = ['row' => $rowIndex, 'column' => 'G', 'value' => (float) $addedTotal];
|
||||
|
||||
$rows[] = ['售后金额(可正负)', '', '', '', '', '', '', (float) $afterSaleTotal];
|
||||
$rows[] = ['售后金额(可正负)', '', '', '', '', '', (float) $afterSaleTotal];
|
||||
$this->specialRows[++$rowIndex] = 'summary';
|
||||
$this->mergeRows[] = $rowIndex;
|
||||
$this->fillCells[] = ['row' => $rowIndex, 'column' => 'H', 'value' => (float) $afterSaleTotal];
|
||||
$this->fillCells[] = ['row' => $rowIndex, 'column' => 'G', 'value' => (float) $afterSaleTotal];
|
||||
|
||||
$rows[] = ['总金额(商品金额+配送费+附加金额+售后金额)', '', '', '', '', '', '', (float) $grandTotal];
|
||||
$rows[] = ['总金额(商品金额+配送费+附加金额+售后金额)', '', '', '', '', '', (float) $grandTotal];
|
||||
$this->specialRows[++$rowIndex] = 'grand';
|
||||
$this->mergeRows[] = $rowIndex;
|
||||
$this->fillCells[] = ['row' => $rowIndex, 'column' => 'H', 'value' => (float) $grandTotal];
|
||||
$this->fillCells[] = ['row' => $rowIndex, 'column' => 'G', 'value' => (float) $grandTotal];
|
||||
$this->lastRow = $rowIndex;
|
||||
|
||||
return $this->rows = collect($rows);
|
||||
}
|
||||
|
||||
/**
|
||||
* 标题/列头/合计行加粗,合计行标签左合并 A:G;全表居中 + 全边框;金额类列货币格式;
|
||||
* 标题/列头/合计行加粗,合计行标签左合并 A:F;全表居中 + 全边框;金额列货币格式;
|
||||
* 数量(浅黄)/金额(浅红)非 0 条件填色(0 不填、列头固定填色),冻结列头
|
||||
*/
|
||||
public function styles(Worksheet $sheet): array
|
||||
@@ -171,18 +171,17 @@ class BillExport implements FromCollection, WithStyles
|
||||
$sheet->getColumnDimension('A')->setWidth(6);
|
||||
$sheet->getColumnDimension('B')->setWidth(24);
|
||||
$sheet->getColumnDimension('C')->setWidth(14);
|
||||
$sheet->getColumnDimension('D')->setWidth(8);
|
||||
$sheet->getColumnDimension('D')->setWidth(12);
|
||||
$sheet->getColumnDimension('E')->setWidth(10);
|
||||
$sheet->getColumnDimension('F')->setWidth(10);
|
||||
$sheet->getColumnDimension('F')->setWidth(12);
|
||||
$sheet->getColumnDimension('G')->setWidth(12);
|
||||
$sheet->getColumnDimension('H')->setWidth(12);
|
||||
|
||||
foreach ($this->mergeRows as $row) {
|
||||
$sheet->mergeCells('A' . $row . ':G' . $row);
|
||||
$sheet->mergeCells('A' . $row . ':F' . $row);
|
||||
}
|
||||
|
||||
// 全部单元格水平/垂直居中(合计行标签保持左对齐)
|
||||
$sheet->getStyle('A1:H' . $this->lastRow)
|
||||
$sheet->getStyle('A1:G' . $this->lastRow)
|
||||
->getAlignment()
|
||||
->setHorizontal(Alignment::HORIZONTAL_CENTER)
|
||||
->setVertical(Alignment::VERTICAL_CENTER);
|
||||
@@ -191,21 +190,19 @@ class BillExport implements FromCollection, WithStyles
|
||||
}
|
||||
|
||||
// 表格区域(列头 → 末行)添加所有边框
|
||||
$sheet->getStyle('A' . $this->headerRow . ':H' . $this->lastRow)
|
||||
$sheet->getStyle('A' . $this->headerRow . ':G' . $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);
|
||||
}
|
||||
// 金额列货币格式:¥ + 两位小数(单价列为文本,不设格式)
|
||||
$sheet->getStyle('G' . ($this->headerRow + 1) . ':G' . $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);
|
||||
// 列头固定填色:数量列(E)浅黄、金额列(G)浅红
|
||||
$this->fillCell($sheet, 'E' . $this->headerRow, self::QUANTITY_FILL);
|
||||
$this->fillCell($sheet, 'G' . $this->headerRow, self::AMOUNT_FILL);
|
||||
|
||||
// 数据区条件填色:非 0 填色,0 无背景(金额含负值)
|
||||
foreach ($this->fillCells as $cell) {
|
||||
@@ -213,7 +210,7 @@ class BillExport implements FromCollection, WithStyles
|
||||
$this->fillCell(
|
||||
$sheet,
|
||||
$cell['column'] . $cell['row'],
|
||||
$cell['column'] === 'F' ? self::QUANTITY_FILL : self::AMOUNT_FILL
|
||||
$cell['column'] === 'E' ? self::QUANTITY_FILL : self::AMOUNT_FILL
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -246,6 +243,22 @@ class BillExport implements FromCollection, WithStyles
|
||||
->setARGB($argb);
|
||||
}
|
||||
|
||||
/**
|
||||
* 单价列文本:每件零售价(明细每件价 ÷ 包规折算每件斤数)+ 单价单位(如「1.74元/斤」);
|
||||
* 包规无法折算(散装/无数字)时按每件原价显示
|
||||
*
|
||||
* @param array{price: string, product_spec: string, unit: string, price_unit?: string} $item
|
||||
*/
|
||||
private static function retailPriceLabel(array $item): string
|
||||
{
|
||||
$jinPerPiece = WeightEstimator::perUnitJin($item['product_spec'], $item['unit']);
|
||||
$retail = (float) $jinPerPiece > 0
|
||||
? bcdiv($item['price'], $jinPerPiece, 2)
|
||||
: $item['price'];
|
||||
|
||||
return $retail . ($item['price_unit'] ?? '');
|
||||
}
|
||||
|
||||
/**
|
||||
* 按一级分类过滤明细:沿 parent_id 上溯取根分类(商品含软删除,保证历史账单可导出),
|
||||
* 仅保留根分类为所选分类的行
|
||||
|
||||
Reference in New Issue
Block a user