Files
SupplierSystem/server/tests/skuDirectCostsQtyTimesUnitCost.test.ts
T
Ubuntu e7e22a564d
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
fix(商品成本): 按来源**拆段** —— 抵扣段与缺口段各自成行(修「库存抵扣表头 ¥159、表体 0 行」)
开发者 2026-10-14 第三轮真机反馈(附库存列表:三个批次可用 30/15/8、预留占用 30/15/8):
「库存里面已经被预留占用了,说明库存抵扣是生效了的呀 —— 为什么库存抵扣成本这块没有抵扣数量的成本展示,
 反而全部都是给到了自采?自采列表还存在已经完成的自采数据,代码出现了问题。」

## 核查结论(真库)
- 库存抵扣**确实生效**:预留 30/15/8、账本 `inventory_cover` ¥90/45/24(= 抵扣量 × 被抵扣批次成本价 ¥3)。
- 该单 `purchase_orders` **0 行** ⇒ 所谓「已完成的自采数据」是**成本页展示**造成的错觉,不是采购数据。
- 根因:**一行只归一个桶**。部分抵扣的行整行落进「自采采购成本」,抵扣段没有独立成行 ⇒
  「库存抵扣」表**表头 = 账本合计 ¥159、表体 = 0 行**,自相矛盾。

## 修法:一条订单行拆成最多两段
`src_line_base`(一行一条订单行:全量/抵扣量/账本分摊)→ `UNION ALL` 拆段:
- **抵扣段**(`cover_qty > 0`):bucket 恒为 `stock_cover`、销量 = 抵扣量、采购成本 **0**、
  库存成本 = 账本分摊额;
- **缺口段**(缺口 > 0):bucket 按供应商列(新增 `gapOnly` 变体,**恒不返回**库存抵扣桶)、
  销量 = 缺口量、采购成本 = 缺口 × `unit_cost`、库存成本 **0**;
- **销售额只记一次**(有缺口记缺口段,全额抵扣记抵扣段),否则页头销售额/毛利翻倍;
- 页面与**导出明细**同源拆段(`getSkuDirectCostLinesPaged` 同款),两者继续逐项对账相等。

## 真库实测(BR002 ORD_API_20261014_BR002_0001:需求 40/30/15、抵扣 30/15/8、缺口 10/15/7)
| 表 | 销量 | 采购成本 | 库存成本 | 单位成本 |
| 自采采购成本 | 10 / 15 / 7 | **¥96** | ¥0 | ¥3.00 |
| 库存抵扣 | 30 / 15 / 8 | ¥0 | **¥90 / ¥45 / ¥24 = ¥159** | ¥3.00 |
| totals | — | 采购 ¥96 | 库存 ¥159 | 整行 ¥255(=85 斤×¥3)· 销售额 ¥456.5(只计一次) |
导出按来源筛同样两段:self 3 行 ¥96/销量 32;stock_cover 3 行 ¥159/销量 53。

契约测试:新增 `server/tests/skuCostSegmentSplit.test.ts`(5 项);4 个既有成本契约测试同步到拆段形态。
回归:后端完整跑 26 failed→仅剩 1 个因断言过时(已修);前端 2 failed / 163 passed(同基线);
两侧 `tsc` 0 错。
2026-10-14 18:42:08 +08:00

