Files
SupplierSystem/server/tests/mainSystemImportUnassigned.test.ts
T
Ubuntu e96e88c156
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
feat(订单分配): 主系统导入单默认「未分配」——先库存抵扣,缺口留人工选择
开发者 2026-10-13 报障(真库 BR003 土豆单 ORD_API_20261013_BR003_0003):
「主系统分配过来的应该是先库存抵扣,然后缺少的部分才是进行选择分配给供应商还是自采,
不能替他直接分配好给谁;而且分配给供应商的默认是直接供应完成,导致库存抵扣也无法生效。」

改前实测:土豆行 supplier_code=SUP61936431(测试仓上)、stock_cover_qty=0、状态 40;
同 SKU 批次物理自由 30 斤,抵扣日志 8 连发「账外只够 0」。行一旦有供应商备货 ⇒
isOrderItemFulfilled 为真 ⇒ tryFulfillFromInventory 直接返回 ⇒ 抵扣永远轮不到。

改法(复用既有语义,不新造分配模式):
1) 主系统导入建单/追加两条路径传 keepItemsUnassigned / keepNewItemsUnassigned,
   映射为 reprocessOrderAllocation 的 preferItemSuppliers(只落已有供应商,
   未指定行保持待分配、不升 30、不报履约不全),并禁止新行进规则引擎;
   抵扣仍在分配入口最前(applyStockCoverForOrder)先跑。
2) 连带必修:裸行不再走明细级抵扣 ⇒ 无人写「库存履约」行 ⇒ 全额抵扣的单会卡在 20。
   新增 linkFullInventoryCoverFulfillments(),在覆盖检查前对
   stock_cover_qty >= quantity 且无真实履约链、状态 <50 的行补一行库存履约(幂等)。

真库实测(本地 dev,BR003):
- 追加路径:米块 10 斤 ⇒ supplier=NULL / cover=6(账外只够 6)/ 无备货无自采无结算行,缺口 4 斤未分配;
- 建单路径:海带丝 20 斤全额抵扣 ⇒ supplier=NULL / 无备货无自采 / 补履约行后 20 → 45;
- 账外持有 18→0、90→30,抵扣成本记在被抵扣单上(inventory_cover 18.00 / 60.00)。

不改:可抵扣只认账外持有的口径、抵扣上限算法、财务结算口径(P1/P2 待批准,见设计文 §3/§4)。
设计文:docs/design/20261013-主系统导入单默认未分配(先抵扣后人工选择供应商或自采).md
2026-10-13 17:07:33 +08:00

