Files
SupplierSystem/server/tests/independentInbound.test.ts
T
Ubuntu fe110ed040
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
fix(独立进货): 禁止库存抵扣 —— 自动抵扣不再发生,按钮也不显示
开发者 2026-10-12:「还有这是进货的单子 不能被库存抵扣 库存抵扣的按钮给我取消掉
录完单的自动库存抵扣也别出现」。

服务端(**在唯一写入点拦**):`inventoryStockCoverService.applyStockCoverForOrderItem`
对哨兵单直接 `return 0` —— 它是全库唯一把 `stock_cover_qty` 写大的入口,因此一次性覆盖
录单后的自动抵扣(分配路线 `applyStockCoverForOrder`、竞价补偿抵扣)与手工抵扣按钮的所有调用方。

前端:`canApplyInventoryOffset` 增加哨兵客户判断 ⇒ 进货单**永不显示**「库存抵扣」按钮
(此前靠 `is_main_system_order` 恰好不显示,现在是显式保证)。

真库实测:
  · 新录一张进货单(库存充足)⇒ 明细 `stock_cover_qty=0`、无活跃预留、正常生成采购单 ✅
    (改前会被自动抵扣掉,正是截图里「库存抵扣: 苹果、抄手皮」的由来)
  · 存量单 ORD_WEB_20261013_BR003_0003(我加拦截前建的、被自动抵扣过)⇒
    用产品路径 `releaseReservationsForOrder` 撤销:苹果 30→0、抄手皮 8→0 ✅(提示随之消失)
  · 冒烟单用后即移入垃圾桶

测试:independentInbound 22 例(新增"唯一写入点返回 0");inventoryOffsetButton 5 + stockCoverGuard 13 全绿。
2026-10-13 09:03:55 +08:00

