账单导出
This commit is contained in:
@@ -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'
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user