sozsoft-platform/configs/seeds/postgres/object/Prc_P_PurchaseRfqFromAgreement.sql

20 lines
1 KiB
MySQL
Raw Normal View History

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
SELECT "AgreementId", "TenantId" INTO v_agr, v_tenant FROM "Prc_T_PurchaseOrder" WHERE "Id" = p_id;
IF v_agr IS NULL THEN RETURN; END IF;
IF EXISTS (SELECT 1 FROM "Prc_T_PurchaseOrderLine" WHERE "PurchaseOrderId" = p_id AND "IsDeleted" = false) THEN RETURN; END IF;
INSERT INTO "Prc_T_PurchaseOrderLine" ("Id", "TenantId", "PurchaseOrderId", "ProductId", "Description", "Quantity", "UomId",
"UnitPrice", "TaxRate", "Amount", "CreationTime", "IsDeleted")
SELECT gen_random_uuid(), v_tenant, p_id, al."ProductId", al."Description",
GREATEST(al."Quantity" - al."OrderedQuantity", 0), al."UomId", al."UnitPrice",
COALESCE(p."PurchaseTaxRate", 0), 0, CURRENT_TIMESTAMP, false
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;
$$;