From aea68c2d2db5195d6b6faf4ca4e94f5aca3bdc73 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8B=8F=E6=9C=A8?= <594326199@qq.com> Date: Fri, 4 Dec 2026 10:01:10 +0800 Subject: [PATCH] =?UTF-8?q?feat(=E4=BE=9B=E5=BA=94=E5=95=86=E5=AF=B9?= =?UTF-8?q?=E8=B4=A6):=20=E5=8E=9F=E5=A7=8B=E8=B4=A6=E5=8D=95=E6=94=AF?= =?UTF-8?q?=E6=8C=81=20Excel=20=E5=AF=BC=E5=87=BA=20(#1720)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 原始账单支持按厂家流式导出完整 Excel,保持模板表头并限制为财务端权限。 Co-authored-by: 苏木 <594326199@qq.com> Co-committed-by: 苏木 <594326199@qq.com> --- .../Business/SupplierReconciliation.php | 19 ++++ .../Services/SupplierPayableOverrideStore.php | 22 +++++ .../Services/SupplierStatementStore.php | 6 ++ .../SupplierStatementWorkbookExporter.php | 96 +++++++++++++++++++ .../raw-statements.blade.php | 5 + routes/admin.php | 4 + .../Business/SupplierReconciliationTest.php | 32 +++++++ 7 files changed, 184 insertions(+) create mode 100644 modules/EcommerceFinance/Services/SupplierStatementWorkbookExporter.php diff --git a/app/Http/Controllers/Business/SupplierReconciliation.php b/app/Http/Controllers/Business/SupplierReconciliation.php index 3bda4f2ac..9df9aafee 100644 --- a/app/Http/Controllers/Business/SupplierReconciliation.php +++ b/app/Http/Controllers/Business/SupplierReconciliation.php @@ -15,6 +15,7 @@ use Modules\EcommerceFinance\Services\SupplierReturnConfirmation; use Modules\EcommerceFinance\Services\SupplierStatementMatcher; use Modules\EcommerceFinance\Services\SupplierStatementStore; use Modules\EcommerceFinance\Services\SupplierStatementTemplate; +use Modules\EcommerceFinance\Services\SupplierStatementWorkbookExporter; use Modules\EcommerceFinance\Services\SupplierSummaryConfirmation; use Modules\EcommerceFinance\Services\SupplierZeroDifferenceConfirmation; use Modules\EcommerceFinance\View\SupplierReconciliationPageData; @@ -38,6 +39,7 @@ class SupplierReconciliation extends Controller private readonly SupplierPortalAccess $supplierPortalAccess, private readonly SupplierSummaryConfirmation $summaryConfirmations, private readonly SupplierZeroDifferenceConfirmation $zeroDifferenceConfirmations, + private readonly SupplierStatementWorkbookExporter $statementWorkbookExporter, ) { $this->middleware('auth'); $this->middleware('company.identify'); @@ -166,6 +168,23 @@ class SupplierReconciliation extends Controller ); } + public function exportRawStatement(Request $request): StreamedResponse + { + [$supplier, $companyId] = $this->returnTarget($request); + $latestUpload = $this->statements->uploads($supplier, $companyId, 1)[0] ?? null; + if ($latestUpload === null) { + throw ValidationException::withMessages([ + 'raw_statement_export' => '该厂家还没有上传对账单,暂无原始账单可导出。', + ]); + } + + return $this->statementWorkbookExporter->download( + $this->statements->streamRows($supplier, $companyId), + $supplier, + (string) ($latestUpload['uploaded_at'] ?? ''), + ); + } + public function saveDingTalkRobot(Request $request): RedirectResponse { $request->validate([ diff --git a/modules/EcommerceFinance/Services/SupplierPayableOverrideStore.php b/modules/EcommerceFinance/Services/SupplierPayableOverrideStore.php index 6b80d74a7..5940a6970 100644 --- a/modules/EcommerceFinance/Services/SupplierPayableOverrideStore.php +++ b/modules/EcommerceFinance/Services/SupplierPayableOverrideStore.php @@ -324,6 +324,28 @@ class SupplierPayableOverrideStore ->all(); } + /** + * 按记录顺序逐行读取数据集,供大文件导出使用。 + * + * @return iterable> + */ + public function streamRows(string $dataset, int $companyId): iterable + { + $records = $this->connection()->table('supplier_payable_overrides') + ->where('company_id', $companyId) + ->where('dataset', $dataset) + ->orderBy('id') + ->select(['record_key', 'values']) + ->cursor(); + + foreach ($records as $record) { + $values = json_decode((string) $record->values, true) ?: []; + $values['_action_key'] = (string) $record->record_key; + + yield $values; + } + } + /** * 按厂家汇总指定筛选范围的数量和金额,供财务端进度概览使用。 * diff --git a/modules/EcommerceFinance/Services/SupplierStatementStore.php b/modules/EcommerceFinance/Services/SupplierStatementStore.php index 5d7189cec..edcffb2db 100644 --- a/modules/EcommerceFinance/Services/SupplierStatementStore.php +++ b/modules/EcommerceFinance/Services/SupplierStatementStore.php @@ -41,6 +41,12 @@ class SupplierStatementStore return $this->store->listRows(self::PREFIX . $supplier, $companyId); } + /** @return iterable> */ + public function streamRows(string $supplier, int $companyId): iterable + { + yield from $this->store->streamRows(self::PREFIX . $supplier, $companyId); + } + /** @return array{rows:list>,total:int} */ public function paginate(string $supplier, int $companyId, int $perPage, int $page): array { diff --git a/modules/EcommerceFinance/Services/SupplierStatementWorkbookExporter.php b/modules/EcommerceFinance/Services/SupplierStatementWorkbookExporter.php new file mode 100644 index 000000000..eb23c0506 --- /dev/null +++ b/modules/EcommerceFinance/Services/SupplierStatementWorkbookExporter.php @@ -0,0 +1,96 @@ +> $rows + */ + public function download( + iterable $rows, + string $supplier, + string $uploadedAt = '', + ): StreamedResponse { + $safeSupplier = preg_replace('/[\\\\\\/:*?"<>|]+/u', '_', $supplier) ?: '厂家'; + $timestamp = $this->fileTimestamp($uploadedAt); + $filename = $safeSupplier . '_原始账单_' . $timestamp . '.xlsx'; + + return response()->streamDownload(function () use ($rows): void { + $writer = new StreamingWorkbook(); + $writer->row('原始账单', array_values(SupplierStatementTemplate::COLUMNS), true); + + foreach ($rows as $row) { + $cells = []; + foreach (self::FIELDS as $field) { + $value = $row[$field] ?? ''; + $cells[] = in_array($field, ['qty', 'unitPrice', 'amount'], true) + ? $this->numberCell($value) + : trim((string) $value); + } + $writer->row('原始账单', $cells); + } + + $output = fopen('php://output', 'wb'); + if ($output === false) { + throw new RuntimeException('无法创建原始账单导出文件。'); + } + $writer->writeTo($output); + }, $filename, [ + 'Content-Type' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', + 'Cache-Control' => 'private, no-store', + ]); + } + + private function numberCell(mixed $value): int|float|string + { + $text = str_replace([',', ',', ' '], '', trim((string) $value)); + if ($text === '') { + return ''; + } + if (preg_match('/^-?\d+$/', $text) === 1) { + return (int) $text; + } + if (is_numeric($text)) { + return (float) $text; + } + + return (string) $value; + } + + private function fileTimestamp(string $uploadedAt): string + { + $uploadedAt = trim($uploadedAt); + if ($uploadedAt === '') { + return now('Asia/Shanghai')->format('Ymd_His'); + } + + try { + return \Illuminate\Support\Carbon::createFromFormat( + 'Y-m-d H:i:s', + $uploadedAt, + 'Asia/Shanghai', + )->format('Ymd_His'); + } catch (\Throwable) { + return now('Asia/Shanghai')->format('Ymd_His'); + } + } +} diff --git a/resources/views/business/pages/supplier-reconciliation/raw-statements.blade.php b/resources/views/business/pages/supplier-reconciliation/raw-statements.blade.php index baad7a196..61f5eb38d 100644 --- a/resources/views/business/pages/supplier-reconciliation/raw-statements.blade.php +++ b/resources/views/business/pages/supplier-reconciliation/raw-statements.blade.php @@ -294,6 +294,11 @@

当前显示最新一版上传数据,共 {{ number_format($statementTotal) }} 行。

+ @if ($latestUpload) + 导出 Excel + @endif