sozsoft-platform/configs/seeds/postgres/object/Prc_P_PurchaseRfqRecalc.sql
2026-10-11 22:57:23 +03:00

20 lines
1.3 KiB
PL/PgSQL
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

CREATE OR REPLACE PROCEDURE public."Prc_P_PurchaseRfqRecalc"(p_id uuid)
LANGUAGE plpgsql AS $$
BEGIN
-- TenderLineId alternatiflere kopyalanır; ihaledeki aynı kalemi kıyasta birleştiren anahtardır.
UPDATE "Prc_T_PurchaseRfqLine" SET "Amount" = ROUND("Quantity" * "UnitPrice", 2), "TenderLineId" = COALESCE("TenderLineId", "Id")
WHERE "PurchaseRfqId" = p_id AND "IsDeleted" = false;
UPDATE "Prc_T_PurchaseRfq" r SET
"UntaxedAmount" = COALESCE((SELECT SUM(l."Amount") FROM "Prc_T_PurchaseRfqLine" l WHERE l."PurchaseRfqId" = p_id AND l."IsDeleted" = false), 0),
"TaxAmount" = COALESCE((SELECT SUM(ROUND(l."Amount" * l."TaxRate" / 100, 2)) FROM "Prc_T_PurchaseRfqLine" l WHERE l."PurchaseRfqId" = p_id AND l."IsDeleted" = false), 0),
"SourceDocument" = (SELECT string_agg(DISTINCT m."RequestNo", ', ')
FROM "Prc_T_PurchaseRfqLine" l
INNER JOIN "Prc_T_MaterialRequestItem" i ON i."Id" = l."MaterialRequestItemId"
INNER JOIN "Prc_T_MaterialRequest" m ON m."Id" = i."MaterialRequestId"
WHERE l."PurchaseRfqId" = p_id AND l."IsDeleted" = false)
WHERE r."Id" = p_id;
UPDATE "Prc_T_PurchaseRfq" SET "TotalAmount" = "UntaxedAmount" + "TaxAmount" WHERE "Id" = p_id;
END;
$$;