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

31 lines
1.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_PurchaseOrderSetStatus]
@Id uniqueidentifier,
@Action nvarchar(16),
@UserId uniqueidentifier = NULL
AS
BEGIN
SET NOCOUNT ON;
DECLARE @Status nvarchar(16);
SELECT @Status = Status FROM Prc_T_PurchaseOrder WHERE Id = @Id;
UPDATE Prc_T_PurchaseOrder SET PendingAction = NULL WHERE Id = @Id;
IF @Action = 'Send'
BEGIN
IF @Status = 'Cancelled' THROW 50121, N'İptal edilmiş belge gönderilemez.', 1;
EXEC Prc_P_PurchaseOrderSend @Id, @UserId;
END
ELSE IF @Action = 'Confirm'
BEGIN
IF @Status NOT IN ('Draft', 'Sent') THROW 50122, N'Yalnız teklif aşamasındaki belge siparişe çevrilebilir.', 1;
IF NOT EXISTS (SELECT 1 FROM Prc_T_PurchaseOrderLine WHERE PurchaseOrderId = @Id AND IsDeleted = 0)
THROW 50123, N'Kalemi olmayan teklif onaylanamaz.', 1;
UPDATE Prc_T_PurchaseOrder SET Status = 'Purchase', ConfirmDate = GETUTCDATE() WHERE Id = @Id;
END
ELSE IF @Action = 'Cancel'
UPDATE Prc_T_PurchaseOrder SET Status = 'Cancelled' WHERE Id = @Id;
ELSE IF @Action = 'Draft'
BEGIN
IF @Status <> 'Cancelled' THROW 50124, N'Yalnız iptal edilmiş belge taslağa alınabilir.', 1;
UPDATE Prc_T_PurchaseOrder SET Status = 'Draft', ConfirmDate = NULL WHERE Id = @Id;
END
END