fix(加工原料): 补齐原料待验独立入库标记与回链
镜像自采完成时标记 independent_inbound 并回写 inspection_id;历史数据迁移补齐,入库列表可见。
This commit is contained in:
@@ -0,0 +1,53 @@
|
||||
-- 加工原料待验可见性/回链补齐(历史行)
|
||||
-- 原料采购镜像到 purchase_orders 后,老数据可能已有待验单但:
|
||||
-- · processing_details 缺 independent_inbound=true ⇒ 入库列表不可见
|
||||
-- · processing_raw_purchases.inspection_id 为空
|
||||
DO $$
|
||||
DECLARE
|
||||
sch text;
|
||||
BEGIN
|
||||
FOR sch IN
|
||||
SELECT nspname FROM pg_namespace
|
||||
WHERE nspname IN ('legacy_public','tenant_template') OR nspname LIKE 'tenant\_%'
|
||||
ORDER BY nspname
|
||||
LOOP
|
||||
IF to_regclass(format('%I.inspections', sch)) IS NULL
|
||||
OR to_regclass(format('%I.purchase_orders', sch)) IS NULL
|
||||
OR to_regclass(format('%I.processing_raw_purchases', sch)) IS NULL THEN
|
||||
CONTINUE;
|
||||
END IF;
|
||||
|
||||
EXECUTE format(
|
||||
'UPDATE %I.inspections i
|
||||
SET processing_details = COALESCE(i.processing_details, ''{}''::jsonb)
|
||||
|| ''{"independent_inbound": true}''::jsonb
|
||||
WHERE i.order_code IS NULL
|
||||
AND i.inspection_time IS NULL
|
||||
AND COALESCE(i.processing_details->>''independent_inbound'', '''') <> ''true''
|
||||
AND (
|
||||
EXISTS (
|
||||
SELECT 1 FROM %I.purchase_orders po
|
||||
WHERE po.source_type = ''processing_raw_material''
|
||||
AND (po.purchase_code = i.processing_details->>''purchase_code''
|
||||
OR COALESCE(i.processing_details->''purchase_codes'', ''[]''::jsonb) ? po.purchase_code)
|
||||
)
|
||||
OR EXISTS (
|
||||
SELECT 1 FROM %I.processing_raw_purchases r
|
||||
WHERE r.purchase_code = i.processing_details->>''purchase_code''
|
||||
)
|
||||
)',
|
||||
sch, sch, sch
|
||||
);
|
||||
|
||||
EXECUTE format(
|
||||
'UPDATE %I.processing_raw_purchases r
|
||||
SET inspection_id = i.id, updated_at = NOW()
|
||||
FROM %I.inspections i
|
||||
WHERE r.inspection_id IS NULL
|
||||
AND i.inspection_time IS NULL
|
||||
AND (i.processing_details->>''purchase_code'' = r.purchase_code
|
||||
OR COALESCE(i.processing_details->''purchase_codes'', ''[]''::jsonb) ? r.purchase_code)',
|
||||
sch, sch
|
||||
);
|
||||
END LOOP;
|
||||
END $$;
|
||||
@@ -602,7 +602,7 @@ export async function ensurePurchaseInspectionForCompletedPurchase(
|
||||
|
||||
const poResult = await client.query(
|
||||
`SELECT purchase_code, order_code, sku_code, supplier_code, purchase_qty, purchase_price,
|
||||
actual_qty, actual_price, purchase_status, branch_code, self_processed
|
||||
actual_qty, actual_price, purchase_status, branch_code, source_type, self_processed
|
||||
FROM purchase_orders
|
||||
WHERE purchase_code = $1
|
||||
LIMIT 1`,
|
||||
@@ -628,6 +628,43 @@ export async function ensurePurchaseInspectionForCompletedPurchase(
|
||||
const prepareQty = Number(po.actual_qty ?? po.purchase_qty) || 0;
|
||||
const purchasePrice = Number(po.actual_price ?? po.purchase_price) || 0;
|
||||
if (prepareQty <= 0) return;
|
||||
const isProcessingRawPurchase = String(po.source_type || '').trim() === 'processing_raw_material';
|
||||
|
||||
const existingPending = await client.query(
|
||||
`SELECT id::text AS id
|
||||
FROM inspections
|
||||
WHERE processing_details->>'purchase_code' = $1
|
||||
AND inspection_time IS NULL
|
||||
AND inbound_batch_no IS NULL
|
||||
ORDER BY created_at ASC, id ASC
|
||||
LIMIT 1`,
|
||||
[code]
|
||||
);
|
||||
if (existingPending.rows[0]?.id) {
|
||||
const keepId = String(existingPending.rows[0].id);
|
||||
await client.query(
|
||||
`UPDATE inspections
|
||||
SET prepare_qty = $2::numeric,
|
||||
purchase_price = $3::numeric,
|
||||
processing_details = CASE
|
||||
WHEN $4::boolean
|
||||
THEN COALESCE(processing_details, '{}'::jsonb) || '{"independent_inbound": true}'::jsonb
|
||||
ELSE processing_details
|
||||
END
|
||||
WHERE id = $1::uuid
|
||||
AND inspection_time IS NULL
|
||||
AND inbound_batch_no IS NULL`,
|
||||
[keepId, prepareQty, purchasePrice, isProcessingRawPurchase]
|
||||
);
|
||||
if (isProcessingRawPurchase) {
|
||||
await client.query(
|
||||
`UPDATE processing_raw_purchases SET inspection_id = $1::uuid, updated_at = NOW()
|
||||
WHERE purchase_code = $2 AND inspection_id IS NULL`,
|
||||
[keepId, code]
|
||||
).catch(() => undefined);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
const matchKey = {
|
||||
orderCode: po.order_code || null,
|
||||
@@ -702,11 +739,22 @@ export async function ensurePurchaseInspectionForCompletedPurchase(
|
||||
purchasePrice,
|
||||
newBatchNo(),
|
||||
po.branch_code || null,
|
||||
JSON.stringify({ purchase_code: code, purchase_codes: [code] }),
|
||||
JSON.stringify({
|
||||
purchase_code: code,
|
||||
purchase_codes: [code],
|
||||
...(isProcessingRawPurchase ? { independent_inbound: true } : {}),
|
||||
}),
|
||||
itemName,
|
||||
]
|
||||
);
|
||||
const inspectionId = inserted.rows[0]?.id;
|
||||
if (inspectionId && isProcessingRawPurchase) {
|
||||
await client.query(
|
||||
`UPDATE processing_raw_purchases SET inspection_id = $1::uuid, updated_at = NOW()
|
||||
WHERE purchase_code = $2 AND inspection_id IS NULL`,
|
||||
[inspectionId, code]
|
||||
).catch(() => undefined);
|
||||
}
|
||||
if (inspectionId && po.order_code) {
|
||||
await ensureProcessingTaskForInspection(
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user