231 lines
13 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 { describe, expect, it } from 'vitest';
import { readFileSync } from 'node:fs';
import path from 'node:path';
/**
* 「独立进货」(自用哨兵客户,2026-10-12 开发者立项)。
*
* 开发者口径:名字叫**独立进货**;「商品全部入库了就行,自己就完成」;
* 「注意那个主数据不卡流程,别把这种单子也强制推到那个待配送去了」;
* 「厂商应付就是我们现在的供应商结算体系,不用动」;
* 「这种单子别计入销售,因为就是纯纯的一个进货单」;成本价必填 >0。
*
* 设计:docs/design/20261012-独立进货(自用哨兵客户).md
* 关键取舍:**哨兵编码、不建 customers 行**(租户库 orders.customer_code 无外键)
* ⇒ 客户管理/绑定/预算/回款/客户结算天然看不到它,也无从删除。
*/
const svc = (rel: string) => readFileSync(path.join(__dirname, '..', rel), 'utf8');
const sentinel = svc('src/services/independentInbound.ts');
const entry = svc('src/services/orderEntryScopeService.ts');
const fulfill = svc('src/services/orderWorkflowFulfillmentService.ts');
const reconcile = svc('src/services/orderStatusReconcileService.ts');
const autoAdvance = svc('src/services/orderAutoAdvanceService.ts');
const processing = svc('src/services/processingTaskService.ts');
const picking = svc('src/services/pickingService.ts');
const finance = svc('src/services/financeReconciliationService.ts');
describe('① 哨兵客户:只在录单出现,不建客户档案', () => {
it('编码/名称固定,且只落在订单上(不写入 customers)', () => {
expect(sentinel).toContain("INDEPENDENT_INBOUND_CUSTOMER_CODE = '__INDEPENDENT_INBOUND__'");
expect(sentinel).toContain("INDEPENDENT_INBOUND_CUSTOMER_NAME = '独立进货'");
// 本模块不得出现对 customers 表的写
expect(sentinel).not.toMatch(/INSERT INTO\s+customers/i);
expect(sentinel).not.toMatch(/UPDATE\s+customers/i);
});
it('录单校验放行哨兵,但仍要求录单员绑定分公司', () => {
const idx = entry.indexOf('export async function assertOrderEntryCustomerAccess');
const body = entry.slice(idx, idx + 900);
expect(body).toContain('isIndependentInboundCustomer(customerCode)');
expect(body).toContain("code: 'invalid_branch'");
// 哨兵必须在查 customers 之前短路(否则会被判「客户不存在」)
expect(body.indexOf('isIndependentInboundCustomer(customerCode)')).toBeLessThan(
body.indexOf('FROM customers')
);
});
});
describe('② 全部入库 ⇒ 直接完成(90),且绝不被推到待配送', () => {
it('主数据不卡流程(定时强推)对这类单直接 skip', () => {
const idx = autoAdvance.indexOf('export async function autoAdvanceOrderToDelivery');
const body = autoAdvance.slice(idx, idx + 3000);
expect(body).toContain('isOrderIndependentInbound');
expect(body).toContain('独立进货单,跳过定时强推');
});
it('验收完成钩子:齐套 ⇒ 结案;不齐套 ⇒ 不推进', () => {
const idx = processing.indexOf('export async function maybeAdvanceOrderAfterFulfillment');
const body = processing.slice(idx, idx + 2000);
expect(body).toContain('isOrderIndependentInbound');
expect(body).toContain('completeOrderWithoutDriverDelivery');
expect(body).toContain('独立进货:全部入库,订单直接完成');
// 该分支必须在 45/50 推进逻辑之前
expect(body.indexOf('isOrderIndependentInbound')).toBeLessThan(body.indexOf('isPickingWorkflowEnabled'));
});
it('15 分钟对账:哨兵单走单列分支并提前 return(不进 45/50/60 链)', () => {
const idx = reconcile.indexOf('if (await isOrderIndependentInbound(code, pool))');
expect(idx).toBeGreaterThan(-1);
const body = reconcile.slice(idx, idx + 1200);
expect(body).toContain('fetchOrderInspectionState');
expect(body).toContain('尚未全部入库,保持当前状态');
expect(body).toContain('return {');
// 提前 return 必须早于配送/分拣推进调用
expect(idx).toBeLessThan(reconcile.indexOf('tryAdvanceOrderToDelivery(code)'));
});
it('结案入口:独立进货允许从 40 待验收结案(普通单仍只允许 30/50)', () => {
const idx = fulfill.indexOf('const fromInspectionForIndependentInbound');
expect(idx).toBeGreaterThan(-1);
expect(fulfill.slice(idx, idx + 400)).toContain("status === 40 && (await isOrderIndependentInbound(orderCode, db))");
});
it('不补核货/拣货行(skip-inbound 补行入口对哨兵单直接返回)', () => {
const skip = svc('src/services/skipInboundLoadingService.ts');
const idx = skip.indexOf('export async function ensureSkipInboundLoadingLines');
expect(idx).toBeGreaterThan(-1);
const body = skip.slice(idx, idx + 6000);
expect(body).toContain('await isOrderIndependentInbound(code, db)');
// 必须在真正建行之前返回(函数体里第一处写 picking_items 的 SQL)
const firstInsert = body.indexOf('INSERT INTO picking_items');
expect(firstInsert).toBeGreaterThan(-1);
expect(body.indexOf('isOrderIndependentInbound')).toBeLessThan(firstInsert);
});
it('不生成拣货任务(候选查询排除哨兵单)', () => {
const idx = picking.indexOf('export async function syncMissingPickingTasks');
expect(idx).toBeGreaterThan(-1);
expect(picking.slice(idx, idx + 3000)).toContain("salesVisibleOrderSql('o')");
});
});
describe('②b 入库优先级(开发者裁决):独立进货必须入库,普通单照旧', () => {
const prep = svc('src/services/preparationFulfillmentService.ts');
it('建待验:哨兵单忽略"免入库厂商/商品直送"两个跳过集合', () => {
const idx = prep.indexOf('export async function ensureInspectionsForOrder');
const body = prep.slice(idx, idx + 3000);
expect(body).toContain('const independentInboundOrder = await isOrderIndependentInbound(orderCode, db)');
expect(body).toMatch(/independentInboundOrder\s*\?\s*new Set<string>\(\)\s*:\s*await loadSkipInboundSupplierSet\(/);
expect(body).toMatch(/independentInboundOrder\s*\?\s*new Set<string>\(\)\s*:\s*await loadDirectDeliverySkuSetForOrder\(/);
// 普通单仍走原来的两个集合
expect(body).toContain('loadSkipInboundSupplierSet(');
expect(body).toContain('loadDirectDeliverySkuSetForOrder(');
});
it('建待验的**内部**免入库判断也要对哨兵单让路(否则夹具实测 A 单也不建待验)', () => {
const reject = svc('src/services/inspectionRejectFulfillmentService.ts');
const idx = reject.indexOf('export async function createSupplierInspectionIfNeeded');
expect(idx).toBeGreaterThan(-1);
const body = reject.slice(idx, idx + 3000);
expect(body).toMatch(/!\(await isOrderIndependentInbound\(input\.orderCode, client\)\)\s*&&\s*\n?\s*\(await isSupplierSkipInbound\(input\.supplierCode, client\)\)/);
// 普通单仍按免入库标志拦(该谓词必须还在)
expect(body).toContain('isSupplierSkipInbound(input.supplierCode, client)');
});
it('30→40 覆盖判定:哨兵单不吃"免入库/直送"两条过滤,普通单原样', () => {
const idx = prep.indexOf('const independentInboundOrder = await isOrderIndependentInbound(orderCode, db);', prep.indexOf('export async function tryAdvanceOrderToInspection'));
expect(idx).toBeGreaterThan(-1);
const body = prep.slice(idx, idx + 3500);
// 哨兵单:条件整体为空 ⇒ 该行必须靠验收覆盖才能算齐套
expect(body).toMatch(/const inboundCoverageFilters = independentInboundOrder\s*\n?\s*\? ''/);
// 普通单:原样两条过滤(另有 supplierSkipInbound 契约测试钉住字面量)
expect(body).toContain("AND NOT ${supplierSkipInboundExistsSql('oi.supplier_code')}");
expect(body).toContain("AND NOT ${skuDirectDeliveryAppliesSql('oi.sku_code', 'oi.order_code')}");
// 覆盖判定 SQL 里用的是这个 fragment
expect(body).toContain('${inboundCoverageFilters}');
});
});
describe('③ 结算:供应商结算不动,客户结算不生成', () => {
it('客户结算入口(唯一实现)对哨兵单直接 skipped', () => {
const idx = finance.indexOf('async function runUpsertCustomerStatementForOrder');
const body = finance.slice(idx, idx + 800);
expect(body).toContain('isOrderIndependentInbound(code, db)');
expect(body).toContain("return 'skipped'");
});
it('供应商结算未被本功能改动(仍按 qty × unit_cost)', () => {
expect(finance).toContain('ensureSupplierStatementsForOrder');
expect(sentinel).not.toMatch(/supplier_statement/i);
});
it('成本价**不强制**(开发者修订):结案口不再拦 0 成本', () => {
expect(fulfill).not.toContain('independent_inbound_cost_required');
});
});
describe('③b 结构上零销售:录单写 0 售价、零价闸门对哨兵放行', () => {
const submit = svc('src/services/customerOrderSubmitService.ts');
const alloc = svc('src/services/orderAllocationService.ts');
it('录单落库:哨兵单 price/amount 写 0(成本价照旧)', () => {
expect(submit).toContain('revenuePrice = independentInbound ? 0 : price');
expect(submit).toContain('revenueAmount = independentInbound ? 0 : amount');
expect(submit).toContain('independentInbound ? false : price <= 0');
});
it('提交/分配/结案三处零价闸门:仅对哨兵单放行', () => {
const submitIdx = submit.indexOf('const independentInboundOrder = isIndependentInboundCustomer');
expect(submitIdx).toBeGreaterThan(-1);
expect(submit.slice(submitIdx, submitIdx + 400)).toContain('input.allowUnpriced === true || independentInboundOrder');
expect(alloc).toContain('await isOrderIndependentInbound(orderCode, client)');
expect(fulfill).toContain('await isOrderIndependentInbound(orderCode, db)');
});
it('信用校验与客户×商品映射学习:哨兵跳过(真跑撞到过)', () => {
expect(submit).toContain('isIndependentInboundCustomer(input.customerCode)');
expect(submit).toContain('__skip');
});
});
describe('②c 禁止库存抵扣(开发者:进货单不能被库存抵扣,按钮也取消)', () => {
it('服务端唯一写入点对哨兵单返回 0 ⇒ 录单后的自动抵扣/手工抵扣都不生效', () => {
const cover = svc('src/services/inventoryStockCoverService.ts');
const idx = cover.indexOf('export async function applyStockCoverForOrderItem');
expect(idx).toBeGreaterThan(-1);
const body = cover.slice(idx, idx + 1200);
expect(body).toContain('await isOrderIndependentInbound(orderCode, db)');
expect(body).toContain('return 0;');
// 必须在真正算抵扣之前(守卫之前只有入参清洗)
expect(body.indexOf('isOrderIndependentInbound')).toBeLessThan(body.indexOf('loadLockedStockCoverIfInboundOrPicking'));
});
});
describe('③c 信用校验:修在**最底层**函数上(否则录单页的预检接口仍报"客户不存在")', () => {
const budget = svc('src/services/customerBudgetService.ts');
it('checkCustomerCredit 对哨兵直接返回 allowed(覆盖所有包装/接口)', () => {
const idx = budget.indexOf('export async function checkCustomerCredit');
expect(idx).toBeGreaterThan(-1);
const body = budget.slice(idx, idx + 1600);
expect(body).toContain('isIndependentInboundCustomer(customerCode)');
expect(body).toContain("level: 'ok'");
// 必须在查 customers 之后仍无条件放行(真实客户逻辑不变)
expect(body).toContain('FROM customers WHERE customer_code = $1');
// 冻结口径:真实客户仍只有"不存在"才 block
expect(budget).toContain('不存在,无法下单');
});
it('录单页提交前预检接口链路存在(GET /finance/customer/:code/can-order → checkCustomerOrderBudget)', () => {
const routes = svc('src/modules/workflow/routes/financeRoutes.ts');
expect(routes).toContain("'/finance/customer/:customerCode/can-order'");
expect(routes).toContain('checkCustomerOrderBudget');
expect(budget).toContain('export async function checkCustomerOrderBudget');
});
});
describe('④ 不计入销售(口径谓词统一,成本口径不动)', () => {
it('统一谓词来自 new 模块', () => {
expect(sentinel).toContain('export function salesVisibleOrderSql');
// 模板里内插常量:`${alias}.customer_code IS DISTINCT FROM '${INDEPENDENT_INBOUND_CUSTOMER_CODE}'`
expect(sentinel).toContain("customer_code IS DISTINCT FROM '${INDEPENDENT_INBOUND_CUSTOMER_CODE}'");
});
it('已排除:看板 KPI、客户返利、SKU 毛利/排行', () => {
expect(svc('src/services/dashboardService.ts')).toContain('salesVisibleOrderSql');
expect(svc('src/services/customerRebateService.ts')).toContain('salesVisibleOrderSql');
expect(svc('src/services/marginQueryService.ts')).toContain('salesVisibleOrderSql');
});
});