import { beforeEach, describe, expect, it, vi } from 'vitest'; /** * BIZ-14(2026-10-19):入库后转挂到自采,**不得由转挂服务直写 `purchase_status=30`/`actual_qty`**。 * * 缺陷:`inboundFulfillmentReattributeService.ensureCompletedSelfPurchase` 自己 UPDATE/INSERT * `purchase_status = 30` 并预填 `actual_qty = 计划量`,破坏方案 C「验收是实收唯一写入方」。 * * 修复(AC-T1.7.1/2/4): * ① 转挂服务的 UPDATE/INSERT 不再写 30、不再写 actual_qty(新单落 20 待验收); * ② 实收由验收模块 `writeReattributedSelfPurchaseActual` 按**累计入库量**落 30 + actual_qty; * ③ actual_qty 必须真的被写入(不是只改状态)。 */ const H = vi.hoisted(() => ({ poolQuery: vi.fn(), upsertCost: vi.fn(), recalcStatements: vi.fn(), })); vi.mock('../src/db', () => ({ pool: { query: (...args: unknown[]) => H.poolQuery(...args), connect: vi.fn() }, query: (...args: unknown[]) => H.poolQuery(...args), getRawPoolQuery: () => H.poolQuery, })); vi.mock('../src/services/orderCostPostingService', () => ({ upsertOrderSkuPurchaseCostFromUnitCost: (...args: unknown[]) => H.upsertCost(...args), })); vi.mock('../src/services/financeReconciliationService', () => ({ recalculateStatementsAffectedByOrder: (...args: unknown[]) => H.recalcStatements(...args), })); import { reattributeInboundFulfillment } from '../src/services/inboundFulfillmentReattributeService'; type Row = Record; type Call = { sql: string; params: unknown[] }; const state = { calls: [] as Call[], existingPurchase: true, /** 累计入库量(inspection actual_qty 合计) */ accumulatedInbound: 5, }; function makeDb() { return { query: async (sql: string, params: unknown[] = []) => { const text = String(sql); state.calls.push({ sql: text, params }); if (text.includes('FROM inspections') && text.includes('inbound_batch_no') && text.includes('LIMIT 1')) { return { rowCount: 1, rows: [{ 1: 1 }] }; } if (text.includes('SELECT DISTINCT NULLIF(TRIM(inbound_batch_no)')) { return { rowCount: 1, rows: [{ batch_no: 'B1' }] }; } if (text.includes('FROM order_item_fulfillments f')) { return { rowCount: 0, rows: [] }; } if (text.includes('FROM purchase_orders') && text.includes('SELECT purchase_code')) { return state.existingPurchase ? { rowCount: 1, rows: [{ purchase_code: 'PURC1' }] } : { rowCount: 0, rows: [] }; } // 验收模块按累计入库量取批次 if (text.includes('FROM inspections') && text.includes('actual_qty') && text.includes('parent_id')) { return { rowCount: 1, rows: [{ id: 'insp-1', actual_qty: state.accumulatedInbound, created_at: new Date(), parent_id: null }], }; } return { rowCount: 0, rows: [] }; }, }; } async function runReattribute(): Promise { await reattributeInboundFulfillment(makeDb() as never, { orderCode: 'ORD1', itemId: 'item-1', skuCode: 'SKU1', itemName: '草鱼', unit: '斤', previousSupplierCode: 'SUP1', nextSupplierCode: null, // 改派到自采 quantity: 4, unitCost: 3, branchCode: 'BR002', }); } beforeEach(() => { state.calls = []; state.existingPurchase = true; state.accumulatedInbound = 5; H.poolQuery.mockReset(); H.upsertCost.mockReset(); H.recalcStatements.mockReset(); H.upsertCost.mockResolvedValue(undefined); H.recalcStatements.mockResolvedValue(undefined); }); describe('BIZ-14:转挂自采不再直写 purchase_status=30 / actual_qty', () => { it('既有自采单:转挂 UPDATE 不写 30/actual_qty;验收模块按累计入库量写 30 + actual_qty', async () => { await runReattribute(); const purchaseUpdates = state.calls .filter((call) => call.sql.includes('UPDATE purchase_orders')) .map((call) => call); const handoverUpdate = purchaseUpdates.find((call) => call.sql.includes('SET supplier_code = NULL')); expect(handoverUpdate, '转挂服务应更新自采单基础字段').toBeTruthy(); expect(handoverUpdate!.sql).not.toMatch(/purchase_status\s*=\s*30/); expect(handoverUpdate!.sql).not.toMatch(/actual_qty\s*=/); const inspectionWrite = purchaseUpdates.find((call) => /purchase_status\s*=\s*30/.test(call.sql)); expect(inspectionWrite, '必须由验收模块统一写 30').toBeTruthy(); expect(inspectionWrite!.sql).toMatch(/actual_qty\s*=\s*\$2::numeric/); // 实收量来自累计入库量(5),不是转挂入参的计划量(4) expect(inspectionWrite!.params[1]).toBe(5); // 全仓 SQL 里 30 只出现于验收模块那一条;转挂自己的 INSERT 也不写 30 const status30Calls = state.calls.filter((call) => /purchase_status\s*=\s*30/.test(call.sql)); expect(status30Calls).toHaveLength(1); expect(state.calls.some((call) => /INSERT INTO purchase_orders/.test(call.sql))).toBe(false); expect(H.upsertCost).toHaveBeenCalledTimes(1); }); it('无既有自采单:INSERT 落 20(待验收)且 actual_qty 为空,随后由验收模块写 30 + 实收', async () => { state.existingPurchase = false; await runReattribute(); const insert = state.calls.find((call) => call.sql.includes('INSERT INTO purchase_orders')); expect(insert, '应新建自采单').toBeTruthy(); expect(insert!.sql).not.toMatch(/,\s*30\s*,/); // 不得把 30 塞进状态列 expect(insert!.sql).toMatch(/NULL,\s*\$5,\s*20,/); // actual_qty=NULL、purchase_status=20 const insertArgs = insert!.params; expect(insertArgs[3]).toBe(4); // purchase_qty = 计划量 expect(insertArgs[4]).toBe(3); // purchase_price const inspectionWrite = state.calls.find((call) => /purchase_status\s*=\s*30/.test(call.sql)); expect(inspectionWrite).toBeTruthy(); expect(inspectionWrite!.params[1]).toBe(state.accumulatedInbound); }); });