fix(采购): 配送核货/分拣自动补完成允许0成本
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

手工采购完成仍受允许成本价为0开关控制;配送核货自动收口不被0成本卡住。
This commit is contained in:
Ubuntu
2026-10-26 17:23:08 +08:00
parent f1fe645f8c
commit 495d5907ca
5 changed files with 44 additions and 4 deletions
@@ -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
+1
View File
@@ -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 || '',
@@ -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);
}
}
+1 -1
View File
@@ -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) {');
});
@@ -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)');
});
});