Files
SupplierSystem/server/tests/stockCoverSupplierValue.test.ts
T
Ubuntu cd7c29e708
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
feat(库存抵扣): 三张表定稿 + 部分抵扣的缺口**默认自采**(含补自采单)
开发者 2026-10-14 第二轮真机裁定:
「不需要展示缺口待采 就三个 库存抵扣成本 自采采购成本 供应商采购成本 然后分配过来的
 如果是部分抵扣 缺少的部分默认设置为自采 就行」。

- **表数量回到三张**:上一版试做的第四张表「缺口待采购」按本口径回退(分桶/类型/接口/三语文案一并回退,
  不留死代码);页面仍是 供应采购成本 / 自采采购成本 / 库存抵扣(`xl:grid-cols-3`)。
- **部分抵扣 ⇒ 缺口默认自采**(两处):
  ① `STOCK_COVER_SUPPLIER_SQL` 新增第二分支:抵扣量 > 0 且未抵满,且供应商为空/UNASSIGNED/残留标记
     ⇒ 写 `INTERNAL`;**已有真厂商码的行不动**(缺口仍交该厂商备货)。
  ② `orderAllocationService.ensureSelfPurchaseForCoverGap`:抵扣后在**同一事务**里为这些行补
     **自采采购单**(幂等:已有未完成采购单的行跳过)—— 否则行上写着自采、采购员待办里却没有它,
     缺口永远没人买。释放/归零路径传 `$2 = 0`,前两条分支都不命中(释放不会把行改成自采)。

真库事务内验证(BR002 `ORD_API_20261014_BR002_0001` 苹果行,验证后 ROLLBACK):
  全额抵扣(原厂商) ⇒ `STOCK_COVER`;部分抵扣(原厂商) ⇒ **不动**;
  部分抵扣(未分配) ⇒ **`INTERNAL`**;释放归零(原标记) ⇒ `UNASSIGNED`。

页面现状(同一单,三张表):自采采购成本 **采购 ¥96 / 库存 ¥159 / 单位成本 ¥3.00**(3 个商品);
库存抵扣表为空(该单是部分抵扣);页头合计 ¥255 = 85 斤 × ¥3 ⇒ 与「采购+库存=商品成本」逐项相等。
本地测试库已按新规则把该单三行补成自采,便于继续测试;再点「分配」会自动补出三张自采单。

回归:后端 25 failed / 407 passed(干净 HEAD 同集合,**无新增失败**);前端 2 failed / 163 passed(同基线);
两侧 `tsc` 0 错;门禁 8 通过 / 1 已知红 / 1 未执行。
2026-10-14 18:35:30 +08:00

