回筐导出

This commit is contained in:
liu
2026-08-15 00:14:19 +08:00
parent 7c3f6147ac
commit cd35213ae6
7 changed files with 459 additions and 3 deletions
@@ -2,12 +2,18 @@
namespace App\Http\Controllers\Recon;
use App\Exceptions\RepositoryException;
use App\Exports\ContainerReturnExport;
use App\Models\ContainerReturnModel;
use App\Models\StoreModel;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Carbon;
use Maatwebsite\Excel\Facades\Excel;
use Modules\AnnoRoute\Attribute\GetRoute;
use Modules\AnnoRoute\Attribute\RequestAttribute;
use Modules\Common\Http\Controllers\BaseController;
use Symfony\Component\HttpFoundation\Response;
/**
* 压回筐记录(周转筐/托盘跟账单走,生成账单时自动写入完整快照,只读)
@@ -39,4 +45,61 @@ class ContainerReturnController extends BaseController
->toArray();
return $this->success($data);
}
/**
* 汇总导出:按日期区间 + 门店导出抵扣(附加)金额矩阵,
* 行=日期(同日记录合并),列=门店,含行合计/列合计,当天门店无记录填 0
*/
#[GetRoute(route: '/export', authorize: 'export')]
public function export(Request $request): Response
{
$data = $request->validate([
'start_date' => 'required|date_format:Y-m-d',
'end_date' => 'required|date_format:Y-m-d|after_or_equal:start_date',
'store_ids' => 'nullable|string',
], [
'start_date.required' => '请选择开始日期',
'start_date.date_format' => '开始日期格式为 Y-m-d',
'end_date.required' => '请选择结束日期',
'end_date.date_format' => '结束日期格式为 Y-m-d',
'end_date.after_or_equal' => '结束日期不能早于开始日期',
]);
$startDate = $data['start_date'];
$endDate = $data['end_date'];
if (Carbon::parse($startDate)->diffInDays(Carbon::parse($endDate)) > 366) {
throw new RepositoryException('日期区间不能超过 366 天');
}
$storeIds = array_values(array_filter(
array_map(intval(...), explode(',', (string) ($data['store_ids'] ?? '')))
));
$records = ContainerReturnModel::query()
->whereBetween('created_at', [$startDate . ' 00:00:00', $endDate . ' 23:59:59'])
->when($storeIds !== [], static fn ($query) => $query->whereIn('store_id', $storeIds))
->get(['id', 'store_id', 'amount', 'created_at']);
// 已选门店:列取所选门店(无记录填 0);未选门店:列取区间内有记录的门店
$columnStoreIds = $storeIds !== []
? $storeIds
: $records->pluck('store_id')->unique()->map(static fn ($id): int => (int) $id)->all();
if ($columnStoreIds === []) {
throw new RepositoryException('所选日期区间内无压回筐记录');
}
sort($columnStoreIds);
$names = StoreModel::withTrashed()
->whereIn('id', $columnStoreIds)
->pluck('name', 'id');
$storeNames = [];
foreach ($columnStoreIds as $storeId) {
$storeNames[$storeId] = $names->get($storeId, '门店#' . $storeId);
}
return Excel::download(
new ContainerReturnExport($records, $storeNames, $startDate, $endDate),
'回筐记录_' . $startDate . '_' . $endDate . '.xlsx'
);
}
}