商品成本:剔除可核对的变体父行 (#500)
This commit is contained in:
parent
4e5896d8c3
commit
054b239867
@ -297,6 +297,7 @@ class SyncStoreReceiptProductCost extends Command
|
||||
$placeholderExcludedRows = 0;
|
||||
$bundleParentExcludedRows = 0;
|
||||
$plusParentExcludedRows = 0;
|
||||
$variantParentExcludedRows = 0;
|
||||
$autoReplacedRows = 0;
|
||||
$mappingAmbiguousRows = 0;
|
||||
$mappingMissingRows = 0;
|
||||
@ -311,15 +312,18 @@ class SyncStoreReceiptProductCost extends Command
|
||||
$excludedPlaceholders = 0;
|
||||
$excludedBundleParents = 0;
|
||||
$excludedPlusParents = 0;
|
||||
$excludedVariantParents = 0;
|
||||
$costLines = $this->costLines(
|
||||
$lines[$core] ?? [],
|
||||
$excludedPlaceholders,
|
||||
$excludedBundleParents,
|
||||
$excludedPlusParents,
|
||||
$excludedVariantParents,
|
||||
);
|
||||
$placeholderExcludedRows += $excludedPlaceholders;
|
||||
$bundleParentExcludedRows += $excludedBundleParents;
|
||||
$plusParentExcludedRows += $excludedPlusParents;
|
||||
$variantParentExcludedRows += $excludedVariantParents;
|
||||
$mapped = $this->mapPlatformSkus($costLines, $platformSkuMapper);
|
||||
$costLines = $mapped['lines'];
|
||||
$autoReplacedRows += $mapped['auto_replaced_rows'];
|
||||
@ -433,11 +437,12 @@ class SyncStoreReceiptProductCost extends Command
|
||||
));
|
||||
$pendingCostQuantity = array_sum(array_column($pendingCostRows, 'product_quantity'));
|
||||
$this->info(sprintf(
|
||||
' 正常匹配单价:%d 行;排除平台占位行:%d 行;排除组合装主行:%d 行;排除加号组合父行:%d 行;待核验成本:%d 行 / %s 件',
|
||||
' 正常匹配单价:%d 行;排除平台占位行:%d 行;排除组合装主行:%d 行;排除加号组合父行:%d 行;排除变体父行:%d 行;待核验成本:%d 行 / %s 件',
|
||||
$matchedPriceRows,
|
||||
$placeholderExcludedRows,
|
||||
$bundleParentExcludedRows,
|
||||
$plusParentExcludedRows,
|
||||
$variantParentExcludedRows,
|
||||
$missingPriceRows,
|
||||
number_format((float) $pendingCostQuantity, 2),
|
||||
));
|
||||
@ -460,7 +465,9 @@ class SyncStoreReceiptProductCost extends Command
|
||||
'placeholder_excluded_rows' => $placeholderExcludedRows,
|
||||
'bundle_parent_excluded_rows' => $bundleParentExcludedRows,
|
||||
'plus_parent_excluded_rows' => $plusParentExcludedRows,
|
||||
'excluded_rows' => $placeholderExcludedRows + $bundleParentExcludedRows + $plusParentExcludedRows,
|
||||
'variant_parent_excluded_rows' => $variantParentExcludedRows,
|
||||
'excluded_rows' => $placeholderExcludedRows + $bundleParentExcludedRows
|
||||
+ $plusParentExcludedRows + $variantParentExcludedRows,
|
||||
'auto_replaced_platform_sku_rows' => $autoReplacedRows,
|
||||
'platform_sku_mapping_ambiguous_rows' => $mappingAmbiguousRows,
|
||||
'platform_sku_mapping_missing_rows' => $mappingMissingRows,
|
||||
@ -687,6 +694,25 @@ class SyncStoreReceiptProductCost extends Command
|
||||
. "\0" . $shopSkuId;
|
||||
}
|
||||
|
||||
/**
|
||||
* 非数字父行先限定到同一订单、平台商品和发货仓,再比较可用的平台规格维度。
|
||||
*
|
||||
* @param array<string,mixed> $row
|
||||
*/
|
||||
private function variantParentGroupKey(array $row): ?string
|
||||
{
|
||||
$shopItemId = trim((string) ($row['shop_i_id'] ?? ''));
|
||||
$warehouse = trim((string) ($row['warehouse'] ?? ''));
|
||||
if ($shopItemId === '' || $warehouse === '') {
|
||||
return null;
|
||||
}
|
||||
|
||||
return (string) $row['source']
|
||||
. "\0" . (string) $row['o_id']
|
||||
. "\0" . $shopItemId
|
||||
. "\0" . $warehouse;
|
||||
}
|
||||
|
||||
private function pendingCodeType(string $code): string
|
||||
{
|
||||
$code = mb_strtoupper(trim($code));
|
||||
@ -729,11 +755,13 @@ class SyncStoreReceiptProductCost extends Command
|
||||
?int &$placeholderExcludedRows = null,
|
||||
?int &$bundleParentExcludedRows = null,
|
||||
?int &$plusParentExcludedRows = null,
|
||||
?int &$variantParentExcludedRows = null,
|
||||
): array
|
||||
{
|
||||
$placeholderExcludedRows = 0;
|
||||
$bundleParentExcludedRows = 0;
|
||||
$plusParentExcludedRows = 0;
|
||||
$variantParentExcludedRows = 0;
|
||||
if ($rows === []) {
|
||||
return [];
|
||||
}
|
||||
@ -775,7 +803,63 @@ class SyncStoreReceiptProductCost extends Command
|
||||
}
|
||||
}
|
||||
|
||||
// 3) 组合装拆分后的组件会记录 src_combine_sku_id。源订单上的组合装主行
|
||||
// 3) 非纯数字父行:同一订单、平台商品、平台规格、子订单和发货仓内,
|
||||
// 如果只有一个严格扩展编码的子商品,且父子数量、金额都一致,则父行是可核对的别名占位。
|
||||
// 只依赖编码前缀会误删正常的相似商品,因此任一项不一致都保留父行待核验。
|
||||
$variantGroups = [];
|
||||
foreach ($unique as $key => $row) {
|
||||
$groupKey = $this->variantParentGroupKey($row);
|
||||
if ($groupKey !== null) {
|
||||
$variantGroups[$groupKey][$key] = $row;
|
||||
}
|
||||
}
|
||||
$variantParents = [];
|
||||
foreach ($variantGroups as $groupRows) {
|
||||
foreach ($groupRows as $parentKey => $parent) {
|
||||
$parentSku = mb_strtoupper(trim((string) $parent['sku']));
|
||||
if ($parentSku === '' || preg_match('/^\d+$/', $parentSku) === 1) {
|
||||
continue;
|
||||
}
|
||||
$parentShopSkuId = trim((string) ($parent['shop_sku_id'] ?? ''));
|
||||
$parentOuterItemId = trim((string) ($parent['outer_oi_id'] ?? ''));
|
||||
|
||||
$children = [];
|
||||
foreach ($groupRows as $childKey => $child) {
|
||||
$childSku = mb_strtoupper(trim((string) $child['sku']));
|
||||
$childShopSkuId = trim((string) ($child['shop_sku_id'] ?? ''));
|
||||
$childOuterItemId = trim((string) ($child['outer_oi_id'] ?? ''));
|
||||
if ($parentShopSkuId !== '' && $childShopSkuId !== '' && $parentShopSkuId !== $childShopSkuId) {
|
||||
continue;
|
||||
}
|
||||
if ($parentOuterItemId !== '' && $childOuterItemId !== '' && $parentOuterItemId !== $childOuterItemId) {
|
||||
continue;
|
||||
}
|
||||
if ($childKey === $parentKey || $childSku === $parentSku || ! str_starts_with($childSku, $parentSku)) {
|
||||
continue;
|
||||
}
|
||||
$children[$childSku]['qty'] = ($children[$childSku]['qty'] ?? 0.0) + (float) $child['qty'];
|
||||
$children[$childSku]['amount'] = ($children[$childSku]['amount'] ?? 0.0) + (float) $child['amount'];
|
||||
}
|
||||
|
||||
// 多子编码的父子关系可能来自真实组合,当前规则不自动猜测,继续待核验。
|
||||
if (count($children) !== 1) {
|
||||
continue;
|
||||
}
|
||||
$child = reset($children);
|
||||
if (
|
||||
abs(round((float) $child['qty'], 4) - round((float) $parent['qty'], 4)) <= 0.0001
|
||||
&& abs(round((float) $child['amount'], 2) - round((float) $parent['amount'], 2)) <= 0.01
|
||||
) {
|
||||
$variantParents[$parentKey] = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
foreach (array_keys($variantParents) as $key) {
|
||||
$variantParentExcludedRows++;
|
||||
unset($unique[$key]);
|
||||
}
|
||||
|
||||
// 4) 组合装拆分后的组件会记录 src_combine_sku_id。源订单上的组合装主行
|
||||
// 只是汇总占位,不能与组件同时计入成本。
|
||||
$bundleParents = [];
|
||||
foreach ($unique as $row) {
|
||||
@ -793,7 +877,7 @@ class SyncStoreReceiptProductCost extends Command
|
||||
}
|
||||
}
|
||||
|
||||
// 4) 平台原始商品编码通常是一长串数字。同一订单、同一 shop_i_id
|
||||
// 5) 平台原始商品编码通常是一长串数字。同一订单、同一 shop_i_id
|
||||
// 如果已经有内部商品编码,则数字行只是平台占位,不参与成本。
|
||||
$internalSiblings = [];
|
||||
foreach ($unique as $row) {
|
||||
@ -816,7 +900,7 @@ class SyncStoreReceiptProductCost extends Command
|
||||
}
|
||||
}
|
||||
|
||||
// 5) 组合装条码(ZH-/MKH-条码-数量-条码-数量)按子订单拆成组件行:
|
||||
// 6) 组合装条码(ZH-/MKH-条码-数量-条码-数量)按子订单拆成组件行:
|
||||
// 一个订单有几个发货仓库、商品编码、数量就分开列,组件数量 = 明细数量 × 组件倍数。
|
||||
// 解析不了的组合条码(条码被截断等)保持原样,避免丢行。
|
||||
$expanded = [];
|
||||
@ -835,7 +919,7 @@ class SyncStoreReceiptProductCost extends Command
|
||||
return [];
|
||||
}
|
||||
|
||||
// 6) 只保留叶子行:o_id 没有被同组其他行的 link_o_id 引用的,才是实际发货行。
|
||||
// 7) 只保留叶子行:o_id 没有被同组其他行的 link_o_id 引用的,才是实际发货行。
|
||||
$parentIds = [];
|
||||
foreach ($unique as $row) {
|
||||
$parent = trim((string) ($row['parent_o_id'] ?? ''));
|
||||
@ -854,7 +938,7 @@ class SyncStoreReceiptProductCost extends Command
|
||||
}
|
||||
}
|
||||
|
||||
// 7) 组内只要出现带仓库的行,空仓库行就是汇总/占位行,直接丢弃。
|
||||
// 8) 组内只要出现带仓库的行,空仓库行就是汇总/占位行,直接丢弃。
|
||||
$hasWarehouse = false;
|
||||
foreach ($unique as $row) {
|
||||
if (trim((string) $row['warehouse']) !== '') {
|
||||
@ -869,13 +953,13 @@ class SyncStoreReceiptProductCost extends Command
|
||||
);
|
||||
}
|
||||
|
||||
// 8) 排除已取消行;若带仓库行全部取消,上面已把空仓库汇总行丢掉,这里结果为空 → 整单不计。
|
||||
// 9) 排除已取消行;若带仓库行全部取消,上面已把空仓库汇总行丢掉,这里结果为空 → 整单不计。
|
||||
$unique = array_filter(
|
||||
$unique,
|
||||
static fn (array $row): bool => strtoupper(trim((string) ($row['status'] ?? ''))) !== 'CANCELLED'
|
||||
);
|
||||
|
||||
// 9) 汇总到「发货仓库 + 商品编码」。
|
||||
// 10) 汇总到「发货仓库 + 商品编码」。
|
||||
$aggregated = [];
|
||||
foreach ($unique as $row) {
|
||||
$isPlatformSku = preg_match('/^\d+$/', (string) $row['sku']) === 1;
|
||||
|
||||
@ -119,6 +119,8 @@
|
||||
ZH-、MKH- 开头的组合装按「组件条码 + 数量」拆分,无法解析的组合装保留原编码;
|
||||
商品编码含「+」的组合父行,在同一订单、outer_oi_id、shop_sku_id 下有多条组件且金额合计一致时剔除,仅保留组件行;
|
||||
组件缺失或金额合计不一致时保留父行待核验。
|
||||
非纯数字父编码在同一订单、shop_i_id 和发货仓内,仅有一个严格扩展编码的子商品;outer_oi_id、shop_sku_id
|
||||
在父子两边都有值时还必须一致,且父子数量、金额均一致时才剔除父行。多子编码或任一项不一致时保留父行待核验。
|
||||
套餐/服务、运费及指定配件、组合装占位编码 TCXJ、FUWU、AZF、DF-PJZY0001、
|
||||
JIAXIANGJIANCAIJIAJUZIZHUANG964A、YUNFEI、BCJ 全局不取数(组合装里拆出的这些编码同样剔除)。
|
||||
发货仓库默认排除「常州简尚家居有限公司」「简尚-苏州福利品仓」「简尚-自动发货仓」,
|
||||
|
||||
@ -70,6 +70,44 @@ class StoreReceiptProductCostLineTest extends TestCase
|
||||
$this->assertContains('GBGJ-A+V01+GBGJ-B+V01', array_column($lines, 'sku'));
|
||||
}
|
||||
|
||||
public function test_non_numeric_variant_parent_is_removed_when_quantity_and_amount_match(): void
|
||||
{
|
||||
[$lines, $placeholders, $bundles, $plusParents, $variantParents] = $this->costLines([
|
||||
$this->row('variant-parent', 'TESTP001', '3767501840732782900', 1, 91),
|
||||
$this->row('variant-child', 'TESTP001N1150V01', '3767501840732782900', 1, 91),
|
||||
]);
|
||||
|
||||
$this->assertSame(0, $placeholders);
|
||||
$this->assertSame(0, $bundles);
|
||||
$this->assertSame(0, $plusParents);
|
||||
$this->assertSame(1, $variantParents);
|
||||
$this->assertSame(['TESTP001N1150V01'], array_column($lines, 'sku'));
|
||||
}
|
||||
|
||||
public function test_non_numeric_variant_parent_remains_when_amounts_do_not_match(): void
|
||||
{
|
||||
[$lines, , , , $variantParents] = $this->costLines([
|
||||
$this->row('variant-parent', 'TESTP001', '3767501840732782900', 1, 91),
|
||||
$this->row('variant-child', 'TESTP001N1150V01', '3767501840732782900', 1, 90),
|
||||
]);
|
||||
|
||||
$this->assertSame(0, $variantParents);
|
||||
$this->assertContains('TESTP001', array_column($lines, 'sku'));
|
||||
$this->assertContains('TESTP001N1150V01', array_column($lines, 'sku'));
|
||||
}
|
||||
|
||||
public function test_non_numeric_variant_parent_does_not_cross_platform_item_groups(): void
|
||||
{
|
||||
[$lines, , , , $variantParents] = $this->costLines([
|
||||
$this->row('variant-parent', 'TESTP001', '3767501840732782900', 1, 91),
|
||||
$this->row('variant-child', 'TESTP001N1150V01', '3767501840732782901', 1, 91),
|
||||
]);
|
||||
|
||||
$this->assertSame(0, $variantParents);
|
||||
$this->assertContains('TESTP001', array_column($lines, 'sku'));
|
||||
$this->assertContains('TESTP001N1150V01', array_column($lines, 'sku'));
|
||||
}
|
||||
|
||||
public function test_unique_platform_sku_is_replaced_after_line_filtering(): void
|
||||
{
|
||||
[$lines] = $this->costLines([
|
||||
@ -134,7 +172,7 @@ class StoreReceiptProductCostLineTest extends TestCase
|
||||
$this->assertSame(552392.0, $result['cost']);
|
||||
}
|
||||
|
||||
/** @return array{0:list<array<string,mixed>>,1:int,2:int,3:int} */
|
||||
/** @return array{0:list<array<string,mixed>>,1:int,2:int,3:int,4:int} */
|
||||
private function costLines(array $rows): array
|
||||
{
|
||||
$command = new SyncStoreReceiptProductCost();
|
||||
@ -143,9 +181,10 @@ class StoreReceiptProductCostLineTest extends TestCase
|
||||
$placeholders = 0;
|
||||
$bundles = 0;
|
||||
$plusParents = 0;
|
||||
$lines = $method->invokeArgs($command, [$rows, &$placeholders, &$bundles, &$plusParents]);
|
||||
$variantParents = 0;
|
||||
$lines = $method->invokeArgs($command, [$rows, &$placeholders, &$bundles, &$plusParents, &$variantParents]);
|
||||
|
||||
return [$lines, $placeholders, $bundles, $plusParents];
|
||||
return [$lines, $placeholders, $bundles, $plusParents, $variantParents];
|
||||
}
|
||||
|
||||
/** @return array<string,mixed> */
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user