import { beforeEach, describe, expect, it, vi } from 'vitest'; import { resolveCompleteReceivedSettlementQty } from '../src/services/orderItemSupplyUnitSync'; /** * **待补录入库量必须按「实收」口径取数,不能被核货镜像的 `loaded_qty` 覆盖** * (2026-10-15 统一实收口径 `15622fb` 漏改本调用方,2026-10-15 对抗式复核发现)。 * * 背景:核货「没人改实配」时,`deliveryLoadingService` 会把 `required_qty`(应拣量)原样 * 镜像进 `loaded_qty` 作为"已核货"标记(`loaded_done` 承重)。`15622fb` 把 * `resolveCompleteReceivedSettlementQty` 改为「只有 `loaded_qty ≠ required_qty` 才算真人实配」, * 并给函数加了第 5 参 `requiredQty`;但 `pendingBackfillQtyService.ensureBackfillInboundOnConfirm` * 仍只传 4 个实参 ⇒ `required` 被当作 0 ⇒ `loaded > 0` 恒判为"真人实配" ⇒ 非斤类返回 * 镜像的 `loaded_qty`,把真实的 `picked_qty` 覆盖掉。 * * 后果:待补录「确认入账」把入库实收写成计划量(镜像值),比真实实拣少,库存/采购成本账随之少记。 * * 本文件钉住:① 纯函数在给出 `required_qty` 时不再把镜像当实收;② 该调用方确实传了第 5 参, * 用假 db 跑 `ensureBackfillInboundOnConfirm` 断言落库量。 */ const poolQuery = vi.hoisted(() => vi.fn()); const submitInspectionCompliance = vi.hoisted(() => vi.fn()); const findCompletedInboundQty = vi.hoisted(() => vi.fn()); vi.mock('../src/db', () => ({ pool: { query: (...args: unknown[]) => poolQuery(...args), connect: vi.fn() }, query: (...args: unknown[]) => poolQuery(...args), getRawPoolQuery: () => (...args: unknown[]) => poolQuery(...args), getRawPoolConnect: () => vi.fn(), isRlsRoleAvailable: async () => false, allowRlsFailOpen: () => true, })); vi.mock('../src/services/inspectionInboundQtyAdjust', () => ({ findCompletedInboundQty: (...args: unknown[]) => findCompletedInboundQty(...args), adjustInboundInspectionQty: vi.fn(), })); vi.mock('../src/services/inspectionComplianceService', () => ({ submitInspectionCompliance: (...args: unknown[]) => submitInspectionCompliance(...args), reconcileOrderPurchaseFromLatestInspections: vi.fn(), })); import { ensureBackfillInboundOnConfirm } from '../src/services/pendingBackfillQtyService'; /** 一行待补录明细:单位为「件」,实拣 7、核货镜像 loaded=required=5,无库存抵扣 */ function fakeRow(overrides: Record = {}) { return { sku_code: 'SKU1', unit: '件', uncovered_qty: 7, cover_qty: 0, picked_weight: 0, picked_qty: 7, loaded_qty: 5, required_qty: 5, pick_unit: null, ...overrides, }; } beforeEach(() => { poolQuery.mockReset(); submitInspectionCompliance.mockReset(); findCompletedInboundQty.mockReset(); findCompletedInboundQty.mockResolvedValue(null); submitInspectionCompliance.mockResolvedValue(undefined); poolQuery.mockImplementation(async (sql: string) => { const text = String(sql); if (text.includes('AS uncovered_qty')) { return { rowCount: 1, rows: [fakeRow()] }; } // hasUnresolvedFailedInspectionForSku:无不合格未入库 if (text.includes("COALESCE(i.quality_rating, '') IN ('D', '不合格')")) { return { rowCount: 0, rows: [] }; } // closeOrCreateInspectionForSku:命中「待验且未写实收」的既有验收单 if (text.includes('actual_qty IS NULL') && text.includes('quality_rating IS NULL')) { return { rowCount: 1, rows: [{ id: 'INS1' }] }; } return { rowCount: 0, rows: [] }; }); }); describe('待补录确认入账:实收口径', () => { it('纯函数:给出 required_qty(=loaded)⇒ 镜像是标记不是实收,取实拣数量', () => { expect(resolveCompleteReceivedSettlementQty(0, 5, '件', 7, 5)).toBe(7); // 配送员真改过(loaded ≠ required)才取实配 expect(resolveCompleteReceivedSettlementQty(0, 5, '件', 7, 3)).toBe(5); }); it('ensureBackfillInboundOnConfirm 落库量取实拣 7(而非镜像 loaded 5)', async () => { await ensureBackfillInboundOnConfirm({ orderCode: 'ORD_BACKFILL_1' }); expect(submitInspectionCompliance).toHaveBeenCalledTimes(1); const call = submitInspectionCompliance.mock.calls[0][0] as { actual_qty: number }; expect(call.actual_qty, '应按实拣 7 入账,镜像 loaded=5 不算实收').toBe(7); }); });