入库明细仓库简称匹配规则:仓库名匹配,含代发仓字样的按仓储方匹配

This commit is contained in:
chenxi 2026-09-30 16:43:19 +08:00
parent fdb7f77173
commit 1c679f0215

View File

@ -25,8 +25,8 @@ class ImportInventoryInboundDetail extends Command
$forcedMonth = trim((string) ($this->option('month') ?? ''));
$now = now();
$shortNames = $this->shortNameMap();
$this->info('仓库名称映射:' . count($shortNames) . ' 个仓库名可唯一匹配');
$maps = $this->shortNameMap();
$this->info('仓库名称映射:仓库名 ' . count($maps['warehouse']) . ' 个、仓储方 ' . count($maps['provider']) . ' 个可唯一匹配');
// 原样导入:以本次文件为准整体重建,不做去重、不做加工。
DB::connection($connection)->table($table)->delete();
@ -58,7 +58,13 @@ class ImportInventoryInboundDetail extends Command
continue;
}
$shortName = $warehouse !== '' ? ($shortNames[$warehouse] ?? '') : '';
// 一般用"仓库"匹配仓库简称;含"代发仓"字样的按"仓储方"匹配。
$shortName = '';
if ($warehouse !== '') {
$shortName = str_contains($warehouse, '代发仓')
? ($maps['provider'][$warehouse] ?? $maps['warehouse'][$warehouse] ?? '')
: ($maps['warehouse'][$warehouse] ?? '');
}
if ($shortName !== '') {
$mapped++;
}
@ -114,7 +120,10 @@ class ImportInventoryInboundDetail extends Command
return self::SUCCESS;
}
/** @return array<string,string> 仓库名 => 仓库简称(仅唯一匹配的) */
/**
* @return array{warehouse:array<string,string>,provider:array<string,string>}
* 仅保留能唯一匹配的键
*/
private function shortNameMap(): array
{
$rows = InventoryWarehouseNameMapping::withoutGlobalScopes()
@ -128,16 +137,14 @@ class ImportInventoryInboundDetail extends Command
$grouped[$warehouse][] = trim((string) $row->warehouse_short_name);
}
$map = [];
$byWarehouse = [];
foreach ($grouped as $warehouse => $names) {
$unique = array_values(array_unique($names));
if (count($unique) === 1 && $unique[0] !== '') {
$map[$warehouse] = $unique[0];
$byWarehouse[$warehouse] = $unique[0];
}
}
// 明细表里“仓库”一列有时填的是仓储方名称(例如"工厂-沐野竹木代发仓"),
// 仓库名匹配不到时再按仓储方唯一匹配一次。
$byProvider = [];
foreach ($rows as $row) {
$provider = trim((string) $row->warehouse_provider);
@ -147,16 +154,13 @@ class ImportInventoryInboundDetail extends Command
$byProvider[$provider][] = trim((string) $row->warehouse_short_name);
}
foreach ($byProvider as $provider => $names) {
if (isset($map[$provider])) {
continue;
}
$unique = array_values(array_unique($names));
if (count($unique) === 1 && $unique[0] !== '') {
$map[$provider] = $unique[0];
$byProvider[$provider] = $unique[0];
}
}
return $map;
return ['warehouse' => $byWarehouse, 'provider' => $byProvider];
}
private function monthOf(string $value): string