fix(cost): 快照变更即重算,0 元占位视为缺失,通用成本入账

- 预估→实际、补配价格时 supersede 旧快照再写新行,不再一次写死
- 全局 0 元版本不产生快照,转入 cost_missing
- 比例成本按 rate_bps 计算,费率为 0 视为未配置
- 平台费/支付费/售后等通用成本模型按渠道+通用各写一条快照
This commit is contained in:
yml2213
2026-10-31 20:41:38 +08:00
parent fef1d0b77e
commit 42d5874411
2 changed files with 286 additions and 30 deletions
@@ -3,6 +3,7 @@ import test from 'node:test'
import {
resolveCost,
resolveGenericCosts,
selectCostVersion,
type CostItemRow,
type CostVersionRow,
@@ -74,6 +75,8 @@ function item(patch: Record<string, unknown> = {}) {
work_product_rule_id: null as number | null,
work_order_status: null as string | null,
reward_amount: null as number | null,
revenue_fen: 10000,
basis_date: '2026-10-31',
...patch,
}
}
@@ -172,3 +175,102 @@ test('成本解析:人工渠道不在这里算成本', () => {
)
assert.equal(resolution, null)
})
test('全局 0 元占位版本不能当有效成本', () => {
const zeroVersions: CostVersionRow[] = [
{
id: 900,
cost_item_id: 1,
scope_type: 'global',
canonical_product_id: null,
work_product_rule_id: null,
amount_fen: 0,
rate_bps: 0,
effective_from: '2026-09-01T00:00:00+08:00',
},
]
assert.equal(
resolveCost(
item({ fulfillment_channel: 'work_order', canonical_product_id: null }),
costItems,
zeroVersions,
pricingModes,
),
null,
)
})
test('比例成本按费率计算,费率为 0 视为未配置', () => {
const percentItems = new Map<string, CostItemRow>([
[
'sys_work_order_reward',
{
id: 1,
code: 'sys_work_order_reward',
pricing_mode: 'percent_of_revenue',
cost_model: 'work_order_reward',
},
],
])
const percentVersions: CostVersionRow[] = [
{
id: 901,
cost_item_id: 1,
scope_type: 'rule',
canonical_product_id: null,
work_product_rule_id: 9,
amount_fen: 0,
rate_bps: 500,
effective_from: '2026-09-01T00:00:00+08:00',
},
]
const resolution = resolveCost(
item({ fulfillment_channel: 'work_order', work_product_rule_id: 9, revenue_fen: 10000 }),
percentItems,
percentVersions,
new Map([[1, 'percent_of_revenue']]),
)
assert.equal(resolution?.totalAmountFen, 500)
const zeroRate = percentVersions.map((row) => ({ ...row, rate_bps: 0 }))
assert.equal(
resolveCost(
item({ fulfillment_channel: 'work_order', work_product_rule_id: 9 }),
percentItems,
zeroRate,
new Map([[1, 'percent_of_revenue']]),
),
null,
)
})
test('通用成本模型按商品/全局版本解析且能与渠道成本共存', () => {
const genericItems: CostItemRow[] = [
{
id: 20,
code: 'sys_platform_fee',
pricing_mode: 'percent_of_revenue',
cost_model: 'platform_fee',
},
]
const versionsWithGeneric: CostVersionRow[] = [
{
id: 902,
cost_item_id: 20,
scope_type: 'global',
canonical_product_id: null,
work_product_rule_id: null,
amount_fen: 0,
rate_bps: 300,
effective_from: '2026-09-01T00:00:00+08:00',
},
]
const generic = resolveGenericCosts(
item({ revenue_fen: 20000 }),
genericItems,
versionsWithGeneric,
new Map([[20, 'percent_of_revenue']]),
)
assert.equal(generic.length, 1)
assert.equal(generic[0]?.totalAmountFen, 600)
})