| 37713 |
amit |
1 |
-- Record the vendor on stock received against orders raised before lines carried an origin (2026-09-18)
|
|
|
2 |
--
|
|
|
3 |
-- WHY
|
|
|
4 |
-- r37694 stamps a receipt with the origin held on its purchase order line, and lines only carry one from that
|
|
|
5 |
-- revision onward. Goods arriving on older open orders therefore landed with a cost but no vendor - 282 rows on the
|
|
|
6 |
-- first day alone - even where the order itself names an outside vendor we bought from.
|
|
|
7 |
-- The code now falls back to the order's own supplier; this repairs what was received before that fix.
|
|
|
8 |
--
|
|
|
9 |
-- Facts only: an outside vendor named on the unit's own purchase order. Internal movements are untouched, and stock
|
|
|
10 |
-- whose origin was never knowable keeps NULL.
|
|
|
11 |
|
|
|
12 |
USE warehouse;
|
|
|
13 |
|
|
|
14 |
CREATE TABLE warehouse._bak_receipt_origin_20260918 AS
|
|
|
15 |
SELECT ii.id, ii.origin_vendor_id, ii.receipt_unit_cost
|
|
|
16 |
FROM warehouse.inventoryItem ii
|
|
|
17 |
JOIN warehouse.purchase p ON p.id = ii.purchaseId
|
|
|
18 |
JOIN warehouse.purchaseorder po ON po.id = p.purchaseOrder_id
|
|
|
19 |
JOIN warehouse.supplier s ON s.id = po.supplierId AND s.internal = 0
|
|
|
20 |
WHERE ii.origin_vendor_id IS NULL;
|
|
|
21 |
|
|
|
22 |
SELECT COUNT(*) AS rows_to_repair FROM warehouse._bak_receipt_origin_20260918;
|
|
|
23 |
|
|
|
24 |
START TRANSACTION;
|
|
|
25 |
|
|
|
26 |
UPDATE warehouse.inventoryItem ii
|
|
|
27 |
JOIN warehouse._bak_receipt_origin_20260918 b ON b.id = ii.id
|
|
|
28 |
JOIN warehouse.purchase p ON p.id = ii.purchaseId
|
|
|
29 |
JOIN warehouse.purchaseorder po ON po.id = p.purchaseOrder_id
|
|
|
30 |
LEFT JOIN warehouse.lineitem li ON li.purchaseOrder_id = po.id AND li.itemId = ii.itemId
|
|
|
31 |
SET ii.origin_vendor_id = po.supplierId,
|
|
|
32 |
ii.receipt_unit_cost = COALESCE(ii.receipt_unit_cost, li.unitPrice);
|
|
|
33 |
|
|
|
34 |
COMMIT;
|
|
|
35 |
|
|
|
36 |
-- Verify: repaired = 0 left, and no internal supplier ever recorded as an origin
|
|
|
37 |
SELECT (SELECT COUNT(*) FROM warehouse.inventoryItem ii
|
|
|
38 |
JOIN warehouse.purchase p ON p.id = ii.purchaseId
|
|
|
39 |
JOIN warehouse.purchaseorder po ON po.id = p.purchaseOrder_id
|
|
|
40 |
JOIN warehouse.supplier s ON s.id = po.supplierId AND s.internal = 0
|
|
|
41 |
WHERE ii.origin_vendor_id IS NULL) AS external_receipts_still_blank,
|
|
|
42 |
(SELECT COUNT(*) FROM warehouse.inventoryItem ii
|
|
|
43 |
JOIN warehouse.supplier s ON s.id = ii.origin_vendor_id AND s.internal = 1) AS internal_wrongly_recorded,
|
|
|
44 |
(SELECT COUNT(*) FROM warehouse.inventoryItem WHERE currentQuantity > 0 AND origin_vendor_id IS NULL) AS in_stock_unknown;
|
|
|
45 |
|
|
|
46 |
-- Rollback:
|
|
|
47 |
-- UPDATE warehouse.inventoryItem ii JOIN warehouse._bak_receipt_origin_20260918 b ON b.id = ii.id
|
|
|
48 |
-- SET ii.origin_vendor_id = b.origin_vendor_id, ii.receipt_unit_cost = b.receipt_unit_cost;
|