开发者真机提问:「当前库存抵扣选项 是不是给全额抵扣使用的?如果是当前的库存无法全额抵扣, 那么他应该不能出现在可选项中」。 后端语义本来就是「全额才打 STOCK_COVER」(STOCK_COVER_SUPPLIER_SQL ①:抵扣量 ≥ 计划量), 部分抵扣时供应商列不动;但订单详情下拉**无条件**给了这一项 ⇒ 能选出「写着库存抵扣、 实际抵扣 0/部分」的谎报行,而且这类行会被智能分配(orderAllocationService:377)与 竞价(bidRoundService:703)跳过。 落地: · 口径唯一入口 inventoryStockCoverService.resolveCoverableQtyForSku (默认物理自由库存 getFreeStockQty;回退档 STOCK_COVER_REQUIRE_OUTSOURCED=1 用折算量); listCoverableSkuOptions 与新增的接口池都走它,不再各写一份三元 · GET /orders/:code/stock-cover-batches 增返 coverable_by_sku(**一行都没抵扣时也返回**) · 页面把池贴到每行 coverable_qty(接口失败=空池 ⇒ 严格口径下隐藏) · 面板纯函数 resolveStockCoverOptionVisible:池 + 本行已抵 ≥ 计划量才出现; 已是 STOCK_COVER 的行永远保留(否则下拉当前值消失);同 SKU 多行按行序贪心扣减 真库实测(BR002 ORD_API_20261014_BR002_0001,服务函数直调):coverable_by_sku 8 个 SKU 全 0, 与库存交叉核对一致(抄手皮 available 8 / 活跃预留 8 ⇒ 池 0;其余物理在库 0) ⇒ 该单没有任何行可被新标为库存抵扣(3 条已抵行仍保留)。 零数据变更、零文案变更(沿用既有 orders.detail.stockCover,未新增小字)。 测试:+9(纯函数)/+4(前后端接线契约)/+4(后端口径守卫)/+1(批次查询 ⑮) 回归:后端 2969 passed / 32 failed(0 新增,全部在基线集内;另有 2 个基线红已被并发会话修绿); 前端 3 个既有失败与本轮无关;前后端 tsc 均 0 错误。 设计:docs/design/20261014-订单成本排除独立进货与库存成本分类.md §6.1.6
265 lines
13 KiB
TypeScript
265 lines
13 KiB
TypeScript
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');
|
||
/**
|
||
* 2026-10-14:口径判断(物理自由库存/回退档折算)**抽到唯一入口** `resolveCoverableQtyForSku`
|
||
* (订单详情「库存抵扣」选项门槛用同一个)⇒ 这里改钉「谁在用 + 入口里怎么选」,
|
||
* 免得下次改口径又漏一处。
|
||
*/
|
||
expect(body).toContain('resolveCoverableQtyForSku');
|
||
const coverSrc = readFileSync(
|
||
path.join(__dirname, '../src/services/inventoryStockCoverService.ts'),
|
||
'utf8'
|
||
);
|
||
const entryIdx = coverSrc.indexOf('export async function resolveCoverableQtyForSku');
|
||
expect(entryIdx).toBeGreaterThan(0);
|
||
const entry = coverSrc.slice(entryIdx, entryIdx + 700);
|
||
expect(entry).toContain('sumCoverableOutsourcedQty');
|
||
expect(entry).toContain('getFreeStockQty');
|
||
const modalSrc = readFileSync(
|
||
path.join(__dirname, '../../src/components/order/OrderItemCoverSkuModal.tsx'),
|
||
'utf8'
|
||
);
|
||
expect(modalSrc).toContain('coverable_qty');
|
||
expect(modalSrc).toContain("t('orders.coverSku.coverable'");
|
||
});
|
||
});
|
||
|
||
|
||
describe('状态推进:全靠代码、不靠定时器(2026-10-13 开发者要求)', () => {
|
||
/**
|
||
* 开发者原话:「都抵扣完了为什么订单还是卡在待分配……我需要后续不能出现类似的情况,
|
||
* 虽然现在是待分拣了,但是这个待分拣应该是定时器兜底机制推进过去的」。
|
||
*
|
||
* 两条硬要求:
|
||
* ① 抵扣完毕 ⇒ **当次请求内**推进(不是等 15 分钟调度器);
|
||
* ② 有缺口 ⇒ 任何路径都不许推进(真库 2026-10-13 现场:还没抵完就成了待分拣)。
|
||
*/
|
||
const manageSrc = readFileSync(
|
||
path.join(__dirname, '../src/services/orderManageItemsService.ts'),
|
||
'utf8'
|
||
);
|
||
const submitSrc2 = readFileSync(
|
||
path.join(__dirname, '../src/services/customerOrderSubmitService.ts'),
|
||
'utf8'
|
||
);
|
||
const fulfillmentSrc = readFileSync(
|
||
path.join(__dirname, '../src/services/orderItemFulfillmentService.ts'),
|
||
'utf8'
|
||
);
|
||
|
||
it('① 三条写入路径都在**提交后当次请求内**推进(导入 / 改明细 / 下单)', () => {
|
||
// 导入:落单后推进
|
||
expect(importSrc).toContain('advanceImportedOrders');
|
||
// 改明细保存(订单详情 / 小程序)
|
||
expect(manageSrc).toContain('reconcileOrderWorkflowStatus(orderCode)');
|
||
expect(manageSrc).toContain('allocation.orderStatusUpdated === 30');
|
||
// 下单提交
|
||
expect(submitSrc2).toContain('reconcileOrderWorkflowStatus(orderCode)');
|
||
expect(submitSrc2).toContain('allocation.orderStatusUpdated === 30');
|
||
});
|
||
|
||
it('② 「已履约」判据只有一份(两份漂移会让分配侧与推进器互相打脸)', () => {
|
||
const defs = fulfillmentSrc.match(/const FULFILLED_BRANCHES_SQL =/g) || [];
|
||
expect(defs.length).toBe(1);
|
||
// 两份查询都必须用它,且不得再各写一份 <= 分支
|
||
const users = fulfillmentSrc.match(/\$\{FULFILLED_BRANCHES_SQL\}/g) || [];
|
||
expect(users.length).toBe(2);
|
||
expect(fulfillmentSrc).not.toContain('-- 注意:本判据在本文件有**两份**');
|
||
});
|
||
|
||
it('③ 库存履约分支仍要求**足额**覆盖(不足额一律判未履约)', () => {
|
||
expect(fulfillmentSrc).toContain(
|
||
'COALESCE(oi.stock_cover_qty, f.inventory_cover_qty, 0) >= COALESCE(oi.quantity, 0) - 1e-9'
|
||
);
|
||
// 30→40 的闸门必须查缺口,不能只看「有没有备货/采购」
|
||
const prepSrc = readFileSync(
|
||
path.join(__dirname, '../src/services/preparationFulfillmentService.ts'),
|
||
'utf8'
|
||
);
|
||
const idx = prepSrc.indexOf('const uncoveredBeforeAdvance = await listUncoveredOrderItems');
|
||
expect(idx).toBeGreaterThan(0);
|
||
expect(prepSrc.slice(idx, idx + 600)).toContain('尚有未履约明细');
|
||
});
|
||
});
|