From afb4e7507d31314667e27294d2a8568583f6eefb Mon Sep 17 00:00:00 2001 From: Ubuntu Date: Wed, 14 Oct 2026 19:03:54 +0800 Subject: [PATCH] =?UTF-8?q?docs+test(=E6=88=90=E6=9C=AC=E5=8F=A3=E5=BE=84)?= =?UTF-8?q?:=20=E6=96=B9=E6=A1=88=20A=20=E5=AE=9E=E6=B5=8B=E4=B8=8E?= =?UTF-8?q?=E9=92=89=E6=A1=A9=20=E2=80=94=E2=80=94=20=E8=87=AA=E9=87=87/?= =?UTF-8?q?=E5=A4=87=E8=B4=A7=E5=88=97=E8=A1=A8=E6=94=B9=E4=BB=B7=20?= =?UTF-8?q?=E2=87=92=20=E8=AE=A2=E5=8D=95=E8=A1=8C=E6=88=90=E6=9C=AC?= =?UTF-8?q?=E4=BB=B7=E5=9B=9E=E5=86=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 开发者 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 已注明)。 --- ...1014-订单成本排除独立进货与库存成本分类.md | 22 ++++++++++ .../amendActualWritesOrderItemCost.test.ts | 41 +++++++++++++++++++ 2 files changed, 63 insertions(+) create mode 100644 server/tests/amendActualWritesOrderItemCost.test.ts diff --git a/docs/design/20261014-订单成本排除独立进货与库存成本分类.md b/docs/design/20261014-订单成本排除独立进货与库存成本分类.md index 90e5e0d..e4d9a89 100644 --- a/docs/design/20261014-订单成本排除独立进货与库存成本分类.md +++ b/docs/design/20261014-订单成本排除独立进货与库存成本分类.md @@ -264,6 +264,28 @@ const outsourcedNew = isStockCoverOutsourcedOnlyEnabled() 自采单(缺口):**10 / 15 / 7,状态 10(待采购)** ⇒ 采购管理「自采列表」显示**进行中 3**; 成本页:自采表 销量 10/15/7 + 采购 ¥96,库存抵扣表 销量 30/15/8 + 库存 ¥159,整行 ¥255。 +### 6.6 方案 A 实测:自采/备货列表改价 ⇒ 订单行成本价回写(2026-10-14 开发者选定 A) + +口径(开发者原话):「这种部分抵扣的,如果是在**自采与备货列表修改了成本价**,那么应该是单独的一个列表展示, +因为我们的**商品的成本价 = 库存抵扣的总成本 + 缺失部分(自采/供应商备货)的总成本**,再 ÷ 计算数量」。 + +- **链路已存在**:`purchaseAmendService` 的「实采价 → 订单行成本回写」段(`setOrderItemUnitCostByOrderSku`, + 与「有无实收」解耦,注释即写明「方案 A:改价本来就要回写给订单行」)—— 本轮**未改代码**,只做实测与钉桩。 +- **真库实测**(BR002 `ORD_API_20261014_BR002_0001`,苹果 计划40/抵扣30/缺口10;把该自采单临时置 20 以过改价闸门): + +| 动作 | 订单行 unit_cost | 自采段(苹果) | 抵扣段(苹果) | totals 采购 | totals 整行 | +| ---- | ---------------- | -------------- | -------------- | ----------- | ----------- | +| 改前 | ¥3.00 | 10 斤 / **¥30** / 单位 ¥3.00 | 30 斤 / **¥90** / 单位 ¥3.00 | ¥96 | ¥255 | +| 自采单价改 **¥4** | **¥4.00** | 10 斤 / **¥40** / 单位 ¥4.00 | 30 斤 / ¥90 / 单位 ¥3.00(**未动**) | **¥106** | **¥265** | +| 改回 ¥3 | ¥3.00 | 10 斤 / ¥30 / ¥3.00 | 30 斤 / ¥90 / ¥3.00 | ¥96 | ¥255 | + +⇒ 苹果的**商品成本价 =(¥90 + ¥40)÷ 40 斤 = ¥3.25/斤**(两表各自成行后逐行相加即得)。 +- **抵扣段天然隔离**:抵扣成本走账本 `cost_details(inventory_cover)`,不读 `unit_cost` ⇒ 改价只影响缺口段。 +- ⚠️ **改价闸门**:`amend-actual` 只允许 **20 待仓验 / 30 已完成** 的自采单改价(待采购 10 不行)—— + 是否放开「待采购即可改价」属采购冻结面,需开发者另行裁决。 +- 契约测试:`server/tests/amendActualWritesOrderItemCost.test.ts`(4 项:与实收解耦、0 价开关、 + 只动 unit_cost 不碰抵扣、状态闸门未变)。 + ## 7. 回滚 各块独立:`git revert`(无迁移)。数据侧:抵扣行供应商回 `UNASSIGNED`/`INTERNAL`、库存成本随账本走(不新增账本行)。 diff --git a/server/tests/amendActualWritesOrderItemCost.test.ts b/server/tests/amendActualWritesOrderItemCost.test.ts new file mode 100644 index 0000000..495bdf9 --- /dev/null +++ b/server/tests/amendActualWritesOrderItemCost.test.ts @@ -0,0 +1,41 @@ +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))'); + }); +});