审计(只读,真库 BR002 主系统分配单 ORD_API_20261014_BR002_0001:缺口 ¥97 / 抵扣 ¥159 /
销售额 ¥482 ⇒ 采购成本应 ¥256、毛利·净利应 ¥226)发现五个"展示采购成本"的面各算各的:
| 面 | 改前 | 改后 |
| --- | --- | --- |
| 商品成本表 getSkuDirectCostsPaged | 97 + 159 = 256 ✅ | 不变(本来就对) |
| 成本页总览卡 getCostAggregation | 总成本 256 / 毛利 385 / 79.88%(自相矛盾) | 总成本 256 / 毛利 226 / 46.89% |
| 财务概览 getFinanceOverviewStats | 采购 97 / 总成本 97 / 净利 385 | 采购 256 / 总成本 256 / 净利 226 |
| 看板 KPI「订单采购成本」quote_cost | 97 | 256 |
| 看板采购趋势 costTrends | 256(组成错:+独立进货159 −抵扣159 互相抵消) | 256(组成正确) |
| 当日采购账本 getPurchaseCostLedgerWindow | purchase 318 = self 159 + supplier 159 | 318 = self 159 + supplier 0 + stock_cover 159 |
改动(4 处 + 1 处必要伴随修复):
1. getOrderLinePurchaseTotal:purchase = self + supplier + stock_cover,并单独返回
purchase_stock_cover。它是看板 KPI 卡/看板毛利净利/财务概览的唯一采购来源 ⇒ 带动三面。
2. getCostAggregation:循环累计 stockCoverCost;毛利/毛利率基数 = purchaseTotal + stockCoverCost
(财务冻结条款:毛利率=(销售额−采购成本)÷销售额)。
3. purchaseCostBucketSql:purchaseCostBucketExpr 新增第三桶 inventory_cover => 'stock_cover',
放在最前(否则会先被"商品直送"/"验收 source_type=purchase => self"拦下或落进 ELSE 'supplier');
新增 purchaseStockCoverAmountSql;getPurchaseCostLedgerWindow 单独返回 purchase_stock_cover。
4. dashboardService:cost_details 补选 cost_source;采购类按 amount 取数(不认占位 allocated_amount);
过滤放行 inventory_cover(其余采购账本行照旧丢);实时采购补 salesVisibleOrderSql 独立进货守卫。
5. dashboardService.monthKeyOf(必要伴随修复):周期键由 String(date).slice(0,7)(得到 "Wed Oct")
改为 YYYY-MM,否则修好 1–4 后同一月的 97 与 159 会被拆成两个趋势点;并给实时采购行补 window_ymd
与订单侧同按 dateField 归月。
口径保留:purchase_self/purchase_supplier 仍是纯采购两支(抵扣单列);财务概览新增
product_cost_stock_cover 让拆分与总额自洽(前端未展示拆分字段,无文案改动);当日采购台账的
self 159(独立进货单自身)保留 —— 该函数口径是"当天花了多少钱买货",与订单口径不同属正常。
验收:
- 真库六面复测:采购 256 / 毛利·净利 226 / 毛利率 46.89% 一致;趋势 [256] 单桶;账本 self 159 + stock_cover 159;
- 契约测试:新增 purchaseCostThreePartsCaliber.test.ts(9 项跨面钉口径)+ purchaseCostBucketSql.test.ts
第三桶用例 + costAggregationIndependentInbound.test.ts 毛利基数用例(共 23 项全绿);
- 后端回归:0 新增失败(2953 passed / 32 failed,失败文件全在基线集合内);tsc 0 错误;
- 设计门禁:8 通过 / 2 既有红(check-ai-cost 长期红、verify-facts 缺本地 tenant_br003),与基线一致。
存量零数据变更(纯聚合口径)。设计:docs/design/20261014-订单成本排除独立进货与库存成本分类.md §6.9
86 lines
5.0 KiB
TypeScript
86 lines
5.0 KiB
TypeScript
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");
|
||
});
|
||
});
|