151 lines
8.1 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';
import {
STOCK_COVER_SUPPLIER_CODE,
STOCK_COVER_SUPPLIER_NAME,
NON_VENDOR_SUPPLIER_SQL_LIST,
hasRealSupplierCode,
isStockCoverSupplierCode,
INTERNAL_PURCHASER_CODE,
} from '../src/services/allocationRules';
/**
* **订单行供应商第三个取值「库存抵扣」**(2026-10-14 开发者立项,见
* docs/design/20261014-订单成本排除独立进货与库存成本分类.md §6.1)。
*
* 本文件钉两件事:
* ① 取值本体:`STOCK_COVER` 是**非厂商**(`hasRealSupplierCode` 为 false);
* ② 落点清单:写入点、8 处归零点、商品成本第三桶、以及**每一个"找厂商"的落点**都认这个值。
* 这些落点漏一个就会出现"给不存在的厂商建备货单/验收单/应付"或"标记被静默改写"。
*/
const read = (rel: string) => readFileSync(new URL(rel, import.meta.url), 'utf8');
describe('库存抵扣取值本体', () => {
it('常量与三个判据', () => {
expect(STOCK_COVER_SUPPLIER_CODE).toBe('STOCK_COVER');
expect(STOCK_COVER_SUPPLIER_NAME).toBe('库存抵扣');
expect(isStockCoverSupplierCode('stock_cover')).toBe(true);
expect(isStockCoverSupplierCode(' STOCK_COVER ')).toBe(true);
expect(isStockCoverSupplierCode('S1')).toBe(false);
expect(isStockCoverSupplierCode(null)).toBe(false);
// 非厂商:空码 / 自采 / 未分配 / 库存抵扣
expect(hasRealSupplierCode('S1')).toBe(true);
expect(hasRealSupplierCode(STOCK_COVER_SUPPLIER_CODE)).toBe(false);
expect(hasRealSupplierCode(INTERNAL_PURCHASER_CODE)).toBe(false);
expect(hasRealSupplierCode('UNASSIGNED')).toBe(false);
expect(hasRealSupplierCode(null)).toBe(false);
// SQL 取值列表含三个非厂商值
expect(NON_VENDOR_SUPPLIER_SQL_LIST).toBe("('INTERNAL', 'UNASSIGNED', 'STOCK_COVER')");
});
});
describe('写入点与 8 处归零点共用同一片段', () => {
const cover = read('../src/services/inventoryStockCoverService.ts');
it('片段:全额抵扣打标、**部分抵扣缺口默认自采**、残标记回退未分配', () => {
expect(cover).toContain('export const STOCK_COVER_SUPPLIER_SQL');
expect(cover).toContain("THEN '${STOCK_COVER_SUPPLIER_CODE}'");
expect(cover).toContain("THEN 'UNASSIGNED'");
// 开发者 2026-10-14 真机测试裁定:「分配过来的如果是部分抵扣,缺少的部分默认设置为自采」
expect(cover).toContain("THEN '${INTERNAL_PURCHASER_CODE}'");
expect(cover).toContain("UPPER(TRIM(supplier_code)) IN ('UNASSIGNED', '${STOCK_COVER_SUPPLIER_CODE}')");
// 归零路径传 $2 = 0 ⇒ 上面两条都不会命中(释放不会把行改成自采)
expect(cover).toContain('[orderItemId, 0]');
});
it('7 处归零点 + 主写入点全部挂该片段(漏一处会留脏标记)', () => {
const hits = cover.match(/SET stock_cover_qty = \$2, \$\{STOCK_COVER_SUPPLIER_SQL\}/g) || [];
expect(hits.length, '本文件内 7 处(含主写入点)').toBe(7);
// 第 8 处:补货服务(清抵扣标记时同步回退供应商)
const replenish = read('../src/services/inventoryReservationReplenishService.ts');
expect(replenish).toContain('STOCK_COVER_SUPPLIER_SQL');
expect(replenish).toContain('[orderItemId, 0]');
});
it('不再有"只把 stock_cover_qty 清零、不动供应商"的写法', () => {
expect(cover).not.toContain('SET stock_cover_qty = 0 WHERE id = $1::uuid');
expect(cover).not.toContain('SET stock_cover_qty = 0 WHERE order_code = $1');
});
});
describe('商品成本第三桶(页面 + 导出)', () => {
const margin = read('../src/services/marginQueryService.ts');
it('分桶:标记值 或 全额抵扣事实,且抵扣量必须 > 0', () => {
const helper = margin.slice(
margin.indexOf('function skuListDisplayCostBucketSql'),
margin.indexOf('export const CD_POSTED_STATUS_SQL')
);
expect(helper).toContain("= '${STOCK_COVER_SUPPLIER_CODE}'");
expect(helper).toContain("THEN 'stock_cover'");
// 全额抵扣的历史行无需回填也能进第三桶
expect(helper).toContain('COALESCE(oi.stock_cover_qty, 0) >= COALESCE(oi.quantity, 0) - 1e-9');
// 释放后残留的标记落自采表,不进第三桶
expect(helper).toContain(
"WHEN UPPER(TRIM(COALESCE(oi.supplier_code, ''))) = '${STOCK_COVER_SUPPLIER_CODE}' THEN 'self'"
);
});
it('来源过滤与 DTO 都认 stock_cover', () => {
expect(margin).toContain("export type SkuCostSourceFilter = 'self' | 'supplier' | 'stock_cover';");
expect(margin).toContain("costSource === 'self' || costSource === 'supplier' || costSource === 'stock_cover'");
expect(margin).toContain("sourceFilter === 'stock_cover' ? \"AND sc.cost_bucket = 'stock_cover'\" : ''");
expect(margin).toContain("? `AND line.cost_bucket = 'stock_cover'`");
expect(margin).toContain("bucket === 'stock_cover' ? 'stock_cover' : 'self'");
});
it('路由层放行 stock_cover', () => {
const routes = read('../src/modules/workflow/routes/financeRoutes.ts');
const hits = routes.match(/rawSource === 'stock_cover'/g) || [];
expect(hits.length, '列表页与导出明细两个接口各一处').toBe(2);
});
});
describe('「找厂商」的落点必须用非厂商判据(漏一处即造脏数据)', () => {
it('① 分配:pending 过滤与结果落库安全网都跳 STOCK_COVER', () => {
const svc = read('../src/services/orderAllocationService.ts');
const hits = svc.match(/isStockCoverSupplierCode\(/g) || [];
expect(hits.length).toBeGreaterThanOrEqual(2);
expect(svc).toContain('hasRealSupplierCode(item.supplier_code)');
expect(svc).toContain('hasRealSupplierCode(supplierCode)');
});
it('② 转供应商:STOCK_COVER 进黑名单(否则静默建 STOCK_COVER 备货)', () => {
const svc = read('../src/services/purchaseSupplierAssignService.ts');
expect(svc).toContain('STOCK_COVER_SUPPLIER_CODE,');
expect(svc).toContain('!BLOCKED_SUPPLIER.has(normalized)');
});
it('③ 管理明细:加菜补建备货与"已选外部厂商"都排除它', () => {
const svc = read('../src/services/orderManageItemsService.ts');
expect(svc).toContain('isSelfPurchaseSupplier(row.supplier_code) || isStockCoverSupplierCode(row.supplier_code)');
expect(svc).toContain('!isSelfPurchaseSupplier(code) && !isStockCoverSupplierCode(code)');
expect(svc).toContain('!isStockCoverSupplierCode(supplierCode)');
});
it('④ 竞价:指定供应商判定排除它,且回写不覆盖它(两处相反需求分开处置)', () => {
const svc = read('../src/services/bidRoundService.ts');
expect(svc).toContain('return hasRealSupplierCode(code);');
expect(svc).toContain('if (isStockCoverSupplierCode(item.supplier_code)) continue;');
});
it('⑤ 结算/应付/成本/退货/验收/备货/打印各方一致', () => {
expect(read('../src/services/financeReconciliationService.ts')).toContain(
'UPPER(TRIM(oi.supplier_code)) NOT IN ${NON_VENDOR_SUPPLIER_SQL_LIST}'
);
expect(read('../src/services/missingPurchaseCostBackfill.ts')).toContain('NON_VENDOR_SUPPLIER_SQL_LIST');
expect(read('../src/services/preparationFulfillmentService.ts')).toContain('NON_VENDOR_SUPPLIER_SQL_LIST');
expect(read('../src/routes/mobile/mobilePurchaseRoutes.ts')).toContain('NON_VENDOR_SUPPLIER_SQL_LIST');
expect(read('../src/services/orderCostPostingService.ts')).toContain('!hasRealSupplierCode(supplierCode)');
expect(read('../src/services/orderItemReturnService.ts')).toContain('if (!hasRealSupplierCode(supplierCode)) return null;');
expect(read('../src/services/pendingBackfillQtyService.ts')).toContain(
'const isSelfPurchase = !hasRealSupplierCode(supplierCode);'
);
expect(read('../src/services/inboundFulfillmentReattributeService.ts')).toContain(
'return !hasRealSupplierCode(supplierCode);'
);
expect(read('../src/services/skuPreferredSupplierService.ts')).toContain('if (!hasRealSupplierCode(supplierCode))');
expect(read('../src/services/deliveryPrintLineService.ts')).toContain('${STOCK_COVER_SUPPLIER_NAME}');
});
});