fix(procurement): reconcile purchase order lines at commit
This commit is contained in:
parent
467690eb2b
commit
f7dfa9ea47
|
|
@ -17,3 +17,4 @@ Phase 1E adds pricing policies, immutable final-price snapshots and commerce eve
|
|||
Phase 2A appends supplier, material and supplier-material compatibility tables. It grants procurement permissions to existing system roles; custom roles must be updated through the RBAC API.
|
||||
Phase 2B appends purchase-order headers and immutable line snapshots. No existing migration is modified.
|
||||
The subsequent purchase-order integrity migration adds database guards for header state changes and line snapshot edits; historical migrations remain unchanged.
|
||||
The next reconciliation migration checks line arithmetic and the order total at transaction commit, including nested line creation.
|
||||
|
|
|
|||
|
|
@ -14,5 +14,6 @@
|
|||
"20260911111403_operations_integrity": "f40bddf9e29d6518bc765cbaca7688c04cb95d5ff729c52e7dc775eefa1e521e",
|
||||
"20260913140451_supplier_materials": "c1f59e082ddf97443c17d52745068d9e6cbafb3d4e1088126a150f555c1cf0a1",
|
||||
"20260913173146_purchase_orders": "1e226f158e57af94c03b8f749349d06a874a8e82c32094ebd638d0ec41dc762a",
|
||||
"20260923000116_purchase_order_integrity": "ed14ce76d1b29c8de890a89c8dd84216edd002e43732b75d0cdeab4fd8f51fb8"
|
||||
"20260923000116_purchase_order_integrity": "ed14ce76d1b29c8de890a89c8dd84216edd002e43732b75d0cdeab4fd8f51fb8",
|
||||
"20260923001040_purchase_order_reconciliation": "63ddf7618ff14916512597cc16af2de07e0c093d345bb35e607ed830d3ef067e"
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,26 @@
|
|||
ALTER TABLE purchase_order_lines ADD CONSTRAINT purchase_order_line_total_valid
|
||||
CHECK (line_total = ROUND(quantity * unit_price, 2));
|
||||
|
||||
CREATE FUNCTION reconcile_purchase_order_lines() RETURNS trigger LANGUAGE plpgsql AS $$
|
||||
DECLARE
|
||||
target_id UUID;
|
||||
expected NUMERIC;
|
||||
actual NUMERIC;
|
||||
line_count INTEGER;
|
||||
BEGIN
|
||||
IF TG_TABLE_NAME = 'purchase_orders' THEN target_id := NEW.id;
|
||||
ELSE target_id := NEW.purchase_order_id;
|
||||
END IF;
|
||||
SELECT merchandise_total INTO expected FROM purchase_orders WHERE id = target_id;
|
||||
SELECT COALESCE(SUM(line_total), 0), COUNT(*) INTO actual, line_count
|
||||
FROM purchase_order_lines WHERE purchase_order_id = target_id;
|
||||
IF expected IS DISTINCT FROM actual OR line_count = 0 THEN
|
||||
RAISE EXCEPTION 'Purchase order total does not match lines';
|
||||
END IF;
|
||||
RETURN NULL;
|
||||
END;
|
||||
$$;
|
||||
CREATE CONSTRAINT TRIGGER purchase_orders_reconcile_lines AFTER INSERT ON purchase_orders
|
||||
DEFERRABLE INITIALLY DEFERRED FOR EACH ROW EXECUTE FUNCTION reconcile_purchase_order_lines();
|
||||
CREATE CONSTRAINT TRIGGER purchase_order_lines_reconcile_total AFTER INSERT ON purchase_order_lines
|
||||
DEFERRABLE INITIALLY DEFERRED FOR EACH ROW EXECUTE FUNCTION reconcile_purchase_order_lines();
|
||||
|
|
@ -106,6 +106,31 @@ describe('purchase-order API', () => {
|
|||
data: { merchandiseTotal: '1.00' },
|
||||
}),
|
||||
).rejects.toThrow();
|
||||
const extraMaterial = await ctx.db.material.create({
|
||||
data: {
|
||||
organizationId: ctx.owner.organizationId,
|
||||
name: 'Unquoted material',
|
||||
code: `EXTRA-${randomUUID().slice(0, 8)}`,
|
||||
kind: 'WAX',
|
||||
unit: 'KILOGRAM',
|
||||
},
|
||||
});
|
||||
await expect(
|
||||
ctx.db.purchaseOrderLine.create({
|
||||
data: {
|
||||
purchaseOrderId: created.body.id,
|
||||
organizationId: ctx.owner.organizationId,
|
||||
materialId: extraMaterial.id,
|
||||
materialName: extraMaterial.name,
|
||||
materialCode: extraMaterial.code,
|
||||
unit: extraMaterial.unit,
|
||||
supplierSku: 'EXTRA',
|
||||
quantity: '1.000',
|
||||
unitPrice: '1.00',
|
||||
lineTotal: '1.00',
|
||||
},
|
||||
}),
|
||||
).rejects.toThrow();
|
||||
const issued = await ctx
|
||||
.api()
|
||||
.post(`/api/v1/purchase-orders/${created.body.id}/issue`)
|
||||
|
|
|
|||
Loading…
Reference in New Issue