账单导出
This commit is contained in:
+23
-38
@@ -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];
|
||||
|
||||
@@ -87,41 +87,28 @@ class BillController extends BaseMiniController
|
||||
}
|
||||
|
||||
/**
|
||||
* 合并导出账单(静态路由放在 /bill/{id} 之前声明)
|
||||
* 仅可导出本店账单(强制 store_id 过滤,张数不匹配说明混入无效账单整批拒绝);
|
||||
* ?ids=1,2,3 必选(1~100 张);?category_id= 一级分类过滤(0/缺省=全部)
|
||||
* 单独导出账单:仅可导出本店账单(强制 store_id 过滤);
|
||||
* ?category_id= 一级分类过滤(0/缺省=全部)
|
||||
*/
|
||||
#[GetRoute('/bill/export', authorize: true)]
|
||||
public function export(Request $request): Response
|
||||
#[GetRoute('/bill/{id}/export', authorize: true, where: ['id' => '[0-9]+'])]
|
||||
public function export(int $id, Request $request): Response
|
||||
{
|
||||
$data = $request->validate([
|
||||
'ids' => 'required|string',
|
||||
'category_id' => 'nullable|integer|min:0',
|
||||
]);
|
||||
|
||||
$store = $this->currentStore($request);
|
||||
|
||||
$ids = array_values(array_unique(array_filter(array_map('intval', explode(',', (string) $data['ids'])))));
|
||||
if ($ids === [] || count($ids) > 100) {
|
||||
throw new RepositoryException('请选择 1~100 张账单');
|
||||
}
|
||||
|
||||
$bills = BillModel::with('store:id,name')
|
||||
$bill = BillModel::with('store:id,name')
|
||||
->where('store_id', $store->id)
|
||||
->whereIn('id', $ids)
|
||||
->orderBy('bill_date')
|
||||
->orderBy('id')
|
||||
->get();
|
||||
if ($bills->isEmpty()) {
|
||||
->find($id);
|
||||
if ($bill === null) {
|
||||
throw new RepositoryException('账单不存在');
|
||||
}
|
||||
if ($bills->count() !== count($ids)) {
|
||||
throw new RepositoryException('存在无效账单,请刷新后重试');
|
||||
}
|
||||
|
||||
return Excel::download(
|
||||
new BillExport($bills, (int) ($data['category_id'] ?? 0)),
|
||||
'门店账单_' . now()->format('Ymd_His') . '.xlsx'
|
||||
new BillExport($bill, (int) ($data['category_id'] ?? 0)),
|
||||
'门店账单_' . $bill->bill_no . '.xlsx'
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -61,34 +61,24 @@ class BillController extends BaseController
|
||||
}
|
||||
|
||||
/**
|
||||
* 合并导出:勾选账单跨账单按商品合并明细(可按一级分类过滤),
|
||||
* 表尾汇总商品金额/配送费/附加金额/总金额
|
||||
* 单独导出账单:账单内明细按商品合并(可按一级分类过滤),
|
||||
* 表尾汇总商品金额/配送费/附加金额/售后金额/总金额
|
||||
*/
|
||||
#[GetRoute(route: '/export', authorize: 'export')]
|
||||
public function export(Request $request): Response
|
||||
#[GetRoute(route: '/{id}/export', authorize: 'export', where: ['id' => '[0-9]+'])]
|
||||
public function export(int $id, Request $request): Response
|
||||
{
|
||||
$data = $request->validate([
|
||||
'ids' => 'required|string',
|
||||
'category_id' => 'nullable|integer|min:0',
|
||||
]);
|
||||
|
||||
$ids = array_values(array_filter(array_map('intval', explode(',', (string) $data['ids']))));
|
||||
if ($ids === [] || count($ids) > 100) {
|
||||
throw new RepositoryException('请选择 1~100 张账单');
|
||||
}
|
||||
|
||||
$bills = BillModel::with('store:id,name')
|
||||
->whereIn('id', $ids)
|
||||
->orderBy('bill_date')
|
||||
->orderBy('id')
|
||||
->get();
|
||||
if ($bills->isEmpty()) {
|
||||
$bill = BillModel::with('store:id,name')->find($id);
|
||||
if (empty($bill)) {
|
||||
throw new RepositoryException('账单不存在');
|
||||
}
|
||||
|
||||
return Excel::download(
|
||||
new BillExport($bills, (int) ($data['category_id'] ?? 0)),
|
||||
'门店账单_' . now()->format('Ymd_His') . '.xlsx'
|
||||
new BillExport($bill, (int) ($data['category_id'] ?? 0)),
|
||||
'门店账单_' . $bill->bill_no . '.xlsx'
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -29,24 +29,6 @@ class BillDetailService
|
||||
return $this->aggregateItems($items);
|
||||
}
|
||||
|
||||
/**
|
||||
* 合并后的商品明细(跨账单口径):按商品聚合多张账单的全部订单明细
|
||||
* 用于账单合并导出,单价同为加权平均口径
|
||||
*
|
||||
* @param array<int, int> $billIds 账单ID列表
|
||||
* @return array<int, array{product_id: int, product_name: string, product_spec: string, unit: string, price: string, quantity: int, weight: string, amount: string, image: string}>
|
||||
*/
|
||||
public function mergedItemsOfBills(array $billIds): array
|
||||
{
|
||||
$items = StoreOrderItemModel::query()
|
||||
->whereIn('bill_id', $billIds)
|
||||
->where('quantity', '>', 0)
|
||||
->orderBy('id')
|
||||
->get();
|
||||
|
||||
return $this->aggregateItems($items);
|
||||
}
|
||||
|
||||
/**
|
||||
* 按商品聚合订单明细:数量累加、重量/金额 bc 累加、单价加权平均,
|
||||
* 首图取明细快照 image_ids 批量解析,按「分类sort → 商品sort」排序
|
||||
|
||||
Reference in New Issue
Block a user