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

46 lines
2.1 KiB
Transact-SQL
Raw Permalink 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_PurchaseRfqSetStatus]
@Id uniqueidentifier,
@Action nvarchar(16),
@UserId uniqueidentifier = NULL
AS
BEGIN
SET NOCOUNT ON;
DECLARE @Status nvarchar(16), @Group uniqueidentifier, @Draft uniqueidentifier;
SELECT @Status = Status, @Group = TenderGroupId FROM Prc_T_PurchaseRfq WHERE Id = @Id;
UPDATE Prc_T_PurchaseRfq SET PendingAction = NULL WHERE Id = @Id;
IF @Action IN ('Send', 'SendTender', 'Award') AND @Status NOT IN ('Draft', 'Sent')
THROW 50173, N'Yalnız teklif aşamasındaki (Teklif Talebi / Gönderildi) belge gönderilebilir ya da siparişe dönüştürülebilir.', 1;
IF @Action = 'Send'
EXEC Prc_P_PurchaseRfqSend @Id, @UserId;
ELSE IF @Action = 'SendTender'
BEGIN
-- İhalenin henüz gönderilmemiş bütün belgeleri (açık belge dahil) tedarikçilerine gider.
DECLARE drafts CURSOR LOCAL FAST_FORWARD FOR
SELECT Id FROM Prc_T_PurchaseRfq WHERE TenderGroupId = @Group AND IsDeleted = 0 AND Status = 'Draft';
OPEN drafts;
FETCH NEXT FROM drafts INTO @Draft;
WHILE @@FETCH_STATUS = 0
BEGIN
EXEC Prc_P_PurchaseRfqSend @Draft, @UserId;
FETCH NEXT FROM drafts INTO @Draft;
END
CLOSE drafts;
DEALLOCATE drafts;
END
ELSE IF @Action = 'Award'
EXEC Prc_P_PurchaseRfqAward @Id, @UserId;
ELSE IF @Action = 'Cancel'
BEGIN
IF @Status = 'Purchase' THROW 50174, N'Siparişe dönüştürülmüş teklif iptal edilemez; satınalma siparişini iptal edin.', 1;
UPDATE Prc_T_PurchaseRfq SET Status = 'Cancelled' WHERE Id = @Id;
END
ELSE IF @Action = 'Draft'
BEGIN
IF @Status <> 'Cancelled' THROW 50175, N'Yalnız iptal edilmiş teklif taslağa alınabilir.', 1;
IF EXISTS (SELECT 1 FROM Prc_T_PurchaseRfq WHERE TenderGroupId = @Group AND IsDeleted = 0 AND Status = 'Purchase')
THROW 50176, N'İhale siparişe dönüştürüldüğü için teklif yeniden açılamaz.', 1;
UPDATE Prc_T_PurchaseRfq SET Status = 'Draft' WHERE Id = @Id;
END
END