import { beforeEach, describe, expect, it, vi } from 'vitest'; const { query, release, connect } = vi.hoisted(() => { const query = vi.fn(); const release = vi.fn(); const connect = vi.fn(async () => ({ query, release })); return { query, release, connect }; }); vi.mock('../src/db', () => ({ pool: { connect, query }, })); import { EXTRA_DISH_COST_SOURCE, reconcileExtraDishPurchaseCosts, upsertOrderSkuPurchaseCostFromUnitCost, } from '../src/services/orderCostPostingService'; describe('extra dish cost → 自采侧', () => { beforeEach(() => { query.mockReset(); release.mockReset(); connect.mockClear(); }); it('unit_cost_sync 汇总数量排除加菜行', async () => { const client = { query: vi .fn() // order meta .mockResolvedValueOnce({ rowCount: 1, rows: [{ cycle_code: '202608', branch_code: 'BR001' }], }) // qty(应带 is_extra_dish = false 条件) .mockResolvedValueOnce({ rowCount: 1, rows: [{ qty: 0 }] }) // existingAny .mockResolvedValueOnce({ rowCount: 0, rows: [] }) // purchase_orders lookup .mockResolvedValueOnce({ rowCount: 0, rows: [] }) // SAVEPOINT / INSERT path .mockResolvedValue({ rowCount: 0, rows: [] }), }; await upsertOrderSkuPurchaseCostFromUnitCost(client as any, 'ORD1', 'SKU1', 10); const qtySql = String(client.query.mock.calls[1][0] || ''); expect(qtySql).toMatch(/is_extra_dish/); expect(qtySql).toMatch(/false/); }); it('按明细入账时 reference_id 与 order_item_id 分别转型,避免 $7 类型冲突', async () => { const client = { query: vi .fn() .mockResolvedValueOnce({ rowCount: 1, rows: [{ cycle_code: '202608', branch_code: 'BR001' }], }) .mockResolvedValueOnce({ rowCount: 1, rows: [{ quantity: 2, is_extra_dish: false, is_returned: false, supplier_code: 'INTERNAL' }], }) .mockResolvedValue({ rowCount: 0, rows: [] }), }; await upsertOrderSkuPurchaseCostFromUnitCost(client as any, 'ORD1', 'SKU1', 10, { orderItemId: '11111111-1111-1111-1111-111111111111', }); const insertSql = String( client.query.mock.calls.find(([sql]) => String(sql).includes('INSERT INTO cost_details'))?.[0] || '' ); expect(insertSql).toMatch(/\$7::text/); expect(insertSql).toMatch(/\$7::uuid/); }); it('reconcile 写入 extra_dish 并清理误记的 unit_cost_sync 供应行', async () => { const client = { query: vi.fn(async (sql: string, params?: unknown[]) => { const s = String(sql); if (s.includes('FROM orders WHERE order_code')) { return { rowCount: 1, rows: [{ cycle_code: '202608', branch_code: 'BR001', order_status: 90 }], }; } if (s.includes('FROM customer_quote_sheet')) { return { rowCount: 0, rows: [] }; } if (s.includes('FROM order_items oi') && s.includes('is_extra_dish')) { return { rowCount: 1, rows: [ { id: 'item-1', sku_code: 'SKU_EXTRA', quantity: 2, purchase_code: null, supplier_preparation_id: null, }, ], }; } if (s.includes('SELECT id FROM cost_details') && s.includes('reference_id')) { return { rowCount: 0, rows: [] }; } if (s.includes('SAVEPOINT')) return { rowCount: 0, rows: [] }; if (s.includes('RELEASE SAVEPOINT')) return { rowCount: 0, rows: [] }; if (s.startsWith('INSERT INTO cost_details')) { expect(params?.[7] ?? params?.[params.length - 2]).toBeDefined(); // cost_source 参数应为 extra_dish expect(params).toContain(EXTRA_DISH_COST_SOURCE); return { rowCount: 1, rows: [] }; } if (s.includes('DELETE FROM cost_details') && s.includes('cost_source = $2')) { return { rowCount: 0, rows: [] }; } if (s.includes('DELETE FROM cost_details cd') && s.includes('unit_cost_sync')) { return { rowCount: 1, rows: [] }; } return { rowCount: 0, rows: [] }; }), }; const result = await reconcileExtraDishPurchaseCosts(client as any, 'ORD1', { costBySku: { SKU_EXTRA: 5 }, }); expect(result.posted).toBe(1); expect(result.amount).toBe(10); const sqls = client.query.mock.calls.map((c) => String(c[0])); expect(sqls.some((s) => s.includes('unit_cost_sync'))).toBe(true); expect(sqls.some((s) => s.includes("cost_source = 'supplier'"))).toBe(true); }); it('配送中(60)可写入 extra_dish;待配送(50)/待验收(40)不入账', async () => { const client40 = { query: vi.fn(async (sql: string) => { if (String(sql).includes('FROM orders WHERE order_code')) { return { rowCount: 1, rows: [{ cycle_code: '202608', branch_code: 'BR001', order_status: 40 }] }; } return { rowCount: 0, rows: [] }; }), }; const skipped40 = await reconcileExtraDishPurchaseCosts(client40 as any, 'ORD1', { costBySku: { SKU_EXTRA: 5 }, }); expect(skipped40.posted).toBe(0); expect(client40.query.mock.calls.length).toBe(1); const client50 = { query: vi.fn(async (sql: string) => { if (String(sql).includes('FROM orders WHERE order_code')) { return { rowCount: 1, rows: [{ cycle_code: '202608', branch_code: 'BR001', order_status: 50 }] }; } return { rowCount: 0, rows: [] }; }), }; const skipped50 = await reconcileExtraDishPurchaseCosts(client50 as any, 'ORD1', { costBySku: { SKU_EXTRA: 5 }, }); expect(skipped50.posted).toBe(0); expect(client50.query.mock.calls.length).toBe(1); const client60 = { query: vi.fn(async (sql: string, params?: unknown[]) => { const s = String(sql); if (s.includes('FROM orders WHERE order_code')) { return { rowCount: 1, rows: [{ cycle_code: '202608', branch_code: 'BR001', order_status: 60 }], }; } if (s.includes('FROM customer_quote_sheet')) return { rowCount: 0, rows: [] }; if (s.includes('FROM order_items oi') && s.includes('is_extra_dish')) { return { rowCount: 1, rows: [ { id: 'item-1', sku_code: 'SKU_EXTRA', quantity: 2, purchase_code: null, supplier_preparation_id: null, }, ], }; } if (s.includes('SELECT id FROM cost_details') && s.includes('reference_id')) { return { rowCount: 0, rows: [] }; } if (s.includes('SAVEPOINT') || s.includes('RELEASE SAVEPOINT')) { return { rowCount: 0, rows: [] }; } if (s.startsWith('INSERT INTO cost_details')) { expect(params).toContain(EXTRA_DISH_COST_SOURCE); return { rowCount: 1, rows: [] }; } return { rowCount: 0, rows: [] }; }), }; const posted = await reconcileExtraDishPurchaseCosts(client60 as any, 'ORD1', { costBySku: { SKU_EXTRA: 5 }, }); expect(posted.posted).toBe(1); expect(posted.amount).toBe(10); }); });