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'); }); });