From 5e9667a983f8770bcbc4a4ea3b4ef26e0eb01678 Mon Sep 17 00:00:00 2001 From: chenxi Date: Sun, 25 Oct 2026 00:35:17 +0800 Subject: [PATCH] =?UTF-8?q?=E5=95=86=E5=93=81=E6=88=90=E6=9C=AC=EF=BC=9A?= =?UTF-8?q?=E7=BB=84=E5=90=88=E5=9B=9E=E6=BA=AF=E4=BB=85=E4=BD=9C=E7=94=A8?= =?UTF-8?q?=E4=BA=8E=E5=90=AB=E7=BB=84=E5=90=88=E7=BC=96=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Commands/SyncStoreReceiptProductCost.php | 51 +++++++++++-------- docs/商品成本占位行规则清单.md | 6 ++- .../product-costs.blade.php | 3 +- .../Unit/StoreReceiptProductCostLineTest.php | 22 +++++++- 4 files changed, 57 insertions(+), 25 deletions(-) diff --git a/app/Console/Commands/SyncStoreReceiptProductCost.php b/app/Console/Commands/SyncStoreReceiptProductCost.php index 75d0da4f..fba8e511 100644 --- a/app/Console/Commands/SyncStoreReceiptProductCost.php +++ b/app/Console/Commands/SyncStoreReceiptProductCost.php @@ -247,7 +247,7 @@ class SyncStoreReceiptProductCost extends Command if ( $qty == 0.0 && $amount == 0.0 - && ! self::isBundleParentSku($sku, (string) ($row->sku_type ?? '')) + && ! self::isChineseBundleParentSku($sku) ) { continue; } @@ -1091,25 +1091,11 @@ class SyncStoreReceiptProductCost extends Command return '内部商品编码缺少有效价格'; } - private static function isBundleParentSku(string $sku, string $skuType = ''): bool + private static function isChineseBundleParentSku(string $sku): bool { $sku = mb_strtoupper(trim($sku)); - if ($sku === '') { - return false; - } - if (in_array(mb_strtolower(trim($skuType)), ['combine', '组合'], true)) { - return true; - } - if (str_contains($sku, '组合')) { - return true; - } - foreach (self::COMBO_PREFIXES as $prefix) { - if (str_starts_with($sku, $prefix)) { - return true; - } - } - return false; + return $sku !== '' && str_contains($sku, '组合'); } /** @param array $row */ @@ -1119,10 +1105,7 @@ class SyncStoreReceiptProductCost extends Command return false; } - return self::isBundleParentSku( - (string) ($row['sku'] ?? ''), - (string) ($row['sku_type'] ?? ''), - ); + return self::isChineseBundleParentSku((string) ($row['sku'] ?? '')); } /** @param array $row */ @@ -1369,6 +1352,32 @@ class SyncStoreReceiptProductCost extends Command // 只有组件金额能与主行金额对平时才剔除主行并保留组件。 $this->resolveBundleParents($unique, $stats); + // 非中文「组合」编码继续沿用原有 src_combine_sku_id 父子去重逻辑, + // 本次金额回溯不改变 ZH-/MKH- 等结构化组合的既有口径。 + $bundleParents = []; + foreach ($unique as $row) { + $parentSku = mb_strtoupper(trim((string) ($row['src_combine_sku_id'] ?? ''))); + if ($parentSku !== '') { + $bundleParents[$row['source'] . "\0" . $row['o_id'] . "\0" . $parentSku] = true; + } + } + if ($bundleParents !== []) { + foreach ($unique as $key => $row) { + $bundleSku = mb_strtoupper(trim((string) $row['sku'])); + if ( + self::isChineseBundleParentSku($bundleSku) + || ! isset($bundleParents[$row['source'] . "\0" . $row['o_id'] . "\0" . $bundleSku]) + ) { + continue; + } + $stats['bundle_parent_excluded_rows']++; + $stats['bundle_parent_codes'][$bundleSku] = true; + $stats['bundle_parent_code_rows'][$bundleSku] + = ($stats['bundle_parent_code_rows'][$bundleSku] ?? 0) + 1; + unset($unique[$key]); + } + } + // 4) 上游有些平台组合会同时返回「+ 拼接父行」和多条组件行。 // 只有同一订单、outer_oi_id、shop_sku_id 下至少有两条组件,且组件金额 // 合计与父行一致时,父行才是可确认的汇总占位,才允许排除。 diff --git a/docs/商品成本占位行规则清单.md b/docs/商品成本占位行规则清单.md index f11aa885..785177fc 100644 --- a/docs/商品成本占位行规则清单.md +++ b/docs/商品成本占位行规则清单.md @@ -6,6 +6,8 @@ 本清单是商品成本同步的固定校验口径。每月重跑时先完成商品月加权平均单价,再执行商品成本同步;同一规则不得在单月任务中临时改变。 +组合装金额回溯仅适用于商品编码包含中文「组合」的主行;`ZH-`、`MKH-` 等结构化组合继续沿用原有父行与组件解析逻辑,不纳入本次中文组合金额回溯。 + ## 1. 取数与排除顺序 1. 业绩明细只取收入金额非空且不为 0 的账单行。 @@ -23,7 +25,7 @@ | 加号组合父行 | 同一订单、`outer_oi_id`、`shop_sku_id` 下至少两条组件,组件金额合计与父行一致 | 剔除带 `+` 的组合父行,只保留组件 | 保留父行待核验 | | 中文后缀别名 | 占位编码以白名单后缀结尾,当前白名单仅「非」;必须同数据源、平台、店铺、订单、子订单、`shop_sku_id`、`shop_i_id`、仓库 | 正式编码与占位编码各唯一,且数量和金额完全一致时,保留正式编码、剔除占位行 | 任一关系不唯一或数量、金额不一致,两个编码都保留 | | 父子前缀占位 | 同一订单内只有一个严格扩展编码的子商品,且平台商品、规格、子订单和仓库一致 | 数量、金额一致时剔除较短父行,保留明细编码,例如 `CTG022516` 类父行 | 多子编码或任一字段不一致时保留父行 | -| 组合装主行 | 同一 `o_id` 下,组件 `src_combine_sku_id` 指向源组合装编码;没有显式标记时,组件须与主行 `outer_oi_id + shop_sku_id + shop_i_id` 一致 | 组件金额合计与组包总额误差≤0.01 元时剔除主行,只计组件 | 非零主行找不到组件或金额不一致时保留主行待核验,组件暂不计成本 | +| 中文「组合」主行 | 商品编码包含中文「组合」;同一 `o_id` 下,组件 `src_combine_sku_id` 指向源组合装编码;没有显式标记时,组件须与主行 `outer_oi_id + shop_sku_id + shop_i_id` 一致 | 组件金额合计与组包总额误差≤0.01 元时剔除主行,只计组件 | 非零主行找不到组件或金额不一致时保留主行待核验,组件暂不计成本 | | 结构化组合条码 | `ZH-`、`MKH-` 开头,并按「组件编码 + 数量」完整解析 | 按组件及倍数展开;一个订单有几个仓库、商品编码和数量就分几行 | 条码无法完整解析时保留原编码 | | 种菜前缀 | 商品编码以「种菜」开头 | 整行排除 | 不进入替换或价格匹配 | | 运营组合套装 | 商品编码以「运营组合套装」开头 | 不进入中文后缀或通用父子别名清洗,交给组合装组件规则 | 组件缺失时待核验 | @@ -43,7 +45,7 @@ ### 2.1 组合装金额对账 -组合装主行与组件必须限定在同一数据源、同一 `o_id`;兼容推断时还须满足 `outer_oi_id`、`shop_sku_id`、`shop_i_id` 一致。组包总额按以下可用口径依次核对: +中文「组合」主行与组件必须限定在同一数据源、同一 `o_id`;兼容推断时还须满足 `outer_oi_id`、`shop_sku_id`、`shop_i_id` 一致。组包总额按以下可用口径依次核对: 1. 主行数量和金额大于 0 时,优先使用主行 `amount`; 2. 拆单主行金额为 0 时,使用 `base_price × 组件最大数量`; diff --git a/resources/views/business/pages/financial-operations/product-costs.blade.php b/resources/views/business/pages/financial-operations/product-costs.blade.php index 251e3819..090a9b3d 100644 --- a/resources/views/business/pages/financial-operations/product-costs.blade.php +++ b/resources/views/business/pages/financial-operations/product-costs.blade.php @@ -115,7 +115,7 @@ (采购单价优先、其次钉钉代发单价),仍匹配不到时用「普通商品资料」里的商品成本价兜底;商品成本 = 商品数量 × 商品单价。 平台数字 SKU 会按平台 SKU ID + 商品 ID 回查历史已拆分订单;历史无结果时,再按店铺名称 + 店铺商品编码 + 店铺款式编码 回查订单售后费用镜像,仅唯一内部编码自动替换;多候选保持待核验。 - 同一订单下已有内部商品编码时,平台数字商品占位行不参与成本;组合装拆分后,主行与同一 o_id 下的组件先做金额对账, + 同一订单下已有内部商品编码时,平台数字商品占位行不参与成本;商品编码含中文「组合」的主行与同一 o_id 下的组件先做金额对账, 组件金额合计与组包总额误差不超过 0.01 元时,主行不参与成本,只取组件行。 拆单只取最末级订单行(没有任何下级的叶子行),主单和中间汇总层不参与、避免重复计数; 同一单分多仓发货的按末级实际仓库分行;虚拟条码(X0000x)不计入; @@ -130,6 +130,7 @@ 明确指向主行的组件优先;没有标记时,仅接受 outer_oi_id、shop_sku_id、shop_i_id 一致的同订单组件。 组包总额优先取主行 amount;拆单主行金额为 0 时取 base_price × 组件最大数量。非零主行找不到组件或金额不一致时, 主行保留待核验,关联组件暂不计成本,避免重复计价;数量和金额均为 0 的空组合占位不生成新的待核验行。 + ZH-、MKH- 等结构化组合编码不在本次中文「组合」金额回溯范围内,继续沿用原有逻辑。 「原木色广岛」「原木色舒美特实木面1把」「胡桃色舒美特实木面1把」保持原编码计价,禁止自动去掉中文。 套餐/服务、运费及指定配件、组合装占位编码 TCXJ、FUWU、AZF、DF-PJZY0001、 JIAXIANGJIANCAIJIAJUZIZHUANG964A、YUNFEI、BCJ 全局不取数(组合装里拆出的这些编码同样剔除)。 diff --git a/tests/Unit/StoreReceiptProductCostLineTest.php b/tests/Unit/StoreReceiptProductCostLineTest.php index f1255bb8..a2352ea0 100644 --- a/tests/Unit/StoreReceiptProductCostLineTest.php +++ b/tests/Unit/StoreReceiptProductCostLineTest.php @@ -82,6 +82,26 @@ class StoreReceiptProductCostLineTest extends TestCase $this->assertSame(1, $stats['bundle_stats']['held_component_rows']); } + public function test_non_chinese_structured_combo_keeps_legacy_parent_removal(): void + { + [$lines, , $bundles, , , $stats] = $this->costLines([ + $this->row('structured-parent', 'ZH-SJ038017N1410V02-1', '3832980288011632985', 1, 100), + $this->row( + 'structured-child', + 'SJ038017N1410V02', + '3832980288011632985', + 1, + 50, + 'ZH-SJ038017N1410V02-1', + ), + ]); + + $this->assertSame(1, $bundles); + $this->assertSame(['SJ038017N1410V02'], array_column($lines, 'sku')); + $this->assertSame(0, $stats['bundle_stats']['amount_mismatch_groups']); + $this->assertSame(0, $stats['bundle_stats']['held_component_rows']); + } + public function test_bundle_parent_without_components_remains_pending(): void { [$lines, , , , , $stats] = $this->costLines([ @@ -99,7 +119,7 @@ class StoreReceiptProductCostLineTest extends TestCase [$lines, , , , , $stats] = $this->costLines([ $this->row( 'bundle-placeholder', - 'ZH-SJ038009N1410V01-1-SJ038057N1410V01-1', + '运营组合套装13011', '3832980288011632985', 0, 0,