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;'); }); /** * **明细页必须出现「库存抵扣成本」**(开发者 2026-10-14 报障:「明细这块 为什么全是采购成本 * 其中部分不是库存抵扣成本吗」)。 * * `listOrderCostDetailsInWindow` 的账本分支原来写着 `cd.cost_type NOT IN ('purchase','purchase_applied')` * —— 抵扣行的 cost_type 也是 `purchase`,被整类丢掉 ⇒ 明细里**一行抵扣成本都没有** * (实测 BR002:只有缺口 ¥97,抵扣 ¥159 不见),而成本页总成本是 ¥256。现在放行 * `cost_source='inventory_cover'`,前端按该字段把它显示成「库存抵扣成本」。 */ it('⑤ 订单成本明细放行库存抵扣账本行(否则明细里看不到抵扣成本)', () => { const start = src.indexOf('export async function listOrderCostDetailsInWindow'); expect(start).toBeGreaterThan(-1); const next = src.indexOf('export async function', start + 10); const fn = src.slice(start, next > start ? next : start + 12000); expect(fn).toContain("cd.cost_type NOT IN ('purchase', 'purchase_applied')"); expect(fn).toContain("OR COALESCE(cd.cost_source, '') = 'inventory_cover'"); // 实时订单行分支同样要排除独立进货单自身(否则同一批货出现两次:独立进货行 + 抵扣账本行) expect(fn).toContain("AND ${salesVisibleOrderSql('o')}"); // 不得回退成无条件丢采购行 expect(fn).not.toContain("AND cd.cost_type NOT IN ('purchase', 'purchase_applied')\n"); }); });