审计(只读,真库 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
80 lines
3.3 KiB
TypeScript
80 lines
3.3 KiB
TypeScript
import { describe, expect, it } from 'vitest';
|
||
import {
|
||
purchaseCostBucketExpr,
|
||
purchaseSelfAmountSql,
|
||
purchaseSupplierAmountSql,
|
||
purchaseStockCoverAmountSql,
|
||
excludePurchaseDupesForKpiSql,
|
||
} from '../src/services/purchaseCostBucketSql';
|
||
|
||
describe('purchaseCostBucketSql', () => {
|
||
/**
|
||
* 第三桶(2026-10-14 口径定稿:**采购成本 = 自采 + 供应商 + 库存抵扣**)。
|
||
* 改前抵扣行落到 `ELSE 'supplier'` ⇒ 被整笔记成「供应商采购成本」(实测 BR002 虚增 ¥159)。
|
||
*/
|
||
it('把库存抵扣行单独归成 stock_cover(且必须在最前,避免被直送/验收分支先拦下)', () => {
|
||
const expr = purchaseCostBucketExpr('cd');
|
||
expect(expr).toContain("WHEN cd.cost_source = 'inventory_cover' THEN 'stock_cover'");
|
||
// 必须排在其它 WHEN(extradish/直送/验收/加工/单位成本同步)之前 —— 抢先判,避免落到 ELSE 'supplier'
|
||
const idxStockCover = expr.indexOf("WHEN cd.cost_source = 'inventory_cover' THEN 'stock_cover'");
|
||
const idxExtraDish = expr.indexOf("WHEN cd.cost_subtype = 'extra_dish'");
|
||
expect(idxStockCover).toBeGreaterThan(-1);
|
||
expect(idxStockCover).toBeLessThan(idxExtraDish);
|
||
});
|
||
|
||
it('builds the stock_cover amount wrapper', () => {
|
||
expect(purchaseStockCoverAmountSql('amt')).toContain("= 'stock_cover'");
|
||
});
|
||
|
||
it('treats explicit self_purchase / extra_dish as self', () => {
|
||
const expr = purchaseCostBucketExpr('cd');
|
||
expect(expr).toContain("'self_purchase', 'extra_dish'");
|
||
expect(expr).toContain("THEN 'self'");
|
||
});
|
||
|
||
it('forces sku direct delivery to self', () => {
|
||
const expr = purchaseCostBucketExpr('cd');
|
||
expect(expr).toContain('sku_direct_delivery_customers');
|
||
expect(expr).toContain('direct_delivery');
|
||
});
|
||
|
||
it('splits processing finished by processing_self / processing_supplier', () => {
|
||
const expr = purchaseCostBucketExpr('cd');
|
||
expect(expr).toContain('processing_finished');
|
||
expect(expr).toContain('processing_self');
|
||
expect(expr).toContain('processing_supplier');
|
||
expect(expr).toContain("WHEN cd.cost_source IN ('processing_supplier', 'supplier') THEN 'supplier'");
|
||
});
|
||
|
||
it('guards unit_cost_sync against sibling formal purchase rows', () => {
|
||
const expr = purchaseCostBucketExpr('cd');
|
||
expect(expr).toContain('unit_cost_sync');
|
||
expect(expr).toContain('purchase_orders');
|
||
expect(expr).toContain('o.id IS DISTINCT FROM');
|
||
});
|
||
|
||
it('does not blindly reclassify any row that merely has a PO', () => {
|
||
const expr = purchaseCostBucketExpr('cd');
|
||
expect(expr).toContain("COALESCE(cd.remark, '') = 'unit_cost_sync'");
|
||
});
|
||
|
||
it('builds self/supplier amount wrappers', () => {
|
||
expect(purchaseSelfAmountSql('amt')).toContain("= 'self'");
|
||
expect(purchaseSupplierAmountSql('amt')).toContain("= 'supplier'");
|
||
});
|
||
|
||
it('KPI excludes inspection_inbound when sibling order_goods exists', () => {
|
||
const sql = excludePurchaseDupesForKpiSql('cd');
|
||
expect(sql).toContain('inspection_inbound');
|
||
expect(sql).toContain('order_goods');
|
||
expect(sql).toContain('unit_cost_sync');
|
||
});
|
||
|
||
it('KPI excludes processing ledger rows (processing price is not posted)', () => {
|
||
const sql = excludePurchaseDupesForKpiSql('cd');
|
||
expect(sql).toContain('processing_finished');
|
||
expect(sql).toContain('processing_loss');
|
||
expect(sql).toContain('processing_self');
|
||
});
|
||
});
|