2026-10-11 14:01:11 +00:00
|
|
|
CREATE OR REPLACE PROCEDURE public."Prc_P_PurchaseRfqFromAgreement"(p_id uuid)
|
|
|
|
|
LANGUAGE plpgsql AS $$
|
|
|
|
|
DECLARE v_agr uuid; v_tenant uuid;
|
|
|
|
|
BEGIN
|
2026-10-11 19:57:23 +00:00
|
|
|
SELECT "AgreementId", "TenantId" INTO v_agr, v_tenant FROM "Prc_T_PurchaseRfq" WHERE "Id" = p_id AND "Status" IN ('Draft', 'Sent');
|
2026-10-11 14:01:11 +00:00
|
|
|
IF v_agr IS NULL THEN RETURN; END IF;
|
2026-10-11 19:57:23 +00:00
|
|
|
IF EXISTS (SELECT 1 FROM "Prc_T_PurchaseRfqLine" WHERE "PurchaseRfqId" = p_id AND "IsDeleted" = false) THEN RETURN; END IF;
|
2026-10-11 14:01:11 +00:00
|
|
|
|
2026-10-11 19:57:23 +00:00
|
|
|
INSERT INTO "Prc_T_PurchaseRfqLine" ("Id", "TenantId", "PurchaseRfqId", "ProductId", "Description", "Quantity", "UomId",
|
2026-10-11 14:01:11 +00:00
|
|
|
"UnitPrice", "TaxRate", "Amount", "CreationTime", "IsDeleted")
|
2026-10-11 19:57:23 +00:00
|
|
|
SELECT gen_random_uuid(), v_tenant, p_id, al."ProductId", al."Description", al."Quantity" - al."OrderedQuantity",
|
|
|
|
|
al."UomId", al."UnitPrice", COALESCE(p."PurchaseTaxRate", 0), 0, NOW() AT TIME ZONE 'UTC', false
|
2026-10-11 14:01:11 +00:00
|
|
|
FROM "Prc_V_PurchaseAgreementLine" al
|
|
|
|
|
LEFT JOIN "Inv_T_Product" p ON p."Id" = al."ProductId"
|
|
|
|
|
WHERE al."AgreementId" = v_agr AND al."IsDeleted" = false
|
|
|
|
|
AND al."Quantity" - al."OrderedQuantity" > 0;
|
|
|
|
|
END;
|
|
|
|
|
$$;
|