开发者 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 未执行。
98 lines
5.0 KiB
TypeScript
98 lines
5.0 KiB
TypeScript
import { readFileSync } from 'node:fs';
|
|
import { describe, expect, it } from 'vitest';
|
|
|
|
/**
|
|
* 订单管理列表的「库存抵扣」小按钮(2026-10-10 开发者要求):
|
|
* 「这块分配来的订单补一个小按钮『库存抵扣』,点击后走库存抵扣的路线。
|
|
* 应对就是我的订单已经分配过来了,但是**库存忘记上新了**这种情况 ——
|
|
* 因为库存抵扣在分配过来、创建订单时就执行过了。」
|
|
*
|
|
* 钉三件事:
|
|
* ① 按钮只在**主系统分配过来的单**且未进入配送阶段(<50)时出现;
|
|
* ② 点击调的是**既有分配路线**(后端 `reprocessOrderAllocation`),不是自己新写抵扣;
|
|
* ③ 有在途锁(防连点)。
|
|
*/
|
|
const route = readFileSync(
|
|
new URL('../src/modules/workflow/routes/orderRoutes.ts', import.meta.url),
|
|
'utf8'
|
|
);
|
|
/** 逻辑落在 service(路由层不得内嵌 SQL —— `routeLayerBoundary.test.ts` 棘轮) */
|
|
const allocSvc = readFileSync(
|
|
new URL('../src/services/orderAllocationService.ts', import.meta.url),
|
|
'utf8'
|
|
);
|
|
const page = readFileSync(
|
|
new URL('../../src/pages/orders/orderManagement/OrderManagementPage.tsx', import.meta.url),
|
|
'utf8'
|
|
);
|
|
const api = readFileSync(new URL('../../src/utils/workflowApi.ts', import.meta.url), 'utf8');
|
|
const zh = readFileSync(new URL('../../src/i18n/locales/zh-CN/orders.ts', import.meta.url), 'utf8');
|
|
|
|
describe('后端:POST /orders/:orderCode/inventory-offset', () => {
|
|
it('只给主系统分配单用,且配送阶段(>=50)拒绝', () => {
|
|
const svcIdx = allocSvc.indexOf('export async function applyInventoryOffsetForMainSystemOrder');
|
|
expect(svcIdx, '未找到库存抵扣服务函数').toBeGreaterThan(-1);
|
|
const svc = allocSvc.slice(svcIdx, svcIdx + 3200);
|
|
expect(svc).toContain('main_system_order_imports');
|
|
expect(svc).toContain("kind: 'not_main_system_order'");
|
|
expect(svc).toContain("kind: 'too_late'");
|
|
expect(svc).toContain('status >= 50');
|
|
// 路由只是薄壳:错误码映射 + 调用服务(SQL 一律在 service 里)
|
|
const idx = route.indexOf("'/orders/:orderCode/inventory-offset'");
|
|
expect(idx).toBeGreaterThan(-1);
|
|
// 只取本路由自己的函数体(到下一个 router.post 为止),别把邻座路由的 SQL 算进来
|
|
const next = route.indexOf('router.post(', idx + 10);
|
|
const body = route.slice(idx, next > idx ? next : idx + 3200);
|
|
expect(body).toContain('applyInventoryOffsetForMainSystemOrder({');
|
|
expect(body).toContain("error: 'inventory_offset_too_late'");
|
|
expect(body).not.toContain('SELECT ');
|
|
});
|
|
|
|
it('走既有分配路线(reprocessOrderAllocation),不自己实现抵扣', () => {
|
|
const svcIdx = allocSvc.indexOf('export async function applyInventoryOffsetForMainSystemOrder');
|
|
const svc = allocSvc.slice(svcIdx, svcIdx + 4200);
|
|
expect(svc).toContain('await reprocessOrderAllocation(');
|
|
expect(svc).toContain('orderCode,');
|
|
/**
|
|
* 2026-10-13:这个入口必须带 `preferItemSuppliers: true`(只落已有供应商、未指定行留待分配)。
|
|
* 不带选项时 `useItemSuppliers = !smartEnabled`,而智能分配**默认开** ⇒ 点一次「库存抵扣」
|
|
* 就把未分配的行交给规则引擎分配给厂商(真库实测:挂上「测试仓上」且备货直接供应完成,
|
|
* 抵扣从此轮不到它),与开发者「不能替他直接分配好给谁」正相反。
|
|
*/
|
|
expect(svc).toContain('preferItemSuppliers: true');
|
|
// 不直接调 applyStockCoverForOrder(那会留下"既抵扣又采购"的双记)
|
|
expect(svc).not.toContain('applyStockCoverForOrder(');
|
|
// 返回可展示的结果(抵扣增量)
|
|
expect(svc).toContain('coveredDelta');
|
|
expect(svc).toContain('message:');
|
|
});
|
|
});
|
|
|
|
describe('前端:按钮的出现条件与在途锁', () => {
|
|
it('只在主系统分配单且状态 < 50 时出现', () => {
|
|
const idx = page.indexOf('const canApplyInventoryOffset');
|
|
expect(idx, '未找到出现条件').toBeGreaterThan(-1);
|
|
const fn = page.slice(idx, idx + 240);
|
|
expect(fn).toContain('order.is_main_system_order === true');
|
|
expect(fn).toContain('Number(order.order_status) < 50');
|
|
// 列表行里用它包住按钮(不是无条件渲染)
|
|
expect(page).toMatch(/canApplyInventoryOffset\(o\) \? \(/);
|
|
});
|
|
|
|
it('有在途锁:点击期间禁用、结束后释放', () => {
|
|
const idx = page.indexOf('const handleInventoryOffset');
|
|
expect(idx).toBeGreaterThan(-1);
|
|
const body = page.slice(idx, idx + 800);
|
|
expect(body).toContain('setInventoryOffsetCode(order.order_code)');
|
|
expect(body).toContain('finally');
|
|
expect(body).toContain('setInventoryOffsetCode(null)');
|
|
expect(page).toContain('disabled={inventoryOffsetCode === o.order_code}');
|
|
});
|
|
|
|
it('文案:按钮「库存抵扣」中文为开发者原话,三语键齐', () => {
|
|
expect(zh).toContain("'list.inventoryOffset': '库存抵扣'");
|
|
expect(api).toContain('applyInventoryOffsetApi');
|
|
expect(api).toContain('/inventory-offset');
|
|
});
|
|
});
|