开发者真机提问:「当前库存抵扣选项 是不是给全额抵扣使用的?如果是当前的库存无法全额抵扣, 那么他应该不能出现在可选项中」。 后端语义本来就是「全额才打 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
496 lines
20 KiB
TypeScript
496 lines
20 KiB
TypeScript
import { beforeEach, describe, expect, it, vi } from 'vitest';
|
||
import { readFileSync } from 'fs';
|
||
import path from 'path';
|
||
|
||
/**
|
||
* 「这一单已经抵扣了哪些批次」(2026-10-13 开发者要求):
|
||
* 列表那行「库存抵扣:…」点开 ⇒ 几号入库 / 生产日期 / 保质期 / 区域 / 抵扣了多少。
|
||
*
|
||
* 这里钉住做反就要返工的四件事:
|
||
* ① **FIFO 口径不许漂**:无批次计划时的兜底顺序必须与 `stockCoverBatchPlanService.loadFifoBatches`
|
||
* **逐字一致**(`ORDER BY created_at ASC, expiry_date ASC NULLS FIRST`)——
|
||
* 漂了就会出现"界面说要扣这批、实际扣的是那批";
|
||
* ② **只读**:新模块不许出现 UPDATE / INSERT / DELETE(扣库、写计划、写流水都不许在这里干);
|
||
* ③ **事实优先**:库里已有批次事实(`picking_items.outbound_batches`)时按事实走(`source='plan'`),
|
||
* **绝不能**再用现算的 FIFO 覆盖它(库存后来变了,计划才是当初的凭证);
|
||
* ④ 路由只鉴权 + 委派(权限集合与 `batch-reports` 一致),不写 SQL。
|
||
*/
|
||
const query = vi.hoisted(() => vi.fn());
|
||
/** 单 SKU 可抵扣量(口径唯一入口,被动态 import)——本测试只关心「池子有没有被带上」 */
|
||
const resolveCoverableQtyForSku = vi.hoisted(() => vi.fn(async () => 0));
|
||
|
||
vi.mock('../src/db', () => ({
|
||
pool: { query: (...args: unknown[]) => query(...args) },
|
||
}));
|
||
|
||
vi.mock('../src/services/inventoryStockCoverService', () => ({
|
||
resolveCoverableQtyForSku: (...args: unknown[]) => resolveCoverableQtyForSku(...args),
|
||
}));
|
||
|
||
import { listStockCoverBatches } from '../src/services/stockCoverBatchQueryService';
|
||
|
||
const serviceSrc = readFileSync(
|
||
path.join(__dirname, '../src/services/stockCoverBatchQueryService.ts'),
|
||
'utf8'
|
||
);
|
||
const planSrc = readFileSync(
|
||
path.join(__dirname, '../src/services/stockCoverBatchPlanService.ts'),
|
||
'utf8'
|
||
);
|
||
const routeSrc = readFileSync(
|
||
path.join(__dirname, '../src/modules/workflow/routes/orderRoutes.ts'),
|
||
'utf8'
|
||
);
|
||
|
||
const FIFO_ORDER_BY = 'ORDER BY created_at ASC, expiry_date ASC NULLS FIRST';
|
||
|
||
/** 取出 GET /orders/:orderCode/stock-cover-batches 这一个 handler 的源码片段 */
|
||
function coverBatchesRouteBlock(): string {
|
||
const start = routeSrc.indexOf("router.get('/orders/:orderCode/stock-cover-batches'");
|
||
expect(start, '路由必须存在').toBeGreaterThan(-1);
|
||
const end = routeSrc.indexOf('router.get(', start + 10);
|
||
return routeSrc.slice(start, end > start ? end : start + 2000);
|
||
}
|
||
|
||
type CoveredRow = {
|
||
id: string;
|
||
item_name: string | null;
|
||
sku_code: string | null;
|
||
unit: string | null;
|
||
cover_qty: number;
|
||
/** 我方指定的抵扣批次(方案甲新增列) */
|
||
cover_batches?: unknown;
|
||
order_status?: number;
|
||
};
|
||
|
||
/** 把六条 SQL(已抵扣行 / 批次计划 / 抵扣账本额 / 出库锁定 / FIFO 候选 / 批次元数据)分别接上假数据 */
|
||
function wireQueries(input: {
|
||
covered: CoveredRow[];
|
||
plans?: Array<{ item_id: string; outbound_batches: unknown }>;
|
||
fifo?: Array<{ batch_no: string; available_qty: number }>;
|
||
meta?: Array<Record<string, unknown>>;
|
||
/** 抵扣账本(`cost_details(cost_source='inventory_cover')`)按 SKU 汇总 */
|
||
coverCosts?: Array<{ sku_code: string | null; amount: number }>;
|
||
/** 出库锁定判据(默认未出库) */
|
||
lock?: { has_outbound?: boolean; pick_outbound?: boolean };
|
||
}) {
|
||
query.mockImplementation((sql: string) => {
|
||
const text = String(sql);
|
||
// 该单「每个 SKU 的可抵扣量池」:先于下面那条 `FROM order_items`(它带 stock_cover_qty)判
|
||
if (text.includes('DISTINCT TRIM(sku_code)')) {
|
||
const skus = [
|
||
...new Set(input.covered.map((r) => String(r.sku_code || '').trim()).filter(Boolean)),
|
||
];
|
||
return Promise.resolve({ rows: skus.map((sku_code) => ({ sku_code })), rowCount: skus.length });
|
||
}
|
||
if (text.includes('FROM inventory_logs il')) {
|
||
return Promise.resolve({
|
||
rows: [
|
||
{
|
||
has_outbound: Boolean(input.lock?.has_outbound),
|
||
pick_outbound: Boolean(input.lock?.pick_outbound),
|
||
},
|
||
],
|
||
rowCount: 1,
|
||
});
|
||
}
|
||
if (text.includes('FROM order_items') && text.includes('stock_cover_qty')) {
|
||
return Promise.resolve({ rows: input.covered, rowCount: input.covered.length });
|
||
}
|
||
if (text.includes('FROM cost_details') && text.includes("cost_source = 'inventory_cover'")) {
|
||
return Promise.resolve({
|
||
rows: input.coverCosts || [],
|
||
rowCount: (input.coverCosts || []).length,
|
||
});
|
||
}
|
||
if (text.includes('FROM picking_items pi')) {
|
||
return Promise.resolve({ rows: input.plans || [], rowCount: (input.plans || []).length });
|
||
}
|
||
if (text.includes('DISTINCT ON (sku_code, batch_no)')) {
|
||
return Promise.resolve({ rows: input.meta || [], rowCount: (input.meta || []).length });
|
||
}
|
||
if (text.includes('FROM inventory')) {
|
||
return Promise.resolve({ rows: input.fifo || [], rowCount: (input.fifo || []).length });
|
||
}
|
||
throw new Error(`未预期的 SQL: ${text.slice(0, 60)}`);
|
||
});
|
||
}
|
||
|
||
describe('库存抵扣批次明细(只读查询)', () => {
|
||
beforeEach(() => {
|
||
query.mockReset();
|
||
resolveCoverableQtyForSku.mockReset();
|
||
resolveCoverableQtyForSku.mockResolvedValue(0);
|
||
});
|
||
|
||
it('⓪ 附带返回「该单每个 SKU 的可抵扣量池」——口径取唯一入口,且逐 SKU 调用', async () => {
|
||
resolveCoverableQtyForSku.mockImplementation(async (_db: unknown, sku: string) =>
|
||
sku === 'SKU-A' ? 12 : 0
|
||
);
|
||
wireQueries({
|
||
covered: [
|
||
{ id: 'item-1', item_name: '抄手皮', sku_code: 'SKU-A', unit: '斤', cover_qty: 8 },
|
||
{ id: 'item-2', item_name: '瘦肉', sku_code: 'SKU-B', unit: '斤', cover_qty: 0 },
|
||
],
|
||
plans: [],
|
||
fifo: [],
|
||
});
|
||
const res = await listStockCoverBatches({ orderCode: 'ORD-1' });
|
||
expect(res.coverable_by_sku).toEqual({ 'SKU-A': 12, 'SKU-B': 0 });
|
||
// 池子按「本单出现的 SKU」算(去重后逐个走同一入口)
|
||
expect(resolveCoverableQtyForSku).toHaveBeenCalledTimes(2);
|
||
expect(resolveCoverableQtyForSku.mock.calls.map((c) => c[1])).toEqual(['SKU-A', 'SKU-B']);
|
||
});
|
||
|
||
it('① FIFO 口径守卫:兜底顺序与抵扣侧 loadFifoBatches 逐字一致', () => {
|
||
expect(serviceSrc).toContain(FIFO_ORDER_BY);
|
||
expect(planSrc).toContain(FIFO_ORDER_BY);
|
||
// 同口径的另一半:只取有可用量的批次
|
||
expect(serviceSrc).toContain('available_qty > 0');
|
||
});
|
||
|
||
it('② 只读守卫:模块内不得有任何写语句', () => {
|
||
expect(serviceSrc).not.toMatch(/\b(UPDATE|INSERT\s+INTO|DELETE\s+FROM)\b/i);
|
||
});
|
||
|
||
it('③ 已有批次事实 ⇒ 按事实返回(source=plan),展示值不取自现算 FIFO', async () => {
|
||
wireQueries({
|
||
covered: [{ id: 'item-1', item_name: '抄手皮', sku_code: 'SKU62634064428', unit: '斤', cover_qty: 8 }],
|
||
plans: [{ item_id: 'item-1', outbound_batches: [{ batch_no: 'STK-A', qty: 5, source: 'fifo_cover' }, { batch_no: 'STK-B', qty: 3 }] }],
|
||
fifo: [{ batch_no: 'STK-Z', available_qty: 100 }],
|
||
});
|
||
const res = await listStockCoverBatches({ orderCode: 'ORD-1' });
|
||
expect(res.items).toHaveLength(1);
|
||
expect(res.items[0].source).toBe('plan');
|
||
expect(res.items[0].batches.map((b) => [b.batch_no, b.qty])).toEqual([
|
||
['STK-A', 5],
|
||
['STK-B', 3],
|
||
]);
|
||
// 事实优先:展示的批次**不是**现算 FIFO 的 STK-Z(FIFO 只作为编辑候选一起返回)
|
||
expect(res.items[0].batches.map((b) => b.batch_no)).not.toContain('STK-Z');
|
||
expect(res.items[0].candidates.map((c) => c.batch_no)).toEqual(['STK-Z']);
|
||
});
|
||
|
||
it('④ 无批次事实 ⇒ 按先进先出算(source=fifo),数量封顶在已抵扣量', async () => {
|
||
wireQueries({
|
||
covered: [{ id: 'item-1', item_name: '苹果', sku_code: 'SKU-A', unit: '斤', cover_qty: 8 }],
|
||
plans: [],
|
||
fifo: [
|
||
{ batch_no: 'STK-早', available_qty: 5 },
|
||
{ batch_no: 'STK-晚', available_qty: 30 },
|
||
],
|
||
meta: [
|
||
{
|
||
sku_code: 'SKU-A',
|
||
batch_no: 'STK-早',
|
||
created_at: '2026-10-11T07:23:19.300Z',
|
||
production_date: '2026-10-08T16:00:00.000Z',
|
||
expiry_date: '2026-10-14T16:00:00.000Z',
|
||
storage_zone: '冷库A',
|
||
},
|
||
],
|
||
});
|
||
const res = await listStockCoverBatches({ orderCode: 'ORD-1' });
|
||
expect(res.items[0].source).toBe('fifo');
|
||
expect(res.items[0].batches.map((b) => [b.batch_no, b.qty])).toEqual([
|
||
['STK-早', 5],
|
||
['STK-晚', 3],
|
||
]);
|
||
// 元数据富化:入库日期 / 生产日期 / 效期 / 区域(本地日)
|
||
const first = res.items[0].batches[0];
|
||
expect(first.inbound_date).toBe('2026-10-11');
|
||
expect(first.production_date).toBe('2026-10-09');
|
||
expect(first.expiry_date).toBe('2026-10-15');
|
||
expect(first.storage_zone).toBe('冷库A');
|
||
// 查不到的批次照常返回批次号与数量,元数据为 null(不得整条丢掉)
|
||
const second = res.items[0].batches[1];
|
||
expect(second.batch_no).toBe('STK-晚');
|
||
expect(second.qty).toBe(3);
|
||
expect(second.inbound_date).toBeNull();
|
||
expect(second.storage_zone).toBeNull();
|
||
});
|
||
|
||
it('⑤ 库存已空且无计划 ⇒ 该行照常返回、批次为空(前端能说清"没有批次")', async () => {
|
||
wireQueries({
|
||
covered: [{ id: 'item-1', item_name: '水面', sku_code: 'SKU-B', unit: '斤', cover_qty: 13 }],
|
||
plans: [],
|
||
fifo: [],
|
||
meta: [],
|
||
});
|
||
const res = await listStockCoverBatches({ orderCode: 'ORD-1' });
|
||
expect(res.items).toHaveLength(1);
|
||
expect(res.items[0].batches).toEqual([]);
|
||
expect(res.items[0].cover_qty).toBe(13);
|
||
});
|
||
|
||
it('⑥ 未抵扣的订单 / 空单号 ⇒ 空结果,且不查库', async () => {
|
||
expect(await listStockCoverBatches({ orderCode: '' })).toEqual({
|
||
items: [],
|
||
coverable_by_sku: {},
|
||
});
|
||
wireQueries({ covered: [] });
|
||
// 一行都没抵扣:`items` 空,但**池子照给**(否则"想抵还没抵"的单拿不到池子 ⇒ 选项永远出不来)
|
||
expect(await listStockCoverBatches({ orderCode: 'ORD-NONE' })).toEqual({
|
||
items: [],
|
||
coverable_by_sku: {},
|
||
});
|
||
});
|
||
|
||
it('⑦ 已抵扣行的过滤口径写在 SQL 里(未退货 + 抵扣量 > 0)', () => {
|
||
expect(serviceSrc).toContain('COALESCE(oi.stock_cover_qty, 0) > 0.000001');
|
||
expect(serviceSrc).toContain('COALESCE(oi.is_returned, false) = false');
|
||
});
|
||
|
||
/**
|
||
* 方案甲(开发者 2026-10-13):「这边如果改了抵扣的批次后,那么分拣时分拣的批次需要对应得上」。
|
||
* 优先级写死:我方指定 > 抵扣计划/分拣选定 > 先进先出现算。
|
||
*/
|
||
it('⑨ 我方指定优先:有 cover_batches ⇒ source=manual(压过库里的计划)', async () => {
|
||
wireQueries({
|
||
covered: [
|
||
{
|
||
id: 'item-1',
|
||
item_name: '抄手皮',
|
||
sku_code: 'SKU-A',
|
||
unit: '斤',
|
||
cover_qty: 8,
|
||
cover_batches: [{ batch_no: 'STK-我方', qty: 8, source: 'manual_cover' }],
|
||
order_status: 30,
|
||
} as CoveredRow,
|
||
],
|
||
plans: [{ item_id: 'item-1', outbound_batches: [{ batch_no: 'STK-计划', qty: 8 }] }],
|
||
fifo: [{ batch_no: 'STK-FIFO', available_qty: 100 }],
|
||
meta: [
|
||
{
|
||
sku_code: 'SKU-A',
|
||
batch_no: 'STK-我方',
|
||
created_at: '2026-10-11T07:23:19.300Z',
|
||
production_date: null,
|
||
expiry_date: null,
|
||
storage_zone: '冷库A',
|
||
available_qty: '8.00',
|
||
},
|
||
],
|
||
});
|
||
const res = await listStockCoverBatches({ orderCode: 'ORD-1' });
|
||
expect(res.items[0].source).toBe('manual');
|
||
expect(res.items[0].batches.map((b) => [b.batch_no, b.qty])).toEqual([['STK-我方', 8]]);
|
||
expect(res.items[0].read_only).toBe(false);
|
||
});
|
||
|
||
it('⑫ 我方指定里的批次在当前 SKU 下已不存在(换过商品/批次被删)⇒ 不当真,退回计划/FIFO', async () => {
|
||
wireQueries({
|
||
covered: [
|
||
{
|
||
id: 'item-1',
|
||
item_name: '抄手皮',
|
||
sku_code: 'SKU-A',
|
||
unit: '斤',
|
||
cover_qty: 8,
|
||
cover_batches: [{ batch_no: 'STK-旧商品批次', qty: 8, source: 'manual_cover' }],
|
||
order_status: 30,
|
||
} as CoveredRow,
|
||
],
|
||
plans: [{ item_id: 'item-1', outbound_batches: [{ batch_no: 'STK-计划', qty: 8 }] }],
|
||
fifo: [{ batch_no: 'STK-计划', available_qty: 8 }],
|
||
meta: [
|
||
{
|
||
sku_code: 'SKU-A',
|
||
batch_no: 'STK-计划',
|
||
created_at: '2026-10-11T07:23:19.300Z',
|
||
production_date: null,
|
||
expiry_date: null,
|
||
storage_zone: '冷库A',
|
||
available_qty: '8.00',
|
||
},
|
||
],
|
||
});
|
||
const res = await listStockCoverBatches({ orderCode: 'ORD-1' });
|
||
expect(res.items[0].source).toBe('plan');
|
||
expect(res.items[0].batches.map((b) => b.batch_no)).toEqual(['STK-计划']);
|
||
});
|
||
|
||
it('⑩ 已出库 ⇒ read_only 且以拣货行事实为准(我方指定不再顶掉已经扣掉的事实)', async () => {
|
||
wireQueries({
|
||
covered: [
|
||
{
|
||
id: 'item-1',
|
||
item_name: '抄手皮',
|
||
sku_code: 'SKU-A',
|
||
unit: '斤',
|
||
cover_qty: 8,
|
||
cover_batches: [{ batch_no: 'STK-我方', qty: 8, source: 'manual_cover' }],
|
||
order_status: 50,
|
||
} as CoveredRow,
|
||
],
|
||
plans: [{ item_id: 'item-1', outbound_batches: [{ batch_no: 'STK-实际', qty: 8 }] }],
|
||
fifo: [{ batch_no: 'STK-FIFO', available_qty: 100 }],
|
||
lock: { has_outbound: true },
|
||
});
|
||
const res = await listStockCoverBatches({ orderCode: 'ORD-1' });
|
||
expect(res.items[0].read_only).toBe(true);
|
||
expect(res.items[0].read_only_reason).toBe('order_departed');
|
||
expect(res.items[0].source).toBe('plan');
|
||
expect(res.items[0].batches.map((b) => b.batch_no)).toEqual(['STK-实际']);
|
||
// 已出库不给编辑候选
|
||
expect(res.items[0].candidates).toEqual([]);
|
||
});
|
||
|
||
it('⑪ 候选批次与 FIFO 同序、带元数据(编辑界面用)', async () => {
|
||
wireQueries({
|
||
covered: [
|
||
{ id: 'item-1', item_name: '苹果', sku_code: 'SKU-A', unit: '斤', cover_qty: 3 } as CoveredRow,
|
||
],
|
||
plans: [],
|
||
fifo: [
|
||
{ batch_no: 'STK-早', available_qty: 5 },
|
||
{ batch_no: 'STK-晚', available_qty: 9 },
|
||
],
|
||
meta: [
|
||
{
|
||
sku_code: 'SKU-A',
|
||
batch_no: 'STK-早',
|
||
created_at: '2026-10-11T07:23:19.300Z',
|
||
production_date: null,
|
||
expiry_date: null,
|
||
storage_zone: '冷库A',
|
||
available_qty: '5.00',
|
||
},
|
||
],
|
||
});
|
||
const res = await listStockCoverBatches({ orderCode: 'ORD-1' });
|
||
expect(res.items[0].candidates.map((c) => c.batch_no)).toEqual(['STK-早', 'STK-晚']);
|
||
expect(res.items[0].candidates[0].storage_zone).toBe('冷库A');
|
||
expect(res.items[0].candidates[0].inbound_date).toBe('2026-10-11');
|
||
// 展示行也带「当前可用量」(库存后来变了要看得见)
|
||
expect(res.items[0].batches[0].available_qty).toBe(5);
|
||
});
|
||
|
||
/**
|
||
* 开发者 2026-10-14:「将抵扣的库存的成本价展示出来」。
|
||
* 成本口径见 docs/design/20261014-抵扣成本口径(成本随货走+去重).md —— 抵扣部分按
|
||
* **被抵扣批次成本价**计入订单成本,所以弹窗必须能看到"抵的是多少钱的货"。
|
||
*/
|
||
it('⑬ 批次与候选都带成本价;批次未设成本价 ⇒ null(不当 0 展示)', async () => {
|
||
wireQueries({
|
||
covered: [
|
||
{ id: 'item-1', item_name: '抄手皮', sku_code: 'SKU-A', unit: '斤', cover_qty: 8, order_status: 30 },
|
||
],
|
||
plans: [],
|
||
fifo: [
|
||
{ batch_no: 'STK-早', available_qty: 5 },
|
||
{ batch_no: 'STK-无价', available_qty: 9 },
|
||
],
|
||
meta: [
|
||
{
|
||
sku_code: 'SKU-A',
|
||
batch_no: 'STK-早',
|
||
created_at: '2026-10-11T07:23:19.300Z',
|
||
production_date: null,
|
||
expiry_date: null,
|
||
storage_zone: '冷库A',
|
||
available_qty: '5.00',
|
||
cost_price: '6.00',
|
||
},
|
||
{
|
||
sku_code: 'SKU-A',
|
||
batch_no: 'STK-无价',
|
||
created_at: '2026-10-12T07:23:19.300Z',
|
||
production_date: null,
|
||
expiry_date: null,
|
||
storage_zone: '冷库A',
|
||
available_qty: '9.00',
|
||
cost_price: null,
|
||
},
|
||
],
|
||
});
|
||
const res = await listStockCoverBatches({ orderCode: 'ORD-1' });
|
||
expect(res.items[0].batches[0].cost_price).toBe(6);
|
||
expect(res.items[0].candidates.map((c) => [c.batch_no, c.cost_price])).toEqual([
|
||
['STK-早', 6],
|
||
['STK-无价', null],
|
||
]);
|
||
// 取数 SQL 必须把 cost_price 一并取回(否则界面永远是 null)
|
||
expect(serviceSrc).toMatch(/available_qty, cost_price/);
|
||
});
|
||
|
||
it('⑧ 路由:只鉴权 + 委派,权限集合与 batch-reports 一致,带分公司上下文', () => {
|
||
const block = coverBatchesRouteBlock();
|
||
expect(block).toContain('requireWorkflowPermission');
|
||
expect(block).toContain('assertOrderCodeAccess');
|
||
expect(block).toContain('stockCoverBatchQueryService');
|
||
expect(block).toContain('branchCode: ctx?.branchCode');
|
||
// 路由不写 SQL
|
||
expect(block).not.toMatch(/\b(SELECT|UPDATE|INSERT\s+INTO|DELETE\s+FROM)\b/i);
|
||
|
||
const permBlock = (marker: string) => {
|
||
const start = routeSrc.indexOf(marker);
|
||
return routeSrc.slice(start, start + 400);
|
||
};
|
||
const perms = (src: string) => (src.match(/'([a-z_]+)',/g) || []).join(',');
|
||
expect(perms(permBlock("router.get('/orders/:orderCode/stock-cover-batches'"))).toBe(
|
||
perms(permBlock("router.get('/orders/:orderCode/batch-reports'"))
|
||
);
|
||
});
|
||
|
||
/**
|
||
* **②(2026-10-14 缺陷回归)**:`picking_items.outbound_batches` 在分拣保存后装的是**整行出库批次**
|
||
* (抵扣批次 + 缺口自采/备货批次)⇒ 直接当"抵扣批次"会把缺口也算进来,
|
||
* 界面「抵扣 ¥X」被放大 `整行 ÷ 抵扣量` 倍(BR002 实测 45/8=5.63、90/15=6.00、121/30=4.03)。
|
||
*/
|
||
it('⑭ 整行出库批次按抵扣量截断:抵扣批次只留 cover_qty 那几斤,其余进 gap_batches', async () => {
|
||
wireQueries({
|
||
covered: [
|
||
{ id: 'item-1', item_name: '抄手皮', sku_code: 'SKU97144738818', unit: '斤', cover_qty: 8 },
|
||
],
|
||
plans: [
|
||
{
|
||
item_id: 'item-1',
|
||
outbound_batches: [
|
||
{ batch_no: 'COVER-8', qty: 8 }, // 抵扣批次
|
||
{ batch_no: 'GAP-7', qty: 7 }, // 缺口自采/备货入库批次
|
||
],
|
||
},
|
||
],
|
||
coverCosts: [{ sku_code: 'SKU97144738818', amount: 24 }],
|
||
});
|
||
const item = (await listStockCoverBatches({ orderCode: 'ORD-1' })).items[0];
|
||
expect(item.batches.map((b) => [b.batch_no, b.qty])).toEqual([['COVER-8', 8]]);
|
||
expect(item.gap_qty).toBe(7);
|
||
expect(item.gap_batches.map((b) => [b.batch_no, b.qty])).toEqual([['GAP-7', 7]]);
|
||
// 抵扣成本额取**账本**(24),不是批次列表现算(8×1 + 7×1)
|
||
expect(item.cover_cost_amount).toBe(24);
|
||
});
|
||
|
||
it('⑮ 抵扣成本额无账本行 ⇒ null(前端退回批次求和,不显示 0)', async () => {
|
||
wireQueries({
|
||
covered: [{ id: 'item-1', item_name: '苹果', sku_code: 'SKU1', unit: '斤', cover_qty: 30 }],
|
||
plans: [{ item_id: 'item-1', outbound_batches: [{ batch_no: 'B1', qty: 30 }] }],
|
||
coverCosts: [],
|
||
});
|
||
const item = (await listStockCoverBatches({ orderCode: 'ORD-1' })).items[0];
|
||
expect(item.cover_cost_amount).toBeNull();
|
||
expect(item.batches.map((b) => b.batch_no)).toEqual(['B1']);
|
||
expect(item.gap_qty).toBe(0);
|
||
});
|
||
|
||
it('⑯ splitBatchesByCoverQty:跨批次切分与余量(纯函数)', async () => {
|
||
const { splitBatchesByCoverQty } = await import('../src/services/stockCoverBatchQueryService');
|
||
expect(
|
||
splitBatchesByCoverQty(
|
||
[
|
||
{ batch_no: 'A', qty: 5 },
|
||
{ batch_no: 'B', qty: 6 },
|
||
],
|
||
8
|
||
)
|
||
).toEqual({ cover: [{ batch_no: 'A', qty: 5 }, { batch_no: 'B', qty: 3 }], gap: [{ batch_no: 'B', qty: 3 }], gapQty: 3 });
|
||
// 抵扣量大于批次合计 ⇒ 全算抵扣、无缺口
|
||
expect(splitBatchesByCoverQty([{ batch_no: 'A', qty: 2 }], 10)).toEqual({
|
||
cover: [{ batch_no: 'A', qty: 2 }],
|
||
gap: [],
|
||
gapQty: 0,
|
||
});
|
||
});
|
||
});
|