From fb0fdacddd9a6ec2dc832f89e9c178a9c69aaf75 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8B=8F=E6=9C=A8?= <594326199@qq.com> Date: Fri, 27 Nov 2026 09:33:01 +0800 Subject: [PATCH] =?UTF-8?q?feat(=E4=BE=9B=E5=BA=94=E5=95=86=E5=BA=94?= =?UTF-8?q?=E4=BB=98):=20=E5=A2=9E=E5=8A=A0=E7=BB=88=E7=89=88CSV=E6=89=B9?= =?UTF-8?q?=E5=A4=84=E7=90=86=E5=AF=BC=E5=85=A5=E5=91=BD=E4=BB=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Console/Commands/ImportPayablesFinal.php | 118 ++++++++++++++++++ deploy/finance-batch-imports.json | 1 + .../PayablesFinalImportCommandTest.php | 46 +++++++ 3 files changed, 165 insertions(+) create mode 100644 app/Console/Commands/ImportPayablesFinal.php create mode 100644 tests/Feature/Business/PayablesFinalImportCommandTest.php diff --git a/app/Console/Commands/ImportPayablesFinal.php b/app/Console/Commands/ImportPayablesFinal.php new file mode 100644 index 00000000..cab9f3a0 --- /dev/null +++ b/app/Console/Commands/ImportPayablesFinal.php @@ -0,0 +1,118 @@ +option('directory')); + $section = trim((string) $this->option('section')); + $dataset = trim((string) $this->option('dataset')); + $companyId = max(1, (int) $this->option('company')); + $userId = $this->option('user') === null ? null : max(1, (int) $this->option('user')); + + if ($directory === '' || ! is_dir($directory)) { + $this->error('导入目录不存在。'); + + return self::FAILURE; + } + if (! $registry->supports($section, $dataset)) { + $this->error('不支持的终版页面。'); + + return self::FAILURE; + } + + $files = glob(rtrim($directory, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . '*.csv') ?: []; + sort($files, SORT_NATURAL); + if ($files === []) { + $this->error('导入目录中没有 CSV 文件。'); + + return self::FAILURE; + } + + $fields = $registry->fields($section, $dataset); + $storeDataset = 'final:' + . ($section === PayablesFinalRegistry::SECTION_COURIER ? 'courier' : 'supplier') + . ':' + . $dataset; + $totalCreated = 0; + $totalUpdated = 0; + $totalRows = 0; + + try { + foreach ($files as $index => $path) { + $file = new UploadedFile( + $path, + basename($path), + 'text/csv', + null, + true, + ); + $prepared = $runner->prepare( + $file, + $fields, + $storeDataset, + $companyId, + $userId, + ); + $token = $prepared['token']; + $offset = 0; + $created = 0; + $updated = 0; + + do { + $progress = $runner->process($token, $offset, null, $companyId); + $offset = $progress['offset']; + $created = $progress['created']; + $updated = $progress['updated']; + } while (! $progress['done']); + + $totalCreated += $created; + $totalUpdated += $updated; + $totalRows += (int) $prepared['total']; + $this->line(sprintf( + '[%d/%d] %s:%s 行,新增 %s,更新 %s', + $index + 1, + count($files), + basename($path), + number_format((int) $prepared['total']), + number_format($created), + number_format($updated), + )); + } + } catch (Throwable $exception) { + report($exception); + $this->error($exception->getMessage()); + + return self::FAILURE; + } + + $this->info(sprintf( + '导入完成:文件 %s 个,读取 %s 行,新增 %s,更新 %s。', + number_format(count($files)), + number_format($totalRows), + number_format($totalCreated), + number_format($totalUpdated), + )); + + return self::SUCCESS; + } +} diff --git a/deploy/finance-batch-imports.json b/deploy/finance-batch-imports.json index e191701a..e6d5e0df 100644 --- a/deploy/finance-batch-imports.json +++ b/deploy/finance-batch-imports.json @@ -47,6 +47,7 @@ "planting-details:sync-work-orders", "planting-details:import", "payables:unpriced-cycle", + "payables:final-import", "product-catalog:import-authoritative-export", "product:monthly-weighted-average:base", "store-violations:import-rules", diff --git a/tests/Feature/Business/PayablesFinalImportCommandTest.php b/tests/Feature/Business/PayablesFinalImportCommandTest.php new file mode 100644 index 00000000..ab4cb5fa --- /dev/null +++ b/tests/Feature/Business/PayablesFinalImportCommandTest.php @@ -0,0 +1,46 @@ +artisan('payables:final-import', [ + '--directory' => $directory, + '--company' => (int) $this->company->id, + '--user' => (int) $this->user->id, + ]) + ->expectsOutputToContain('文件 2 个,读取 2 行') + ->assertExitCode(0); + + $rows = app(SupplierPayableOverrideStore::class)->listRows( + 'final:supplier:payment-reconciliation', + (int) $this->company->id, + ); + $this->assertCount(2, $rows); + $this->assertSame(['供应商甲', '供应商乙'], array_column($rows, 'supplier')); + } finally { + @unlink($directory . '/part-01.csv'); + @unlink($directory . '/part-02.csv'); + @rmdir($directory); + } + } +}