Files
SupplierSystem/server/tests/costAggregationIndependentInbound.test.ts
T
Ubuntu 260054507c
CI / server typecheck + tests (push) Canceled after 0s
CI / web typecheck + tests + miniprogram syntax (push) Canceled after 0s
CI / eslint (观察项,C7 骨架) (push) Canceled after 0s
CI / npm audit (critical 硬拦) (push) Canceled after 0s
fix(财务): 采购成本口径统一为「自采 + 供应商 + 库存抵扣」,修五个面少算/错记
审计(只读,真库 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
2026-10-14 20:46:59 +08:00

62 lines
3.3 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { readFileSync } from 'node:fs';
import { describe, expect, it } from 'vitest';
/**
* **独立进货不进任何成本面;被抵扣时才认成本**(开发者 2026-10-14 定稿)。
*
* 开发者原话:「独立进货的相当于不能进入任何成本,他的成本是在正常的客户下单或者主系统来单
* 被库存抵扣后进行计算的」「当前的订单商品成本就是正确的展示方式,没有计算到独立进货的成本」。
*
* ⇒ `getCostAggregation`(净利/成本构成)必须与「订单商品成本」页同口径:
* ① 实时采购表达式**排除独立进货单自身的行**(复用销售侧 salesVisibleOrderSql);
* ② 账本里 `cost_source='inventory_cover'` 的**抵扣行要计入 direct**(它是"被消耗时才认"的那笔),
* 不能跟着 `cost_type='purchase'` 一起被跳过。
*
* 真库实测(BR002):MOBILE direct ¥96 → **¥256**(= 采购 ¥97 + 库存 ¥159,与商品成本页
* `totals.total_cost` 逐项相等);202610(独立进货单)direct ¥159 → **¥0**(毛利 ¥-159 → ¥0)。
*/
const src = readFileSync(new URL('../src/services/marginQueryService.ts', import.meta.url), 'utf8');
function aggregationFn(): string {
const start = src.indexOf('export async function getCostAggregation');
const next = src.indexOf('export async function listCostDetailsInWindow', start + 1);
return src.slice(start, next > start ? next : start + 12000);
}
describe('净利/成本构成:独立进货不认自身成本,认被抵扣成本', () => {
it('① 实时采购排除独立进货单(与商品成本页同一判据)', () => {
const fn = aggregationFn();
expect(fn).toContain("AND ${salesVisibleOrderSql('o')}");
expect(src).toContain("import { salesVisibleOrderSql } from './independentInbound';");
});
it('② 库存抵扣账本行放行计入 direct(其余采购行照旧跳过,避免重复计)', () => {
const fn = aggregationFn();
expect(fn).toContain("const isInventoryCover = String(row.cost_source || '') === 'inventory_cover';");
expect(fn).toContain(
'if (CD_PURCHASE_TYPES_SQL.includes(type) && !isInventoryCover) continue;'
);
});
it('③ 账本查询按 类型×来源 分组(否则分不清抵扣行)', () => {
const fn = aggregationFn();
expect(fn).toContain("COALESCE(cd.cost_source, '') AS cost_source");
expect(fn).toContain('GROUP BY cd.cost_type, cd.cost_source');
});
/**
* 2026-10-14 口径定稿:**采购成本 = 自采 + 供应商 + 库存抵扣**,毛利/毛利率的基数就是它。
*
* 改前用 `purchaseTotal`(只有两支)⇒ 同一函数的「总成本」已含抵扣、毛利却不含,
* 卡片自己和自己对不上(实测 BR002:总成本 ¥256 / 毛利 ¥385,而 482−256=226)。
*/
it('④ 毛利/毛利率的基数含库存抵扣(销售额 − 采购成本)', () => {
const fn = aggregationFn();
expect(fn).toContain('if (isInventoryCover) stockCoverCost += amt;');
expect(fn).toContain('const purchaseCostAll = round2(purchaseTotal + stockCoverCost);');
expect(fn).toContain('const quoteProfit = revenue - purchaseCostAll;');
// 不得回退成只用两支的旧写法
expect(fn).not.toContain('const quoteProfit = revenue - purchaseTotal;');
});
});