diff --git a/app/Console/Commands/ImportInventoryInboundDetail.php b/app/Console/Commands/ImportInventoryInboundDetail.php index baa58da0..212aab56 100644 --- a/app/Console/Commands/ImportInventoryInboundDetail.php +++ b/app/Console/Commands/ImportInventoryInboundDetail.php @@ -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 仓库名 => 仓库简称(仅唯一匹配的) */ + /** + * @return array{warehouse:array,provider:array} + * 仅保留能唯一匹配的键 + */ 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