import { readFileSync } from 'node:fs'; import { describe, expect, it } from 'vitest'; /** * **采购成本 = 自采 + 供应商 + 库存抵扣**(开发者 2026-10-14 口径定稿)—— 跨面一致性契约。 * * 审计发现(真库 BR002,主系统分配单 `ORD_API_20261014_BR002_0001`:缺口 ¥97 / 抵扣 ¥159、 * 销售额 ¥482)五个"展示采购成本"的面各算各的: * · 商品成本表 `getSkuDirectCostsPaged` = 97 + 159 = 256 ✅(本来就对) * · 成本页总览卡 `getCostAggregation`:总成本 256、毛利却 385(基数只用两支)❌ * · 财务概览 `getFinanceOverviewStats`:采购 97、净利 385 ❌ * · 看板 KPI 卡 `getDashboardCostOverview.quote_cost`:97 ❌ * · 看板采购趋势:256,但是「独立进货单自身 159(不该计)− 抵扣 159(被丢)」互相抵消的巧合 ❌ * · 当日采购账本 `getPurchaseCostLedgerWindow`:抵扣被记成「供应商采购成本」❌ * * 本文件钉住修复后的四处落点(数值证据见本轮真库实测:六面均为 采购 256 / 毛利·净利 226 / 46.89%)。 */ const margin = readFileSync(new URL('../src/services/marginQueryService.ts', import.meta.url), 'utf8'); const dashboard = readFileSync(new URL('../src/services/dashboardService.ts', import.meta.url), 'utf8'); const bucket = readFileSync(new URL('../src/services/purchaseCostBucketSql.ts', import.meta.url), 'utf8'); describe('① 唯一采购来源 getOrderLinePurchaseTotal 必须并入第三桶', () => { it('purchase = self + supplier + stock_cover,并单独返回 stock_cover', () => { const start = margin.indexOf('export async function getOrderLinePurchaseTotal'); const fn = margin.slice(start, margin.indexOf('export async function getSkuDirectCostsPaged', start)); expect(fn).toContain('purchase_stock_cover: number;'); expect(fn).toContain("const purchaseStockCover = round2(Number(paged.totals.stock_cover) || 0);"); expect(fn).toContain('purchase: round2(purchaseSelf + purchaseSupplier + purchaseStockCover),'); // 不得回退成只加两支的旧写法 expect(fn).not.toContain('purchase: round2(purchaseSelf + purchaseSupplier),'); }); it('看板与财务概览都走这一个来源(各自不再另算采购)', () => { expect(margin).toContain('getOrderLinePurchaseTotal(`${dateFrom.replace(/-/g, \'\')}_${dateTo.replace(/-/g, \'\')}`, null, dashField)'); expect(margin).toContain('const purchaseCost = orderLinePurchase.purchase;'); expect(margin).toContain('const productCostTotal = orderLinePurchase.purchase;'); }); }); describe('② 财务概览三个拆分字段与总额自洽', () => { it('返回 product_cost_stock_cover,且 total = self + supplier + stock_cover', () => { expect(margin).toContain('product_cost_stock_cover: number;'); expect(margin).toContain('product_cost_stock_cover: round2(orderLinePurchase.purchase_stock_cover),'); expect(margin).toContain('const productCostTotal = orderLinePurchase.purchase;'); }); }); describe('③ 看板:抵扣行放行 + 独立进货守卫 + 周期键', () => { it('cost_rows 过滤放行 cost_source=inventory_cover(其余采购账本行仍丢)', () => { expect(dashboard).toContain("String(row.cost_source || '') === 'inventory_cover'"); expect(dashboard).toMatch( /\(row\.cost_type !== 'purchase' && row\.cost_type !== 'purchase_applied'\) \|\|/ ); }); it('实时采购表达式排除独立进货单自身(与成本页同判据)', () => { const start = dashboard.indexOf('const runtimeCostRows = await pool.query('); const fn = dashboard.slice(start, dashboard.indexOf('const independentCostParams', start)); expect(fn).toContain("AND ${salesVisibleOrderSql('o')}"); }); it('抵扣行按 amount 取数(采购类只认 amount,不认占位的 allocated_amount)', () => { expect(dashboard).toContain("WHEN cd.cost_type IN ('purchase', 'purchase_applied') THEN COALESCE(cd.amount, 0)"); }); it('周期键必须归一成 YYYY-MM(禁止 String(Date).slice)', () => { expect(dashboard).toContain('function monthKeyOf(value: unknown): string'); expect(dashboard).toContain('if (value instanceof Date && !Number.isNaN(value.getTime()))'); expect(dashboard).not.toContain("String(row.window_ymd || row.created_at || '').slice(0, 7)"); expect(dashboard).not.toContain('const month = dayKey.slice(0, 7)'); }); }); describe('④ 账本窗口单独返回第三桶(供应商不再虚增)', () => { it('split 查询含 stock_cover 汇总,返回值含 purchase_stock_cover', () => { expect(margin).toContain('purchaseStockCoverAmountSql'); expect(margin).toContain('AS stock_cover_total'); expect(margin).toContain('purchase_stock_cover: round2(Number(split.rows[0]?.stock_cover_total) || 0),'); }); it('桶表达式把 inventory_cover 放最前并归 stock_cover', () => { expect(bucket).toContain("WHEN ${cd}.cost_source = 'inventory_cover' THEN 'stock_cover'"); expect(bucket).toContain("export function purchaseStockCoverAmountSql"); }); });