CREATE TRIGGER [dbo].[Tu_CallFactor]
   ON  [dbo].[TmpDrugH]
   AFTER UPDATE
AS 
BEGIN
  IF UPDATE(Kind_Save)
  Begin
    Declare @KindSave Char(1)
    DECLARE @UserActive SMALLINT
    SELECT @UserActive = Manager.dbo.SessionUser(@@spid,1)
    Select @KindSave = Kind_save From Deleted
    IF ((Select Kind_Save From inserted) = '1')  And (@KindSave = '2' or @KindSave = '0')
    Begin
     Declare @lastId Bigint
     Insert into History_TmpDrugH(Kind_Flag, kind_Save, Nu_Forosh, Nu_Bargasht_Forosh, Note_Code, Dumy_Code,
                                     Grade_Code, Nezam_No, Trans_Name, Resive_Name, Sazman_Code, Computer_Code,
                                     User_id, CallUser, Tarikh, CallTarikh,Tarikh_Etebar, Tarikh_Noskheh, Tarikh_Paziresh, NumberQueue )
                                     
     Select Kind_Flag, @KindSave, Nu_Forosh, Nu_Bargasht_Forosh, Note_Code, Dumy_Code,
                                     Grade_Code, Nezam_No, Trans_Name, Resive_Name, Sazman_Code, Computer_Code,
                                     User_id, @UserActive,Tarikh, Getdate(),Tarikh_Etebar, Tarikh_Noskheh, Tarikh_Paziresh,NumberQueue From inserted                                      
     IF @@Error > 0  
       ROLLBACK TRANSACTION 
   
     Select @LastId=Scope_Identity() 
     Insert into History_TmpDrugHavaleh(Id_Havaleh, K_Code, Radif, Sender, 
                                           Reciver,K_Qty1,Price_Kharid,Price_Forosh,
                                           Main_Price, Status,BimarPercent)
     Select @lastId, K_Code, Radif, Sender, Reciver,K_Qty1,Price_Kharid,Price_Forosh,
            Main_Price, Status,BimarPercent From TmpDrugHavaleh Where Id_Havaleh = (Select Id_Havaleh From inserted)                                                                               
    end
  End
END
