fix: 处理商品成本合单循环 (#1384)

This commit is contained in:
chenxi 2026-11-24 22:06:43 +08:00
parent 40ac376760
commit 11637818b9
3 changed files with 91 additions and 4 deletions

View File

@ -254,6 +254,7 @@ class SyncStoreReceiptProductCost extends Command
. " i.name as item_name, i.qty as qty, i.amount as amount, o.status as status, "
. " i.shop_i_id as shop_i_id, i.shop_sku_id as shop_sku_id, "
. " i.outer_oi_id as outer_oi_id, "
. " i.raw_so_id as raw_so_id, o.is_merge as is_merge, "
. " i.properties_value as properties_value, i.src_combine_sku_id as src_combine_sku_id
FROM tmp_cost_order_nos t
STRAIGHT_JOIN {$source['orders']} o ON o.so_id = t.no
@ -273,6 +274,9 @@ class SyncStoreReceiptProductCost extends Command
if ($core === '' || ! isset($billByCore[$core])) {
continue;
}
if (! $this->mergeOrderItemBelongsToCore($row, $core)) {
continue;
}
$sku = trim((string) $row->sku_id);
if ($sku === '') {
continue;
@ -3293,11 +3297,16 @@ class SyncStoreReceiptProductCost extends Command
return [];
}
// 9) 只保留叶子行:o_id 没有被同组「未取消行」的 link_o_id 引用的,才是实际发货行。
// 全部引用母单的子单都已取消时,不能把已发货的 Sent 母单当成汇总层删除。
// 9) 只保留叶子行:o_id 没有被同组「未取消、非 Merged」行的 link_o_id 引用的,才是实际发货行。
// 全部引用母单的子单都已取消时,不能把已发货的 Sent 母单当成汇总层删除;
// 合单与原始单可能互相 link,Merged 原始单不能反向把 Sent 合单判成母单。
$parentIds = [];
foreach ($unique as $row) {
if (strtoupper(trim((string) ($row['status'] ?? ''))) === 'CANCELLED') {
if (in_array(
strtoupper(trim((string) ($row['status'] ?? ''))),
['CANCELLED', 'MERGED'],
true,
)) {
continue;
}
$parent = trim((string) ($row['parent_o_id'] ?? ''));
@ -3366,6 +3375,24 @@ class SyncStoreReceiptProductCost extends Command
return array_values($aggregated);
}
/**
* 合单可能包含多个线上订单的商品。当前账单订单只能吸收 raw_so_id
* 或 outer_oi_id 指向自己的商品,避免把合单中的其他订单重复计价。
*/
private function mergeOrderItemBelongsToCore(object $row, string $core): bool
{
if (trim((string) ($row->is_merge ?? '0')) !== '1') {
return true;
}
$sourceOrder = self::cleanOrderNo((string) ($row->raw_so_id ?? ''));
if ($sourceOrder === '') {
$sourceOrder = self::cleanOrderNo((string) ($row->outer_oi_id ?? ''));
}
return $sourceOrder === '' || $sourceOrder === $core;
}
/**
* 组合装条码拆组件:聚水潭的组合装 sku 形如
* ZH-<条码>-<数量>[-<条码>-<数量>...]

View File

@ -23,7 +23,7 @@ store-receipt:product-cost:sync YYYY-MM --remap-warehouses-only
3. 商品编码为 `TCXJ`、`FUWU`、`AZF`、`DF-PJZY0001`、`JIAXIANGJIANCAIJIAJUZIZHUANG964A`、`YUNFEI`、`BCJ`、`信息说明`、`GG003-SZ`、`10237635142166`、`10237635142167`、`10228923011349`、`10230781002185`、`10230781002760`、`10230780802455` 的订单行全局排除;组合装拆出的这些编码同样排除。
4. 商品编码以「种菜」开头的订单行整行排除,不尝试去掉前缀后计价。
5. 发货仓库以 `配件-` 开头时整仓排除,不核算商品成本;发货仓库为「常州简尚家居有限公司」「简尚-苏州福利品仓」「简尚-自动发货仓」时也排除,仅「常州简尚家居有限公司」中以 `DF-` 开头的商品行例外保留。
6. 完成上述过滤后,再执行占位行去重、组合装展开、叶子行和已取消订单处理。只有未取消的子单可以把母单判定为汇总层;如果引用母单的子单全部为 Cancelled,保留已发货母单作为成本行。
6. 完成上述过滤后,再执行占位行去重、组合装展开、叶子行和已取消订单处理。只有未取消且非 `Merged` 的子单可以把母单判定为汇总层;如果引用母单的子单全部为 Cancelled,保留已发货母单作为成本行。合单与原始单互相 link 时,不能被 `Merged` 原始单反向把 `Sent` 合单判成母单。Sent 合单包含多个线上订单商品时,只取 `raw_so_id` 或 `outer_oi_id` 与当前账单订单一致的明细,禁止串单计价。
7. 编码映射和组合装展开完成后,最终商品编码以 `-单个数字` 结尾的行视为配件,整行不核算成本,例如 `BJ021807N3370V01-4`;`-N1280`、多位数字日期后缀、`91.` 基础料号和 `ZH-` / `MKH-` 结构化组合不按此规则排除。
## 2. 占位行处理规则

View File

@ -1284,6 +1284,66 @@ class StoreReceiptProductCostLineTest extends TestCase
$this->assertSame(5.0, $lines[0]['qty']);
}
public function test_sent_merge_order_is_kept_when_merged_order_links_back(): void
{
[$lines] = $this->costLines([
$this->row('merged-component-1', 'BG003759N05V02', '765952389562', 1, 342.31, '', [
'o_id' => '9492941',
'parent_o_id' => '9493233',
'status' => 'Merged',
'outer_oi_id' => '3316445222061019351',
]),
$this->row('merged-component-2', 'DNZ032916N887V01', '765952389562', 1, 768.66, '', [
'o_id' => '9492941',
'parent_o_id' => '9493233',
'status' => 'Merged',
'outer_oi_id' => '3316445222061019351',
]),
$this->row('sent-merge-component-1', 'BG003759N05V02', '765952389562', 1, 342.31, '', [
'o_id' => '9493233',
'parent_o_id' => '9492941',
'status' => 'Sent',
'outer_oi_id' => '3316445222061019351',
]),
$this->row('sent-merge-component-2', 'DNZ032916N887V01', '765952389562', 1, 768.66, '', [
'o_id' => '9493233',
'parent_o_id' => '9492941',
'status' => 'Sent',
'outer_oi_id' => '3316445222061019351',
]),
]);
$bySku = collect($lines)->keyBy('sku');
$this->assertCount(2, $lines);
$this->assertSame(1.0, $bySku['BG003759N05V02']['qty']);
$this->assertSame(1.0, $bySku['DNZ032916N887V01']['qty']);
}
public function test_merge_order_items_follow_the_current_online_order(): void
{
$command = new SyncStoreReceiptProductCost();
$method = new ReflectionMethod($command, 'mergeOrderItemBelongsToCore');
$method->setAccessible(true);
$core = '3316445222061019351';
$this->assertTrue($method->invoke($command, (object) [
'is_merge' => 1,
'raw_so_id' => $core,
'outer_oi_id' => $core,
], $core));
$this->assertFalse($method->invoke($command, (object) [
'is_merge' => 1,
'raw_so_id' => '3316445222061000170',
'outer_oi_id' => '3316445222061000170',
], $core));
$this->assertTrue($method->invoke($command, (object) [
'is_merge' => 0,
'raw_so_id' => '3316445222061000170',
'outer_oi_id' => '3316445222061000170',
], $core));
}
/** @return array{0:list<array<string,mixed>>,1:int,2:int,3:int,4:int,5:array<string,mixed>} */
private function costLines(
array $rows,