开发者 2026-10-14 定稿并下令「开始修改」:
「独立进货的相当于不能进入任何成本,他的成本是在正常的客户下单或者主系统来单被库存抵扣后进行计算的」
「当前的订单商品成本就是正确的展示方式,没有计算到独立进货的成本」。
## 改什么(`getCostAggregation`,净利/财务概览/看板成本 KPI/成本构成的唯一实现)
① 实时采购表达式加 `${salesVisibleOrderSql('o')}` —— 独立进货**自身**的行不进成本(与商品成本页同判据);
② 账本循环放行 `cost_source='inventory_cover'`(`cost_type='purchase'` 其余照旧跳过)——
抵扣成本要在"被消耗"时才认;跳过它 = 进货不认、消耗也不认 ⇒ 净利虚高;
`purchase_self/purchase_supplier` 仍是纯采购拆分(不混入库存成本,符合 §5.1)。
## 真库实测(BR002)
| 周期 | direct 改前 | direct 改后 | 说明 |
| MOBILE(主系统单) | ¥96 | **¥256** | = 采购 ¥97 + 库存 ¥159,与商品成本页 totals.total_cost **逐项相等** |
| 202610(独立进货单) | ¥159 | **¥0** | 毛利 ¥-159 → **¥0** |
「当日采购台账」getPurchaseCostLedgerWindow **保持不动**(那是"花了多少钱买货"的事实,独立进货确实是采购)。
契约测试 server/tests/costAggregationIndependentInbound.test.ts(3 项):实时排除、抵扣行放行、账本按类型×来源分组。
回归:后端 25 failed / 410 passed(与干净 HEAD 同集合,**无新增失败**);两侧 tsc 0 错。
47 lines
2.5 KiB
TypeScript
47 lines
2.5 KiB
TypeScript
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');
|
||
});
|
||
});
|