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

45 lines
3.1 KiB
Transact-SQL
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 ALTER PROCEDURE [dbo].[Prc_P_PurchaseOrderSend]
@Id uniqueidentifier,
@UserId uniqueidentifier = NULL
AS
BEGIN
SET NOCOUNT ON;
DECLARE @Tenant uniqueidentifier, @No nvarchar(32), @Email nvarchar(256), @Supplier nvarchar(256),
@Subject nvarchar(256), @Lines nvarchar(MAX), @Total decimal(18, 4), @Cur nvarchar(8);
SELECT @Tenant = o.TenantId, @No = o.OrderNo, @Email = c.Email, @Supplier = c.Name, @Total = o.TotalAmount, @Cur = o.CurrencyId
FROM Prc_T_PurchaseOrder o LEFT JOIN Adm_T_Contact c ON c.Id = o.SupplierId
WHERE o.Id = @Id;
IF NOT EXISTS (SELECT 1 FROM Prc_T_PurchaseOrderLine WHERE PurchaseOrderId = @Id AND IsDeleted = 0)
THROW 50185, N'Kalemi olmayan sipariş gönderilemez.', 1;
IF ISNULL(@Email, N'') = N''
THROW 50186, N'Tedarikçinin kontak kartında e-posta adresi yok.', 1;
SET @Subject = N'Satınalma Siparişi ' + @No;
SELECT @Lines = (SELECT N'<tr><td>' + ISNULL(p.Name, N'') + N'</td><td>' + ISNULL(l.Description, N'') + N'</td><td>'
+ FORMAT(l.Quantity, 'N2', 'tr-TR') + N' ' + l.UomId + N'</td><td>'
+ FORMAT(l.UnitPrice, 'N2', 'tr-TR') + N'</td><td>%'
+ FORMAT(l.TaxRate, 'N0', 'tr-TR') + N'</td><td>'
+ FORMAT(l.Amount, 'N2', 'tr-TR') + N'</td><td>' + ISNULL(FORMAT(l.DeliveryDate, 'dd.MM.yyyy'), N'') + N'</td></tr>'
FROM Prc_T_PurchaseOrderLine l LEFT JOIN Inv_T_Product p ON p.Id = l.ProductId
WHERE l.PurchaseOrderId = @Id AND l.IsDeleted = 0
ORDER BY l.CreationTime
FOR XML PATH(''), TYPE).value('.', 'nvarchar(max)');
-- 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 NEWID(), @Tenant, r.Id, t.Id, r.Channel, @Email, @Subject, LEFT(
N'<p>Sayın ' + ISNULL(@Supplier, N'') + N',</p><p>' + @Subject + N' aşağıda bilgilerinize sunulmuştur.</p>'
+ N'<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>'
+ ISNULL(@Lines, N'') + N'</table><p><b>Toplam: ' + FORMAT(@Total, 'N2', 'tr-TR') + N' ' + @Cur + N'</b></p>',
2048), 0, 0, GETUTCDATE(), @UserId, 0
FROM Adm_T_NotificationType t
INNER JOIN Adm_T_NotificationRule r ON r.NotificationTypeId = t.Id AND r.IsActive = 1 AND r.IsCustomized = 0 AND r.IsDeleted = 0
AND r.RecipientType = N'Custom' AND r.RecipientId IS NULL AND r.Channel = N'Mail'
WHERE t.Name = N'SatinalmaSiparisiGonderildi' AND t.IsDeleted = 0 AND (t.TenantId = @Tenant OR (t.TenantId IS NULL AND @Tenant IS NULL));
UPDATE Prc_T_PurchaseOrder SET LastSentDate = GETUTCDATE() WHERE Id = @Id;
END