145 lines
7.0 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 { describe, expect, it } from 'vitest';
import { readFileSync } from 'node:fs';
import path from 'node:path';
import { resolveWriteAllocationIntent } from '../src/services/orderManageItemsService';
/**
* **主系统导入单默认「未分配」** 的回归(2026-10-13 真库 BR003 土豆单
* `ORD_API_20261013_BR003_0003`,开发者报障):
*
* 「主系统分配过来的应该是先库存抵扣,然后缺少的部分才是进行选择分配给供应商还是自采去采购,
* **不能替他直接分配好给谁**;而且分配给供应商的默认是直接供应完成,导致库存抵扣也无法生效。」
*
* 真库证据(改前):土豆行 `supplier_code=SUP61936431(测试仓上)`、`stock_cover_qty=0`、
* `order_status=40`;同 SKU 批次 `IN20261013X00E9T` 物理自由 30 斤,抵扣日志 8 连发
* `物理自由库存 30,其中账外只够 0`。行一旦有供应商备货 ⇒ `isOrderItemFulfilled` 为真
* ⇒ `tryFulfillFromInventory` 直接返回 ⇒ **抵扣永远轮不到**。
*
* 口径(开发者 2026-10-13 确认):导入行先抵扣、缺口留「未分配」,人工在订单详情选
* 「分配给供应商 / 自采」;应付只对真正采购的量。
* 见 docs/design/20261013-主系统导入单默认未分配(先抵扣后人工选择供应商或自采).md
*/
const itemsSrc = readFileSync(
path.join(__dirname, '../src/services/orderManageItemsService.ts'),
'utf8'
);
const submitSrc = readFileSync(
path.join(__dirname, '../src/services/customerOrderSubmitService.ts'),
'utf8'
);
const importSrc = readFileSync(
path.join(__dirname, '../src/services/mainSystemOrderImport.ts'),
'utf8'
);
const allocationSrc = readFileSync(
path.join(__dirname, '../src/services/orderAllocationService.ts'),
'utf8'
);
describe('分配意图判定:谁能替人分配、谁不能', () => {
it('① 默认(Web 改明细 / 小程序下单):跑规则引擎,新行照常自动分配', () => {
expect(resolveWriteAllocationIntent({})).toEqual({
useItemSuppliers: false,
autoAllocateNewItems: true,
});
});
it('② 手动分配页保存:只落已有供应商,但新增行仍由规则引擎定(原行为不变)', () => {
expect(resolveWriteAllocationIntent({ preferItemSuppliers: true })).toEqual({
useItemSuppliers: true,
autoAllocateNewItems: true,
});
});
it('③ **主系统导入**:只落已有供应商,且新行也不许自动分配 ⇒ 缺口保持未分配', () => {
expect(resolveWriteAllocationIntent({ keepNewItemsUnassigned: true })).toEqual({
useItemSuppliers: true,
autoAllocateNewItems: false,
});
// 两个标志同时给(导入路径可能的组合)也必须是「不许自动分配」
expect(
resolveWriteAllocationIntent({
preferItemSuppliers: true,
keepNewItemsUnassigned: true,
})
).toEqual({ useItemSuppliers: true, autoAllocateNewItems: false });
});
it('④ 显式 false 不等于「不传」以外的行为(只是不覆盖原行为)', () => {
expect(resolveWriteAllocationIntent({ keepNewItemsUnassigned: false })).toEqual({
useItemSuppliers: false,
autoAllocateNewItems: true,
});
});
});
describe('落点接线:标志真的接到了分配调用上', () => {
it('⑤ 新行只在不带导入标志时才进 autoAllocateItemIds', () => {
const pushIdx = itemsSrc.indexOf('autoAllocateItemIds.push(newId)');
expect(pushIdx).toBeGreaterThan(0);
const guard = itemsSrc.slice(Math.max(0, pushIdx - 420), pushIdx);
expect(guard).toContain('allocationIntent.autoAllocateNewItems');
});
it('⑥ 分配调用按 allocationIntent.useItemSuppliers 选择「不跑规则引擎」分支', () => {
const callIdx = itemsSrc.indexOf('allocation = await reprocessOrderAllocation(');
expect(callIdx).toBeGreaterThan(0);
const branch = itemsSrc.slice(callIdx, callIdx + 420);
expect(branch).toContain('allocationIntent.useItemSuppliers');
// 该分支必须把「只认已有供应商」显式传给分配(否则规则引擎仍会替人分配)
expect(branch).toContain('preferItemSuppliers: true');
});
it('⑦ 主系统导入的建单与追加两条路径都传导入标志', () => {
expect(importSrc).toContain('keepItemsUnassigned: true');
expect(importSrc).toContain('keepNewItemsUnassigned: true');
});
it('⑧ 建单路径只在显式置位时才传 preferItemSuppliers(人工录单/小程序行为不变)', () => {
expect(submitSrc).toContain("input.keepItemsUnassigned === true ? { preferItemSuppliers: true } : {}");
});
it('⑨ **先抵扣后分配**:整单抵扣发生在规则引擎分支之前(抵扣不属于「分配」的下游)', () => {
const coverIdx = allocationSrc.indexOf('await applyStockCoverForOrder(');
const ruleIdx = allocationSrc.indexOf('const pendingItemsRaw = !useItemSuppliers');
expect(coverIdx).toBeGreaterThan(0);
expect(ruleIdx).toBeGreaterThan(0);
expect(coverIdx).toBeLessThan(ruleIdx);
});
it('⑩ 已有供应商备货的行会短路抵扣(这正是「替人分配 ⇒ 抵扣失效」的机制,勿删)', () => {
const fulfillIdx = allocationSrc.indexOf('async function tryFulfillFromInventory');
const guardIdx = allocationSrc.indexOf('isOrderItemFulfilled(', fulfillIdx);
expect(guardIdx).toBeGreaterThan(fulfillIdx);
});
});
describe('全额抵扣的收尾:不分配也必须能升状态', () => {
/**
* 2026-10-13 真库实测回归(`ORD_API_20261013_BR003_0004` 海带丝 20 斤):
* 导入行不再走规则引擎后,**裸行不会进明细级抵扣**(`tryFulfillFromInventory` 的 `coveredFully`
* 那一支才会写「库存履约」行)⇒ 整单抵扣虽写了 `stock_cover_qty=20`,却没有任何
* `order_item_fulfillments` 行 ⇒ `isOrderItemFulfillmentValid` 判未履约 ⇒ 订单卡在 20 待分配。
* 修法:覆盖检查之前先按 `stock_cover_qty >= quantity` 补齐库存履约行(幂等,已有备货/自采不碰)。
*/
it('⑪ 覆盖检查之前先补「全额库存抵扣」的履约行', () => {
const callIdx = allocationSrc.indexOf('await linkFullInventoryCoverFulfillments(orderCode, client)');
expect(callIdx).toBeGreaterThan(0);
const checkIdx = allocationSrc.indexOf('if (await isOrderItemFulfilled(orderCode, item, client)) continue;');
expect(checkIdx).toBeGreaterThan(callIdx);
});
it('⑫ 补履约行只认「全额覆盖 + 无真实履约链」,且不碰 >=50 的单', () => {
const fnIdx = allocationSrc.indexOf('async function linkFullInventoryCoverFulfillments');
expect(fnIdx).toBeGreaterThan(0);
const body = allocationSrc.slice(fnIdx, fnIdx + 1800);
expect(body).toContain('COALESCE(oi.stock_cover_qty, 0) >= COALESCE(oi.quantity, 0) - 1e-9');
expect(body).toContain('COALESCE(o.order_status, 0) < 50');
expect(body).toContain('row.supplier_preparation_id != null || row.purchase_code != null');
expect(body).toContain('linkOrderItemToInventoryCover(');
});
});