CREATE TRIGGER [dbo].[TU_HamrahDrug] ON [dbo].[TmpDrugHavaleh]
For Update
AS 
 Declare @Qty1          Real
 Declare @Qty2          Real
 Declare @Hamrah_Flag   Bit
 Declare @Make_Flag     Bit
 Declare @Autoid        Numeric(9)
 Declare @K_Code        varchar(20)
 Declare @HKqty         Real
 Declare @MainKCode     varchar(20)
 Declare @EditQty       Flag
 Declare @KQty          Real
 Declare @Sender        varchar(5)
 Declare @Price         Money
 Declare @Price_Forosh  Money
 Declare @Sazman_Code   Varchar(3)
 Declare @Darou_Flag    Char(1)
 Declare @SumDiffer     Money





 IF Update(K_qty1)  
 Begin
   Select    @Qty1=K_qty1 ,  @Hamrah_Flag=Hamrah_Flag,  @Autoid = Autoid, @MainKCode=  K_Code from inserted
   Select    @Qty2=k_qty1 , @Sender = Sender from deleted

   Select @Sazman_Code = TmpDrugH.Sazman_Code From TmpDrugH Inner Join Inserted 
   On TmpDrugH.Id_Havaleh = Inserted.Id_Havaleh
   Group By Sazman_Code


   IF  ( @Qty1 != @Qty2 )   
   Begin 
      IF (@Hamrah_Flag=1 ) 
      Begin   
         Declare TmpDrug Cursor For 
         Select   Autoid, K_Code  From TmpDrugHavaleh  Where Link_Record =@Autoid
         Open TmpDrug
         Fetch Next From TmpDrug  Into @Autoid, @K_Code
         WHILE @@FETCH_STATUS = 0 
         Begin
            --------------------------------------------------------------------------------
            Select @Price_Forosh = Price_Forosh,@Darou_Flag = Darou_Flag ,@Price = Price From 
            (SELECT Kalaid.K_Code, Anbar.Price_Forosh,
            Case
              When KalaId.Darou_Flag <> '6' then Kalaid.Darou_Flag
              When KalaId.Darou_Flag = '6'  then (select IsNull(Price_Sazman.Status,0) From Price_Sazman Where Sazman_Code = @Sazman_Code
              And Price_Sazman.K_Code = KalaId.K_Code )
            End As Darou_Flag,
            Case
              When KalaId.Darou_Flag <> '6' then 0
              When KalaId.Darou_Flag = '6'  then (select IsNull(Price_Sazman.Price,0) From Price_Sazman Where Sazman_Code = @Sazman_Code
              And Price_Sazman.K_Code = KalaId.K_Code And Status = '6' )
            End As Price,
            Kalaid.Meli_Code FROM KalaId INNER JOIN Anbar ON KalaId.K_Code = Anbar.K_Code
            WHERE (Anbar.Active_Flag = '0') AND (Anbar.A_Code = @Sender)) PublicTable
            Where K_Code =@K_Code

            --------------------------------------------------------------------------------
            Select @HKqty =  K_Qty1 , @EditQty = EditQty_Flag From Hamrah_Kala Where K_Code = @MainKCode and K_Code_H =  @K_Code        
            IF @EditQty  = 1 
               Set @KQty =  @Qty1 * @HKqty
            Else
               Set  @KQty = @HKqty 

            Set @SumDiffer = 0
            IF @Darou_Flag  = '6' 
              IF (@Price <> 0) and (@Price < @Price_Forosh)  
                Set @SumDiffer  = (@KQty * @Price_Forosh) - (@KQty * @Price)
            

            Update Dbo.TmpDrughavaleh Set K_Qty1= @KQty , Tot_Differ = @SumDiffer Where Autoid = @Autoid
            Fetch Next From TmpDrug  Into @Autoid, @K_Code
         End
         CLOSE TmpDrug
         DEALLOCATE TmpDrug
      End   
   End
 End
