账单导出

This commit is contained in:
liu
2026-09-13 23:42:35 +08:00
parent 5784875939
commit 80cbd452e5
12 changed files with 112 additions and 228 deletions
+23 -38
View File
@@ -16,7 +16,7 @@ use PhpOffice\PhpSpreadsheet\Style\Fill;
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
/**
* 门店账单合并导出:勾选账单跨账单按商品合并明细(可按一级分类过滤),
* 门店账单单独导出:账单内明细按商品合并(可按一级分类过滤),
* 表尾汇总商品金额/配送费/附加金额/售后金额/总金额
*/
class BillExport implements FromCollection, WithStyles
@@ -48,11 +48,11 @@ class BillExport implements FromCollection, WithStyles
private int $lastRow = 1;
/**
* @param Collection<int, BillModel> $bills 勾选账单(已按 bill_date/id 排序,with store)
* @param BillModel $bill 导出账单(with store)
* @param int $categoryId 一级分类ID(0=全部)
*/
public function __construct(
private readonly Collection $bills,
private readonly BillModel $bill,
private readonly int $categoryId = 0,
) {
}
@@ -66,35 +66,29 @@ class BillExport implements FromCollection, WithStyles
return $this->rows;
}
$items = app(BillDetailService::class)->mergedItemsOfBills(
$this->bills->pluck('id')->map(static fn ($id): int => (int) $id)->all()
);
$items = app(BillDetailService::class)->mergedItems($this->bill);
$categoryName = '全部';
if ($this->categoryId > 0) {
[$items, $categoryName] = $this->filterByRootCategory($items);
}
$bill = $this->bill;
$rows = [];
$rowIndex = 0;
// 标题行
$rows[] = ['门店账单合并导出(共 ' . $this->bills->count() . ' 张)'];
$rows[] = ['门店账单 ' . $bill->bill_no];
$this->specialRows[++$rowIndex] = 'title';
// 范围行:账单号 / 门店 / 分类 / 导出时间
$billNos = $this->bills->pluck('bill_no')->all();
$billNoText = count($billNos) > 8
? implode('、', array_slice($billNos, 0, 8)) . ' 等 ' . count($billNos) . ' 张'
: implode('、', $billNos);
$storeNames = $this->bills
->map(static fn (BillModel $bill): string => $bill->store->name ?? ('门店#' . $bill->store_id))
->unique()
->values()
->all();
$rows[] = ['账单号:' . $billNoText];
$this->specialRows[++$rowIndex] = 'scope';
$rows[] = ['门店:' . implode('、', $storeNames) . ' 分类:' . $categoryName . ' 导出时间:' . now('Asia/Shanghai')->format('Y-m-d H:i:s')];
// 范围行:门店 / 账单日期 / 分类 / 导出时间
$storeName = $bill->store->name ?? ('门店#' . $bill->store_id);
$rows[] = [
'门店:' . $storeName
. ' 账单日期:' . $bill->bill_date?->toDateString()
. ' 分类:' . $categoryName
. ' 导出时间:' . now('Asia/Shanghai')->format('Y-m-d H:i:s'),
];
$this->specialRows[++$rowIndex] = 'scope';
// 空行
@@ -106,7 +100,7 @@ class BillExport implements FromCollection, WithStyles
$this->specialRows[++$rowIndex] = 'header';
$this->headerRow = $rowIndex;
// 明细行(跨账单同商品已合并,单价为加权平均)
// 明细行(账单内同商品已合并,单价为加权平均)
$totalQuantity = 0;
$totalWeight = '0';
$totalAmount = '0';
@@ -135,32 +129,23 @@ class BillExport implements FromCollection, WithStyles
$this->fillCells[] = ['row' => $rowIndex, 'column' => 'F', 'value' => (float) $totalQuantity];
$this->fillCells[] = ['row' => $rowIndex, 'column' => 'H', 'value' => (float) $totalAmount];
// 配送费/附加金额/售后金额/总金额(账单级费用全额汇总,不受分类过滤影响)
$deliveryTotal = '0';
$addedTotal = '0';
$afterSaleTotal = '0';
$boxNum = 0;
$trayNum = 0;
foreach ($this->bills as $bill) {
$deliveryTotal = bcadd($deliveryTotal, (string) $bill->delivery_fee, 2);
$addedTotal = bcadd($addedTotal, (string) $bill->added_amount, 2);
$afterSaleTotal = bcadd($afterSaleTotal, (string) $bill->after_sale, 2);
$boxNum += (int) $bill->box_num;
$trayNum += (int) $bill->tray_num;
}
// 配送费/附加金额/售后金额/总金额(账单级费用全额计入,不受分类过滤影响)
$deliveryTotal = (string) $bill->delivery_fee;
$addedTotal = (string) $bill->added_amount;
$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];
$rows[] = ['附加金额合计(周转筐 ' . $boxNum . ' 个 / 周转托盘 ' . $trayNum . ' 个)', '', '', '', '', '', '', (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];
$rows[] = ['售后金额合计(可正负)', '', '', '', '', '', '', (float) $afterSaleTotal];
$rows[] = ['售后金额(可正负)', '', '', '', '', '', '', (float) $afterSaleTotal];
$this->specialRows[++$rowIndex] = 'summary';
$this->mergeRows[] = $rowIndex;
$this->fillCells[] = ['row' => $rowIndex, 'column' => 'H', 'value' => (float) $afterSaleTotal];
@@ -301,7 +286,7 @@ class BillExport implements FromCollection, WithStyles
));
if ($filtered === []) {
throw new RepositoryException('所选账单在「' . $categoryName . '」分类下无商品明细');
throw new RepositoryException('该账单在「' . $categoryName . '」分类下无商品明细');
}
return [$filtered, $categoryName];