'代发运费账单', 'courier' => '快递账单']; public function __construct( private readonly FreightBillTemplate $template, private readonly PortalFreightBills $portal, private readonly SupplierPortalAccess $access, ) { $this->middleware('auth'); $this->middleware('company.identify'); } public function template(): StreamedResponse { return $this->template->download('运费账单上传模板.xlsx'); } public function upload(Request $request): RedirectResponse { $data = $request->validate([ 'kind' => ['required', 'in:dropship,courier'], 'month' => ['required', 'date_format:Y-m'], 'group' => ['nullable', 'integer'], 'import' => ['required', 'file', 'max:51200'], ], [], ['import' => '账单文件', 'month' => '账单月份']); $file = $request->file('import'); $extension = strtolower($file->getClientOriginalExtension()); if (! in_array($extension, ['xlsx', 'xls', 'csv'], true)) { throw ValidationException::withMessages(['freight_bill' => '只支持 xlsx、xls、csv,请用上传模板。']); } $name = $this->effectiveName($request); $original = (string) $file->getClientOriginalName(); try { $summary = $this->template->validate((string) $file->getRealPath(), $extension); } catch (RuntimeException $error) { throw ValidationException::withMessages(['freight_bill' => $error->getMessage()]); } $uploader = mb_substr((string) ($request->user()?->name ?: $request->user()?->email ?: '厂家门户') . '(门户)', 0, 64); if ($data['kind'] === 'dropship') { $factory = $this->portal->factory($name); if ($factory === null) { throw ValidationException::withMessages(['freight_bill' => '「' . $name . '」不在代发厂家里,请联系财务。']); } $id = app(DropshipFreightStore::class)->uploadFor($original, (string) $file->getRealPath(), $data['month'], (int) $factory->id, $uploader); $row = app(DropshipFreightStore::class)->db()->table('dropship_bill_files')->find($id); } else { $groups = $this->portal->groups($name); if ($groups === []) { throw ValidationException::withMessages(['freight_bill' => '「' . $name . '」不在快递账单对账分组里,请联系财务。']); } $group = count($groups) === 1 ? $groups[0] : collect($groups)->firstWhere('id', (int) ($data['group'] ?? 0)); if ($group === null) { throw ValidationException::withMessages(['freight_bill' => '请选择是哪一个快递的账单。']); } $ids = app(CourierBillStore::class)->upload([$file], $data['month'], [(int) $group->id], $uploader); $row = app(CourierBillStore::class)->db()->table('courier_bill_files')->find($ids[0] ?? 0); } if ($row === null || $row->status !== 'parsed') { throw ValidationException::withMessages(['freight_bill' => '文件已收到,但没有读入:' . ($row->message ?? '未知原因') . '。请联系财务。']); } $message = '已上传' . self::KINDS[$data['kind']] . ':' . $name . ' ' . $data['month'] . ',' . number_format($summary['rows']) . ' 行,运费合计 ' . number_format($summary['amount'], 2) . ' 元,已进入财务核对'; try { if (app(SupplierDingTalkNotifier::class)->sendFreightBillUploadReceipt($name, self::KINDS[$data['kind']], $data['month'], $original, $summary)) { $message .= ';上传回执已发到钉钉群'; } } catch (Throwable $error) { report($error); } return back()->with('success', $message . '。'); } /** 厂家/快递账号只能是钉钉组织绑定的那几家;财务账号按页面上选的那家。 */ private function effectiveName(Request $request): string { $user = $request->user(); $name = trim((string) $request->input('supplier', '')); if ($user && SupplierPortalAccess::isSupplierUser($user)) { $names = $this->access->supplierNamesFor($user); abort_if($names === [], 403, '当前钉钉账号未绑定有效厂家,请联系财务人员。'); if (count($names) === 1 && $name === '') { return $names[0]; } abort_unless(in_array($name, $names, true), 403, '该厂家不在当前账号的授权范围内。'); return $name; } if ($name === '') { throw ValidationException::withMessages(['freight_bill' => '请先选择厂家。']); } return $name; } }