feat(供应商应付): 增加终版CSV批处理导入命令

This commit is contained in:
苏木 2026-11-27 09:33:01 +08:00
parent cc204fbb97
commit fb0fdacddd
3 changed files with 165 additions and 0 deletions

View File

@ -0,0 +1,118 @@
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Illuminate\Http\UploadedFile;
use Modules\EcommerceFinance\Services\PayablesFinalImportRunner;
use Modules\EcommerceFinance\Services\PayablesFinalRegistry;
use Throwable;
class ImportPayablesFinal extends Command
{
protected $signature = 'payables:final-import
{--directory= : 已解压 CSV 文件目录}
{--section=supplier-payables-final : 终版目录}
{--dataset=payment-reconciliation : 数据集页面}
{--company=1 : 公司 ID}
{--user= : 操作人 ID}';
protected $description = '将终版页面的 CSV 文件分片导入业务数据库';
public function handle(
PayablesFinalRegistry $registry,
PayablesFinalImportRunner $runner,
): int {
$directory = trim((string) $this->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;
}
}

View File

@ -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",

View File

@ -0,0 +1,46 @@
<?php
namespace Tests\Feature\Business;
use Modules\EcommerceFinance\Services\SupplierPayableOverrideStore;
use Tests\Feature\FeatureTestCase;
class PayablesFinalImportCommandTest extends FeatureTestCase
{
public function test_command_imports_every_csv_in_a_directory(): void
{
$directory = sys_get_temp_dir() . '/payables-final-command-' . uniqid('', true);
mkdir($directory, 0777, true);
file_put_contents(
$directory . '/part-01.csv',
"\xEF\xBB\xBF月份,供应商,货号,商品编码,规格名称,数量,单价,金额\n"
. "2026-09,供应商甲,SKU-1,BAR-1,规格甲,2,10,20\n",
);
file_put_contents(
$directory . '/part-02.csv',
"\xEF\xBB\xBF月份,供应商,货号,商品编码,规格名称,数量,单价,金额\n"
. "2026-09,供应商乙,SKU-2,BAR-2,规格乙,1,30,30\n",
);
try {
$this->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);
}
}
}