Create  TRIGGER [dbo].[Del_Anbar] ON [dbo].[Havaleh] 
FOR DELETE 
AS

Declare @Sender varchar(8)
Declare @Reciver varchar(8)
Declare @KQty money
Declare @KCode varchar(20)
Declare @Tot_kharid Money
Declare @Tot_Forosh Money
Declare @Tot_Omdeh Money
Declare @IdHavaleh Bigint

---------------------------------------------------------------------(مراحل اصلاح موجودي در انبار)--------------------------------------------------------------
Declare Cursor_Insert Cursor For
Select  Sender, Reciver, K_Code, K_Qty1  From Deleted
Open Cursor_Insert
Fetch Next From  Cursor_Insert into @Sender, @Reciver, @KCode, @KQty 
While @@Fetch_Status = 0 
Begin
    IF Len(@Sender) = 3
        Update Anbar Set Mojodi = Mojodi +  @KQty ,  Exit_Fa = Exit_Fa - @KQty Where K_Code = @KCode And  A_Code  = @Sender
    IF Len(@Reciver) = 3
        Update Anbar Set Mojodi = Mojodi - @KQty , Resive_Fa  = Resive_Fa - @KQty Where K_Code = @KCode And  A_Code  = @Reciver
    Fetch Next From  Cursor_Insert into @Sender, @Reciver, @KCode, @KQty 
End
Close Cursor_Insert
Deallocate Cursor_Insert
