diff --git a/server/src/services/deliveryLoadingBackfillService.ts b/server/src/services/deliveryLoadingBackfillService.ts index cd0180a..c5c240e 100644 --- a/server/src/services/deliveryLoadingBackfillService.ts +++ b/server/src/services/deliveryLoadingBackfillService.ts @@ -517,7 +517,7 @@ export async function backfillLoadingItemUpstream(input: LoadingBackfillInput): Number(pendingSources.preps) > 0 || Number(pendingSources.purchases) > 0; // 40/45 无实际 inbound:即使有 pending 上游也走无入库路径,不补验收/库存。 - const shouldBackfillInbound = + let shouldBackfillInbound = !inboundDone && !skipAutoInbound && orderStatus < 40 && hasPendingInboundSource; const pickingDone = Number(item.review_status) >= 20 && Number(item.picked_qty || item.picked_weight || 0) > 0; @@ -738,10 +738,13 @@ export async function backfillLoadingItemUpstream(input: LoadingBackfillInput): newlyCreatedInspectionIds = pending.rows.map((row) => String(row.id)).filter((id) => !beforeIds.has(id)); const inspectionId = pending.rows[0]?.id || pendingInspection?.id; if (!inspectionId) { - const error = Object.assign(new Error('未找到可补入库的验收单,无法核货'), { - status: 409, - code: 'backfill_inspection_missing', - }); + /** + * 找不到待验收时不能一律阻断核货: + * 库存抵扣/已实际入库/免入库/直送/无有效上游行时,本行本来就不需要再入库。 + * 按无入库直送收口 pending 上游并继续补分拣;只有确实存在待验收但提交失败时才报错。 + */ + // 注意:这里不能整单调用 closePendingUpstreamWithoutInbound(会把其它行真实待入库也关掉)。 + // 本行无待验收只代表本行不需要补入库;继续补分拣/核货,pending 上游留待各自行处理。 await audit({ branchCode: task.branch_code ? String(task.branch_code) : null, orderCode, @@ -757,13 +760,14 @@ export async function backfillLoadingItemUpstream(input: LoadingBackfillInput): confirmedQty, inboundQty: 0, pickingQty: 0, - inventoryAction: 'failed', + inventoryAction: 'no_inventory', inboundBatchNo: null, pickCode: pickCode || null, - remark: `补入库失败:${error.message}`, + remark: '无可用待验收单:按无入库直送收口,继续核货/分拣,不阻断配送', }).catch(() => undefined); - throw error; - } + shouldBackfillInbound = false; + inboundQty = 0; + } else { const costRow = await pool.query( `SELECT COALESCE(i.purchase_price, oi.unit_cost, 0) AS cost, o.branch_code @@ -852,6 +856,7 @@ export async function backfillLoadingItemUpstream(input: LoadingBackfillInput): String(inspectionId), ]); inboundBatchNo = batch.rows[0]?.inbound_batch_no ? String(batch.rows[0].inbound_batch_no) : null; + } } // 补录后把订单从 30 推到 40/45(裸 SQL 改备货/采购不会自动触发既有推进链路)。 diff --git a/server/tests/deliveryLoadingEarlyBackfill.test.ts b/server/tests/deliveryLoadingEarlyBackfill.test.ts index a11ca2f..c5e0818 100644 --- a/server/tests/deliveryLoadingEarlyBackfill.test.ts +++ b/server/tests/deliveryLoadingEarlyBackfill.test.ts @@ -73,7 +73,7 @@ describe('T3 早期任务核货/发车/补录', () => { expect(backfill).toContain('const skipAutoInbound = isPickingOnly40 || isPickingOnly45'); expect(backfill).toContain('isPickingOnly40 = autoAdvancedFrom === 40'); expect(backfill).toContain('isPickingOnly45 = autoAdvancedFrom === 45'); - expect(backfill).toContain('const shouldBackfillInbound ='); + expect(backfill).toContain('let shouldBackfillInbound ='); expect(backfill).toContain('hasPendingInboundSource'); expect(backfill).toContain('pendingSources.preps'); expect(backfill).toContain('closePendingUpstreamWithoutInbound'); @@ -130,4 +130,11 @@ describe('T3 早期任务核货/发车/补录', () => { expect(read('../src/modules/kernel/orderStatus.ts')).not.toContain('bumpedFromStatus'); expect(read('../src/services/financeNetProfitFormula.ts')).not.toContain('delivery_loading_backfill_logs'); }); + + it('无可用待验收时按无入库直送收口,不阻断核货', () => { + expect(backfill).toContain('无可用待验收单'); + expect(backfill).toContain('inventoryAction: \'no_inventory\''); + expect(backfill).toContain('shouldBackfillInbound = false'); + expect(backfill).not.toContain('throw error; // backfill_inspection_missing'); + }); });