Create TRIGGER [dbo].[Ti_DrugH_Beiane] ON [dbo].[DrugH] 
FOR INSERT
AS
Declare @Msg Smallint
Declare @Par_No Varchar(12)
Declare @Bedeh Money
Declare @Bestan Money
Declare @Price Money
Declare @Id Bigint
Declare @Note nvarchar(120)
Declare @Code varchar(5)
Declare @StStatus  Char (1)

 Select   @Bedeh = Bedehkar , @Bestan = Bestankar, @Par_No = Par_No, @Id = Id_Havaleh  From Inserted
 IF (@Bedeh > 0  Or @Bestan > 0 )
Begin
     IF @Bestan > 0
         SET @StStatus = '1'  -- دريافت بيعانه 
     ELSE IF @Bedeh > 0      
         SET @StStatus = '2' -- تحويل شد
     ELSE IF (@Bedeh > 0) AND (@bedeh = @Bestan)
         SET @StStatus = '3' -- انصراف داده شد
     
     Update tbl_HParvande Set Bestan = IsNull(Bestan,0) + @Bestan,   Bedeh = IsNull(Bedeh,0) + @Bedeh  ,par_Status = @StStatus
     Where Par_No = @Par_No
   
End
