开发者要求「核查确保不影响其他业务逻辑,比如领料加工那块」——核查结论(详见设计文 §8): 被改查询(listInventoryForManagement)**只有三个消费方**:库存管理页、AI 助手库存工具 (读 SKU 级 reserved_qty/claimable_qty,语义未变)、inventoryOps(仅 re-export)。 领料/加工链路(重点): - 领料可用量走 getClaimableStockQty(本会话未改动该函数);独立加工自算 SQL(不经我改的查询) - 领料需求重算 recalcDemandSheetFromInspections 第 1189 行 `if (is_returned) continue;` ⇒ 退货行本来就被跳过 ⇒「退货行 cover 归零」的清扫不可能改变需求口径 - processing*.ts 不读本次新增的 batch_* 字段 - 冒烟:/processing/tasks、/processing/workflow-enabled、/processing/standalone/sku-options 均 200 其它面:SKU 级旧字段保留且语义不变(老消费方零改动);导出列预设不含占用/可调配; 库存修改弹窗/批次卡/低库存告警均不读这两列;助手文案键未改。 两处加固: 1) 降级回落:归属整体算不出来时**不再静默显示 0/—**,改为回落 SKU 级显示(= 改造前表现, 宁可粗也不错)+ 行上标 batch_attribution_ok=false 供排查(batchDisplayForBatch,新增 6 例) 2) 性能/边界:只为"本页里真的有活跃预留"的 SKU 跑归属查询(reserved_qty > 0 才进), 无预留时一次都不跑 真接口复验:库存列表 200 —— 抄手皮 STK202610118WEOS7 本批占用 12.5(可调配 0)、 STK202610118BMTTP 3.5(9)、其余 0;**各批占用之和 = SKU 占用 16**、 **各批可调配之和 = SKU 可调配 54**、reservation_over_available=false、batch_attribution_ok=true。 回归:加工/领料 9 文件 47/50(2 个失败均在既定基线名单);库存/助手/分拣 14 文件 112/113 (唯一失败是对方未提交 WIP 里的 pickingZoneOrder 断言,我从未碰过该文件); 前端 inventory/processing/picking 7 文件 20/20;两端 tsc 0 错。
256 lines
12 KiB
TypeScript
256 lines
12 KiB
TypeScript
import { describe, expect, it, vi } from 'vitest';
|
||
|
||
/**
|
||
* 库存列表「预留占用」的**批次归属**(2026-10-13 方案 A,开发者立项)。
|
||
*
|
||
* 起因(开发者原话):「抄手皮同一天入库了四批,可调配 54 斤、占用 16 斤,
|
||
* 但**每一个入库批次都展示了被占用** —— 不应该是那一批被占用就显示那一批、多少显示多少吗?」
|
||
*
|
||
* 数据事实:`inventory_reservations` 没有 batch_no(预留是 SKU 级的),
|
||
* 旧实现把 SKU 汇总值贴到每一行批次上 ⇒ 4 批各显示 16、每行都标红「预留超过可用」(假警报)。
|
||
* 本文件钉住归属口径:**计划优先 → FIFO 现算**,且不超批次可用量。
|
||
*/
|
||
const query = vi.hoisted(() => vi.fn());
|
||
vi.mock('../src/db', () => ({
|
||
pool: { query: (...args: unknown[]) => query(...args) },
|
||
}));
|
||
|
||
import {
|
||
attributeReservationsToBatches,
|
||
attributionKey,
|
||
batchDisplayForBatch,
|
||
loadReservationBatchAttribution,
|
||
skuHasAttribution,
|
||
} from '../src/services/inventoryReservationBatchAttribution';
|
||
|
||
const SKU = 'SKU62634064428';
|
||
|
||
/**
|
||
* 真库抄手皮的实际形态(BR003)——**FIFO 顺序 = WEOS7 → BMTTP → AJOYYS → XB1L75 → NJLG01**:
|
||
* 前四批 10-11 当天入库(间隔 12~36 秒)、可用各 12.5;最后一批 10-12 入库、可用 20;
|
||
* 预留 8 + 8 = 16(`ORD_WEB_20261013_BR003_0001` 与 `ORD_API_20261013_BR003_0001`,均无批次计划)。
|
||
* 时间戳用真库值(UTC;北京 07:23:19.300 ⇒ UTC 前一日 23:23:19.300)。
|
||
*/
|
||
const t = (iso: string) => iso;
|
||
const batches = [
|
||
{ skuCode: SKU, batchNo: 'STK202610118WEOS7', availableQty: 12.5, createdAt: t('2026-10-10T23:23:19.300Z'), expiryDate: t('2026-10-14T16:00:00.000Z') },
|
||
{ skuCode: SKU, batchNo: 'STK202610118BMTTP', availableQty: 12.5, createdAt: t('2026-10-10T23:23:31.983Z'), expiryDate: t('2026-10-14T16:00:00.000Z') },
|
||
{ skuCode: SKU, batchNo: 'STK20261011AJOYYS', availableQty: 12.5, createdAt: t('2026-10-10T23:24:00.940Z'), expiryDate: t('2026-10-14T16:00:00.000Z') },
|
||
{ skuCode: SKU, batchNo: 'STK20261011XB1L75', availableQty: 12.5, createdAt: t('2026-10-10T23:24:37.233Z'), expiryDate: t('2026-10-14T16:00:00.000Z') },
|
||
{ skuCode: SKU, batchNo: 'STK20261012NJLG01', availableQty: 20, createdAt: t('2026-10-11T17:08:39.266Z'), expiryDate: t('2026-10-14T16:00:00.000Z') },
|
||
];
|
||
const reservations = [
|
||
{ orderItemId: 'item-a', orderCode: 'ORD_A', skuCode: SKU, qty: 8, createdAt: t('2026-10-10T23:30:00.000Z') },
|
||
{ orderItemId: 'item-b', orderCode: 'ORD_B', skuCode: SKU, qty: 8, createdAt: t('2026-10-10T23:31:00.000Z') },
|
||
];
|
||
|
||
describe('预留按批次归属(方案 A)', () => {
|
||
it('① 无计划 ⇒ FIFO 现算:16 斤落在 12.5 + 3.5,其余批次为 0(真库同形)', () => {
|
||
const r = attributeReservationsToBatches({ reservations, batches });
|
||
expect(r.bySkuBatch.get(attributionKey(SKU, 'STK202610118WEOS7'))?.qty).toBe(12.5);
|
||
expect(r.bySkuBatch.get(attributionKey(SKU, 'STK202610118BMTTP'))?.qty).toBe(3.5);
|
||
expect(r.bySkuBatch.get(attributionKey(SKU, 'STK20261011AJOYYS'))).toBeUndefined();
|
||
expect(r.bySkuBatch.get(attributionKey(SKU, 'STK20261011XB1L75'))).toBeUndefined();
|
||
expect(r.bySkuBatch.get(attributionKey(SKU, 'STK20261012NJLG01'))).toBeUndefined();
|
||
// 分摊总和 = 预留总量
|
||
const total = [...r.bySkuBatch.values()].reduce((s, v) => s + v.qty, 0);
|
||
expect(Math.round(total * 100) / 100).toBe(16);
|
||
expect(r.unallocatedBySku.get(SKU)).toBeUndefined();
|
||
});
|
||
|
||
it('② 有计划 ⇒ 按计划批次(即使它不是 FIFO 第一批)', () => {
|
||
const r = attributeReservationsToBatches({
|
||
reservations,
|
||
batches,
|
||
planByItemId: new Map([['item-a', [{ batchNo: 'STK20261012NJLG01', qty: 8 }]]]),
|
||
});
|
||
expect(r.bySkuBatch.get(attributionKey(SKU, 'STK20261012NJLG01'))?.qty).toBe(8);
|
||
// 另一条无计划 ⇒ 仍按 FIFO(从最早批次开始,不受上一条影响)
|
||
expect(r.bySkuBatch.get(attributionKey(SKU, 'STK202610118WEOS7'))?.qty).toBe(8);
|
||
expect(r.bySkuBatch.get(attributionKey(SKU, 'STK202610118BMTTP'))).toBeUndefined();
|
||
});
|
||
|
||
it('③ 计划不足 ⇒ 余量回落 FIFO;计划里已不存在的批次被忽略', () => {
|
||
const r = attributeReservationsToBatches({
|
||
reservations: [{ orderItemId: 'item-a', orderCode: 'ORD_A', skuCode: SKU, qty: 10, createdAt: t('2026-10-10T23:30:00.000Z') }],
|
||
batches,
|
||
planByItemId: new Map([
|
||
['item-a', [{ batchNo: 'DELETED-BATCH', qty: 10 }]],
|
||
]),
|
||
});
|
||
// 陈旧计划整条不可用 ⇒ 全按 FIFO
|
||
expect(r.bySkuBatch.get(attributionKey(SKU, 'STK202610118WEOS7'))?.qty).toBe(10);
|
||
const r2 = attributeReservationsToBatches({
|
||
reservations: [{ orderItemId: 'item-a', orderCode: 'ORD_A', skuCode: SKU, qty: 10, createdAt: t('2026-10-10T23:30:00.000Z') }],
|
||
batches,
|
||
planByItemId: new Map([['item-a', [{ batchNo: 'STK202610118BMTTP', qty: 3 }]]]),
|
||
});
|
||
expect(r2.bySkuBatch.get(attributionKey(SKU, 'STK202610118BMTTP'))?.qty).toBe(3);
|
||
expect(r2.bySkuBatch.get(attributionKey(SKU, 'STK202610118WEOS7'))?.qty).toBe(7); // 余量按 FIFO
|
||
});
|
||
|
||
it('④ 不超批次可用量:一批不够就顺延下一批', () => {
|
||
const r = attributeReservationsToBatches({
|
||
reservations: [{ orderItemId: 'item-a', orderCode: 'ORD_A', skuCode: SKU, qty: 15, createdAt: t('2026-10-10T23:30:00.000Z') }],
|
||
batches: [
|
||
{ skuCode: SKU, batchNo: 'B1', availableQty: 5, createdAt: t('2026-10-10T22:00:00.000Z'), expiryDate: null },
|
||
{ skuCode: SKU, batchNo: 'B2', availableQty: 20, createdAt: t('2026-10-10T22:01:00.000Z'), expiryDate: null },
|
||
],
|
||
});
|
||
expect(r.bySkuBatch.get(attributionKey(SKU, 'B1'))?.qty).toBe(5);
|
||
expect(r.bySkuBatch.get(attributionKey(SKU, 'B2'))?.qty).toBe(10);
|
||
});
|
||
|
||
it('⑤ SKU 级预留超过总可用 ⇒ 记 unallocated(界面据此按 SKU 级报警,而不是逐批误报)', () => {
|
||
const r = attributeReservationsToBatches({
|
||
reservations: [{ orderItemId: 'item-a', orderCode: 'ORD_A', skuCode: SKU, qty: 30, createdAt: t('2026-10-10T23:30:00.000Z') }],
|
||
batches: [{ skuCode: SKU, batchNo: 'B1', availableQty: 12.5, createdAt: t('2026-10-10T22:00:00.000Z'), expiryDate: null }],
|
||
});
|
||
expect(r.bySkuBatch.get(attributionKey(SKU, 'B1'))?.qty).toBe(12.5);
|
||
expect(r.unallocatedBySku.get(SKU)).toBe(17.5);
|
||
});
|
||
|
||
it('⑥ FIFO 顺序:先入库先出;同刻空效期优先;同刻同效期按批次号稳定', () => {
|
||
const r = attributeReservationsToBatches({
|
||
reservations: [{ orderItemId: 'item-a', orderCode: 'ORD_A', skuCode: SKU, qty: 3, createdAt: t('2026-10-10T23:30:00.000Z') }],
|
||
batches: [
|
||
{ skuCode: SKU, batchNo: 'LATE', availableQty: 10, createdAt: t('2026-10-10T22:50:00.000Z'), expiryDate: null },
|
||
{ skuCode: SKU, batchNo: 'EARLY', availableQty: 10, createdAt: t('2026-10-10T22:10:00.000Z'), expiryDate: null },
|
||
],
|
||
});
|
||
expect(r.bySkuBatch.get(attributionKey(SKU, 'EARLY'))?.qty).toBe(3);
|
||
expect(r.bySkuBatch.get(attributionKey(SKU, 'LATE'))).toBeUndefined();
|
||
});
|
||
|
||
it('⑦ 每批的占用单号可回溯(tooltip 用)', () => {
|
||
const r = attributeReservationsToBatches({ reservations, batches });
|
||
expect(r.bySkuBatch.get(attributionKey(SKU, 'STK202610118WEOS7'))?.orderCodes).toEqual([
|
||
'ORD_A',
|
||
'ORD_B',
|
||
]);
|
||
expect(r.bySkuBatch.get(attributionKey(SKU, 'STK202610118BMTTP'))?.orderCodes).toEqual(['ORD_B']);
|
||
});
|
||
|
||
it('⑧ 装配层:只读预留(status=10) + 全部批次 + 行级批次计划', async () => {
|
||
query.mockImplementation(async (sql: string) => {
|
||
const text = String(sql);
|
||
if (text.includes('FROM inventory_reservations')) {
|
||
return {
|
||
rowCount: 2,
|
||
rows: [
|
||
{ order_item_id: 'item-a', order_code: 'ORD_A', sku_code: SKU, qty: 8, created_at: t('2026-10-10T23:30:00.000Z') },
|
||
{ order_item_id: 'item-b', order_code: 'ORD_B', sku_code: SKU, qty: 8, created_at: t('2026-10-10T23:31:00.000Z') },
|
||
],
|
||
};
|
||
}
|
||
if (text.includes('FROM inventory')) {
|
||
return {
|
||
rowCount: batches.length,
|
||
rows: batches.map((b) => ({
|
||
sku_code: b.skuCode,
|
||
batch_no: b.batchNo,
|
||
available_qty: b.availableQty,
|
||
created_at: b.createdAt,
|
||
expiry_date: b.expiryDate,
|
||
})),
|
||
};
|
||
}
|
||
if (text.includes('FROM picking_items')) {
|
||
return { rowCount: 1, rows: [{ item_id: 'item-a', outbound_batches: [{ batch_no: 'STK20261012NJLG01', qty: 8 }] }] };
|
||
}
|
||
return { rowCount: 0, rows: [] };
|
||
});
|
||
const r = await loadReservationBatchAttribution('BR003', [SKU]);
|
||
expect(r.bySkuBatch.get(attributionKey(SKU, 'STK20261012NJLG01'))?.qty).toBe(8);
|
||
expect(r.bySkuBatch.get(attributionKey(SKU, 'STK202610118WEOS7'))?.qty).toBe(8);
|
||
// 只读:不得出现写语句
|
||
const sqls = query.mock.calls.map((c) => String(c[0]));
|
||
expect(sqls.some((s) => s.includes('status = 10'))).toBe(true);
|
||
for (const sql of sqls) {
|
||
expect(sql).not.toMatch(/\b(UPDATE|INSERT\s+INTO|DELETE\s+FROM)\b/i);
|
||
}
|
||
});
|
||
|
||
it('⑨ 空入参不炸', async () => {
|
||
expect(attributeReservationsToBatches({ reservations: [], batches: [] }).bySkuBatch.size).toBe(0);
|
||
expect((await loadReservationBatchAttribution('', [])).bySkuBatch.size).toBe(0);
|
||
});
|
||
});
|
||
|
||
/**
|
||
* **降级路径**(2026-10-13 加固):归属整体算不出来时**不能**显示成 0/—,
|
||
* 必须回落到 SKU 级(宁可粗也不错)——否则界面会静默丢掉"这批被占"的信息。
|
||
*/
|
||
describe('单批显示值:归属不可用时回落 SKU 级', () => {
|
||
const base = {
|
||
skuCode: SKU,
|
||
batchNo: 'STK202610118WEOS7',
|
||
availableQty: 12.5,
|
||
skuReservedQty: 16,
|
||
skuClaimableQty: 54,
|
||
skuOrderCodes: ['ORD_A', 'ORD_B'],
|
||
};
|
||
|
||
it('① 无预留 ⇒ 显示 0 占用、可调配=该批可用', () => {
|
||
const d = batchDisplayForBatch({
|
||
...base,
|
||
skuReservedQty: 0,
|
||
attribution: { bySkuBatch: new Map(), unallocatedBySku: new Map() },
|
||
skuHasAttribution: false,
|
||
});
|
||
expect(d).toMatchObject({ reservedQty: 0, claimableQty: 12.5, attributed: true });
|
||
});
|
||
|
||
it('② 有归属 ⇒ 显示本批占用与"本批可用−本批占用"', () => {
|
||
const attribution = {
|
||
bySkuBatch: new Map([[attributionKey(SKU, base.batchNo), { qty: 12.5, orderCodes: ['ORD_A'] }]]),
|
||
unallocatedBySku: new Map(),
|
||
};
|
||
const d = batchDisplayForBatch({ ...base, attribution, skuHasAttribution: true });
|
||
expect(d).toMatchObject({ reservedQty: 12.5, claimableQty: 0, attributed: true });
|
||
expect(d.orderCodes).toEqual(['ORD_A']);
|
||
});
|
||
|
||
it('③ SKU 算出来了、但这批没被占 ⇒ 0(不是回落)', () => {
|
||
const attribution = {
|
||
bySkuBatch: new Map([[attributionKey(SKU, 'OTHER-BATCH'), { qty: 16, orderCodes: ['ORD_A'] }]]),
|
||
unallocatedBySku: new Map(),
|
||
};
|
||
const d = batchDisplayForBatch({ ...base, attribution, skuHasAttribution: true });
|
||
expect(d).toMatchObject({ reservedQty: 0, claimableQty: 12.5, attributed: true });
|
||
});
|
||
|
||
it('④ 归属整体不可用(异常/全部分摊不完)⇒ **回落 SKU 级**并标记 attributed=false', () => {
|
||
const d = batchDisplayForBatch({
|
||
...base,
|
||
attribution: { bySkuBatch: new Map(), unallocatedBySku: new Map([[SKU, 16]]) },
|
||
skuHasAttribution: false,
|
||
});
|
||
expect(d).toMatchObject({ reservedQty: 16, claimableQty: 54, attributed: false });
|
||
expect(d.orderCodes).toEqual(['ORD_A', 'ORD_B']);
|
||
});
|
||
|
||
it('⑤ 归属不可用但 SKU 侧也没预留 ⇒ 仍按 0 显示(不制造假占用)', () => {
|
||
const d = batchDisplayForBatch({
|
||
...base,
|
||
skuReservedQty: 0,
|
||
skuClaimableQty: 12.5,
|
||
attribution: { bySkuBatch: new Map(), unallocatedBySku: new Map() },
|
||
skuHasAttribution: false,
|
||
});
|
||
expect(d).toMatchObject({ reservedQty: 0, attributed: true });
|
||
});
|
||
});
|
||
|
||
describe('skuHasAttribution:区分「这批没被占」与「整体没算出来」', () => {
|
||
it('① 有该 SKU 的批次键 ⇒ true;只有别的 SKU ⇒ false', () => {
|
||
const attribution = {
|
||
bySkuBatch: new Map([[attributionKey(SKU, 'B1'), { qty: 1, orderCodes: [] }]]),
|
||
unallocatedBySku: new Map(),
|
||
};
|
||
expect(skuHasAttribution(attribution, SKU)).toBe(true);
|
||
expect(skuHasAttribution(attribution, 'SKU_OTHER')).toBe(false);
|
||
expect(skuHasAttribution({ bySkuBatch: new Map(), unallocatedBySku: new Map() }, SKU)).toBe(false);
|
||
});
|
||
});
|