CREATE TRIGGER [dbo].[Less_Anbar] ON [dbo].[Havaleh]
FOR INSERT
AS 

Declare @Sender varchar(8)
Declare @Reciver varchar(8)
Declare @KQty money
Declare @KCode varchar(20)
Declare @IdHavaleh Bigint
Declare @Tot_kharid Money
Declare @Tot_Forosh Money
Declare @Tot_Omdeh Money
Declare @Mojodi Real
Declare @Price_Kharid     Money
Declare @Avg_Price_Kharid Money
Declare @Temp             Money
Declare @AutoId           BigInt
Declare @Kind_Flag Char(1)
DECLARE @UserId SMALLINT
DECLARE @JetId Int

  

-- در صورت تعريف نشدن کالاها در انبار فرستنده ايجاد گردد   
-- جدول انبار - جدول حوله = اطلاعات کالاهائي که در جدول انبار وجود ندارد
IF (Exists  (select  Sender,K_Code,Price_Kharid,Price_Forosh,Price_Omdeh  From inserted I
                 where not Exists (select A_Code,K_Code,Price_Kharid,Price_Forosh,Price_Omdeh from Anbar A
                                            where A.A_Code =I.Sender  and  A.K_Code=I.K_Code  ) and  Len(I.Sender) =  3  ) )  
Begin
     SELECT @UserId = User_Id FROM Headh WHERE Id_Havaleh = (SELECT TOP 1 Id_Havaleh FROM Inserted)
     INSERT Into Dbo.Anbar(A_Code ,K_Code , Price_Kharid ,Price_Forosh, Price_Omdeh, User_Id)  
                            Select  Sender ,  K_Code, Price_Kharid ,Price_Forosh ,Price_Omdeh, @UserId  from inserted I
                            where not Exists (select A_Code,K_Code,Price_Kharid,Price_Forosh,Price_Omdeh from Anbar A
                                                    where A.A_Code =I.Sender and  A.K_Code=I.K_Code  )  and Len(I.Sender) =  3
                            Group By  I.Sender ,  I.K_Code, I.Price_Kharid ,I.Price_Forosh ,I.Price_Omdeh  
End  

-- در صورت تعريف نشدن کالا در انبار فرستنده ايجاد گردد   
-- جدول انبار - جدول حوله گيرنده = اطلاعات کالاهائي که در جدول انبار وجود ندارد
IF   (Exists  (select Reciver,K_Code,Price_Kharid,Price_Forosh,Price_Omdeh  from inserted I
                 where not exists (select A_Code,K_Code,Price_Kharid,Price_Forosh,Price_Omdeh from Anbar A
                                            where Cast(A.A_Code As Int) = Cast(I.Reciver As Int ) and  A.K_Code=I.K_Code  ) and Len(I.Reciver) = 3 ) )  
Begin
     SELECT @UserId = User_Id FROM Headh WHERE Id_Havaleh = (SELECT TOP 1 Id_Havaleh FROM Inserted)
     INSERT Into Dbo.Anbar(A_Code,K_Code,Price_Kharid,Price_Forosh,Price_Omdeh, User_Id)  
                           Select  Reciver, K_Code,Price_Kharid,Price_Forosh,Price_Omdeh, @UserId  from inserted I
                           where not exists (select A_Code,K_Code,Price_Kharid,Price_Forosh,Price_Omdeh from Anbar A
                                                      where A.A_Code =I.Reciver   and  A.K_Code=I.K_Code  )  and Len(I.Reciver ) =  3
                           Group By  I.Reciver ,  I.K_Code, I.Price_Kharid ,I.Price_Forosh ,I.Price_Omdeh
End  


---------------------------------------------------------------------(مراحل اصلاح موجودي در انبار)--------------------------------------------------------------
Declare Cursor_Insert Cursor For
Select  Sender, Reciver, K_Code, K_Qty1, AutoId, Kind_Flag, Price_Kharid, JetId  From Inserted
Inner Join HeadH on HeadH.Id_Havaleh = Inserted.Id_Havaleh
Open Cursor_Insert
Fetch Next From  Cursor_Insert into @Sender, @Reciver, @KCode, @KQty, @AutoId, @Kind_Flag, @Price_Kharid, @JetId 
While @@Fetch_Status = 0 
Begin
	IF Len(@Sender) = 3
	Begin
		Update Anbar Set Mojodi = Mojodi - @KQty , 
		             Exit_Fa = Exit_Fa + @KQty Where K_Code = @KCode And  A_Code  = @Sender

    	IF @JetId IS NOT NULL
		   UPDATE dbo.JetPrint SET SaleQty = SaleQty + @KQty WHERE Id= @JetId	
	End   
    IF Len(@Reciver) = 3
    Begin
      Update Anbar Set Mojodi = Mojodi +  @KQty , Resive_Fa = Resive_Fa + @KQty Where K_Code = @KCode And  A_Code  = @Reciver

    End 
    Fetch Next From  Cursor_Insert into @Sender, @Reciver, @KCode, @KQty, @AutoId, @Kind_Flag,@Price_Kharid,@JetId 
End
Close Cursor_Insert
Deallocate Cursor_Insert
