fix(订单实收): 按主系统 handoff 收口写入与回填口径
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

移除采购/验收额外叠加库存抵扣的偏离;回填改为 quantity/unit + stage=0,匹配配送系统设计。
This commit is contained in:
Ubuntu
2026-11-03 14:37:19 +08:00
parent 39569df290
commit 6ad746cc11
5 changed files with 15 additions and 59 deletions
@@ -1,10 +1,10 @@
/**
* 回填 order_items 的客户计划留档与实收字段(供应商侧,移植主系统 68555db7)。
*
* 只补 NULL,不覆盖已有值:
* demand_* = COALESCE(demand, ordered_*, quantity/unit)
* received_* = COALESCE(received, actual_received_*, quantity/unit)
* received_stage = 有 actual_received 时 30(分拣称重),否则 0(历史值)
* 按主系统 handoff 口径,只补 NULL,不覆盖已有值:
* demand_* = COALESCE(demand, quantity/unit)
* received_* = COALESCE(received, quantity/unit)
* received_stage = 0(历史值,不代表真实流程阶段)
*
* 用法:
* npx tsx --env-file=.env server/scripts/backfill-order-item-received-qty.ts --dry-run
@@ -44,11 +44,7 @@ async function main() {
console.log(`skip ${nspname}: 缺少 received_qty(先迁移)`);
continue;
}
const hasActual = await hasColumn(nspname, 'order_items', 'actual_received_qty');
const table = `${quoteIdent(nspname)}.order_items`;
const actualExpr = hasActual ? 'COALESCE(actual_received_qty, 0) > 0' : 'FALSE';
const actualQty = hasActual ? 'actual_received_qty' : 'NULL';
const actualUnit = hasActual ? 'actual_received_unit' : 'NULL';
if (dryRun) {
await run(async () => {
const r = await pool.query(
@@ -65,11 +61,11 @@ async function main() {
await run(async () => {
const r = await pool.query(
`UPDATE ${table}
SET demand_qty = COALESCE(demand_qty, ordered_quantity, quantity),
demand_unit = COALESCE(demand_unit, ordered_unit, unit),
received_qty = COALESCE(received_qty, CASE WHEN ${actualExpr} THEN ${actualQty} ELSE quantity END),
received_unit = COALESCE(received_unit, CASE WHEN ${actualExpr} THEN COALESCE(${actualUnit}, unit) ELSE unit END),
received_stage = COALESCE(received_stage, CASE WHEN ${actualExpr} THEN 30 ELSE 0 END)
SET demand_qty = COALESCE(demand_qty, quantity),
demand_unit = COALESCE(demand_unit, unit),
received_qty = COALESCE(received_qty, quantity),
received_unit = COALESCE(received_unit, unit),
received_stage = COALESCE(received_stage, 0)
WHERE demand_qty IS NULL OR demand_unit IS NULL OR received_qty IS NULL
OR received_unit IS NULL OR received_stage IS NULL`
);
@@ -3,7 +3,7 @@ import { isZeroSupplyCostAllowed } from './zeroSupplyCostConfigService';
import { pool } from '../db';
const PROCESSING_RAW_PURCHASE_SOURCE_TYPE = 'processing_raw_material' as const;
import { orderReceivedQtyDb, writeOrderItemReceivedQtyWithStockCover } from './orderItemReceivedQtyService';
import { orderReceivedQtyDb, writeOrderItemReceivedQty } from './orderItemReceivedQtyService';
import { deriveExpiryDate } from '../utils/shelfLife';
import { applyArchiveWriteback } from './stockInboundService';
import type { ArchiveWritebackInput } from './stockInboundService';
@@ -2152,7 +2152,7 @@ export async function submitInspectionCompliance(
}
if (!isRejected && actualQty > 0 && row.order_code) {
await writeOrderItemReceivedQtyWithStockCover(orderReceivedQtyDb(client), {
await writeOrderItemReceivedQty(orderReceivedQtyDb(client), {
orderCode: String(row.order_code),
skuCode: row.sku_code ? String(row.sku_code) : null,
qty: actualQty,
@@ -121,46 +121,6 @@ export async function writeOrderReceivedQtyBySku(
}
/**
* 采购/验收实收写整行口径:来源数量通常是“缺口量”
* (`purchase_orders.actual_qty` / `inspections.actual_qty`),
* 订单行还有 `stock_cover_qty`(库存抵扣)时必须一起计入,否则完结冻结会少算。
*/
export async function writeOrderItemReceivedQtyWithStockCover(
db: Db,
input: WriteOrderItemReceivedQtyInput
): Promise<number> {
const orderCode = trimText(input.orderCode);
const orderItemId = trimText(input.orderItemId);
const skuCode = trimText(input.skuCode);
if (!orderCode || (!orderItemId && !skuCode)) return writeOrderItemReceivedQty(db, input);
const conditions = [`order_code = $1`, `COALESCE(is_returned, false) = false`];
const params: unknown[] = [orderCode];
if (orderItemId) {
params.push(orderItemId);
conditions.push(`id::text = $${params.length}`);
} else {
params.push(skuCode);
conditions.push(`sku_code IS NOT DISTINCT FROM $${params.length}`);
}
const rows = await db.query(
`SELECT id::text AS id, COALESCE(stock_cover_qty, 0)::float8 AS stock_cover_qty
FROM order_items WHERE ${conditions.join(' AND ')}`,
params
);
if (!rows.rows.length) return writeOrderItemReceivedQty(db, input);
let written = 0;
for (const row of rows.rows) {
const cover = Math.max(0, Number(row.stock_cover_qty) || 0);
written += await writeOrderItemReceivedQty(db, {
...input,
orderItemId: String(row.id),
qty: Number(input.qty) + cover,
});
}
return written;
}
/** 从池或事务客户端取连接;调用方已有事务时直接传 client。 */
export function orderReceivedQtyDb(client?: PoolClient | null): Db {
return (client || pool) as unknown as Db;
+2 -2
View File
@@ -1,6 +1,6 @@
import type { PoolClient } from 'pg';
import { pool } from '../db';
import { orderReceivedQtyDb, writeOrderItemReceivedQtyWithStockCover } from './orderItemReceivedQtyService';
import { orderReceivedQtyDb, writeOrderItemReceivedQty } from './orderItemReceivedQtyService';
import { assertPurchaseOrderInBranch } from './purchaserBranchScope';
import { writeAuditLog } from './auditService';
import { syncOrderItemsFromSupplyCompletion } from './orderItemSupplyUnitSync';
@@ -543,7 +543,7 @@ export async function amendPurchaseActual(
});
if (nextQty > 0 && row.order_code && row.sku_code) {
await writeOrderItemReceivedQtyWithStockCover(orderReceivedQtyDb(ownsClient ? null : client), {
await writeOrderItemReceivedQty(orderReceivedQtyDb(ownsClient ? null : client), {
orderCode: String(row.order_code),
skuCode: String(row.sku_code),
qty: nextQty,
@@ -1,6 +1,6 @@
import type { PoolClient } from 'pg';
import { pool } from '../db';
import { orderReceivedQtyDb, writeOrderItemReceivedQtyWithStockCover } from './orderItemReceivedQtyService';
import { orderReceivedQtyDb, writeOrderItemReceivedQty } from './orderItemReceivedQtyService';
import { syncProcessingRawPurchaseFromPurchaseOrder } from './processingRawMaterialService';
import { assertPurchaseOrderInBranch } from './purchaserBranchScope';
import { PURCHASE_STATUS_QUALITY_REJECTED } from './purchaseInboundService';
@@ -238,7 +238,7 @@ export async function updatePurchaseOrderStatus(
const orderCode = String(link.rows[0]?.order_code || '').trim();
const skuCode = String(link.rows[0]?.sku_code || currentRow.sku_code || '').trim();
if (orderCode && skuCode) {
await writeOrderItemReceivedQtyWithStockCover(orderReceivedQtyDb(client), {
await writeOrderItemReceivedQty(orderReceivedQtyDb(client), {
orderCode,
skuCode,
qty: Number(actualQty),