/****** Object:  Trigger [dbo].[AccFactor_CheckDelete]    Script Date: 2/1/2025 08:39:32 ق.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER TRIGGER [dbo].[AccFactor_CheckDelete]
ON [dbo].[AccFactor] INSTEAD OF DELETE 
	AS
		DECLARE @TaxUID as UNIQUEIDENTIFIER
		DECLARE @ID as bigint
		DECLARE @Number as bigint
		DECLARE @MSG as nvarchar(100)
		
		DECLARE DelCursor CURSOR LOCAL FAST_FORWARD READ_ONLY FOR 
			select TaxUID, ID, Number from Deleted
		OPEN DelCursor
		FETCH NEXT FROM DelCursor INTO @TaxUID, @ID, @Number
		WHILE @@FETCH_STATUS = 0 
		BEGIN
			if @TaxUID is null or @TaxUID = CAST(0x0 AS UNIQUEIDENTIFIER) 
			BEGIN
				delete From dbo.AccFactor where id=@ID
			END
			else
			BEGIN
				SET @MSG='امکان حذف فاکتور '+Str(@Number)+' ارسال شده به سامانه نیست'
			    RAISERROR(@MSG,16,20) WITH LOG, SETERROR
			END
		FETCH NEXT FROM DelCursor INTO @TaxUID, @ID, @Number
		END
		CLOSE DelCursor
		DEALLOCATE DelCursor
	return