feat(finance): 商品归一、成本快照与渠道销售事实三条管道
- 商品归一统一为一个匹配器,禁止自动建商品,未命中进未归一队列 - 成本快照收敛为唯一入口:商品/规则/全局版本解析,工单验收自动升级 actual - 异常改为 SKU 级聚合,补齐后自动关闭 - 销售事实按渠道汇总,pending 收入进 unclassified 桶,归档不进报表
This commit is contained in:
@@ -0,0 +1,174 @@
|
||||
import assert from 'node:assert/strict'
|
||||
import test from 'node:test'
|
||||
|
||||
import {
|
||||
resolveCost,
|
||||
selectCostVersion,
|
||||
type CostItemRow,
|
||||
type CostVersionRow,
|
||||
} from './cost-snapshot-service.js'
|
||||
|
||||
const costItems = new Map<string, CostItemRow>([
|
||||
['sys_affiliate_purchase', { id: 3, code: 'sys_affiliate_purchase', pricing_mode: 'per_item' }],
|
||||
['sys_work_order_reward', { id: 1, code: 'sys_work_order_reward', pricing_mode: 'per_order' }],
|
||||
[
|
||||
'sys_third_party_voucher_cost',
|
||||
{ id: 4, code: 'sys_third_party_voucher_cost', pricing_mode: 'per_item' },
|
||||
],
|
||||
])
|
||||
|
||||
const pricingModes = new Map([
|
||||
[1, 'per_order'],
|
||||
[3, 'per_item'],
|
||||
[4, 'per_item'],
|
||||
])
|
||||
|
||||
const versions: CostVersionRow[] = [
|
||||
{
|
||||
id: 100,
|
||||
cost_item_id: 3,
|
||||
scope_type: 'product',
|
||||
canonical_product_id: 11,
|
||||
work_product_rule_id: null,
|
||||
amount_fen: 1200,
|
||||
effective_from: '2026-09-29T00:00:00+08:00',
|
||||
},
|
||||
{
|
||||
id: 101,
|
||||
cost_item_id: 1,
|
||||
scope_type: 'rule',
|
||||
canonical_product_id: null,
|
||||
work_product_rule_id: 9,
|
||||
amount_fen: 840,
|
||||
effective_from: '2026-09-29T00:00:00+08:00',
|
||||
},
|
||||
{
|
||||
id: 102,
|
||||
cost_item_id: 4,
|
||||
scope_type: 'product',
|
||||
canonical_product_id: 33,
|
||||
work_product_rule_id: null,
|
||||
amount_fen: 5000,
|
||||
effective_from: '2026-10-20T00:00:00+08:00',
|
||||
},
|
||||
{
|
||||
id: 103,
|
||||
cost_item_id: 4,
|
||||
scope_type: 'product',
|
||||
canonical_product_id: 33,
|
||||
work_product_rule_id: null,
|
||||
amount_fen: 6000,
|
||||
effective_from: '2026-11-01T00:00:00+08:00',
|
||||
},
|
||||
]
|
||||
|
||||
function item(patch: Record<string, unknown> = {}) {
|
||||
return {
|
||||
id: 1,
|
||||
order_id: 1,
|
||||
fulfillment_channel: 'affiliate_dash',
|
||||
canonical_product_id: 11,
|
||||
quantity: 2,
|
||||
basis_at: '2026-10-31T10:00:00+08:00',
|
||||
work_order_id: null as number | null,
|
||||
work_product_rule_id: null as number | null,
|
||||
work_order_status: null as string | null,
|
||||
reward_amount: null as number | null,
|
||||
...patch,
|
||||
}
|
||||
}
|
||||
|
||||
test('成本解析:自动发货按件计,单价乘数量', () => {
|
||||
const resolution = resolveCost(item(), costItems, versions, pricingModes)
|
||||
assert.ok(resolution)
|
||||
assert.equal(resolution.costVersionId, 100)
|
||||
assert.equal(resolution.totalAmountFen, 2400)
|
||||
assert.equal(resolution.stage, 'estimated')
|
||||
})
|
||||
|
||||
test('成本解析:卡券渠道按件计,取业务日期前的最新版本', () => {
|
||||
const resolution = resolveCost(
|
||||
item({ fulfillment_channel: 'third_party_voucher', canonical_product_id: 33, quantity: 1 }),
|
||||
costItems,
|
||||
versions,
|
||||
pricingModes,
|
||||
)
|
||||
assert.ok(resolution)
|
||||
assert.equal(resolution.costVersionId, 102)
|
||||
assert.equal(resolution.unitAmountFen, 5000)
|
||||
})
|
||||
|
||||
test('成本解析:生效日期晚于业务日期的版本不参与', () => {
|
||||
const version = selectCostVersion(versions, {
|
||||
costItemId: 4,
|
||||
productId: 33,
|
||||
ruleId: null,
|
||||
basisAt: '2026-10-31T10:00:00+08:00',
|
||||
})
|
||||
assert.equal(version?.id, 102)
|
||||
|
||||
const future = selectCostVersion(versions, {
|
||||
costItemId: 4,
|
||||
productId: 33,
|
||||
ruleId: null,
|
||||
basisAt: '2026-11-02T10:00:00+08:00',
|
||||
})
|
||||
assert.equal(future?.id, 103)
|
||||
})
|
||||
|
||||
test('成本解析:接单平台优先按规则版本', () => {
|
||||
const resolution = resolveCost(
|
||||
item({
|
||||
fulfillment_channel: 'work_order',
|
||||
canonical_product_id: 55,
|
||||
work_product_rule_id: 9,
|
||||
quantity: 1,
|
||||
}),
|
||||
costItems,
|
||||
versions,
|
||||
pricingModes,
|
||||
)
|
||||
assert.ok(resolution)
|
||||
assert.equal(resolution.costVersionId, 101)
|
||||
assert.equal(resolution.totalAmountFen, 840)
|
||||
})
|
||||
|
||||
test('成本解析:工单已验收时用实际奖励金额,阶段为 actual', () => {
|
||||
const resolution = resolveCost(
|
||||
item({
|
||||
fulfillment_channel: 'work_order',
|
||||
canonical_product_id: 55,
|
||||
work_product_rule_id: 9,
|
||||
work_order_id: 77,
|
||||
work_order_status: 'accepted',
|
||||
reward_amount: 3300,
|
||||
quantity: 1,
|
||||
}),
|
||||
costItems,
|
||||
versions,
|
||||
pricingModes,
|
||||
)
|
||||
assert.ok(resolution)
|
||||
assert.equal(resolution.totalAmountFen, 3300)
|
||||
assert.equal(resolution.stage, 'actual')
|
||||
})
|
||||
|
||||
test('成本解析:没有可用版本时返回 null(进成本缺失队列,不记 0)', () => {
|
||||
const resolution = resolveCost(
|
||||
item({ canonical_product_id: 999 }),
|
||||
costItems,
|
||||
versions,
|
||||
pricingModes,
|
||||
)
|
||||
assert.equal(resolution, null)
|
||||
})
|
||||
|
||||
test('成本解析:人工渠道不在这里算成本', () => {
|
||||
const resolution = resolveCost(
|
||||
item({ fulfillment_channel: 'manual' }),
|
||||
costItems,
|
||||
versions,
|
||||
pricingModes,
|
||||
)
|
||||
assert.equal(resolution, null)
|
||||
})
|
||||
Reference in New Issue
Block a user