47 lines
2.4 KiB
PHP
47 lines
2.4 KiB
PHP
<?php
|
||
|
||
namespace App\Console\Commands;
|
||
|
||
use App\Support\DropshipFreight\DropshipFreightStore;
|
||
use Illuminate\Console\Command;
|
||
|
||
/**
|
||
* 导入代发厂家手工汇总的样子(JSON:[{"factory":"厂家名","title":"10月临沂恒思","headers":["原始","简尚核对","差异"],"labels":["运费",…],"total":"合计"}]),
|
||
* 导出代发对账时「汇总」表照它套。页面上「导入手工汇总」读的是手工做好的账单本身;这个命令给整理好的清单用(如 10 月首批)。
|
||
*/
|
||
final class ImportDropshipSummaryTemplates extends Command
|
||
{
|
||
protected $signature = 'dropship-freight:import-summary-templates {file : JSON 文件} {--month= : 这些汇总是哪个月的账单,如 2026-10}';
|
||
|
||
protected $description = '导入代发厂家手工汇总的样子,作为代发对账导出「汇总」表的模板';
|
||
|
||
public function handle(DropshipFreightStore $store): int
|
||
{
|
||
$month = (string) $this->option('month');
|
||
if (! preg_match('/^20\d{2}-(0[1-9]|1[0-2])$/', $month)) {
|
||
$this->error('--month 要写成 2026-10 这样');
|
||
|
||
return self::FAILURE;
|
||
}
|
||
$blocks = json_decode((string) @file_get_contents((string) $this->argument('file')), true);
|
||
if (! is_array($blocks)) {
|
||
$this->error('读不了 JSON 文件');
|
||
|
||
return self::FAILURE;
|
||
}
|
||
$blocks = array_values(array_filter(array_map(static fn ($b): ?array => is_array($b) && isset($b['title'], $b['headers'], $b['labels'])
|
||
&& is_array($b['headers']) && is_array($b['labels'])
|
||
? ['title' => (string) $b['title'], 'headers' => array_map('strval', $b['headers']), 'labels' => array_map('strval', $b['labels']),
|
||
'total' => isset($b['total']) ? (string) $b['total'] : '合计', 'factory' => isset($b['factory']) ? (string) $b['factory'] : null,
|
||
'source' => isset($b['source']) ? (string) $b['source'] : null]
|
||
: null, $blocks)));
|
||
$result = $store->saveSummaryTemplates($blocks, $month);
|
||
$this->info('读到 ' . $result['blocks'] . ' 家,记下 ' . count($result['saved']) . ' 家 ' . $month . ' 的汇总样子:' . implode('、', $result['saved']));
|
||
if ($result['unknown'] !== []) {
|
||
$this->warn('系统里没找到这些厂家(没有代发运费的厂家可以不管):' . implode('、', $result['unknown']));
|
||
}
|
||
|
||
return self::SUCCESS;
|
||
}
|
||
}
|