feat(顾家过账): 按款式编码匹配品牌货号 (#1195)

品牌货号改为按款式编码匹配,物料编号继续按聚水潭原始货号匹配;同一款式下多个货号共享品牌货号。
This commit is contained in:
chenxi 2026-11-16 15:30:20 +08:00
parent b662a340e1
commit 6dff7620b6
6 changed files with 82 additions and 53 deletions

View File

@ -84,7 +84,7 @@ class KukaPostingBasePageData
return [
'item_number' => trim((string) ($values['货号'] ?? '')),
'product_code' => trim((string) ($values['商品编码'] ?? '')),
'style_code' => trim((string) ($values['款式编码'] ?? '')),
'values' => $values,
];
});
@ -94,7 +94,7 @@ class KukaPostingBasePageData
);
$brandItems = $this->repository->brandItemMaps(
$companyId,
$decodedRows->pluck('product_code')->filter()->unique()->values()->all(),
$decodedRows->pluck('style_code')->filter()->unique()->values()->all(),
);
$quotations = $this->repository->effectiveQuotations(
$companyId,
@ -105,7 +105,7 @@ class KukaPostingBasePageData
$materialNumber = trim((string) ($maps[$row['item_number']]->material_number ?? ''));
$quotation = $materialNumber === '' ? null : ($quotations[$materialNumber] ?? null);
$brandItemNumber = trim((string) (
$brandItems[$row['product_code']]->brand_item_number ?? ''
$brandItems[$row['style_code']]->brand_item_number ?? ''
));
if ($brandItemNumber !== '') {

View File

@ -63,7 +63,7 @@ class KukaPostingMaterialMapSynchronizer
[$direct, $archive, $brandSku] = $this->sourceCandidates($items);
$rows = $this->resolveRows($companyId, $items, $quotations, $direct, $archive, $brandSku);
$stats = $this->stats($rows);
$displayItemResult = $this->displayItemRows($companyId, $identifiers['product_codes']);
$displayItemResult = $this->displayItemRows($companyId, $identifiers['style_codes']);
$brandItemRows = $displayItemResult['rows'];
if (! $dryRun) {
@ -100,18 +100,18 @@ class KukaPostingMaterialMapSynchronizer
}
}
/** @return array{items:list<string>,product_codes:list<string>} */
/** @return array{items:list<string>,style_codes:list<string>} */
private function sourceIdentifiers(ConnectionInterface $connection, int $companyId, ?string $period): array
{
$items = [];
$productCodes = [];
$styleCodes = [];
$query = $connection->table('kuka_posting_base_rows')
->where('company_id', $companyId)
->when($period !== null, fn ($builder) => $builder->where('period', $period));
$query->select(['id', 'source_values'])
->orderBy('id')
->chunkById(1000, function ($rows) use (&$items, &$productCodes): void {
->chunkById(1000, function ($rows) use (&$items, &$styleCodes): void {
foreach ($rows as $row) {
$values = json_decode((string) $row->source_values, true);
if (! is_array($values)) {
@ -122,32 +122,32 @@ class KukaPostingMaterialMapSynchronizer
if ($itemNumber !== '') {
$items[$itemNumber] = true;
}
$productCode = trim((string) ($values['商品编码'] ?? ''));
if ($productCode !== '') {
$productCodes[$productCode] = true;
$styleCode = trim((string) ($values['款式编码'] ?? ''));
if ($styleCode !== '') {
$styleCodes[$styleCode] = true;
}
}
});
$items = array_keys($items);
sort($items, SORT_STRING);
$productCodes = array_keys($productCodes);
sort($productCodes, SORT_STRING);
$styleCodes = array_keys($styleCodes);
sort($styleCodes, SORT_STRING);
return ['items' => $items, 'product_codes' => $productCodes];
return ['items' => $items, 'style_codes' => $styleCodes];
}
/**
* @param list<string> $productCodes
* @param list<string> $styleCodes
* @return array{
* rows:list<array<string,mixed>>,
* brand_sku:int,
* archive_item:int
* }
*/
private function displayItemRows(int $companyId, array $productCodes): array
private function displayItemRows(int $companyId, array $styleCodes): array
{
if ($productCodes === []) {
if ($styleCodes === []) {
return ['rows' => [], 'brand_sku' => 0, 'archive_item' => 0];
}
@ -155,18 +155,18 @@ class KukaPostingMaterialMapSynchronizer
$rows = [];
$now = now();
foreach (array_chunk($productCodes, self::SOURCE_ITEM_CHUNK) as $chunk) {
foreach (array_chunk($styleCodes, self::SOURCE_ITEM_CHUNK) as $chunk) {
foreach ($query->select($this->brandItemSql(count($chunk)), $chunk) as $sourceRow) {
$productCode = trim((string) ($sourceRow['product_code'] ?? ''));
$styleCode = trim((string) ($sourceRow['style_code'] ?? ''));
$brandItemNumber = trim((string) ($sourceRow['brand_item_number'] ?? ''));
if ($productCode === '' || $brandItemNumber === '') {
if ($styleCode === '' || $brandItemNumber === '') {
continue;
}
$sourceImportedAt = trim((string) ($sourceRow['source_imported_at'] ?? ''));
$rows[] = [
'company_id' => $companyId,
'product_code' => $productCode,
'product_code' => $styleCode,
'brand_item_number' => $brandItemNumber,
'source_imported_at' => $sourceImportedAt === '' ? null : $sourceImportedAt,
'synced_at' => $now,
@ -175,25 +175,25 @@ class KukaPostingMaterialMapSynchronizer
}
$brandSkuCount = count($rows);
$brandProductCodes = array_fill_keys(array_column($rows, 'product_code'), true);
$missingProductCodes = array_values(array_filter(
$productCodes,
static fn (string $productCode): bool => ! isset($brandProductCodes[$productCode]),
$brandStyleCodes = array_fill_keys(array_column($rows, 'product_code'), true);
$missingStyleCodes = array_values(array_filter(
$styleCodes,
static fn (string $styleCode): bool => ! isset($brandStyleCodes[$styleCode]),
));
$archiveItemCount = 0;
foreach (array_chunk($missingProductCodes, self::SOURCE_ITEM_CHUNK) as $chunk) {
foreach (array_chunk($missingStyleCodes, self::SOURCE_ITEM_CHUNK) as $chunk) {
foreach ($query->select($this->archiveItemSql(count($chunk)), $chunk) as $sourceRow) {
$productCode = trim((string) ($sourceRow['product_code'] ?? ''));
$styleCode = trim((string) ($sourceRow['style_code'] ?? ''));
$archiveItemNumber = trim((string) ($sourceRow['archive_item_number'] ?? ''));
if ($productCode === '' || $archiveItemNumber === '') {
if ($styleCode === '' || $archiveItemNumber === '') {
continue;
}
$sourceUpdatedAt = trim((string) ($sourceRow['source_imported_at'] ?? ''));
$rows[] = [
'company_id' => $companyId,
'product_code' => $productCode,
'product_code' => $styleCode,
'brand_item_number' => $archiveItemNumber,
'source_imported_at' => $sourceUpdatedAt === '' ? null : $sourceUpdatedAt,
'synced_at' => $now,
@ -310,7 +310,7 @@ class KukaPostingMaterialMapSynchronizer
$placeholders = implode(', ', array_fill(0, $count, '?'));
return 'SELECT DISTINCT ON ("normalizedSku") '
. '"normalizedSku" AS product_code, '
. '"normalizedSku" AS style_code, '
. 'TRIM(updates->>\'brandSkuCode\') AS brand_item_number, '
. '"importedAt" AS source_imported_at '
. 'FROM public."BarcodePrintImportRecord" '
@ -324,7 +324,7 @@ class KukaPostingMaterialMapSynchronizer
{
$placeholders = implode(', ', array_fill(0, $count, '?'));
return 'SELECT barcode AS product_code, '
return 'SELECT barcode AS style_code, '
. 'TRIM(sku) AS archive_item_number, '
. 'COALESCE("updatedAt", "createdAt") AS source_imported_at '
. 'FROM public."SupplyChainProductArchive" '

View File

@ -14,7 +14,7 @@ class SyncKukaPostingMaterialMap extends Command
{--dry-run : 只读取和校验,不覆盖镜像}
{--json : 输出 JSON}';
protected $description = '按货号匹配物料编号,并按商品编码匹配品牌货号及货号回退';
protected $description = '按货号匹配物料编号,并按款式编码匹配品牌货号及货号回退';
public function handle(KukaPostingMaterialMapSynchronizer $synchronizer): int
{

View File

@ -17,7 +17,7 @@
| 状态 | 说明 |
| --- | --- |
| 本次接入 | 顾家过账基础数据读取聚水潭“销售主题分析(新版)→ 商品利润 → 明细(订单商品)”10 月导出,仅保留天猫、京东、拼多多和抖音的 12 家 HX 旗舰店,写入 `kuka_posting_base_rows`;导入摘要写入 `kuka_posting_base_imports`。只保存前 40 个聚水潭原始字段,后续成本映射、报价和过账分类不混入基础数据。XLSX 通过 `business-mirror:import-kuka-posting-base` 流式读取,校验 12 家店铺均有数据后,在单一事务内整月替换;文件缺失、字段错误或中断时保留上一版。当前 2026 年 10 月导出为 13,949 行。 |
| 本次接入 | `furniture3d_prod` PostgreSQL 只读库用于顾家发货明细映射:物料编号优先使用 `public.BarcodePrintImportRecord.updates.materialNo`,其次用 `public.SupplyChainProductArchive.sku` 对应聚水潭“货号”并校验候选 `barcode`,最后按 `updates.brandSkuCode` 对应报价单物料描述前缀回退;命中后必须存在报价单生效 `MaterialNumber`。同一物料编号允许多个货号共用,只要求单个货号内部候选唯一。页面“货号”先按聚水潭“商品编码”匹配 `BarcodePrintImportRecord.normalizedSku`,取最新非空 `updates.brandSkuCode` 作为品牌货号;没有品牌货号时按商品编码匹配 `SupplyChainProductArchive.barcode`,取 `sku` 作为货号;两级都未命中时保留聚水潭原货号。物料编号和成本仍按原货号计算。紧凑结果分别写入 `kuka_posting_material_map_rows` 和 `kuka_posting_brand_item_map_rows`,不复制整库、不在页面直连上游;无命中或仍有多个报价候选时保留空值,不猜测。每日 05:50 执行 `business-mirror:sync-kuka-posting-material-map`。 |
| 本次接入 | `furniture3d_prod` PostgreSQL 只读库用于顾家发货明细映射:物料编号优先使用 `public.BarcodePrintImportRecord.updates.materialNo`,其次用 `public.SupplyChainProductArchive.sku` 对应聚水潭“货号”并校验候选 `barcode`,最后按 `updates.brandSkuCode` 对应报价单物料描述前缀回退;命中后必须存在报价单生效 `MaterialNumber`。同一物料编号允许多个货号共用,只要求单个货号内部候选唯一。页面“货号”按聚水潭“款式编码”匹配 `BarcodePrintImportRecord.normalizedSku`,取最新非空 `updates.brandSkuCode` 作为品牌货号;没有品牌货号时按款式编码匹配 `SupplyChainProductArchive.barcode`,取 `sku` 作为货号;两级都未命中时保留聚水潭原货号。因此同一款式编码下的多个原始货号会共享同一个品牌货号。物料编号和成本仍按聚水潭原始货号计算。紧凑结果分别写入 `kuka_posting_material_map_rows` 和 `kuka_posting_brand_item_map_rows`,不复制整库、不在页面直连上游;无命中或仍有多个报价候选时保留空值,不猜测。每日 05:50 执行 `business-mirror:sync-kuka-posting-material-map`。 |
| 本次接入 | 钉钉考勤 `attendance/list` 按财务中心在职成员同步到 `ding_attendance_records`,保留打卡记录 ID、用户、工作日期、上下班类型、打卡结果、审批标识、审批实例和打卡时间;每 15 分钟回看 35 天,按 7 天窗口和 50 人上限分页。钉钉排班通过 `getScheduleByRange` 同步到 `ding_attendance_schedule_records`,保留逐日排班 ID、用户、考勤组、班次、是否休息、变更状态和完整源数据;本机脚本为 `scripts/sync-ding-attendance-schedules.py`,导入命令为 `business-mirror:import-ding-attendance-schedules`,默认覆盖最近 95 天,查询不完整时拒绝覆盖旧镜像。日志漏写扣分只统计钉钉排班明确为工作日的日期,并继续排除没有日志但有“请假 / 休息 / 调休”等审批标记的日期;排班或考勤镜像未完成时扣分暂不计算,周五、周末或法定班次调整均以公司真实排班为准。依赖权限 `qyapi_attendance_isv_query_result`、`qyapi_get_attendance_data`。 |
| 已完成 | `operations_replica/rpt.Bulk_Price_Limit` 已进入类型化表 `product_price_mirror_rows`,商品价格页面已读取镜像并支持完整分页。 |
| 已完成 | `operations_replica/rpt.jst_sku` 已进入 `ops_jst_sku`,普通商品资料页面固定读取 `sku_type = normal` 的共享镜像,并支持稳定分页和业务筛选。 |

View File

@ -237,7 +237,7 @@ class KukaPostingBasePageTest extends FeatureTestCase
@unlink($file);
}
public function test_display_item_uses_brand_then_archive_then_original_item(): void
public function test_display_item_uses_style_brand_and_item_material(): void
{
$file = $this->workbook();
@ -248,17 +248,21 @@ class KukaPostingBasePageTest extends FeatureTestCase
])->assertExitCode(0);
$connection = DB::connection('business_mirror');
$secondBaseRow = $connection->table('kuka_posting_base_rows')
->where('period', '2026-10')
->where('internal_order_no', 'KUKA-01')
->first();
$this->assertNotNull($secondBaseRow);
$secondValues = json_decode((string) $secondBaseRow->source_values, true, 512, JSON_THROW_ON_ERROR);
$secondValues['款式编码'] = 'STYLE-00';
$connection->table('kuka_posting_base_rows')
->where('id', $secondBaseRow->id)
->update(['source_values' => json_encode($secondValues, JSON_UNESCAPED_UNICODE | JSON_THROW_ON_ERROR)]);
$connection->table('kuka_posting_brand_item_map_rows')->insert([
'company_id' => $this->company->id,
'product_code' => 'SKU-00',
'brand_item_number' => 'BRAND-SKU-00',
'source_imported_at' => now(),
'synced_at' => now(),
]);
$connection->table('kuka_posting_brand_item_map_rows')->insert([
'company_id' => $this->company->id,
'product_code' => 'SKU-01',
'brand_item_number' => 'ARCHIVE-FALLBACK-01',
'product_code' => 'STYLE-00',
'brand_item_number' => 'BRAND-STYLE-SHARED',
'source_imported_at' => now(),
'synced_at' => now(),
]);
@ -272,6 +276,16 @@ class KukaPostingBasePageTest extends FeatureTestCase
'candidate_materials' => '["91.BRAND.00"]',
'synced_at' => now(),
]);
$connection->table('kuka_posting_material_map_rows')->insert([
'company_id' => $this->company->id,
'item_number' => 'ITEM-01',
'material_number' => '91.C00001.01',
'match_status' => 'matched',
'match_source' => 'barcode_material_no',
'candidate_count' => 1,
'candidate_materials' => '["91.C00001.01"]',
'synced_at' => now(),
]);
$connection->table('kuka_posting_quotation_rows')->insert([
'company_id' => $this->company->id,
'material_number' => '91.BRAND.00',
@ -284,6 +298,18 @@ class KukaPostingBasePageTest extends FeatureTestCase
'source_row_number' => 4,
'imported_at' => now(),
]);
$connection->table('kuka_posting_quotation_rows')->insert([
'company_id' => $this->company->id,
'material_number' => '91.C00001.01',
'material_desc' => '第二个原货号对应物料',
'record_status' => '生效',
'price_including_tax' => '99.0000',
'price_excluding_tax' => '87.6100',
'row_hash' => str_repeat('c', 64),
'source_values' => '{}',
'source_row_number' => 5,
'imported_at' => now(),
]);
$this->loginAs()
->get(route('business.pages.show', [
@ -292,7 +318,7 @@ class KukaPostingBasePageTest extends FeatureTestCase
'store_name' => '天猫-HX旗舰店101',
]))
->assertOk()
->assertSeeText('BRAND-SKU-00')
->assertSeeText('BRAND-STYLE-SHARED')
->assertSeeText('91.BRAND.00')
->assertSeeText('原货号对应物料')
->assertDontSeeText('ITEM-00');
@ -303,7 +329,9 @@ class KukaPostingBasePageTest extends FeatureTestCase
'store_name' => '京东-HX旗舰店101',
]))
->assertOk()
->assertSeeText('ARCHIVE-FALLBACK-01')
->assertSeeText('BRAND-STYLE-SHARED')
->assertSeeText('91.C00001.01')
->assertSeeText('第二个原货号对应物料')
->assertDontSeeText('ITEM-01');
$this->get(route('business.pages.show', [

View File

@ -72,6 +72,7 @@ class KukaPostingMaterialMapSynchronizerTest extends TestCase
'source_values' => json_encode([
'货号' => 'ITEM-'.$itemNumber,
'商品编码' => 'PRODUCT-'.$itemNumber,
'款式编码' => 'STYLE-'.$itemNumber,
], JSON_THROW_ON_ERROR),
]);
}
@ -96,7 +97,7 @@ class KukaPostingMaterialMapSynchronizerTest extends TestCase
return [
[
'product_code' => 'PRODUCT-B',
'style_code' => 'STYLE-B',
'archive_item_number' => 'ARCHIVE-ITEM-B',
'source_imported_at' => '2026-10-03 10:00:00',
],
@ -106,12 +107,12 @@ class KukaPostingMaterialMapSynchronizerTest extends TestCase
if (str_contains($sql, 'DISTINCT ON')) {
return [
[
'product_code' => 'PRODUCT-A',
'style_code' => 'STYLE-A',
'brand_item_number' => 'BRAND-A',
'source_imported_at' => '2026-10-01 10:00:00',
],
[
'product_code' => 'PRODUCT-G',
'style_code' => 'STYLE-G',
'brand_item_number' => 'BRAND-DISPLAY-G',
'source_imported_at' => '2026-10-02 10:00:00',
],
@ -182,13 +183,13 @@ class KukaPostingMaterialMapSynchronizerTest extends TestCase
->pluck('brand_item_number', 'product_code')
->all();
$this->assertSame([
'PRODUCT-A' => 'BRAND-A',
'PRODUCT-B' => 'ARCHIVE-ITEM-B',
'PRODUCT-G' => 'BRAND-DISPLAY-G',
'STYLE-A' => 'BRAND-A',
'STYLE-B' => 'ARCHIVE-ITEM-B',
'STYLE-G' => 'BRAND-DISPLAY-G',
], $brandItems);
$this->assertCount(1, $fallbackBindings);
$this->assertContains('PRODUCT-B', $fallbackBindings[0]);
$this->assertNotContains('PRODUCT-A', $fallbackBindings[0]);
$this->assertNotContains('PRODUCT-G', $fallbackBindings[0]);
$this->assertContains('STYLE-B', $fallbackBindings[0]);
$this->assertNotContains('STYLE-A', $fallbackBindings[0]);
$this->assertNotContains('STYLE-G', $fallbackBindings[0]);
}
}