Files
SupplierSystem/server/tests/independentInbound.test.ts
T
雨天 54efb2a41d
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(独立进货): 建档为系统默认客户,客户结算仍为 0
把「独立进货」从「不落 customers 的哨兵」改为客户表里的一行
(方案甲,开发者 2026-10-14 裁决):迁移 20261044 在每个租户
customers 幂等建 1 行(编码 __INDEPENDENT_INBOUND__ / 名字「独立
进货」/ status=10),clone_tenant_schema() 末尾为新租户补插 ⇒ 客户
列表 / 订单管理客户筛选 / 客户结算客户下拉 / 录单下拉自动可见且只一条。

客户结算金额恒为 0:既有 financeReconciliationService 对哨兵单直接
skipped 的短路不动,哨兵豁免与销售口径排除全部保留。

录单页客户下拉按编码去重,避免「库里有 + 内建注入」两条重复。
2026-10-14 09:11:24 +08:00

250 lines
14 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 行」已于 2026-10-14 改为**系统默认客户行**(方案甲):
* 迁移 `20261044` 在每个租户 `customers` 建 1 行(编码/名字沿用哨兵常量),客户面自动可见;
* 客户结算仍恒为 0。设计:docs/design/20261014-独立进货建客户档案(方案甲).md
*/
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('① 哨兵客户:编码/名字固定,且业务代码不写 customers(建档走迁移)', () => {
it('编码/名称固定,且业务服务模块不写入 customers', () => {
expect(sentinel).toContain("INDEPENDENT_INBOUND_CUSTOMER_CODE = '__INDEPENDENT_INBOUND__'");
expect(sentinel).toContain("INDEPENDENT_INBOUND_CUSTOMER_NAME = '独立进货'");
// 业务服务模块不得直接写 customers(建档统一走迁移 20261044)
expect(sentinel).not.toMatch(/INSERT INTO\s+customers/i);
expect(sentinel).not.toMatch(/UPDATE\s+customers/i);
});
it('已作为系统默认客户建档:迁移 20261044 幂等插入 + 新租户克隆补插(方案甲 2026-10-14)', () => {
const migration = readFileSync(
path.join(__dirname, '..', '..', 'migrations', '20261044_independent_inbound_customer.sql'),
'utf8'
);
// 建行:编码/名字沿用哨兵常量(订单上的 customer_code 无需迁移),且幂等
expect(migration).toContain("'__INDEPENDENT_INBOUND__'");
expect(migration).toContain("'独立进货'");
expect(migration).toContain('ON CONFLICT (customer_code) DO NOTHING');
// 存量 + 新租户两处都覆盖:助手函数 + clone_tenant_schema 末尾补插
expect(migration).toContain('CREATE OR REPLACE FUNCTION public.ensure_independent_inbound_customer');
expect(migration).toContain('CREATE OR REPLACE FUNCTION public.clone_tenant_schema');
expect(migration).toContain('PERFORM public.ensure_independent_inbound_customer(v_schema)');
// 存量:遍历 tenant_% schema(排除 tenant_template)
expect(migration).toContain("nspname LIKE 'tenant_%'");
expect(migration).toContain("nspname <> 'tenant_template'");
});
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');
});
});