Files
SupplierSystem/server/tests/supplierSkipInbound.test.ts
Ubuntu bf94aa0a1f
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(订单成本口径): C 块 —— 供应商第三个分类「库存抵扣」(写入点+8处归零+第三张表+全域非厂商守卫)
开发者 2026-10-14:「开始做 C 块;文案你自己编写;布局自行调整」,并确认
「独立进货就是一个客户、这个客户就是他自己、相当于补充库存」、
「订单管理列表就可以不用隐藏独立进货单了」(后者本轮未动)。
设计文 docs/design/20261014-订单成本排除独立进货与库存成本分类.md §6.1(本轮新增)。

## 取值与写入
- 新增 `STOCK_COVER_SUPPLIER_CODE='STOCK_COVER'` / 名称「库存抵扣」(服务端 + 前端 allocationRules 同名常量)。
- `applyStockCoverForOrderItem`(唯一把 stock_cover_qty 写大的入口)在**同一条 UPDATE**里维护标记:
  全额抵扣 ⇒ `supplier_code='STOCK_COVER'`;部分抵扣 ⇒ **不动**(缺口仍要厂商/自采,§5.1);
  释放/归零且原标记是 STOCK_COVER ⇒ 回退 `UNASSIGNED`。片段 `STOCK_COVER_SUPPLIER_SQL` 与
  **8 处归零点**共用(本文件 7 处 + inventoryReservationReplenishService 1 处),漏一处即留脏标记。

## 商品成本第三张表(页面 + 导出)
- 分桶取「或」:写入点打的标记 **或** 全额抵扣事实(`stock_cover_qty >= quantity`),且要求 `stock_cover_qty > 0`
  ⇒ **历史全额抵扣行无需回填**即进第三桶;释放后残留标记落自采表。
- `SkuCostSourceFilter` / 路由 / 列表与导出两个接口都放行 `stock_cover`;第三张表 `xl:grid-cols-3`、
  标题「库存抵扣」、表头「库存成本合计 ¥x · n 个商品」(该表采购成本恒 0)。
- 订单详情供应商下拉新增第三项「库存抵扣」;订单列表/订单导出/打印供货清单都不再出现裸码或空白。

## 全域「非厂商」守卫(R1 排查,新增 hasRealSupplierCode + NON_VENDOR_SUPPLIER_SQL_LIST)
- 会**静默造脏数据/抹掉标记**的三处最要命:`orderAllocationService` 结果落库安全网与 pending 过滤、
  `purchaseSupplierAssignService` 转供应商黑名单(原本不传 supplier_code 会静默建 STOCK_COVER 备货)、
  `orderManageItemsService` 加菜裸 INSERT 备货与 `isAssignedExternalSupplier`(否则保存会取消在途自采+删分拣行)。
- 其余 A 清单:竞价 `isRealBidSupplier` + 回写额外再挡一次、结算/应付×3、成本来源标签、退货红冲、
  验收单/再归属、备货成本回填、待备货/采购列表、商品指定供应商(后端拒 + 前端候选滤)。
- **B 类一行不动**(`isUnassignedSupplier` 系列):对"是否还要再分配"而言「已有供应商」就是对的。

## 实测(真库 BR002/BR003,服务函数直调,见 §6.1.5)
| 分支/周期 | 供应采购 | 自采采购 | 库存抵扣 |
| BR002 202610 | 0 行 ¥0 | 0 行 ¥0 | **3 行 / ¥137** |
| BR002 MOBILE | 0 行 ¥0 | 6 行 / ¥187 | **3 行 / ¥54** |
| BR003 MOBILE | 0 行 ¥0 | 4 行 / ¥61.8 | **8 行 / ¥942.2** |
三张表行数 = 表头合计,逐项对账相等;导出按来源筛同样落第三桶。
标记 SQL 语义在真库事务内验证并 ROLLBACK:全额→STOCK_COVER;部分(原标记)→UNASSIGNED;
部分(原厂商)→不动;释放归零→UNASSIGNED。

## 回归
- 后端 25 failed / 407 passed(干净 HEAD 890c055 为 27/396)⇒ **无新增失败**,另 2 个文件转好;
- 前端 2 failed / 163 passed(与基线同集合:SystemConfig.allowZeroCost、PickingManagement.outboundPhotoPreview);
- `tsc --noEmit`(前端)与 `tsc --noEmit -p server/tsconfig.json --module esnext` 均 0 错;
- 门禁 8 通过 / 1 已知红(check-ai-cost) / 1 未执行(verify-facts)。

