From 495d5907cae80eb8248525c81beae45a1772c168 Mon Sep 17 00:00:00 2001 From: Ubuntu Date: Mon, 26 Oct 2026 17:23:08 +0800 Subject: [PATCH] =?UTF-8?q?fix(=E9=87=87=E8=B4=AD):=20=E9=85=8D=E9=80=81?= =?UTF-8?q?=E6=A0=B8=E8=B4=A7/=E5=88=86=E6=8B=A3=E8=87=AA=E5=8A=A8?= =?UTF-8?q?=E8=A1=A5=E5=AE=8C=E6=88=90=E5=85=81=E8=AE=B80=E6=88=90?= =?UTF-8?q?=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 手工采购完成仍受允许成本价为0开关控制;配送核货自动收口不被0成本卡住。 --- .../deliveryLoadingBackfillService.ts | 3 ++ server/src/services/pickingService.ts | 1 + .../src/services/purchaseOperationService.ts | 14 ++++++++-- server/tests/allowZeroSupplyCost.test.ts | 2 +- .../autoCompletePurchaseZeroCost.test.ts | 28 +++++++++++++++++++ 5 files changed, 44 insertions(+), 4 deletions(-) create mode 100644 server/tests/autoCompletePurchaseZeroCost.test.ts diff --git a/server/src/services/deliveryLoadingBackfillService.ts b/server/src/services/deliveryLoadingBackfillService.ts index 88f4ee2..b5b8036 100644 --- a/server/src/services/deliveryLoadingBackfillService.ts +++ b/server/src/services/deliveryLoadingBackfillService.ts @@ -190,6 +190,7 @@ async function closePendingUpstreamWithoutInbound(input: { await updatePurchaseOrderStatus({ purchaseCode: String(row.purchase_code), status: 20, + allowZeroCost: true, actualQty: Number(row.qty) || undefined, actualPrice: Number(row.price) || undefined, branchCode: branchCode || '', @@ -401,6 +402,7 @@ export async function backfillLoadingItemUpstream(input: LoadingBackfillInput): await updatePurchaseOrderStatus({ purchaseCode: String(purchase.purchase_code), status: 20, + allowZeroCost: true, actualQty: confirmedQty, actualPrice: purchase.actual_price != null @@ -699,6 +701,7 @@ export async function backfillLoadingItemUpstream(input: LoadingBackfillInput): await updatePurchaseOrderStatus({ purchaseCode: String(purchase.purchase_code), status: 20, + allowZeroCost: true, actualQty: confirmedQty, actualPrice: purchase.actual_price != null diff --git a/server/src/services/pickingService.ts b/server/src/services/pickingService.ts index c9a987b..ec30ef9 100644 --- a/server/src/services/pickingService.ts +++ b/server/src/services/pickingService.ts @@ -1264,6 +1264,7 @@ async function backfillPendingFulfillmentForPicking(input: { await updatePurchaseOrderStatus({ purchaseCode: String(row.purchase_code), status: 20, + allowZeroCost: true, actualQty: Number(row.qty) || undefined, actualPrice: Number(row.price) || undefined, branchCode: branchCode || '', diff --git a/server/src/services/purchaseOperationService.ts b/server/src/services/purchaseOperationService.ts index 69c1f2a..c596102 100644 --- a/server/src/services/purchaseOperationService.ts +++ b/server/src/services/purchaseOperationService.ts @@ -31,6 +31,11 @@ export interface UpdatePurchaseOrderStatusInput { unit?: string | null; /** 自由池完成时写入归属;已挂他人则拒绝(见 free-pool 设计) */ claimPurchaser?: { purchaser_code: string; purchaser_name: string } | null; + /** + * 内部自动补完成(分拣/核货)专用:允许成本为 0/未填按 0 落库, + * 不读分公司「允许成本价为 0」开关;负数/NaN 仍拒绝。手工完成不传。 + */ + allowZeroCost?: boolean; } export interface UpdatePurchaseOrderStatusResult { @@ -59,6 +64,7 @@ export async function updatePurchaseOrderStatus( const categoryCode = input.categoryCode === undefined ? null : String(input.categoryCode).trim(); const categoryName = input.categoryName === undefined ? null : String(input.categoryName).trim(); const unit = input.unit === undefined ? null : String(input.unit).trim(); + const internalAllowZeroCost = input.allowZeroCost === true; if (!purchaseCode || ![20, 30, 40].includes(status)) { throw Object.assign(new Error('采购状态参数无效'), { status: 400, code: 'invalid_request' }); @@ -155,11 +161,13 @@ export async function updatePurchaseOrderStatus( const warehouseDemand = await purchaseLinkedToWarehouseDemand(purchaseCode, client); // 仓内加工/挂加工需求单:成本必须为 0(既有反向规则,开关不改); // 其余单据:默认必须 >0,「允许成本价为 0」开关开启后可按 0 保存(负数/NaN 仍拒)。 - const allowZeroCost = selfProcessed || warehouseDemand; + const allowZeroCost = selfProcessed || warehouseDemand || internalAllowZeroCost; const effectivePrice = actualPrice !== null && Number.isFinite(actualPrice) ? actualPrice - : Number(existing.actual_price ?? existing.purchase_price) || 0; + : internalAllowZeroCost + ? 0 + : Number(existing.actual_price ?? existing.purchase_price) || 0; if (allowZeroCost) { if (!Number.isFinite(effectivePrice) || effectivePrice < 0) { throw Object.assign(new Error('实采单价无效'), { status: 400, code: 'invalid_request' }); @@ -235,7 +243,7 @@ export async function updatePurchaseOrderStatus( const branchAllowsZero = await isZeroSupplyCostAllowed( String(flagRow.rows[0]?.branch_code || '').trim() || null ); - if (Boolean(flagRow.rows[0]?.self_processed) || linked || branchAllowsZero) { + if (internalAllowZeroCost || Boolean(flagRow.rows[0]?.self_processed) || linked || branchAllowsZero) { await clearPlanPriceForZeroCost(client, purchaseCode); } } diff --git a/server/tests/allowZeroSupplyCost.test.ts b/server/tests/allowZeroSupplyCost.test.ts index fdae453..85871d4 100644 --- a/server/tests/allowZeroSupplyCost.test.ts +++ b/server/tests/allowZeroSupplyCost.test.ts @@ -125,7 +125,7 @@ describe('放行点必须接开关(缺一处即失败)', () => { it('自采侧保留"仓内加工成本必须为 0"的反向规则(开关不能把它放开成随便填)', () => { const ops = read('src/services/purchaseOperationService.ts'); - expect(ops).toContain('const allowZeroCost = selfProcessed || warehouseDemand;'); + expect(ops).toContain('const allowZeroCost = selfProcessed || warehouseDemand || internalAllowZeroCost;'); const amend = read('src/services/purchaseAmendService.ts'); expect(amend).toContain('if (!row.self_processed && nextPrice === 0) {'); }); diff --git a/server/tests/autoCompletePurchaseZeroCost.test.ts b/server/tests/autoCompletePurchaseZeroCost.test.ts new file mode 100644 index 0000000..efcf85a --- /dev/null +++ b/server/tests/autoCompletePurchaseZeroCost.test.ts @@ -0,0 +1,28 @@ +import { readFileSync } from 'node:fs'; +import { describe, expect, it } from 'vitest'; + +const read = (rel: string) => readFileSync(new URL(`../${rel}`, import.meta.url), 'utf8'); +const svc = read('src/services/purchaseOperationService.ts'); +const backfill = read('src/services/deliveryLoadingBackfillService.ts'); +const picking = read('src/services/pickingService.ts'); +const manual = read('src/routes/mobile/mobilePurchaseRoutes.ts'); + +describe('自动补完成采购允许 0 成本', () => { + it('internal allowZeroCost 仅 status20 放行 0/未填,负数/NaN 仍拒', () => { + expect(svc).toContain('allowZeroCost?: boolean'); + expect(svc).toContain('const internalAllowZeroCost = input.allowZeroCost === true'); + expect(svc).toContain('selfProcessed || warehouseDemand || internalAllowZeroCost'); + expect(svc).toContain('!Number.isFinite(effectivePrice) || effectivePrice < 0'); + expect(svc).toContain('if (actualPrice === null || actualPrice <= 0) actualPrice = 0'); + }); + + it('自动补完成 4 处传 true;手工路由不传', () => { + expect((backfill.match(/allowZeroCost: true/g) || []).length).toBe(3); + expect((picking.match(/allowZeroCost: true/g) || []).length).toBe(1); + expect(manual).not.toContain('allowZeroCost'); + }); + + it('zeroPurchaseCost 清计划价逻辑认 internal flag', () => { + expect(svc).toContain('if (internalAllowZeroCost || Boolean(flagRow.rows[0]?.self_processed)'); + }); +});