validate(['file' => ['required', 'file', 'max:51200']]); $file = $request->file('file'); $name = (string) $file->getClientOriginalName(); $redirect = redirect()->route('business.pages.show', [ 'company_id' => company_id(), 'section' => 'planting-data', 'page' => 'planting-reimbursement-workbench', 'date' => (string) $request->input('date', ''), 'paid_from' => (string) $request->input('paid_from', ''), 'scope' => (string) $request->input('scope', ''), ]); try { $rows = $references->parse($kind, (string) $file->getRealPath(), $name); $references->replace((int) company_id(), $kind, $rows, $name, optional(user())->id); } catch (InvalidArgumentException $exception) { return $redirect->with('error', References::KINDS[$kind] . '上传失败:' . $exception->getMessage()); } catch (\Throwable $exception) { report($exception); return $redirect->with('error', References::KINDS[$kind] . '上传失败,请检查文件格式后重新上传。'); } return $redirect->with('success', sprintf('%s已更新:%s 条。', References::KINDS[$kind], number_format(count($rows)))); } public function export(string $type, PageData $page, Exporter $exporter) { abort_unless(isset(Exporter::TYPES[$type]), 404); $filters = $page->filters(); // 导出前必须先在聚工单核对条数、金额一致。 if (request('verified') !== '1') { return redirect()->route('business.pages.show', [ 'company_id' => company_id(), 'section' => 'planting-data', 'page' => 'planting-reimbursement-workbench', ] + array_diff_key($filters, ['decision' => true]))->with('error', '请先在聚工单按同样条件查询,核对条数和最终报销金额一致后,勾选「已核对」再导出。'); } try { $result = $page->build($filters); $spreadsheet = $exporter->build($type, $result); } catch (\Throwable $exception) { report($exception); abort(503, '聚工单镜像未就绪,未生成不完整的导出文件,请稍后重试。'); } return response()->streamDownload(static function () use ($spreadsheet): void { (new Xlsx($spreadsheet))->save('php://output'); }, $exporter->fileName($type, $result['summary']), [ 'Content-Type' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', ]); } }