## 未做(需裁决,见 §8 D 组)
D1 实时采购成本分桶(财务冻结)、D2 发车/入库照 skip_inbound(配送冻结)、
D3 改派真厂商的取消在途(分拣冻结)、D4 退货改派批次对齐、D5 小程序订单详情同改。
2026-10-14 17:32:29 +08:00

206 lines
12 KiB
TypeScript
Raw Permalink 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 'fs';
import { describe, expect, it } from 'vitest';
const prep = readFileSync(new URL('../src/services/preparationFulfillmentService.ts', import.meta.url), 'utf8');
const picking = readFileSync(new URL('../src/services/pickingRouteSortService.ts', import.meta.url), 'utf8');
const loading = readFileSync(new URL('../src/services/deliveryLoadingService.ts', import.meta.url), 'utf8');
const tasks = readFileSync(new URL('../src/services/deliveryTaskQueryService.ts', import.meta.url), 'utf8');
const outbound = readFileSync(new URL('../src/services/inventoryOutboundService.ts', import.meta.url), 'utf8');
const items = readFileSync(new URL('../src/services/orderManageItemsService.ts', import.meta.url), 'utf8');
const skip = readFileSync(new URL('../src/services/supplierSkipInbound.ts', import.meta.url), 'utf8');
const earlyLoad = readFileSync(new URL('../src/services/skipInboundLoadingService.ts', import.meta.url), 'utf8');
const fulfill = readFileSync(new URL('../src/services/orderWorkflowFulfillmentService.ts', import.meta.url), 'utf8');
const mobile = readFileSync(
new URL('../src/routes/mobile/mobileDeliveryRoutes.ts', import.meta.url),
'utf8'
);
describe('供应商直送免入库(Node)', () => {
it('备货完成不建验收;全直送 30→50;混单仍要求仓内验收', () => {
expect(prep).toContain('skipInboundSuppliers.has');
expect(prep).toContain('skipInboundItemSuppliers.has');
expect(prep).toContain('ensureSkipInboundLoadingLines');
expect(prep).toContain('need_inbound_count');
expect(prep).toContain("SET order_status = 50 WHERE order_code = $1 AND order_status = 30");
expect(prep).toContain('AND NOT ${supplierSkipInboundExistsSql(\'oi.supplier_code\')}');
expect(prep).toContain('needInboundCount > 0 && inspectionCount === 0 && created === 0');
});
it('分拣建单排除直送行;提前补已完成核货行', () => {
expect(picking).toContain("AND NOT ${supplierSkipInboundExistsSql('oi.supplier_code')}");
expect(earlyLoad).toContain('pick_status, branch_code, pick_completed_at');
expect(earlyLoad).toContain('VALUES ($1, $2, NULL, 40, $3, NOW())');
});
it('配送列表提前可见;核货可称重;发车要求 order_status >= 50', () => {
expect(tasks).toContain("orderVisibleToDeliverySql('o')");
expect(skip).toContain('COALESCE(${orderAlias}.order_status, 0) >= 50');
expect(loading).toContain('qty_editable: skipInbound || qtyEditEnabled');
expect(loading).toContain('weigh_editable:');
expect(loading).toContain('qtyEditEnabled ? false : weighLocked');
expect(loading).toContain('orderStatus >= 50');
expect(loading).toContain(
'needs_loading_photo: skipInbound || extraDish || (categoryRequiresPhoto && !hasInboundPhoto)'
);
expect(loading).toContain('isDeliveryLoadingQtyEditEnabled');
expect(loading).toContain('实配数量须大于 0');
expect(loading).toContain('applySkipInboundSettlementForOrder');
expect(mobile).toContain('operatorId: req.userId || null');
expect(fulfill).toContain('applySkipInboundSettlementForOrder');
expect(fulfill).toContain('countWarehouseItemsMissingPicking');
expect(fulfill).toContain('直送核货单不算仓内分拣完成');
// 免入库 / 直送 / 「配送直供」(极简流转)/ 加菜 都不参与扣库:
// 这些货从未入库、没有可扣的库存批次,不跳就会抛「库存不足」
expect(outbound).toMatch(/if \(skipInbound \|\| skuDirect \|\| directDeliveryCover \|\| extraDish\) continue;/);
// 结案/回冲扣库计划同样豁免直送免入库行(否则全直送订单结案必报扣库失败)
const planStart = outbound.indexOf('async function planDeductLinesForOrder');
const planSrc = outbound.slice(planStart, planStart + 3500);
expect(planSrc).toContain('skip_inbound');
expect(planSrc).toContain('skip_sup');
});
it('直送供应商禁止仓内加工保存', () => {
expect(items).toContain('skip_inbound_no_warehouse_processing');
expect(items).toContain('供应商已开启不入仓');
});
it('这家供应商备货完成即建核货行,不等整单齐套', () => {
const prepStatus = readFileSync(
new URL('../src/services/supplierPreparationStatusService.ts', import.meta.url),
'utf8'
);
expect(prepStatus).toContain('ensureSkipInboundLoadingLines');
expect(prepStatus).toContain('isSupplierSkipInbound');
const afterFn = prepStatus.slice(prepStatus.indexOf('async function afterPreparationCompleted'));
expect(afterFn.indexOf('ensureSkipInboundLoadingLines')).toBeLessThan(
afterFn.indexOf('createSupplierInspectionIfNeeded')
);
});
it('商品直送未命中客户时,供应商不入仓仍建核货行(禁止只看 SKU 开关排除)', () => {
expect(earlyLoad).toContain("skuDirectDeliveryAppliesSql('oi.sku_code', 'oi.order_code')");
expect(earlyLoad).toContain("supplierSkipInboundExistsSql('oi.supplier_code')");
expect(earlyLoad).not.toContain('COALESCE(sku.direct_delivery');
expect(earlyLoad).not.toContain('COALESCE(s.direct_delivery');
expect(loading).toContain("skuDirectDeliveryAppliesSql('oi.sku_code'");
expect(loading).not.toContain('AND COALESCE(s.direct_delivery, false) = false');
const prepStatus = readFileSync(
new URL('../src/services/supplierPreparationStatusService.ts', import.meta.url),
'utf8'
);
const afterFn = prepStatus.slice(prepStatus.indexOf('async function afterPreparationCompleted'));
const skuDirectIf = afterFn.indexOf('if (skuDirect && prep.order_code)');
const skipInboundIf = afterFn.indexOf('if (skipInbound && prep.order_code)');
expect(skuDirectIf).toBeGreaterThan(-1);
expect(skipInboundIf).toBeGreaterThan(skuDirectIf);
expect(afterFn.slice(skuDirectIf, skipInboundIf)).toContain('return;');
expect(afterFn.slice(skuDirectIf, skipInboundIf)).not.toContain('ensureSkipInboundLoadingLines');
expect(afterFn.indexOf('ensureSkipInboundLoadingLines', skipInboundIf)).toBeGreaterThan(skipInboundIf);
});
it('供应商不入仓关闭时仍走仓内验收(不因 SKU 开了直送但客户未命中而免仓)', () => {
const prepStatus = readFileSync(
new URL('../src/services/supplierPreparationStatusService.ts', import.meta.url),
'utf8'
);
const afterFn = prepStatus.slice(prepStatus.indexOf('async function afterPreparationCompleted'));
const skipInboundIf = afterFn.indexOf('if (skipInbound && prep.order_code)');
const inspect = afterFn.indexOf('createSupplierInspectionIfNeeded');
expect(inspect).toBeGreaterThan(skipInboundIf);
expect(afterFn.slice(skipInboundIf, inspect)).toContain('return;');
expect(afterFn.slice(inspect - 80, inspect + 40)).toContain('createSupplierInspectionIfNeeded');
// 过滤条件 2026-10-07 抽成变量:极简流转传 includeAllItems=true 时换成 TRUE,
// **默认(不传)仍按供应商免入库标志过滤** —— 这条不变量必须继续成立。
expect(earlyLoad).toContain("includeAllItems ? 'TRUE' : supplierSkipInboundExistsSql('oi.supplier_code')");
expect(earlyLoad).toContain('AND ${skipInboundCondition}');
expect(prep).toContain('AND NOT ${supplierSkipInboundExistsSql(\'oi.supplier_code\')}');
expect(prep).toContain("AND NOT ${skuDirectDeliveryAppliesSql('oi.sku_code', 'oi.order_code')}");
expect(prep).toContain("SET order_status = 40 WHERE order_code = $1 AND order_status = 30");
expect(picking).toContain("AND NOT ${supplierSkipInboundExistsSql('oi.supplier_code')}");
expect(picking).toContain("AND NOT ${skuDirectDeliveryAppliesSql('oi.sku_code', 'oi.order_code')}");
});
it('直送备货完成即入供应商结算桶(无验收门槛)—— 方案 B 后归集日恒为下单日', () => {
const fin = readFileSync(
new URL('../src/services/financeReconciliationService.ts', import.meta.url),
'utf8'
);
// 方案 B(2026-09-24):供应商结算行来源改为订单行,归集日=下单日,
// 因此不再需要「免入库豁免 inspections / order_status>=45」的分支。
const bucketStart = fin.indexOf('async function loadSupplierSettlementBuckets');
const bucketSrc = fin.slice(bucketStart, bucketStart + 3000);
expect(bucketSrc).toContain('async function loadSupplierSettlementBuckets');
expect(fin).toContain("to_char((o.created_at AT TIME ZONE 'Asia/Shanghai')::date, 'YYYY-MM-DD') AS completion_day");
expect(fin).toContain('AND o.order_status >= 20');
// 新口径不再需要免入库豁免分支
expect(bucketSrc).not.toContain('supplierSkipInboundExistsSql');
// 触发点:备货完成时按订单重建(事务连接透传,方案 B 归集日=下单日)
const prepStatus = readFileSync(
new URL('../src/services/supplierPreparationStatusService.ts', import.meta.url),
'utf8'
);
expect(prepStatus).toContain('ensureSupplierStatementsForOrder(String(prep.order_code), db)');
expect(prepStatus).not.toContain("const settleDay = new Intl.DateTimeFormat('en-CA'");
// ensure 入口按订单行的(供应商 × 下单日)重建
const ensureStart = fin.indexOf('export async function ensureSupplierStatementsForOrder');
const ensureSrc = fin.slice(ensureStart, ensureStart + 1600);
expect(ensureSrc).toContain("to_char((o.created_at AT TIME ZONE 'Asia/Shanghai')::date, 'YYYY-MM-DD') AS day");
expect(ensureSrc).toContain('UPPER(TRIM(oi.supplier_code)) NOT IN ${NON_VENDOR_SUPPLIER_SQL_LIST}');
});
it('改单/删行/退货联动直送核货行', () => {
// 删行清掉直送核货行(真实分拣行不动);改数量同步申报量;保存后兜底补建
expect(items).toContain('DELETE FROM picking_items pi');
expect(items).toContain('UPDATE picking_items SET required_qty = $2::numeric, picked_qty = $2::numeric');
expect(items).toContain("await import('./skipInboundLoadingService')");
// 直送行允许退货(无验收记录),退货即清核货行
const returns = readFileSync(
new URL('../src/services/orderItemReturnService.ts', import.meta.url),
'utf8'
);
expect(returns).toContain('isSupplierSkipInbound');
expect(returns).toContain('skipInboundRow');
expect(returns).toContain('DELETE FROM picking_items WHERE order_item_id = $1::uuid');
});
it('发车不再把核货量写回订单计划数量', () => {
const settleStart = loading.indexOf('export async function applySkipInboundSettlementForOrder');
const settleSrc = loading.slice(settleStart, settleStart + 800);
expect(settleSrc).toContain('不改计划数量');
expect(settleSrc).not.toContain('syncOrderItemFromPickingComplete');
});
it('Web 分拣管理列表不露出纯直送的假拣货单', () => {
const pickingSvc = readFileSync(
new URL('../src/services/pickingService.ts', import.meta.url),
'utf8'
);
const listStart = pickingSvc.indexOf('export async function listPickingTasks');
// 窗口需覆盖整个函数体(按订单号直查分支增加了行数,原 4000 会截断到断言目标之前)
const listSrc = pickingSvc.slice(listStart, listStart + 6000);
expect(listSrc).toContain("NOT ${supplierSkipInboundExistsSql('oi2.supplier_code')}");
});
it('混单误进50可打回45;heal 不把缺仓内拣货的单推50', () => {
const pickingSvc = readFileSync(new URL('../src/services/pickingService.ts', import.meta.url), 'utf8');
const pickingCfg = readFileSync(
new URL('../src/services/pickingConfigService.ts', import.meta.url),
'utf8'
);
const processing = readFileSync(
new URL('../src/services/processingTaskService.ts', import.meta.url),
'utf8'
);
expect(pickingSvc).toContain('repairMissingWarehousePickingForOrder');
expect(pickingSvc).toContain('o.order_status = 50');
expect(pickingSvc).toContain('配送已开始,不回退');
expect(pickingSvc).toContain('待补录强推单不回退');
expect(pickingSvc).toContain('COALESCE(o.pending_backfill, false) = false');
expect(pickingCfg).toContain('WHERE COALESCE(pt.pick_status, 10) < 30');
expect(pickingCfg).toContain('AND NOT ${supplierSkipInboundExistsSql(\'oi.supplier_code\')}');
expect(processing).toContain('repairMissingWarehousePickingForOrder');
});
});