Files
SupplierSystem/server/tests/processingDemandZeroLoss.test.ts
T
Ubuntu be9bc282af
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(领料加工): 全损耗(成品净重 0)收不了口 ⇒ 需求单永久卡在「加工中」
事故(真跑 P1):领料 13 斤、成品验收 actual_qty=0 合格(全损耗)后,
processing_tasks.process_status 永久停 20(加工中)、material_cost ¥39.00 悬空在制品、
订单行 processing_settled_unit_cost 恒 NULL。

根因:recalcDemandSheetFromInspections 的 allFulfilled 只认三种情况
(① 有 actual_qty>0 的验收 ② 还有 inspection_time IS NULL 的待验行 ③ 被库存抵扣全覆盖),
全损耗时三条都不成立 ⇒ 永不 completed ⇒ settleDemandSheetCostOnCompletion 从不被调用。

改法(开发者 2026-10-15 裁决 A 的「收口」部分):补第四种放行口径 ——
「已完工但实收为 0」的验收(inspection_time 非空、未作废、非退回)⇒ 视为已交付(净重 0),
接上既有「净重为 0」结算分支(写 settled=0、清账、不做 0 除)。

改前 → 改后(真跑 BR002,同一脚本):
· 验收(实收 0) 后 process_status:20(永不推进)→ 40(已完工)
· 订单行 processing_settled_unit_cost:NULL → 0
· net_weight / loss_qty:0 / 13 不变;unit_cost 0 不变(仓内加工成品行成本固定 0,口径未动)

⚠️ 裁决 A 的第二步「损耗整批入 processing_loss」**未实施**(设计文 §5 已列证据):
① 与冻结口径「仓内加工成品行不计入合计」冲突;
② 写 cost_type='purchase' 的损耗行会被 ORDER_ITEM_MARGIN_COST_AMOUNT_SQL 直接加总,
   而同一笔原料成本在入库时已计过一次(去重判据按 SKU,拦不住跨 SKU 的同一笔钱)⇒ 重复计成本;
③ 设计文假设的「材料 → 在制品」转移链未实现(BR002 实测 cost_details 里 inventory_holding 0 行)。
另有反向信号:代码引用的 20260831-processing-not-on-ledger 文档在仓库中不存在,
且 warehouseProcessingDemandSheet.test.ts:258 有断言钉住「领料不写 cost_details」。
=> 两个落位(甲:仅任务级留痕,与现行口径一致;乙:建转移链并改冻结口径)待裁决。

测试:新增 processingDemandZeroLoss.test.ts(6 例,含「摘掉零损耗分支必须 FAIL」反向验证)。
验证:server tsc 0 error;vitest 32 failed / 2999 passed(443 文件)—— 失败数与基线 32 例相同、
失败文件集与基线一致(无新增);设计门禁 8 通过 / 1 红(check-ai-cost) / 1 未执行,与改动前同基线。
造数清理:真实批次 IN20261014HDY0BA 已还原(首次试跑误吃 13 斤,available_qty 15→2→15 并删对应流水),
RLS 感知全库 TST% 扫描 0 命中。

设计:docs/design/20261015-领料加工全损耗收口.md
2026-10-15 07:24:33 +08:00

