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

45 lines
3.2 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_PurchaseOrderSend"(p_id uuid, p_user_id uuid DEFAULT NULL)
LANGUAGE plpgsql AS $$
DECLARE v_tenant uuid; v_no varchar(32); v_email varchar(256); v_supplier varchar(256);
v_subject varchar(256); v_lines text; v_total numeric; v_cur varchar(8);
BEGIN
SELECT o."TenantId", o."OrderNo", c."Email", c."Name", o."TotalAmount", o."CurrencyId"
INTO v_tenant, v_no, v_email, v_supplier, v_total, v_cur
FROM "Prc_T_PurchaseOrder" o LEFT JOIN "Adm_T_Contact" c ON c."Id" = o."SupplierId"
WHERE o."Id" = p_id;
IF NOT EXISTS (SELECT 1 FROM "Prc_T_PurchaseOrderLine" WHERE "PurchaseOrderId" = p_id AND "IsDeleted" = false) THEN
RAISE EXCEPTION 'Kalemi olmayan sipariş gönderilemez.' USING ERRCODE = 'P0001', DETAIL = '50185';
END IF;
IF COALESCE(v_email, '') = '' THEN
RAISE EXCEPTION 'Tedarikçinin kontak kartında e-posta adresi yok.' USING ERRCODE = 'P0001', DETAIL = '50186';
END IF;
v_subject := 'Satınalma Siparişi ' || v_no;
SELECT string_agg('<tr><td>' || COALESCE(p."Name", '') || '</td><td>' || COALESCE(l."Description", '') || '</td><td>'
|| TRIM(TO_CHAR(l."Quantity", 'FM999G999G990D00')) || ' ' || l."UomId" || '</td><td>'
|| TRIM(TO_CHAR(l."UnitPrice", 'FM999G999G990D00')) || '</td><td>%'
|| TRIM(TO_CHAR(l."TaxRate", 'FM990')) || '</td><td>'
|| TRIM(TO_CHAR(l."Amount", 'FM999G999G990D00')) || '</td><td>'
|| COALESCE(TO_CHAR(l."DeliveryDate", 'DD.MM.YYYY'), '') || '</td></tr>', '' ORDER BY l."CreationTime")
INTO v_lines
FROM "Prc_T_PurchaseOrderLine" l LEFT JOIN "Inv_T_Product" p ON p."Id" = l."ProductId"
WHERE l."PurchaseOrderId" = p_id AND l."IsDeleted" = false;
-- Alıcı tedarikçinin e-postasıdır; tipin aktif Custom Mail kuralı gönderimi açar (kural kapatılırsa e-posta kuyruğa girmez).
INSERT INTO "Adm_T_Notification" ("Id", "TenantId", "NotificationRuleId", "NotificationTypeId", "NotificationChannel", "Identifier", "Subject", "Message",
"IsSent", "IsRead", "CreationTime", "CreatorId", "IsDeleted")
SELECT gen_random_uuid(), v_tenant, r."Id", t."Id", r."Channel", v_email, v_subject, LEFT(
'<p>Sayın ' || COALESCE(v_supplier, '') || ',</p><p>' || v_subject || ' aşağıda bilgilerinize sunulmuştur.</p>'
|| '<table border="1" cellpadding="4" cellspacing="0"><tr><th>Ürün</th><th>Açıklama</th><th>Miktar</th><th>Birim Fiyat</th><th>KDV</th><th>Tutar</th><th>Termin</th></tr>'
|| COALESCE(v_lines, '') || '</table><p><b>Toplam: ' || TRIM(TO_CHAR(v_total, 'FM999G999G990D00')) || ' ' || v_cur || '</b></p>',
2048), false, false, NOW() AT TIME ZONE 'UTC', p_user_id, false
FROM "Adm_T_NotificationType" t
INNER JOIN "Adm_T_NotificationRule" r ON r."NotificationTypeId" = t."Id" AND r."IsActive" = true AND r."IsCustomized" = false AND r."IsDeleted" = false
AND r."RecipientType" = 'Custom' AND r."RecipientId" IS NULL AND r."Channel" = 'Mail'
WHERE t."Name" = 'SatinalmaSiparisiGonderildi' AND t."IsDeleted" = false AND t."TenantId" IS NOT DISTINCT FROM v_tenant;
UPDATE "Prc_T_PurchaseOrder" SET "LastSentDate" = NOW() AT TIME ZONE 'UTC' WHERE "Id" = p_id;
END;
$$;