72 lines
2.5 KiB
PHP
72 lines
2.5 KiB
PHP
<?php
|
||
|
||
namespace App\Console\Commands;
|
||
|
||
use Illuminate\Console\Command;
|
||
use InvalidArgumentException;
|
||
use Modules\EcommerceFinance\Services\VipshopMpOpeningBalanceImporter;
|
||
use RuntimeException;
|
||
use Throwable;
|
||
|
||
class ImportVipshopMpOpeningBalances extends Command
|
||
{
|
||
protected $signature = 'vipshop-mp:import-opening-balances
|
||
{files* : 唯品会后台账单余额表导出的 XLSX}
|
||
{--period-end= : 余额截止日期,必须是月末 YYYY-MM-DD}
|
||
{--store-map= : 后台店铺名到财务系统店铺全称的 JSON 映射}
|
||
{--dry-run : 只校验,不写数据库}';
|
||
|
||
protected $description = '导入唯品会MP后台账单余额表,作为后续月份期初余额的滚动基准';
|
||
|
||
public function handle(VipshopMpOpeningBalanceImporter $importer): int
|
||
{
|
||
$periodEnd = trim((string) $this->option('period-end'));
|
||
$mapPath = trim((string) $this->option('store-map'));
|
||
if ($periodEnd === '' || $mapPath === '') {
|
||
$this->error('缺少 --period-end 或 --store-map。');
|
||
|
||
return self::FAILURE;
|
||
}
|
||
$realMapPath = realpath($mapPath);
|
||
if ($realMapPath === false || ! is_file($realMapPath) || ! is_readable($realMapPath)) {
|
||
$this->error('店铺映射文件不存在或不可读。');
|
||
|
||
return self::FAILURE;
|
||
}
|
||
$storeMap = json_decode((string) file_get_contents($realMapPath), true, 512, JSON_THROW_ON_ERROR);
|
||
if (! is_array($storeMap)) {
|
||
$this->error('店铺映射文件必须是 JSON 对象。');
|
||
|
||
return self::FAILURE;
|
||
}
|
||
|
||
try {
|
||
$result = $importer->import(
|
||
array_values((array) $this->argument('files')),
|
||
$periodEnd,
|
||
$storeMap,
|
||
(bool) $this->option('dry-run'),
|
||
);
|
||
} catch (InvalidArgumentException|RuntimeException $exception) {
|
||
$this->error($exception->getMessage());
|
||
|
||
return self::FAILURE;
|
||
} catch (Throwable $exception) {
|
||
report($exception);
|
||
$this->error('唯品会MP期初余额导入失败,详细原因已记录到受保护日志。');
|
||
|
||
return self::FAILURE;
|
||
}
|
||
|
||
$this->table(['截止日期', '状态', '店铺数', '明细行数', '余额合计'], [[
|
||
$result['period_end'],
|
||
$result['status'],
|
||
$result['store_count'],
|
||
$result['row_count'],
|
||
$result['balance_total'],
|
||
]]);
|
||
|
||
return self::SUCCESS;
|
||
}
|
||
}
|