151 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';
/**
* 领料加工「全损耗」必须能收口(2026-10-15 开发者裁决 A)
*
* 事故(真跑,评审员 D):成品验收实收 0(全损耗)时,`recalcDemandSheetFromInspections` 的
* 完工判定只认三种情况 ——
* ① 该行有 `actual_qty > 0` 的验收;② 还有 `inspection_time IS NULL` 的待验行;③ 该行被库存抵扣全覆盖。
* 全损耗时三条都不成立 ⇒ `allFulfilled=false` ⇒ `process_status` **永久停在 20(加工中)**、
* `settleDemandSheetCostOnCompletion` 从不被调用 ⇒ 材料成本悬空在制品、订单行
* `processing_settled_unit_cost` 恒 NULL(真跑:领料 13 斤 ¥39.00 卡在 20)。
*
* 修法:把「**已完工但实收为 0** 的验收」(`inspection_time` 非空、未作废、非退回)也算作
* 已交付(净重 0)⇒ 与既有 `settle` 的「净重为 0」分支接上(写 `processing_settled_unit_cost = 0`
* 并清账),需求单正常完工。
*
* 见 docs/design/20260823-demand-sheet-cost-settlement.md(「净重为 0 ⇒ 整批记 processing_loss」)
* 与 docs/design/20261015-领料加工全损耗收口.md。
*/
const src = readFileSync(
new URL('../src/services/processingDemandSheetService.ts', import.meta.url),
'utf8'
);
function functionBody(source: string, name: string): string {
const start = source.indexOf(`export async function ${name}`);
expect(start, `${name} 应存在`).toBeGreaterThanOrEqual(0);
const rest = source.slice(start + 1);
const candidates = [
rest.indexOf('export async function'),
rest.indexOf('export function'),
].filter((n) => n >= 0);
const next = candidates.length ? Math.min(...candidates) : -1;
return next >= 0 ? source.slice(start, start + 1 + next) : source.slice(start);
}
/** 只保留代码(去注释),避免注释里的同名字样让断言失去意义 */
function stripComments(text: string): string {
let out = '';
let i = 0;
let quote: string | null = null;
while (i < text.length) {
const ch = text[i];
const next = text[i + 1];
if (quote) {
out += ch;
if (ch === '\\') {
out += next ?? '';
i += 2;
continue;
}
if (ch === quote) quote = null;
i += 1;
continue;
}
if (ch === "'" || ch === '"' || ch === '`') {
quote = ch;
out += ch;
i += 1;
continue;
}
if (ch === '/' && next === '/') {
while (i < text.length && text[i] !== '\n') i += 1;
continue;
}
if (ch === '/' && next === '*') {
i += 2;
while (i < text.length && !(text[i] === '*' && text[i + 1] === '/')) i += 1;
i += 2;
continue;
}
out += ch;
i += 1;
}
return out;
}
const body = functionBody(src, 'recalcDemandSheetFromInspections');
const code = stripComments(body);
const ZERO_LOSS_GUARD = 'if (zeroLossDone.rowCount) continue;';
/** 判据本体:只查「已完工但实收 0」的验收 */
const ZERO_LOSS_SQL_MARK = 'COALESCE(i.actual_qty, 0) = 0';
/** 反向验证:把零损耗分支整体摘掉(= 修前形态) */
function withoutZeroLoss(text: string): string {
const start = text.indexOf('const zeroLossDone = await db.query(');
expect(start, '应存在 zeroLossDone 查询').toBeGreaterThanOrEqual(0);
const end = text.indexOf(ZERO_LOSS_GUARD);
expect(end, '应存在 zeroLossDone 守卫').toBeGreaterThan(start);
return text.slice(0, start) + text.slice(end + ZERO_LOSS_GUARD.length);
}
describe('全损耗(成品净重 0)必须判定为「已交付」', () => {
it('存在「已完工但实收 0」的验收判据', () => {
expect(code).toContain('const zeroLossDone = await db.query(');
expect(code).toContain(ZERO_LOSS_SQL_MARK);
expect(code).toContain('i.inspection_time IS NOT NULL');
expect(code).toContain('COALESCE(i.compliance_status, 0) <> 30');
});
it('该分支在完工判定里被当作已交付(不置 allFulfilled=false)', () => {
expect(code).toContain(ZERO_LOSS_GUARD);
const at = code.indexOf(ZERO_LOSS_GUARD);
const around = code.slice(Math.max(0, at - 400), at + 60);
expect(around).not.toContain('allFulfilled = false');
});
it('顺序正确:待验行优先(还有待验 ⇒ 不得提前完工),零损耗分支在其后', () => {
const pendingAt = code.indexOf('if (pending.rowCount)');
const zeroAt = code.indexOf('const zeroLossDone');
const coveredAt = code.indexOf('if (orderedQty > 0 && coveredQty >= orderedQty - 1e-9)');
expect(pendingAt).toBeGreaterThanOrEqual(0);
expect(zeroAt).toBeGreaterThan(pendingAt);
expect(coveredAt).toBeGreaterThan(zeroAt);
});
it('反向验证:摘掉零损耗分支 ⇒ 判据消失(修前形态必须被本断言拒绝)', () => {
const stripped = stripComments(withoutZeroLoss(body));
expect(stripped).not.toContain('zeroLossDone');
expect(stripped).not.toContain(ZERO_LOSS_SQL_MARK);
// 修前只剩三种放行口径:实收>0、有待验、被抵扣覆盖
expect(stripped).toContain('if (actualQty > 0) continue;');
expect(stripped).toContain('if (pending.rowCount)');
expect(stripped).toContain('coveredQty >= orderedQty - 1e-9');
});
});
describe('与既有「净重为 0」结算分支接上(写 0 并清账,不除零)', () => {
const settle = stripComments(functionBody(src, 'settleDemandSheetCostOnCompletion'));
it('总净重为 0 时写 settled=0 并清账,不产生 unit_cost 除零', () => {
expect(settle).toContain('if (totalInspected <= 0)');
expect(settle).toContain('writeDemandLinkedSettledUnitCost(db, id, 0)');
expect(settle).toContain('clearProcessingLedger()');
// 分摊调用必须在零净重早退之后,否则会被 0 除
const earlyReturnAt = settle.indexOf('if (totalInspected <= 0)');
const allocateAt = settle.indexOf('allocateProcessingCostByWeight(');
expect(allocateAt).toBeGreaterThan(earlyReturnAt);
});
it('完工后才可能调用结算(不能反过来)', () => {
const completedAt = code.indexOf('const completed = allFulfilled && hasPick;');
const settleAt = code.indexOf('settleDemandSheetCostOnCompletion(');
expect(completedAt).toBeGreaterThanOrEqual(0);
expect(settleAt).toBeGreaterThan(completedAt);
});
});