sozsoft-platform/configs/seeds/sql/object/Prc_P_PurchaseOrderSend.sql
2026-10-11 17:01:11 +03:00

50 lines
3 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), @Status nvarchar(16), @Email nvarchar(256), @Supplier nvarchar(256),
@TypeName nvarchar(64), @Type uniqueidentifier, @Subject nvarchar(256), @Lines nvarchar(max), @Total decimal(18,4), @Cur nvarchar(8);
SELECT @Tenant = o.TenantId, @No = o.OrderNo, @Status = o.Status, @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 50111, N'Kalemi olmayan belge gönderilemez.', 1;
IF ISNULL(@Email, N'') = N''
THROW 50112, N'Tedarikçinin kontak kartında e-posta adresi yok.', 1;
IF @Status = 'Purchase'
SELECT @TypeName = N'Prc.PurchaseOrderSent', @Subject = N'Satınalma Siparişi ' + @No;
ELSE
SELECT @TypeName = N'Prc.RfqSent', @Subject = N'Teklif Talebi ' + @No;
SELECT TOP 1 @Type = Id FROM Adm_T_NotificationType
WHERE Name = @TypeName AND IsDeleted = 0 AND (TenantId = @Tenant OR (TenantId IS NULL AND @Tenant IS NULL));
SELECT @Lines = (SELECT N'<tr><td>' + ISNULL(p.Name, N'') + N'</td><td>' + ISNULL(l.Description, N'') + N'</td><td style="text-align:right">'
+ FORMAT(l.Quantity, 'N2', 'tr-TR') + N' ' + l.UomId + N'</td><td style="text-align:right">'
+ FORMAT(l.UnitPrice, 'N2', 'tr-TR') + N'</td><td style="text-align:right">%'
+ FORMAT(l.TaxRate, 'N0', 'tr-TR') + N'</td><td style="text-align:right">'
+ FORMAT(l.Amount, 'N2', 'tr-TR') + 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)');
INSERT INTO Adm_T_Notification (Id, TenantId, NotificationTypeId, NotificationChannel, Identifier, Subject, Message,
IsSent, IsRead, CreationTime, CreatorId, IsDeleted)
VALUES (NEWID(), @Tenant, @Type, N'Mail', @Email, @Subject,
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></tr>'
+ ISNULL(@Lines, N'') + N'</table><p><b>Toplam: ' + FORMAT(@Total, 'N2', 'tr-TR') + N' ' + @Cur + N'</b></p>',
0, 0, GETUTCDATE(), @UserId, 0);
UPDATE Prc_T_PurchaseOrder
SET LastSentDate = GETUTCDATE(),
Status = CASE WHEN Status = 'Draft' THEN 'Sent' ELSE Status END
WHERE Id = @Id;
END