Files
SupplierSystem/server/tests/amendActualWritesOrderItemCost.test.ts
T
Ubuntu afb4e7507d
CI / server typecheck + tests (push) Canceled after 0s
CI / web typecheck + tests + miniprogram syntax (push) Canceled after 0s
CI / eslint (观察项,C7 骨架) (push) Canceled after 0s
CI / npm audit (critical 硬拦) (push) Canceled after 0s
docs+test(成本口径): 方案 A 实测与钉桩 —— 自采/备货列表改价 ⇒ 订单行成本价回写
开发者 2026-10-14 选定「方案 A」:改价要回写订单行,商品成本价 =
(库存抵扣总成本 + 缺口部分实际总成本)÷ 计算数量。本轮**未改业务代码**(链路已存在),
只做真库实测 + 契约测试钉桩 + 设计文记录。

实测(BR002 ORD_API_20261014_BR002_0001,苹果 计划40 / 抵扣30 / 缺口10):
  把该自采单临时置「待仓验 20」以过改价闸门,用真实服务 amendPurchaseActual 改价:
    改前 ¥3  ⇒ 订单行 unit_cost=¥3.00、自采段 10 斤 = ¥30、抵扣段 30 斤 = ¥90、totals 采购 ¥96 / 整行 ¥255
    改 ¥4    ⇒ 订单行 unit_cost=¥4.00、自采段 10 斤 = **¥40**、抵扣段 **未动** ¥90、totals 采购 **¥106** / 整行 **¥265**
    改回 ¥3  ⇒ 全部复原(幂等);自采单状态已恢复「待采购 10」
  ⇒ 苹果商品成本价 =(¥90 + ¥40)÷ 40 斤 = **¥3.25/斤**(两表各自成行后相加即得)。
抵扣段天然隔离:其成本走账本 cost_details(inventory_cover),不读 unit_cost。

契约测试 server/tests/amendActualWritesOrderItemCost.test.ts(4 项):
  ① 价格变了就回写(与「有无实收」解耦,Math.abs(nextPrice-beforePrice)>1e-9);
  ② 0 价按「允许成本价为 0」开关决定是否回写;③ 只动 unit_cost,不碰 stock_cover_qty / inventory_cover;
  ④ 改价闸门未变(仅 20/30 可改)。

⚠️ 记录:`amend-actual` 只允许 20 待仓验 / 30 已完成 的自采单改价 —— 是否放开「待采购即可改价」属采购冻结面,
待开发者裁决(设计文 §6.6 已注明)。
2026-10-14 19:03:54 +08:00

42 lines
2.2 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { readFileSync } from 'node:fs';
import { describe, expect, it } from 'vitest';
/**
* **方案 A(开发者 2026-10-14 批准):自采/备货列表改价必须回写订单行成本**。
*
* 口径:商品成本 = **库存抵扣总成本 + 缺口部分(自采/供应商备货)的实际总成本**,
* 再 ÷ 计算数量;因此缺口那部分的价格一旦在自采/备货列表被改,
* 必须回写 `order_items.unit_cost`(= 「缺口那部分的价」,见 §5.1),
* 成本页的缺口段才会跟着变;抵扣段成本走账本 `inventory_cover`,**不受** unit_cost 影响。
*
* 真库实测(BR002 `ORD_API_20261014_BR002_0001`,苹果 计划40/抵扣30/缺口10):
* 改价 ¥3 → ¥4 ⇒ 订单行 unit_cost=4、自采段 苹果 10 斤 = **¥40**(单位 ¥4.00)、
* totals 采购 ¥96→**¥106**、整行 ¥255→**¥265**;抵扣段仍 ¥90/¥3.00 未动;
* 改回 ¥3 ⇒ 一切复原(幂等)。
*/
const src = readFileSync(new URL('../src/services/purchaseAmendService.ts', import.meta.url), 'utf8');
describe('方案 A:改实采价 → 回写订单行成本', () => {
it('① 价格确实变了就回写(与「有无实收」解耦)', () => {
expect(src).toContain('Math.abs(nextPrice - beforePrice) > 1e-9');
expect(src).toContain('const { setOrderItemUnitCostByOrderSku } = await import(');
expect(src).toContain('await setOrderItemUnitCostByOrderSku(');
});
it('② 0 价按「允许成本价为 0」开关决定是否回写(不把未录价写成 0)', () => {
expect(src).toContain('const allowZeroWriteback =');
expect(src).toContain('isZeroSupplyCostAllowed(');
});
it('③ 回写只动订单行成本价:不得碰抵扣量/抵扣账本', () => {
const block = src.slice(src.indexOf('实采价 → **订单行成本回写**'));
expect(block).toContain('setOrderItemUnitCostByOrderSku');
expect(block).not.toContain('stock_cover_qty');
expect(block).not.toContain('inventory_cover');
});
it('④ 改价入口的状态闸门未变(仅待仓验 20 / 已完成 30 可改)', () => {
expect(src).toContain('if (![20, 30].includes(status))');
});
});