import { describe, expect, it, vi } from 'vitest'; import { syncPrepAmendReceivedQtyByFlow } from '../src/services/supplierPreparationStatusService'; /** * 改价改量后按**履约路径**对齐「实收」来源(2026-10-08 用户选「方案甲」)。 * * 背景:订单列表/详情的「实收数量」按路径取不同字段 —— * 直送(SKU 开直送 **且该单客户在名单里**)→ purchase_orders.actual_qty * 供应商免入库 / 加菜 → picking_items.loaded_qty(核货实配) * 其余(正常入库) → inspections.actual_qty * 旧实现只同步 inspections ⇒ 两条不入库路径(实测占改量单 96%)实收永远不跟着变。 * * 口径(方案甲):只对齐「还没发生」的环节。已出库 / 已称重 / 已验收 / 采购已完成一律不改; * **普通入库行的 loaded_qty 绝对不碰**(它的实收走验收,且 loaded_qty 参与发车扣库); * **inspections.actual_qty 一律不写**(提前填会凭空造出"已收货")。 */ type ItemRow = Record; type InspRow = Record; function fakeDb(opts: { items: ItemRow[]; inspections?: InspRow[]; /** 直送名单命中(loadDirectDeliverySkuSetForOrder 的结果) */ directSkus?: string[]; }) { const updates: Array<{ sql: string; params: unknown[] }> = []; const direct = new Set(opts.directSkus ?? []); const db = { query: vi.fn(async (sql: string, params: unknown[] = []) => { const text = String(sql); if (/SELECT customer_code FROM orders/.test(text)) { return { rows: [{ customer_code: 'CUST_1' }], rowCount: 1 }; } if (/FROM skus s/.test(text)) { const codes = (params[0] as string[]) || []; const rows = codes.filter((c) => direct.has(c)).map((c) => ({ sku_code: c })); return { rows, rowCount: rows.length }; } if (/FROM order_items oi/.test(text)) return { rows: opts.items, rowCount: opts.items.length }; if (/FROM inspections/.test(text)) { const rows = opts.inspections ?? []; return { rows, rowCount: rows.length }; } if (/^UPDATE (picking_items|purchase_orders|inspections)/.test(text.trim())) { updates.push({ sql: text, params }); return { rows: [], rowCount: 1 }; } throw new Error('unexpected sql: ' + text.slice(0, 80)); }), } as never; return { db, updates }; } const PREP = { order_code: 'ORD_1', sku_code: 'SKU_1', supplier_code: 'SUP_1' }; const baseItem = (over: ItemRow = {}): ItemRow => ({ id: 'item-1', is_extra_dish: false, skip_inbound: false, picking_id: 'pick-1', loaded_qty: 0.5, picked_weight: 0, outbound_at: null, purchase_actual_qty: null, purchase_plan_qty: null, purchase_status: null, ...over, }); describe('改价改量 · 按履约路径对齐实收来源', () => { it('① 供应商免入库 + 未出库 + 未称重:核货实配 loaded_qty 对齐到新量', async () => { const { db, updates } = fakeDb({ items: [baseItem({ skip_inbound: true })] }); const res = await syncPrepAmendReceivedQtyByFlow(db, PREP, { qty: 4.3, unit: '斤' }); expect(res.find((r) => r.store === 'picking_items')).toMatchObject({ field: 'loaded_qty', before: 0.5, after: 4.3, applied: true, }); const upd = updates.find((u) => /UPDATE picking_items/.test(u.sql)); expect(upd?.params).toEqual(['item-1', 4.3, '斤']); expect(upd?.sql).toContain('outbound_at IS NULL'); }); it('② 加菜行同样走核货实配', async () => { const { db, updates } = fakeDb({ items: [baseItem({ is_extra_dish: true, loaded_qty: 1 })] }); const res = await syncPrepAmendReceivedQtyByFlow(db, PREP, { qty: 8, unit: '斤' }); expect(res.find((r) => r.store === 'picking_items')).toMatchObject({ applied: true, after: 8 }); expect(updates.some((u) => /UPDATE picking_items/.test(u.sql))).toBe(true); }); it('③ 已出库:不改(已发生的物理事实),留 skipped 原因', async () => { const { db, updates } = fakeDb({ items: [baseItem({ skip_inbound: true, outbound_at: '2026-10-08T10:00:00+08:00' })], }); const res = await syncPrepAmendReceivedQtyByFlow(db, PREP, { qty: 4.3, unit: '斤' }); expect(res.find((r) => r.store === 'picking_items')).toMatchObject({ applied: false, skipped_reason: '该行已出库', before: 0.5, }); expect(updates.some((u) => /UPDATE picking_items/.test(u.sql))).toBe(false); }); it('④ 已核货称重:不改(称重是更强的物理事实)', async () => { const { db, updates } = fakeDb({ items: [baseItem({ skip_inbound: true, picked_weight: 0.62 })], }); const res = await syncPrepAmendReceivedQtyByFlow(db, PREP, { qty: 4.3, unit: '斤' }); expect(res.find((r) => r.store === 'picking_items')).toMatchObject({ applied: false, skipped_reason: '已核货称重(picked_weight > 0)', }); expect(updates.some((u) => /UPDATE picking_items/.test(u.sql))).toBe(false); }); it('⑤ 普通入库行:绝不碰 loaded_qty(它参与发车扣库),只对齐验收应到量', async () => { const { db, updates } = fakeDb({ items: [baseItem({ skip_inbound: false, is_extra_dish: false, loaded_qty: 0.5 })], inspections: [{ id: 'insp-1', prepare_qty: 2, actual_qty: null }], }); const res = await syncPrepAmendReceivedQtyByFlow(db, PREP, { qty: 5, unit: '件' }); expect(updates.some((u) => /UPDATE picking_items/.test(u.sql))).toBe(false); expect(res.some((r) => r.store === 'picking_items')).toBe(false); expect(res.find((r) => r.field === 'prepare_qty')).toMatchObject({ after: 5, applied: true }); }); it('⑥ 验收实收一律不写:未验收不预填,已验收不覆盖', async () => { const { db, updates } = fakeDb({ items: [baseItem({ skip_inbound: false, picking_id: null })], inspections: [ { id: 'insp-empty', prepare_qty: 2, actual_qty: null }, { id: 'insp-done', prepare_qty: 2, actual_qty: 1.8 }, ], }); const res = await syncPrepAmendReceivedQtyByFlow(db, PREP, { qty: 5, unit: '件' }); const actualWrites = updates.filter( (u) => /UPDATE inspections/.test(u.sql) && /actual_qty\s*=/.test(u.sql) ); // SQL 里不得出现对 actual_qty 的赋值 expect(actualWrites).toHaveLength(0); const skipped = res.filter((r) => r.field === 'actual_qty'); expect(skipped).toHaveLength(2); expect(skipped.every((r) => r.applied === false)).toBe(true); expect(skipped.map((r) => r.skipped_reason).sort()).toEqual([ '尚未验收(实收由验收环节录入,不预填)', '该批次已验收(实际数量不改写)', ]); expect(skipped.find((r) => r.before === 1.8)?.after).toBe(1.8); }); it('⑦ 直送(名单命中)+ 采购还没实收:采购量对齐,实收不预填', async () => { const { db, updates } = fakeDb({ items: [baseItem({ picking_id: null, purchase_plan_qty: 1, purchase_status: 10 })], directSkus: ['SKU_1'], }); const res = await syncPrepAmendReceivedQtyByFlow(db, PREP, { qty: 6, unit: '斤' }); expect(res.find((r) => r.field === 'purchase_qty')).toMatchObject({ after: 6, applied: true }); expect(res.find((r) => r.field === 'actual_qty')).toMatchObject({ before: null, after: null, applied: false, skipped_reason: '尚未到货(实收不预填)', }); expect(updates.find((u) => /UPDATE purchase_orders/.test(u.sql))!.params).toEqual([ 'ORD_1', 'SKU_1', 6, false, ]); }); it('⑦b 直送 + 已记录过到货实收(>0)且未完成:实收按新量对齐', async () => { const { db } = fakeDb({ items: [ baseItem({ picking_id: null, purchase_actual_qty: 2, purchase_plan_qty: 1, purchase_status: 10 }), ], directSkus: ['SKU_1'], }); const res = await syncPrepAmendReceivedQtyByFlow(db, PREP, { qty: 6, unit: '斤' }); expect(res.find((r) => r.field === 'actual_qty')).toMatchObject({ before: 2, after: 6, applied: true, }); }); it('⑦c 免入库但尚未核货(loaded_qty = 0):不预填,只留 skip 记录', async () => { const { db, updates } = fakeDb({ items: [baseItem({ skip_inbound: true, loaded_qty: 0 })] }); const res = await syncPrepAmendReceivedQtyByFlow(db, PREP, { qty: 4.3, unit: '斤' }); expect(res.find((r) => r.store === 'picking_items')).toMatchObject({ before: 0, after: 0, applied: false, skipped_reason: '尚未核货(实收不预填)', }); expect(updates.some((u) => /UPDATE picking_items/.test(u.sql))).toBe(false); }); it('⑧ SKU 开了直送但该客户不在名单:不算直送,仍走核货分支', async () => { const { db, updates } = fakeDb({ items: [baseItem({ skip_inbound: true, purchase_plan_qty: 1, purchase_status: 10 })], directSkus: [], }); const res = await syncPrepAmendReceivedQtyByFlow(db, PREP, { qty: 6, unit: '斤' }); expect(res.some((r) => r.store === 'purchase_orders')).toBe(false); expect(updates.some((u) => /UPDATE purchase_orders/.test(u.sql))).toBe(false); expect(res.find((r) => r.store === 'picking_items')).toMatchObject({ applied: true, after: 6 }); }); it('⑨ 直送 + 采购已完成:不改采购实收', async () => { const { db, updates } = fakeDb({ items: [ baseItem({ picking_id: null, purchase_actual_qty: 1, purchase_plan_qty: 1, purchase_status: 20 }), ], directSkus: ['SKU_1'], }); const res = await syncPrepAmendReceivedQtyByFlow(db, PREP, { qty: 6, unit: '斤' }); expect(res.find((r) => r.store === 'purchase_orders')).toMatchObject({ applied: false, before: 1, skipped_reason: '采购实收已发生(已完成/作废)', }); expect(updates.some((u) => /UPDATE purchase_orders/.test(u.sql))).toBe(false); }); it('⑩ 数量非法 / 无订单号:直接返回空,不发任何 SQL', async () => { const { db, updates } = fakeDb({ items: [] }); expect(await syncPrepAmendReceivedQtyByFlow(db, PREP, { qty: 0 })).toEqual([]); expect( await syncPrepAmendReceivedQtyByFlow(db, { ...PREP, order_code: null }, { qty: 3 }) ).toEqual([]); expect(updates).toHaveLength(0); }); });