diff --git a/app/Console/Commands/SyncStoreReceiptProductCost.php b/app/Console/Commands/SyncStoreReceiptProductCost.php index ebcdab50..3164612c 100644 --- a/app/Console/Commands/SyncStoreReceiptProductCost.php +++ b/app/Console/Commands/SyncStoreReceiptProductCost.php @@ -2137,12 +2137,22 @@ class SyncStoreReceiptProductCost extends Command } $componentCodes = array_fill_keys(array_keys($components), true); $parentRows = []; + $bundleParentRows = []; foreach ($groupKeys as $key) { $sku = mb_strtoupper(trim((string) ($unique[$key]['sku'] ?? ''))); if ($sku === '' || isset($componentCodes[$sku])) { continue; } $parentRows[$sku][] = $key; + if (self::isBundleParentRow($unique[$key])) { + $bundleParentRows[$sku][] = $key; + } + } + + // 订单镜像可能同时残留上一版组合组件。存在唯一组合父行时,只按 + // 该父行与商品利润原表组件对账;对平后同组旧组件会一并剔除。 + if (count($bundleParentRows) === 1) { + $parentRows = $bundleParentRows; } if (count($parentRows) !== 1) { diff --git a/tests/Unit/StoreReceiptProductCostLineTest.php b/tests/Unit/StoreReceiptProductCostLineTest.php index 00e29060..7ea0bb97 100644 --- a/tests/Unit/StoreReceiptProductCostLineTest.php +++ b/tests/Unit/StoreReceiptProductCostLineTest.php @@ -594,6 +594,53 @@ class StoreReceiptProductCostLineTest extends TestCase $this->assertSame(2, $stats['bundle_stats']['goods_profit_split_component_rows']); } + public function test_goods_profit_bundle_parent_ignores_stale_order_mirror_components(): void + { + $profileKey = "jst\0" . "8986587\0" . "3533103677164802\0" . 'GOODS-ITEM-BUNDLE'; + $official = [ + 'SJ038067N1410V01' => ['qty' => 1, 'amount' => 6.97], + 'SJ038064N1410V01' => ['qty' => 1, 'amount' => 6.97], + 'SJ038057N1410V01' => ['qty' => 1, 'amount' => 3.49], + 'SJ038054N1410V01' => ['qty' => 1, 'amount' => 3.48], + 'SJ038046N1410V01' => ['qty' => 1, 'amount' => 3.49], + 'SJ038043N1410V01' => ['qty' => 1, 'amount' => 3.48], + 'SJ038013N1410V01' => ['qty' => 1, 'amount' => 485.93], + 'SJ038009N1410V01' => ['qty' => 2, 'amount' => 135.19], + ]; + $rows = [ + $this->row('bundle-parent', '运营组合套装12607', 'GOODS-ITEM-BUNDLE', 1, 649), + ]; + foreach ($official as $code => $component) { + $rows[] = $this->row( + 'official-' . $code, + $code, + 'GOODS-ITEM-BUNDLE', + $component['qty'], + $component['amount'], + '运营组合套装12607', + ); + } + $rows[] = $this->row('stale-12', 'SJ038012N1410V01', 'GOODS-ITEM-BUNDLE', 2, 485.93, '运营组合套装12607'); + $rows[] = $this->row('stale-16', 'SJ038016N1410V01', 'GOODS-ITEM-BUNDLE', 1, 135.19, '运营组合套装12607'); + + [$lines, , , , , $stats] = $this->costLines($rows, [], [ + $profileKey => [ + 'source' => 'goods_profit_current_order', + 'components' => $official, + ], + ]); + + $codes = array_column($lines, 'sku'); + sort($codes, SORT_STRING); + $expected = array_keys($official); + sort($expected, SORT_STRING); + + $this->assertSame($expected, $codes); + $this->assertSame(1, $stats['bundle_stats']['goods_profit_split_groups']); + $this->assertSame(8, $stats['bundle_stats']['goods_profit_split_component_rows']); + $this->assertSame(649.0, round(array_sum(array_column($lines, 'amount')), 2)); + } + public function test_goods_profit_current_order_split_is_not_applied_when_amount_differs(): void { $profileKey = "jst\0" . "8986587\0" . "3533103677164802\0" . 'GOODS-ITEM-3';