sozsoft-platform/configs/seeds/sql/object/Prc_P_PurchaseOrderRecalc.sql
2026-10-12 13:27:54 +03:00

27 lines
1.6 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_PurchaseOrderRecalc]
@Id uniqueidentifier
AS
BEGIN
SET NOCOUNT ON;
UPDATE Prc_T_PurchaseOrderLine SET Amount = ROUND(ROUND(Quantity * UnitPrice, 2) * (1 + TaxRate / 100), 2)
WHERE PurchaseOrderId = @Id AND IsDeleted = 0;
-- Kaynak: teklif talebinden gelen siparişte teklif no, doğrudan yazılan siparişte talep numaraları.
UPDATE o SET
UntaxedAmount = ISNULL((SELECT SUM(ROUND(l.Quantity * l.UnitPrice, 2)) FROM Prc_T_PurchaseOrderLine l WHERE l.PurchaseOrderId = @Id AND l.IsDeleted = 0), 0),
TaxAmount = ISNULL((SELECT SUM(l.Amount - ROUND(l.Quantity * l.UnitPrice, 2)) FROM Prc_T_PurchaseOrderLine l WHERE l.PurchaseOrderId = @Id AND l.IsDeleted = 0), 0),
SourceDocument = COALESCE(q.RfqNo,
STUFF((SELECT DISTINCT N', ' + m.RequestNo
FROM Prc_T_PurchaseOrderLine 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.PurchaseOrderId = @Id AND l.IsDeleted = 0
FOR XML PATH(''), TYPE).value('.', 'nvarchar(512)'), 1, 2, N''))
FROM Prc_T_PurchaseOrder o
LEFT JOIN Prc_T_PurchaseRfq q ON q.Id = o.PurchaseRfqId
WHERE o.Id = @Id;
UPDATE Prc_T_PurchaseOrder SET TotalAmount = UntaxedAmount + TaxAmount WHERE Id = @Id;
EXEC Prc_P_MaterialRequestSyncStatus @Id;
END