fix(单位/抵扣): 收口下游结算回退与库存抵扣字段
下游 auto 无重量不再用件数扣库存;单位不一致 stock_cover_qty 标记全额、新列记录库存量;清理新列;BOM 读回;人工申请用计划量。
This commit is contained in:
@@ -617,7 +617,8 @@ export async function deductInventoryForOrderDeparture(
|
||||
row.settlement_mode,
|
||||
row.picked_qty ?? row.loaded_qty,
|
||||
row.picked_weight,
|
||||
row.inventory_unit || row.unit
|
||||
row.inventory_unit || row.unit,
|
||||
row.unit
|
||||
) ||
|
||||
(options?.fallbackToRequiredQty ? Number(row.required_qty) || 0 : 0);
|
||||
if (qty <= 0) continue;
|
||||
@@ -1008,7 +1009,8 @@ async function planDeductLinesForOrder(
|
||||
row.settlement_mode,
|
||||
row.picked_qty ?? row.loaded_qty,
|
||||
row.picked_weight,
|
||||
row.inventory_unit || row.unit
|
||||
row.inventory_unit || row.unit,
|
||||
row.unit
|
||||
) || 0;
|
||||
if (qty <= 0) continue;
|
||||
const itemKey = trimSku(row.order_item_id) || String(row.id);
|
||||
|
||||
@@ -670,7 +670,7 @@ export async function applyStockCoverForOrderItem(
|
||||
|
||||
if (!orderItemId || !orderCode || !skuCode || need <= 0) {
|
||||
if (orderItemId) {
|
||||
await db.query(`UPDATE order_items SET stock_cover_qty = $2, ${STOCK_COVER_SUPPLIER_SQL} WHERE id = $1::uuid`, [orderItemId, 0]);
|
||||
await db.query(`UPDATE order_items SET stock_cover_qty = $2, stock_cover_stock_qty = 0, stock_cover_stock_unit = NULL, ${STOCK_COVER_SUPPLIER_SQL} WHERE id = $1::uuid`, [orderItemId, 0]);
|
||||
await releaseReservationsForOrderItem(db, orderItemId);
|
||||
await clearAutoCoverBatchPlan(db, [orderItemId]);
|
||||
}
|
||||
@@ -679,7 +679,7 @@ export async function applyStockCoverForOrderItem(
|
||||
|
||||
const enabled = await isStockCoverFromInventoryEnabled(input.branchCode, db);
|
||||
if (!enabled) {
|
||||
await db.query(`UPDATE order_items SET stock_cover_qty = $2, ${STOCK_COVER_SUPPLIER_SQL} WHERE id = $1::uuid`, [orderItemId, 0]);
|
||||
await db.query(`UPDATE order_items SET stock_cover_qty = $2, stock_cover_stock_qty = 0, stock_cover_stock_unit = NULL, ${STOCK_COVER_SUPPLIER_SQL} WHERE id = $1::uuid`, [orderItemId, 0]);
|
||||
await releaseReservationsForOrderItem(db, orderItemId);
|
||||
await clearAutoCoverBatchPlan(db, [orderItemId]);
|
||||
return 0;
|
||||
@@ -836,12 +836,12 @@ export async function applyStockCoverForOrderItem(
|
||||
});
|
||||
await db.query(
|
||||
`UPDATE order_items
|
||||
SET stock_cover_qty = $2,
|
||||
stock_cover_stock_qty = CASE WHEN $3::boolean THEN $2::numeric ELSE COALESCE(stock_cover_stock_qty, $2) END,
|
||||
stock_cover_stock_unit = CASE WHEN $3::boolean THEN $4::varchar ELSE COALESCE(stock_cover_stock_unit, unit) END,
|
||||
SET stock_cover_qty = CASE WHEN $3::boolean THEN $5::numeric ELSE $2::numeric END,
|
||||
stock_cover_stock_qty = CASE WHEN $3::boolean THEN $2::numeric ELSE $2::numeric END,
|
||||
stock_cover_stock_unit = CASE WHEN $3::boolean THEN $4::varchar ELSE unit END,
|
||||
${STOCK_COVER_SUPPLIER_SQL}
|
||||
WHERE id = $1::uuid`,
|
||||
[orderItemId, cover, unitMismatch, inventoryUnit || null]
|
||||
[orderItemId, cover, unitMismatch, inventoryUnit || null, need]
|
||||
);
|
||||
if (unitMismatch && cover > 1e-9) {
|
||||
await db.query(
|
||||
@@ -852,9 +852,9 @@ export async function applyStockCoverForOrderItem(
|
||||
if (cover > 1e-9) {
|
||||
await db.query(
|
||||
`UPDATE order_item_fulfillments
|
||||
SET inventory_cover_unit = $2, updated_at = NOW()
|
||||
SET inventory_cover_qty = $3, inventory_cover_unit = $2, updated_at = NOW()
|
||||
WHERE order_item_id = $1::uuid`,
|
||||
[orderItemId, unitMismatch ? inventoryUnit : null]
|
||||
[orderItemId, unitMismatch ? inventoryUnit : null, cover]
|
||||
).catch(() => undefined);
|
||||
}
|
||||
|
||||
@@ -1140,7 +1140,7 @@ export async function clearCoverForReturnedItem(
|
||||
);
|
||||
if (!returnedRow.rowCount || returnedRow.rows[0]?.is_returned !== true) return false;
|
||||
|
||||
await db.query(`UPDATE order_items SET stock_cover_qty = $2, ${STOCK_COVER_SUPPLIER_SQL} WHERE id = $1::uuid`, [orderItemId, 0]);
|
||||
await db.query(`UPDATE order_items SET stock_cover_qty = $2, stock_cover_stock_qty = 0, stock_cover_stock_unit = NULL, ${STOCK_COVER_SUPPLIER_SQL} WHERE id = $1::uuid`, [orderItemId, 0]);
|
||||
// 释放预留(内部会一并清 FIFO 批次计划与「我方指定抵扣批次」)
|
||||
await releaseReservationsForOrderItem(db, orderItemId);
|
||||
|
||||
@@ -1193,7 +1193,7 @@ export async function clearCoverForPendingOrWarehouseProcessing(
|
||||
if (owner === PROCESSING_OWNER_SUPPLIER) return false;
|
||||
|
||||
if (Number(row.rows[0]?.stock_cover_qty) > 1e-9) {
|
||||
await db.query(`UPDATE order_items SET stock_cover_qty = $2, ${STOCK_COVER_SUPPLIER_SQL} WHERE id = $1::uuid`, [orderItemId, 0]);
|
||||
await db.query(`UPDATE order_items SET stock_cover_qty = $2, stock_cover_stock_qty = 0, stock_cover_stock_unit = NULL, ${STOCK_COVER_SUPPLIER_SQL} WHERE id = $1::uuid`, [orderItemId, 0]);
|
||||
// 释放预留(内部会一并清 FIFO 批次计划与「我方指定抵扣批次」)
|
||||
await releaseReservationsForOrderItem(db, orderItemId);
|
||||
const orderCode = String(input.orderCode || '').trim();
|
||||
@@ -1278,7 +1278,7 @@ async function cancelStockCoverForOrderItemTx(
|
||||
}
|
||||
|
||||
await db.query(
|
||||
`UPDATE order_items SET stock_cover_qty = $2, stock_cover_locked = true, ${STOCK_COVER_SUPPLIER_SQL} WHERE id = $1::uuid`,
|
||||
`UPDATE order_items SET stock_cover_qty = $2, stock_cover_stock_qty = 0, stock_cover_stock_unit = NULL, stock_cover_locked = true, ${STOCK_COVER_SUPPLIER_SQL} WHERE id = $1::uuid`,
|
||||
[orderItemId, 0]
|
||||
);
|
||||
// 释放活跃预留(内部会一并清 FIFO 批次计划与「我方指定抵扣批次」)
|
||||
@@ -1380,7 +1380,7 @@ export async function releaseReservationsForOrder(db: Db, orderCode: string): Pr
|
||||
[code]
|
||||
);
|
||||
await db.query(
|
||||
`UPDATE order_items SET stock_cover_qty = $2, ${STOCK_COVER_SUPPLIER_SQL} WHERE order_code = $1`,
|
||||
`UPDATE order_items SET stock_cover_qty = $2, stock_cover_stock_qty = 0, stock_cover_stock_unit = NULL, ${STOCK_COVER_SUPPLIER_SQL} WHERE order_code = $1`,
|
||||
[code, 0]
|
||||
);
|
||||
// 抵扣释放 ⇒ 自动写的 FIFO 批次计划同步撤掉(否则会推送一份"不会出库的批次"的报告)
|
||||
@@ -1651,7 +1651,7 @@ export async function sweepReturnedItemsWithCoverResidue(
|
||||
const skuCode = String(row.sku_code || '');
|
||||
try {
|
||||
const didVoid = await runWithTenantDbContext(branch, async () => {
|
||||
await pool.query(`UPDATE order_items SET stock_cover_qty = $2, ${STOCK_COVER_SUPPLIER_SQL} WHERE id = $1::uuid`, [itemId, 0]);
|
||||
await pool.query(`UPDATE order_items SET stock_cover_qty = $2, stock_cover_stock_qty = 0, stock_cover_stock_unit = NULL, ${STOCK_COVER_SUPPLIER_SQL} WHERE id = $1::uuid`, [itemId, 0]);
|
||||
// 释放预留(内部会一并清 FIFO 批次计划与「我方指定抵扣批次」)
|
||||
await releaseReservationsForOrderItem(pool as unknown as Db, itemId);
|
||||
/**
|
||||
|
||||
@@ -347,10 +347,13 @@ export function normalizePickingSavedMeasures(
|
||||
export function resolvePickingSettlementQty(
|
||||
pickedQty: unknown,
|
||||
pickedWeight: unknown,
|
||||
unit?: string | null
|
||||
unit?: string | null,
|
||||
/** 订单单位;库存按重量且订单不是重量单位时,无重量不得回退成件数 */
|
||||
orderUnit?: string | null
|
||||
): number {
|
||||
const { picked_qty, picked_weight } = normalizePickingSavedMeasures(pickedQty, pickedWeight, unit);
|
||||
if (isPickingWeightUnit(unit)) {
|
||||
if (isPickingWeightUnit(orderUnit) === false && picked_weight <= 0) return 0;
|
||||
return picked_weight > 0 ? picked_weight : picked_qty;
|
||||
}
|
||||
return picked_qty > 0 ? picked_qty : 0;
|
||||
@@ -469,7 +472,8 @@ export function resolvePickingSettlementQtyByMode(
|
||||
mode: unknown,
|
||||
pickedQty: unknown,
|
||||
pickedWeight: unknown,
|
||||
unit?: string | null
|
||||
unit?: string | null,
|
||||
orderUnit?: string | null
|
||||
): number | null {
|
||||
const normalized = String(mode || 'auto').trim() as PickingSettlementMode;
|
||||
if (normalized === 'weight') {
|
||||
@@ -480,7 +484,7 @@ export function resolvePickingSettlementQtyByMode(
|
||||
const qty = Number(pickedQty) || 0;
|
||||
return qty > 0 ? qty : null;
|
||||
}
|
||||
return resolvePickingSettlementQty(pickedQty, pickedWeight, unit);
|
||||
return resolvePickingSettlementQty(pickedQty, pickedWeight, unit, orderUnit);
|
||||
}
|
||||
|
||||
export function normalizeSettlementMode(value: unknown): PickingSettlementMode {
|
||||
|
||||
@@ -1117,7 +1117,8 @@ async function syncPickingReceivedQty(
|
||||
row.settlement_mode,
|
||||
row.picked_qty,
|
||||
row.picked_weight,
|
||||
String(row.inventory_unit || row.unit || '')
|
||||
String(row.inventory_unit || row.unit || ''),
|
||||
String(row.unit || '')
|
||||
) || 0;
|
||||
if (!orderCode || qty <= 0) continue;
|
||||
await writeOrderItemReceivedQty(orderReceivedQtyDb(client), {
|
||||
@@ -1208,7 +1209,7 @@ export async function updatePickingItemProgress(input: {
|
||||
);
|
||||
}
|
||||
const settlement =
|
||||
resolvePickingSettlementQtyByMode(settlementMode, saved.picked_qty, saved.picked_weight, inventoryUnit || unit) || 0;
|
||||
resolvePickingSettlementQtyByMode(settlementMode, saved.picked_qty, saved.picked_weight, inventoryUnit || unit, unit) || 0;
|
||||
if (settlement <= 0) {
|
||||
throw Object.assign(new Error('实拣数量/重量必须大于 0,不能按 0 确认分拣'), { status: 400 });
|
||||
}
|
||||
@@ -1742,7 +1743,7 @@ export async function confirmPickingComplete(input: {
|
||||
);
|
||||
}
|
||||
const settlement =
|
||||
resolvePickingSettlementQtyByMode(settlementMode, saved.picked_qty, saved.picked_weight, inventoryUnit || unit) || 0;
|
||||
resolvePickingSettlementQtyByMode(settlementMode, saved.picked_qty, saved.picked_weight, inventoryUnit || unit, unit) || 0;
|
||||
if (settlement <= 0) {
|
||||
throw Object.assign(new Error('实拣数量/重量必须大于 0,不能按 0 完成分拣'), { status: 400 });
|
||||
}
|
||||
@@ -2688,7 +2689,8 @@ export async function outboundPickingByContainer(input: {
|
||||
item.settlement_mode,
|
||||
item.picked_qty,
|
||||
item.picked_weight,
|
||||
String(item.inventory_unit || item.unit || '')
|
||||
String(item.inventory_unit || item.unit || ''),
|
||||
String(item.unit || '')
|
||||
) || 0;
|
||||
if (!skipWarehouse && outboundQty <= 0) {
|
||||
throw businessError('该件实拣数量/重量为 0,不能出库');
|
||||
|
||||
@@ -175,6 +175,8 @@ import {
|
||||
resolveOrderItemReceivedQty,
|
||||
normalizeReceivedQtySupplierKey,
|
||||
resolveCoverUnitCost,
|
||||
planDisplayQty,
|
||||
planDisplayUnit,
|
||||
pickInboundPhotosForLine,
|
||||
pickQualityReportFilesForLine,
|
||||
normalizeInboundPhotoSupplier,
|
||||
@@ -4663,8 +4665,8 @@ export const OrderManagement: React.FC = () => {
|
||||
setPriceRequestItemId(String(first?.id || ''));
|
||||
setPriceRequestForm({
|
||||
price: String(Number(first?.cost_price ?? (first as any)?.unit_cost) || ''),
|
||||
qty: String(Number(first?.quantity) || ''),
|
||||
unit: String(first?.unit || ''),
|
||||
qty: String(planDisplayQty(first || {}) || ''),
|
||||
unit: planDisplayUnit(first || {}),
|
||||
reason: '',
|
||||
});
|
||||
setPriceRequestOpen(true);
|
||||
@@ -5228,8 +5230,8 @@ export const OrderManagement: React.FC = () => {
|
||||
setPriceRequestItemId(id);
|
||||
setPriceRequestForm({
|
||||
price: String(Number(item?.cost_price ?? (item as any)?.unit_cost) || ''),
|
||||
qty: String(Number(item?.quantity) || ''),
|
||||
unit: String(item?.unit || ''),
|
||||
qty: String(planDisplayQty(item || {}) || ''),
|
||||
unit: planDisplayUnit(item || {}),
|
||||
reason: priceRequestForm.reason,
|
||||
});
|
||||
}}
|
||||
|
||||
@@ -214,7 +214,7 @@ function chunkBySize<T>(items: T[], size: number): T[][] {
|
||||
}
|
||||
|
||||
const CATALOG_PRODUCT_LIST_COLUMNS =
|
||||
'code, name, parent_code, unit, pricing_unit, pack_factors, spec, barcode, image_url, default_loss_threshold, is_prepackaged, stock_mode, brand, tax_code, tax_rate, status, direct_delivery, low_stock_alert_qty, storage_zone, created_at';
|
||||
'code, name, parent_code, unit, pricing_unit, pack_factors, spec, barcode, image_url, default_loss_threshold, is_prepackaged, stock_mode, brand, tax_code, tax_rate, status, direct_delivery, low_stock_alert_qty, storage_zone, processing_bom, created_at';
|
||||
|
||||
function applyCatalogCategoryFilter<T extends { emptyOrNull: (col: string) => T; eq: (col: string, val: string) => T }>(
|
||||
query: T,
|
||||
|
||||
Reference in New Issue
Block a user