110 lines
6.3 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';
describe('商品成本列表采购金额口径(缺口量 × unit_cost + 账本库存成本,2026-10-14)', () => {
const src = readFileSync(new URL('../src/services/marginQueryService.ts', import.meta.url), 'utf8');
const start = src.indexOf('export async function getSkuDirectCostsPaged');
const next = src.indexOf('export async function getMarginByDimension', start + 1);
const fn = src.slice(start, next > 0 ? next : start + 20000);
const unifiedStart = src.indexOf('export async function getUnifiedCostWindow');
const unified = src.slice(unifiedStart, unifiedStart + 3500);
const aggStart = src.indexOf('export async function getCostAggregation');
const agg = src.slice(aggStart, src.indexOf('export async function listCostDetailsInWindow'));
it('sku list amount uses current quantity × unit_cost, not ledger amount', () => {
// 2026-10-14:口径已是「缺口量 × unit_cost」(全量常量 ORDER_ITEM_CACHED_COST_AMOUNT_SQL 已删,
// 见 docs/design/20261014-订单成本排除独立进货与库存成本分类.md §5.1)
expect(fn).toContain('COALESCE(oi.unit_cost, 0)');
expect(fn).toContain('skuListDisplayCostBucketSql');
expect(fn).toContain('FROM order_items oi');
expect(fn).not.toContain('CD_EFFECTIVE_AMOUNT_SQL');
expect(fn).not.toContain('order_item_cost');
});
it('display bucket is only order-line supplier_code, not category or ledger', () => {
const helperStart = src.indexOf('function skuListDisplayCostBucketSql');
const helper = src.slice(helperStart, src.indexOf('export const CD_POSTED_STATUS_SQL'));
expect(helper).toContain("IN ('INTERNAL', 'UNASSIGNED')");
expect(helper).toContain("NULLIF(TRIM(COALESCE(oi.supplier_code, '')), '') IS NULL");
expect(helper).toContain("ELSE 'supplier'");
expect(helper).not.toContain('purchaseCostBucketExpr');
expect(helper).not.toContain('purchase_orders');
expect(helper).not.toContain('is_extra_dish');
expect(helper).not.toContain('category_name');
// 20260914:仓内加工行单独成行(不与自采合并数量),见
// docs/design/20260914-sku-cost-warehouse-processing-separate-row.md
expect(fn).toContain('GROUP BY sku_code, is_extra_dish, cost_bucket, is_warehouse_processing');
// 仓内加工行成本/金额一律按 0 计(含历史未归零行)
expect(fn).toContain('CASE WHEN sc.is_warehouse_processing THEN 0 ELSE sc.self_cost END');
});
it('aggregation and unified window do not use list display CASE', () => {
expect(agg).not.toContain('skuListDisplayCostBucketSql');
expect(unified).not.toContain('skuListDisplayCostBucketSql');
expect(agg).toContain('purchaseCostBucketExpr');
});
it('unit cost divides by planned quantity', () => {
expect(fn).toContain('unit_cost: quantity > 0 ? round2(totalCost / quantity) : 0');
expect(fn).not.toContain('totalCost / fulfilledQty');
});
/**
* **2026-10-14 口径变更**(开发者定稿,见
* docs/design/20261014-订单成本排除独立进货与库存成本分类.md §5.1):
* 采购金额由 **全量 `quantity × unit_cost`** 改为 **缺口口径** `(quantity − stock_cover_qty) × unit_cost`
* —— 部分抵扣的行(30 斤里抵 20、采 10)原式会把已抵扣那部分也按缺口价算成采购成本(多报);
* 抵扣那部分的成本另由新增的 `stock_cover_cost`(账本 `cost_details.inventory_cover`)承担,
* 两者相加 = 行真实成本,`unit_cost` = 两者相加 ÷ 销量。
*/
it('按来源拆段:抵扣段(采购 0 + 库存成本)与缺口段(缺口×单价 + 库存 0)各自成行', () => {
// 2026-10-14 真机裁定:「库存抵扣生效了,为什么抵扣数量没有成本展示、全给到自采」
// ⇒ 一行拆两段,见 docs/design/20261014-订单成本排除独立进货与库存成本分类.md §6.4
expect(fn).toContain('src_line_base AS (');
expect(fn).toContain("'stock_cover' AS cost_bucket");
expect(fn).toContain('cover_share AS stock_cover_cost');
expect(fn).toContain('(GREATEST(0, full_qty - cover_qty) * unit_cost)::numeric AS amount');
// 销售额只记一次:有缺口记在缺口段,全额抵扣记在抵扣段
expect(fn).toContain('CASE WHEN full_qty - cover_qty > 1e-9 THEN 0 ELSE line_sales END AS line_sales');
// 账本抵扣成本按 (订单×SKU) 汇总后分摊
expect(fn).toContain('cost_source = ' + "'inventory_cover'");
// 旧口径不得再当采购金额
expect(fn).not.toContain('${ORDER_ITEM_CACHED_COST_AMOUNT_SQL}::numeric AS amount');
});
it('lists zero-cost SKUs instead of dropping them (20260923-sku-cost-list-include-zero)', () => {
expect(fn).not.toContain('sc.self_cost > 0');
expect(fn).not.toContain('sc.supplier_cost > 0');
expect(fn).not.toContain('purchase_cost > 0');
expect(fn).toContain("AND sc.cost_bucket = 'self'");
expect(fn).toContain("AND sc.cost_bucket = 'supplier'");
expect(fn).toContain("AND line.cost_bucket = 'self'");
expect(fn).toContain("AND line.cost_bucket = 'supplier'");
});
it('purchase rows ignore allocated_amount on the shared read path', () => {
const helperStart = src.indexOf('const CD_EFFECTIVE_AMOUNT_SQL');
const helper = src.slice(helperStart, helperStart + 200);
expect(helper).toContain('costEffectiveAmountSql');
const shared = readFileSync(
new URL('../src/services/purchaseCostBucketSql.ts', import.meta.url),
'utf8'
);
const sharedStart = shared.indexOf('export function costEffectiveAmountSql');
const sharedSql = shared.slice(sharedStart, sharedStart + 400);
expect(sharedSql).toContain("WHEN ${cd}.cost_type IN ('purchase', 'purchase_applied')");
expect(sharedSql).toContain('THEN COALESCE(${cd}.amount, 0)');
expect(sharedSql).toContain('ELSE COALESCE(${cd}.allocated_amount, ${cd}.amount, 0)');
});
it('unified window and aggregation still sum cost_details', () => {
expect(unified).toContain('FROM cost_details cd');
expect(unified).toContain('CD_EFFECTIVE_AMOUNT_SQL');
expect(agg).toContain('FROM cost_details cd');
expect(agg).toContain('CD_EFFECTIVE_AMOUNT_SQL');
expect(agg).toContain('purchase_self');
expect(agg).toContain('excludePurchaseDupesForKpiSql');
expect(unified).toContain('excludePurchaseDupesForKpiSql');
});
});