Files
SupplierSystem/server/tests/skuCostExcludesIndependentInbound.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

54 lines
2.5 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 path from 'node:path';
import { describe, expect, it } from 'vitest';
/**
* **独立进货单不进「订单商品成本 / 订单采购成本」**(2026-10-14 开发者口径)。
*
* 开发者原话:「当前独立进货的单子是不能在订单采购成本这里面展示的 相当于计算订单的成本
* 默认是排除这个独立进货的」。
*
* 为什么是它:独立进货是**进货/补库存**凭证(哨兵客户 `__INDEPENDENT_INBOUND__`),不是客户订单;
* 它的成本属存货。真库实测(BR002 2026-10-14):`ORD_WEB_20261014_BR002_0002` 15 行 **¥1,449**
* 原本占「供应采购成本 ¥1,586」的主体 ⇒ 排除后 供应 ¥137 / 自采 ¥140 / 合计 ¥277。
*
* 口径来源:**复用销售侧既有判据** `salesVisibleOrderSql`(不新造口径)。
*/
const src = readFileSync(
path.join(__dirname, '../src/services/marginQueryService.ts'),
'utf8'
);
/**
* 取订单行取数那一段(`src_line_base`)—— 拆段后「排除独立进货」的判据在这里,
* 抵扣段与缺口段都从它派生 ⇒ 排一处即覆盖两段。
*/
function srcCostLinesBlock(): string {
const start = src.indexOf('src_line_base AS (');
expect(start, '必须有 src_line_base CTE(2026-10-14 拆段后订单行取数在这里)').toBeGreaterThan(-1);
const next = src.indexOf('AS (', start + 'src_line_base AS ('.length);
return src.slice(start, next > start ? next : start + 3000);
}
describe('订单商品成本:排除独立进货', () => {
it('src_cost_lines 的 WHERE 里挂了销售侧同款判据 salesVisibleOrderSql', () => {
const block = srcCostLinesBlock();
expect(block).toContain("AND ${salesVisibleOrderSql('o')}");
// 判据本身来自 independentInbound(不在本文件手写哨兵串,避免口径分叉)
expect(src).toContain("import { salesVisibleOrderSql } from './independentInbound';");
expect(block).not.toContain("'__INDEPENDENT_INBOUND__'");
});
it('判据函数本体就是"排除哨兵客户"(与销售侧同一个)', () => {
const helper = readFileSync(
path.join(__dirname, '../src/services/independentInbound.ts'),
'utf8'
);
const start = helper.indexOf('export function salesVisibleOrderSql(');
expect(start).toBeGreaterThan(-1);
const body = helper.slice(start, start + 220);
expect(body).toContain('IS DISTINCT FROM');
expect(body).toContain('INDEPENDENT_INBOUND_CUSTOMER_CODE');
});
});