feat(订单实收): 列表展示实收合计并计入库存抵扣
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

订单列表新增实收合计列;采购/验收按缺口量写入时加回 stock_cover_qty,避免完结少算。
This commit is contained in:
Ubuntu
2026-11-03 13:15:56 +08:00
parent 3ccb07acf6
commit 07f849f22b
6 changed files with 81 additions and 4 deletions
@@ -1,7 +1,7 @@
import { PoolClient } from 'pg';
import { isZeroSupplyCostAllowed } from './zeroSupplyCostConfigService';
import { pool } from '../db';
import { orderReceivedQtyDb, writeOrderItemReceivedQty } from './orderItemReceivedQtyService';
import { orderReceivedQtyDb, writeOrderItemReceivedQtyWithStockCover } from './orderItemReceivedQtyService';
import { deriveExpiryDate } from '../utils/shelfLife';
import { applyArchiveWriteback } from './stockInboundService';
import type { ArchiveWritebackInput } from './stockInboundService';
@@ -2133,7 +2133,7 @@ export async function submitInspectionCompliance(
}
if (!isRejected && actualQty > 0 && row.order_code) {
await writeOrderItemReceivedQty(orderReceivedQtyDb(client), {
await writeOrderItemReceivedQtyWithStockCover(orderReceivedQtyDb(client), {
orderCode: String(row.order_code),
skuCode: row.sku_code ? String(row.sku_code) : null,
qty: actualQty,
@@ -120,6 +120,47 @@ export async function writeOrderReceivedQtyBySku(
return written;
}
/**
* 采购/验收实收写整行口径:来源数量通常是“缺口量”
* (`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;
@@ -121,6 +121,10 @@ export interface OrderManagementListRow {
order_status: number;
total_amount: number;
total_weight: number;
/** 订单行持久实收合计(仅 stage>=10;完结历史单回退 quantity) */
received_qty?: number | null;
/** 所有实收行单位一致时返回该单位,多单位返回 null */
received_unit?: string | null;
created_at: string;
updated_at: string | null;
delivery_date: string | null;
@@ -578,6 +582,22 @@ export async function listOrdersForManagement(
const listResult = await pool.query(
`SELECT o.order_code, o.customer_code, c.company_name, o.branch_code, b.branch_name,
o.order_status, o.total_amount, o.total_weight, o.created_at, o.updated_at,
COALESCE((
SELECT SUM(CASE
WHEN COALESCE(oi.received_stage, 0) >= 10 OR o.order_status >= 90
THEN COALESCE(oi.received_qty, 0) ELSE 0 END)
FROM order_items oi
WHERE oi.order_code = o.order_code
AND COALESCE(oi.is_returned, false) = false
), 0)::float8 AS received_qty,
(
SELECT CASE WHEN COUNT(DISTINCT COALESCE(NULLIF(TRIM(oi.received_unit), ''), NULLIF(TRIM(oi.unit), ''))) = 1
THEN MAX(COALESCE(NULLIF(TRIM(oi.received_unit), ''), NULLIF(TRIM(oi.unit), ''))) ELSE NULL END
FROM order_items oi
WHERE oi.order_code = o.order_code
AND COALESCE(oi.is_returned, false) = false
AND (COALESCE(oi.received_stage, 0) >= 10 OR o.order_status >= 90)
) AS received_unit,
COALESCE(o.pending_backfill, false) AS pending_backfill,
o.auto_advanced_from_status,
to_char(o.delivery_date::date, 'YYYY-MM-DD') AS delivery_date,
@@ -688,6 +708,8 @@ export async function listOrdersForManagement(
order_status: storedStatus,
total_amount: Number(row.total_amount) || 0,
total_weight: Number(row.total_weight) || 0,
received_qty: Number(row.received_qty) || 0,
received_unit: row.received_unit ? String(row.received_unit) : null,
created_at: String(row.created_at),
updated_at: row.updated_at ? String(row.updated_at) : null,
delivery_date: normalizeDateOnly(row.delivery_date),
@@ -1,6 +1,6 @@
import type { PoolClient } from 'pg';
import { pool } from '../db';
import { orderReceivedQtyDb, writeOrderItemReceivedQty } from './orderItemReceivedQtyService';
import { orderReceivedQtyDb, writeOrderItemReceivedQtyWithStockCover } from './orderItemReceivedQtyService';
import { assertPurchaseOrderInBranch } from './purchaserBranchScope';
import { PURCHASE_STATUS_QUALITY_REJECTED } from './purchaseInboundService';
import { ensurePurchaseInspectionForCompletedPurchase } from './inspectionComplianceService';
@@ -237,7 +237,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 writeOrderItemReceivedQty(orderReceivedQtyDb(client), {
await writeOrderItemReceivedQtyWithStockCover(orderReceivedQtyDb(client), {
orderCode,
skuCode,
qty: Number(actualQty),
@@ -4162,6 +4162,14 @@ export const OrderManagement: React.FC = () => {
<p className="app-foreground-muted text-xs">{o.total_weight}kg</p>
</td>
);
case 'received_qty':
return (
<td key={col.id} className="app-foreground px-6 py-4 text-sm tabular-nums">
{Number(o.received_qty) > 0
? `${formatMoneyNumber(Number(o.received_qty), locale)}${o.received_unit || ''}`
: '-'}
</td>
);
case 'created_at': {
const dateText = o.delivery_date
? formatYmdLocale(o.delivery_date, locale)
@@ -23,6 +23,10 @@ export interface Order {
order_status: number;
total_amount: number;
total_weight: number;
/** 订单行持久实收合计(stage>=10;完结历史单回退 quantity) */
received_qty?: number | null;
/** 所有实收行单位一致时返回该单位,多单位返回 null */
received_unit?: string | null;
delivery_date: string;
delivery_time_slot: string;
customer_delivery_time?: string | null;
@@ -637,6 +641,7 @@ export type OrderListColumnId =
| 'order_info'
| 'customer'
| 'amount_weight'
| 'received_qty'
| 'created_at'
| 'status'
| 'actions';
@@ -644,6 +649,7 @@ export type OrderListColumnId =
export const ORDER_LIST_REORDER_COLUMNS: Array<TableColumnDef<OrderListColumnId>> = [
{ id: 'customer', label: '客户' },
{ id: 'amount_weight', label: '金额/重量' },
{ id: 'received_qty', label: '实收数量' },
{ id: 'created_at', label: '下单时间' },
{ id: 'order_info', label: '订单信息' },
{ id: 'status', label: '状态' },