部分抵扣链路真库实测(量 20 / 抵扣 15 ⇒ 缺口 5,厂商 SUP61936431)暴露两个收口缺陷: 1. 只重算"本单当前涉及的(厂商×下单日)"不够 —— 把行改回未分配/换厂商后,旧厂商那张单上 本单留下的行不会消失(SS20261013001 仍留 5×6.00、单头 ¥417); 2. 传 `cycle_code`(YYYYMMDD)给 `upsertSupplierStatementForDay` 会静默返回 `empty` (实测:传 20261013 ⇒ 无变化;传 2026-10-13 ⇒ updated、旧行被清)。 修复:新增 `reconcileSupplierStatementsForOrder(orderCode, db?)` = ① 本单当前(厂商×下单日) + ② 已有结算单里仍引用本单的(厂商×下单日,按 order_code / order_item_id 反查),逐个重算; cycle_code 显式还原成 YYYY-MM-DD。生成器守卫不变:已确认单(status>=20) skipped/冻结, 清理只动生成器自己写的 prep/order 行(售后调整、退货冲销行不动)。 挂点仍为分配 COMMIT 后收口(撤备货之后、推 webhook 之前),best-effort。 真库验证(BR003,跑完全部还原): - 部分抵扣 + 分配厂商 ⇒ 厂商备货行 prepare_qty=5、推送给厂商的量=5(不是整单 20); 结算只按缺口:5 × 6.00 = 30.00(单头 387→417;该行成本价按厂商成本价回写 8→6,属既有设计); - 改回未分配 + 量/抵扣复位 ⇒ 备货被撤销、旧厂商结算单上的旧行被清掉、单头回到 387/4 行; - 全抵扣 + 选厂商 ⇒ 仍无备货/无采购/推送 0 行(厂商收不到)。 测试:coverRevokesPrepAndRecalculatesStatement.test.ts 扩到 14 例(含收口范围与日期格式)。 回归:server 25 failed / 391 passed(失败文件集与改动前逐一比对一致,无新增失败);root tsc 0; 门禁 8 通过 / 1 已知红 / 1 未执行。
206 lines
8.9 KiB
TypeScript
206 lines
8.9 KiB
TypeScript
import { readFileSync } from 'node:fs';
|
||
import path from 'node:path';
|
||
import { describe, expect, it } from 'vitest';
|
||
import {
|
||
dropPreparationsCoveredByInventory,
|
||
shouldDropPrepCoveredByInventory,
|
||
type FulfillmentDb,
|
||
} from '../src/services/orderItemFulfillmentService';
|
||
|
||
/**
|
||
* **库存抵扣的收口**(2026-10-13 开发者批准:「先把修复流程做出来」)。
|
||
*
|
||
* 审计发现三个缺口(真库 0 实例、路径确凿):
|
||
* G1 先分配(备货"分配即供货完成"=30)→ 后库存抵扣 ⇒ 旧备货不撤不缩
|
||
* ⇒「待验收」出现永远不会到货的行 + 备货 webhook 继续推给厂商;
|
||
* G2 抵扣入口不重算厂商结算单 ⇒ 已挂账的整单应付留成旧值,财务一确认就永久多计;
|
||
* G3 采购列表「转供应商」按 SUM(quantity) 建备货、不看抵扣 ⇒ 把已抵扣量又算进去。
|
||
*
|
||
* 见 docs/design/20261013-抵扣后撤销多余备货与结算重算(修复).md
|
||
*/
|
||
const SRC = path.join(__dirname, '../src');
|
||
const read = (rel: string) => readFileSync(path.join(SRC, rel), 'utf8');
|
||
|
||
describe('G1 备货撤销策略(纯函数)', () => {
|
||
it('需求已被库存吃干净、且该厂商未入库 ⇒ 删', () => {
|
||
for (const status of [10, 20, 30]) {
|
||
expect(
|
||
shouldDropPrepCoveredByInventory({
|
||
prepareStatus: status,
|
||
hasUncoveredOrderItem: false,
|
||
inboundForThisSupplier: false,
|
||
}),
|
||
`status=${status} 应删`
|
||
).toBe(true);
|
||
}
|
||
});
|
||
|
||
it('还有未覆盖需求 ⇒ 不删(货还得买)', () => {
|
||
expect(
|
||
shouldDropPrepCoveredByInventory({
|
||
prepareStatus: 30,
|
||
hasUncoveredOrderItem: true,
|
||
inboundForThisSupplier: false,
|
||
})
|
||
).toBe(false);
|
||
});
|
||
|
||
it('该厂商已有入库批次 ⇒ 不删(货已到,删了抹掉已入库事实且仓管收不了货)', () => {
|
||
expect(
|
||
shouldDropPrepCoveredByInventory({
|
||
prepareStatus: 30,
|
||
hasUncoveredOrderItem: false,
|
||
inboundForThisSupplier: true,
|
||
})
|
||
).toBe(false);
|
||
});
|
||
|
||
it('缺货 / 已转自采(>=40)⇒ 不删(另一条链路的语义,不得混用)', () => {
|
||
for (const status of [40, 50]) {
|
||
expect(
|
||
shouldDropPrepCoveredByInventory({
|
||
prepareStatus: status,
|
||
hasUncoveredOrderItem: false,
|
||
inboundForThisSupplier: false,
|
||
})
|
||
).toBe(false);
|
||
}
|
||
});
|
||
});
|
||
|
||
describe('G1 执行函数:只删该删的', () => {
|
||
const prep = (id: string, over: Partial<Record<string, unknown>> = {}) => ({
|
||
id,
|
||
sku_code: `SKU-${id}`,
|
||
supplier_code: 'SUPX',
|
||
prepare_status: 30,
|
||
inbound_for_supplier: false,
|
||
has_uncovered_item: false,
|
||
...over,
|
||
});
|
||
|
||
it('逐一按策略判定:只对"无需求且未入库"的行发删除', async () => {
|
||
const deleted: string[] = [];
|
||
const db = {
|
||
query: async (sql: string, params?: unknown[]) => {
|
||
const text = String(sql);
|
||
if (text.includes('FROM supplier_preparations sp') && text.includes('AS inbound_for_supplier')) {
|
||
return {
|
||
rows: [
|
||
prep('keep-demand', { has_uncovered_item: true }),
|
||
prep('keep-inbound', { inbound_for_supplier: true }),
|
||
prep('keep-stockout', { prepare_status: 40 }),
|
||
prep('drop-me'),
|
||
],
|
||
rowCount: 4,
|
||
};
|
||
}
|
||
if (text.startsWith('DELETE FROM supplier_preparations')) {
|
||
deleted.push(String(params?.[0]));
|
||
}
|
||
return { rows: [], rowCount: 0 };
|
||
},
|
||
} as unknown as FulfillmentDb;
|
||
|
||
const n = await dropPreparationsCoveredByInventory(db, { orderCode: 'ORD_X' });
|
||
expect(n).toBe(1);
|
||
// deleteSupplierPreparationRow 先把履约链清掉再删备货行
|
||
expect(deleted.length).toBe(1);
|
||
});
|
||
|
||
it('候选查询自带三道闸(缺货/转自采排除、入库排除、未覆盖需求排除)', () => {
|
||
const src = read('services/orderItemFulfillmentService.ts');
|
||
const slice = src.slice(src.indexOf('export async function dropPreparationsCoveredByInventory'));
|
||
expect(slice).toContain('COALESCE(sp.is_stockout, false) = false');
|
||
expect(slice).toContain('COALESCE(sp.transferred_to_purchase, false) = false');
|
||
expect(slice).toContain('AS inbound_for_supplier');
|
||
expect(slice).toContain('NULLIF(TRIM(i.inbound_batch_no), \'\') IS NOT NULL');
|
||
expect(slice).toContain('AS has_uncovered_item');
|
||
expect(slice).toContain('COALESCE(oi.stock_cover_qty, f.inventory_cover_qty, 0)');
|
||
// 复用既有删除函数(会连带清理"尚无入库批次"的待验行)
|
||
expect(slice).toContain('deleteSupplierPreparationRow(');
|
||
});
|
||
});
|
||
|
||
describe('G1/G2 挂点:分配的 COMMIT 后收口(覆盖抵扣按钮 / 改明细 / 调度对账三条入口)', () => {
|
||
const allocation = read('services/orderAllocationService.ts');
|
||
|
||
it('先撤备货,再重算结算单,最后才推 webhook', () => {
|
||
const dropIdx = allocation.indexOf('await dropPreparationsCoveredByInventory(undefined, { orderCode })');
|
||
const settleIdx = allocation.indexOf('await reconcileSupplierStatementsForOrder(orderCode)');
|
||
const webhookIdx = allocation.indexOf('await notifyPreparationsAllocated({ orderCode, branchCode: orderBranchCode })');
|
||
expect(dropIdx).toBeGreaterThan(-1);
|
||
expect(settleIdx).toBeGreaterThan(-1);
|
||
expect(webhookIdx).toBeGreaterThan(-1);
|
||
// 顺序很重要:撤销必须在推 webhook 之前,否则仍会把不该存在的备货推给厂商
|
||
expect(dropIdx).toBeLessThan(webhookIdx);
|
||
expect(settleIdx).toBeLessThan(webhookIdx);
|
||
});
|
||
|
||
it('两处收口都是 best-effort(失败只记日志,不影响分配结果)', () => {
|
||
expect(allocation).toContain('[allocation] drop covered preparations failed');
|
||
expect(allocation).toContain('[allocation] supplier statement reconcile failed');
|
||
});
|
||
|
||
it('复用既有结算重算入口(已确认单一律 skipped,不动已确认数据)', () => {
|
||
const finance = read('services/financeReconciliationService.ts');
|
||
// 生成器对已确认单直接 skipped
|
||
expect(finance).toContain("if (st >= 20) return 'skipped';");
|
||
// 结算口径:全额抵扣整行剔除 + 部分抵扣只算缺口
|
||
expect(finance).toContain('COALESCE(oi.stock_cover_qty, 0) < COALESCE(oi.quantity, 0) - 1e-9');
|
||
expect(finance).toContain('GREATEST(0, COALESCE(oi.quantity, 0) - COALESCE(oi.stock_cover_qty, 0))');
|
||
});
|
||
});
|
||
|
||
describe('G3「转供应商」备货量按缺口', () => {
|
||
it('合计 SQL 用缺口,不再用 SUM(quantity)', () => {
|
||
const src = read('services/purchaseSupplierAssignService.ts');
|
||
const slice = src.slice(0, src.indexOf('if (existing.rows.length)'));
|
||
expect(slice).toContain(
|
||
'SUM(GREATEST(0, quantity - COALESCE(stock_cover_qty, 0)))'
|
||
);
|
||
expect(slice).not.toContain('SUM(quantity), 0)::float');
|
||
});
|
||
});
|
||
|
||
/**
|
||
* G2 的**收口范围**:真库实测发现只重算"本单当前涉及的(厂商×下单日)"不够 ——
|
||
* 把行**改回未分配 / 换到别的厂商**后,旧厂商那张单上本单留下的行不会消失
|
||
* (SS20261013001 上仍留 5×6.00、单头 ¥417)。所以收口必须把
|
||
* **"已有结算单里仍引用本单"的(厂商×下单日)** 也一起重算。
|
||
*/
|
||
describe('G2 收口范围:旧厂商的旧行也要能清掉', () => {
|
||
const finance = read('services/financeReconciliationService.ts');
|
||
const slice = finance.slice(
|
||
finance.indexOf('export async function reconcileSupplierStatementsForOrder')
|
||
);
|
||
|
||
it('除本单当前供应商外,还要覆盖"已有结算单里引用本单"的(厂商×下单日)', () => {
|
||
expect(slice).toContain('FROM statement_lines sl');
|
||
expect(slice).toContain('JOIN supplier_statements ss ON ss.statement_no = sl.statement_no');
|
||
expect(slice).toContain('OR sl.order_item_id IN (SELECT id FROM order_items WHERE order_code = $1)');
|
||
});
|
||
|
||
it('cycle_code(YYYYMMDD) 必须还原成 YYYY-MM-DD 再传(格式不对会静默 empty)', () => {
|
||
expect(slice).toContain('/^\\d{8}$/.test(cycle)');
|
||
expect(slice).toContain('cycle.slice(4, 6)');
|
||
expect(slice).toContain('cycle.slice(6, 8)');
|
||
});
|
||
|
||
it('挂点用收口版函数(而不是只算当前供应商的那个)', () => {
|
||
const allocation = read('services/orderAllocationService.ts');
|
||
expect(allocation).toContain('await reconcileSupplierStatementsForOrder(orderCode)');
|
||
expect(allocation).not.toContain('await ensureSupplierStatementsForOrder(orderCode)');
|
||
});
|
||
|
||
it('已确认单永不动:清理与重算都带 status>=20 的守卫', () => {
|
||
// 生成器:已确认 ⇒ skipped
|
||
expect(finance).toContain("if (st >= 20) return 'skipped';");
|
||
// 无依据清理:已确认 ⇒ 冻结不动
|
||
expect(finance).toContain('已确认(status=');
|
||
expect(finance).toContain(':冻结不动');
|
||
// 只清生成器自己写的行(售后调整/退货冲销行不动)
|
||
expect(finance).toContain("COALESCE(sl.price_source, '') IN ('prep', 'order')");
|
||
});
|
||
});
|