Files
SupplierSystem/server/tests/mainSystemImportUnassigned.test.ts
T
Ubuntu 5fcd30a876
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(库存抵扣): 全额抵扣当次直达待分拣 + 重复点击不再抹掉已抵扣 + 候选显示可抵扣量
开发者 2026-10-13 三连反馈一次修完:

① 「如果是扣完了、不需要去采购,那么应该是直接待分拣才对」
   改前:全额抵扣的单停在 30 待备货,要等调度器 15 分钟对账才到 45 —— 导入建单/追加走的是
   **外部事务连接**,分配模块 runAllocationAfterCommitSideEffects(只在 ownTransaction 分支跑)
   不触发;finalizeCoverage 只置 30,补下游动作那支又只在**建了备货单**时才走。
   修法:mainSystemOrderImport 新增 advanceImportedOrders(),落单/补行后对 created/appended
   的单调**调度器同一个推进器** reconcileOrderWorkflowStatus(推送与拉取两条通道都经过);
   分配模块侧对「本次没建备货也没建自采、状态刚置 30」的单同样补一次。
   实测:`[main-system-import] …_0004 落单后推进:30 → 45(全部备货完成→待验收 / 验收/加工完成→待分拣)`,
   同一次导入内完成,无备货单、无自采单、无厂商应付。有缺口/未完成采购时不硬推(幂等)。

② 手动「库存抵扣」按钮**不再替人分配**:applyInventoryOffsetForMainSystemOrder 固定传
   preferItemSuppliers: true(原来不带选项 ⇒ useItemSuppliers = !smartEnabled,而智能分配默认开
   ⇒ 点一次就把未分配的行分配给厂商,与「不能替他直接分配好给谁」正相反)。

③ 「重复点击把抵扣抹掉」(真库实测):首次抵扣 6 斤把账外余额 ¥18 结转光 ⇒ 再点一次重算得
   outsourced=0 ⇒ UPDATE stock_cover_qty=0 + 释放预留,而 ¥18 成本仍挂在订单上。
   修法:loadExistingCoverQtyForItem() 把「本行已结转的额度」算进预算
   (cover = min(需求, 已抵扣 + 新可支撑)),只在归属当前 SKU 时才算(判据=本单+本 SKU 有
   inventory_cover 成本行;该成本行**不带 order_item_id**,不能用它匹配)。
   实测再点一次:delta 0、cover 保持 6、成本仍 1 行 ¥18、状态与供应商不变。

④ 「为什么没有展示库存抵扣的商品」:候选原来只给物理在库量,而能不能抵看账外余额
   (米块"可用30斤"实际只能抵6斤、土豆30斤实际可抵0)。listCoverableSkuOptions 现在同时返回
   coverable_qty,弹窗显示「可抵扣 X斤 / 在库 Y斤」、可抵扣 0 的置灰并提示原因、可抵的排前面。

回归:server 套件 27 failed files → 与基线 26 相比**无新增**(inventoryOffsetButton 的旧断言已按
新口径更新);stockCoverOutsourcedOnly 13 例、mainSystemImportUnassigned 15 例、
inventoryOffsetButton 5 例全绿;root/server tsc exit 0;门禁 8 通过/1 已知红/1 未执行。
2026-10-13 17:43:12 +08:00

194 lines
9.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 { 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(');
});
});
describe('手动「库存抵扣」按钮:只抵扣、不替人分配', () => {
/**
* 2026-10-13 开发者:「如果是扣完了、不需要去采购,那么应该是直接待分拣才对」。
*
* 该按钮走 `applyInventoryOffsetForMainSystemOrder` → `reprocessOrderAllocation`。
* 此前**不带选项** ⇒ `useItemSuppliers = !smartEnabled`,而智能分配默认开 ⇒ 规则引擎
* 会把未分配的行一并分配给厂商(真库实测:点一次就挂上「测试仓上」、备货直接供应完成、
* 抵扣从此轮不到它)。现在固定传 `preferItemSuppliers: true`:只落已有供应商、未指定行保持待分配;
* 抵扣照旧在入口最前跑,全额抵扣 ⇒ 30 + 路由 `reconcileOrderWorkflowStatus` 续推 40/45。
*/
it('⑬ 抵扣入口带「只落已有供应商」语义', () => {
const fnIdx = allocationSrc.indexOf('export async function applyInventoryOffsetForMainSystemOrder');
expect(fnIdx).toBeGreaterThan(0);
const body = allocationSrc.slice(fnIdx, fnIdx + 3200);
const callIdx = body.indexOf('await reprocessOrderAllocation(');
expect(callIdx).toBeGreaterThan(0);
expect(body.slice(callIdx, callIdx + 400)).toContain('preferItemSuppliers: true');
});
it('⑭ 抵扣按钮后仍会推进状态(路由复用既有对账推进器)', () => {
const routeSrc = readFileSync(
path.join(__dirname, '../src/modules/workflow/routes/orderRoutes.ts'),
'utf8'
);
const idx = routeSrc.indexOf("router.post('/orders/:orderCode/inventory-offset'");
expect(idx).toBeGreaterThan(0);
const body = routeSrc.slice(idx, idx + 4000);
expect(body).toContain('reconcileOrderWorkflowStatus');
});
it('⑮ 「选择抵扣商品」候选同时给出「可抵扣量」(只看物理在库会误导)', () => {
const mappingSrc = readFileSync(
path.join(__dirname, '../src/services/mainSystemItemSkuMappingService.ts'),
'utf8'
);
const fnIdx = mappingSrc.indexOf('export async function listCoverableSkuOptions');
expect(fnIdx).toBeGreaterThan(0);
const body = mappingSrc.slice(fnIdx, fnIdx + 2600);
expect(body).toContain('coverable_qty');
expect(body).toContain('sumCoverableOutsourcedQty');
const modalSrc = readFileSync(
path.join(__dirname, '../../src/components/order/OrderItemCoverSkuModal.tsx'),
'utf8'
);
expect(modalSrc).toContain('coverable_qty');
expect(modalSrc).toContain("t('orders.coverSku.coverable'");
});
});