import { readFileSync } from 'node:fs'; import { describe, expect, it } from 'vitest'; describe('商品成本列表采购金额口径(缺口量 × unit_cost + 账本库存成本,2026-10-14)', () => { const src = readFileSync(new URL('../src/services/marginQueryService.ts', import.meta.url), 'utf8'); const start = src.indexOf('export async function getSkuDirectCostsPaged'); const next = src.indexOf('export async function getMarginByDimension', start + 1); const fn = src.slice(start, next > 0 ? next : start + 20000); const unifiedStart = src.indexOf('export async function getUnifiedCostWindow'); const unified = src.slice(unifiedStart, unifiedStart + 3500); const aggStart = src.indexOf('export async function getCostAggregation'); const agg = src.slice(aggStart, src.indexOf('export async function listCostDetailsInWindow')); it('sku list amount uses current quantity × unit_cost, not ledger amount', () => { // 2026-10-14:口径已是「缺口量 × unit_cost」(全量常量 ORDER_ITEM_CACHED_COST_AMOUNT_SQL 已删, // 见 docs/design/20261014-订单成本排除独立进货与库存成本分类.md §5.1) expect(fn).toContain('COALESCE(oi.unit_cost, 0)'); expect(fn).toContain('skuListDisplayCostBucketSql'); expect(fn).toContain('FROM order_items oi'); expect(fn).not.toContain('CD_EFFECTIVE_AMOUNT_SQL'); expect(fn).not.toContain('order_item_cost'); }); it('display bucket is only order-line supplier_code, not category or ledger', () => { const helperStart = src.indexOf('function skuListDisplayCostBucketSql'); const helper = src.slice(helperStart, src.indexOf('export const CD_POSTED_STATUS_SQL')); expect(helper).toContain("IN ('INTERNAL', 'UNASSIGNED')"); expect(helper).toContain("NULLIF(TRIM(COALESCE(oi.supplier_code, '')), '') IS NULL"); expect(helper).toContain("ELSE 'supplier'"); expect(helper).not.toContain('purchaseCostBucketExpr'); expect(helper).not.toContain('purchase_orders'); expect(helper).not.toContain('is_extra_dish'); expect(helper).not.toContain('category_name'); // 20260914:仓内加工行单独成行(不与自采合并数量),见 // docs/design/20260914-sku-cost-warehouse-processing-separate-row.md expect(fn).toContain('GROUP BY sku_code, is_extra_dish, cost_bucket, is_warehouse_processing'); // 仓内加工行成本/金额一律按 0 计(含历史未归零行) expect(fn).toContain('CASE WHEN sc.is_warehouse_processing THEN 0 ELSE sc.self_cost END'); }); it('aggregation and unified window do not use list display CASE', () => { expect(agg).not.toContain('skuListDisplayCostBucketSql'); expect(unified).not.toContain('skuListDisplayCostBucketSql'); expect(agg).toContain('purchaseCostBucketExpr'); }); it('unit cost divides by planned quantity', () => { expect(fn).toContain('unit_cost: quantity > 0 ? round2(totalCost / quantity) : 0'); expect(fn).not.toContain('totalCost / fulfilledQty'); }); /** * **2026-10-14 口径变更**(开发者定稿,见 * docs/design/20261014-订单成本排除独立进货与库存成本分类.md §5.1): * 采购金额由 **全量 `quantity × unit_cost`** 改为 **缺口口径** `(quantity − stock_cover_qty) × unit_cost` * —— 部分抵扣的行(30 斤里抵 20、采 10)原式会把已抵扣那部分也按缺口价算成采购成本(多报); * 抵扣那部分的成本另由新增的 `stock_cover_cost`(账本 `cost_details.inventory_cover`)承担, * 两者相加 = 行真实成本,`unit_cost` = 两者相加 ÷ 销量。 */ it('按来源拆段:抵扣段(采购 0 + 库存成本)与缺口段(缺口×单价 + 库存 0)各自成行', () => { // 2026-10-14 真机裁定:「库存抵扣生效了,为什么抵扣数量没有成本展示、全给到自采」 // ⇒ 一行拆两段,见 docs/design/20261014-订单成本排除独立进货与库存成本分类.md §6.4 expect(fn).toContain('src_line_base AS ('); expect(fn).toContain("'stock_cover' AS cost_bucket"); expect(fn).toContain('cover_share AS stock_cover_cost'); expect(fn).toContain('(GREATEST(0, full_qty - cover_qty) * unit_cost)::numeric AS amount'); // 销售额只记一次:有缺口记在缺口段,全额抵扣记在抵扣段 expect(fn).toContain('CASE WHEN full_qty - cover_qty > 1e-9 THEN 0 ELSE line_sales END AS line_sales'); // 账本抵扣成本按 (订单×SKU) 汇总后分摊 expect(fn).toContain('cost_source = ' + "'inventory_cover'"); // 旧口径不得再当采购金额 expect(fn).not.toContain('${ORDER_ITEM_CACHED_COST_AMOUNT_SQL}::numeric AS amount'); }); it('lists zero-cost SKUs instead of dropping them (20260923-sku-cost-list-include-zero)', () => { expect(fn).not.toContain('sc.self_cost > 0'); expect(fn).not.toContain('sc.supplier_cost > 0'); expect(fn).not.toContain('purchase_cost > 0'); expect(fn).toContain("AND sc.cost_bucket = 'self'"); expect(fn).toContain("AND sc.cost_bucket = 'supplier'"); expect(fn).toContain("AND line.cost_bucket = 'self'"); expect(fn).toContain("AND line.cost_bucket = 'supplier'"); }); it('purchase rows ignore allocated_amount on the shared read path', () => { const helperStart = src.indexOf('const CD_EFFECTIVE_AMOUNT_SQL'); const helper = src.slice(helperStart, helperStart + 200); expect(helper).toContain('costEffectiveAmountSql'); const shared = readFileSync( new URL('../src/services/purchaseCostBucketSql.ts', import.meta.url), 'utf8' ); const sharedStart = shared.indexOf('export function costEffectiveAmountSql'); const sharedSql = shared.slice(sharedStart, sharedStart + 400); expect(sharedSql).toContain("WHEN ${cd}.cost_type IN ('purchase', 'purchase_applied')"); expect(sharedSql).toContain('THEN COALESCE(${cd}.amount, 0)'); expect(sharedSql).toContain('ELSE COALESCE(${cd}.allocated_amount, ${cd}.amount, 0)'); }); it('unified window and aggregation still sum cost_details', () => { expect(unified).toContain('FROM cost_details cd'); expect(unified).toContain('CD_EFFECTIVE_AMOUNT_SQL'); expect(agg).toContain('FROM cost_details cd'); expect(agg).toContain('CD_EFFECTIVE_AMOUNT_SQL'); expect(agg).toContain('purchase_self'); expect(agg).toContain('excludePurchaseDupesForKpiSql'); expect(unified).toContain('excludePurchaseDupesForKpiSql'); }); });