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

38 lines
1.9 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_PurchaseRfqAfterSave]
@Id uniqueidentifier,
@UserId uniqueidentifier = NULL
AS
BEGIN
SET NOCOUNT ON;
DECLARE @Act nvarchar(16) = (SELECT PendingAction FROM Prc_T_PurchaseRfq WHERE Id = @Id);
IF EXISTS (SELECT 1 FROM Prc_T_PurchaseRfq WHERE Id = @Id AND PriceWeight + DeliveryWeight <> 100)
THROW 50153, N'Fiyat ve termin ağırlıklarının toplamı 100 olmalıdır.', 1;
-- İhalenin kökü ilk belgedir; alternatifler kökün Id'sini taşır.
UPDATE r SET
TenderGroupId = ISNULL(r.TenderGroupId, r.Id),
QuoteDate = CASE WHEN r.SupplierResponse <> 'Waiting' THEN ISNULL(r.QuoteDate, GETUTCDATE()) ELSE r.QuoteDate END,
PaymentTermId = ISNULL(r.PaymentTermId, c.PurchasePaymentTermId),
IncotermId = ISNULL(r.IncotermId, c.PurchaseIncotermId),
IncotermLocation = ISNULL(r.IncotermLocation, c.PurchaseIncotermLocation),
BuyerUserId = CASE WHEN r.LastModificationTime IS NULL AND c.BuyerUserId IS NOT NULL THEN c.BuyerUserId ELSE r.BuyerUserId END
FROM Prc_T_PurchaseRfq r
INNER JOIN Adm_T_Contact c ON c.Id = r.SupplierId
WHERE r.Id = @Id;
-- Kıyas ağırlıkları ihale geneli tek değerdir; hangi belgede değişirse diğerlerine yazılır.
UPDATE x SET PriceWeight = r.PriceWeight, DeliveryWeight = r.DeliveryWeight
FROM Prc_T_PurchaseRfq x
INNER JOIN Prc_T_PurchaseRfq r ON r.Id = @Id
WHERE x.TenderGroupId = r.TenderGroupId AND x.Id <> r.Id
AND (x.PriceWeight <> r.PriceWeight OR x.DeliveryWeight <> r.DeliveryWeight);
EXEC Prc_P_PurchaseRfqFromAgreement @Id;
EXEC Prc_P_PurchaseRfqRecalc @Id;
-- Durum butonlari islemi PendingAction alanina yazip ekranin guncelleme ucuyla kaydeder.
IF @Act IS NOT NULL
BEGIN
EXEC Prc_P_PurchaseRfqSetStatus @Id, @Act, @UserId;
EXEC Prc_P_MaterialRequestSyncStatus @Id;
END
END