Create PROCEDURE [dbo].[Reverse_FactorFinal_Drug] @Msg_Num Numeric(18,0) OUTPUT,@Id  Numeric(18,0), @TypeFactor Char(1), @ReverseFactor Smallint AS

  Declare @Sender       Varchar(5)
  Declare @Reciver      Varchar(8)
  Declare @SenderNew    Varchar(5)
  Declare @ReciverNew   Varchar(8)
  Declare @Radif        smallint
  Declare @K_Code       Varchar(15)
  Declare @K_Qty1       Float
  Declare @Price_Kharid Money
  Declare @Price_Forosh Money
  Declare @Main_Price   Money
  Declare @Status       Smallint
  Declare @Takhfif_Flag Bit
  Declare @Exp_Flag     Bit
  Declare @Point_Flag   Bit
  Declare @Serial_Flag  Bit
  Declare @Make_Flag    Bit
  Declare @Hamrah_Flag  Bit
  Declare @CtrMojodi    Char(1)
  Declare @Tot_Differ   Money
  Declare @Link_Like    Money
  Declare @OldAutoId    Numeric(9)
  Declare @NewAutoId    Numeric(9)
  Declare @KindFlag     Char(1)
  Declare @Nu_ForoshNew Varchar(8)
  Declare @Nu_Bargasht_ForoshNew Varchar(8)
  Declare @Nu_Forosh    Varchar(8)
  Declare @Nu_Bargasht_Forosh Varchar(8)
  Declare @TransName    Nvarchar(35)
  Declare @ResiveName   Nvarchar(35)
  Declare @TransNameNew    Nvarchar(35)
  Declare @ResiveNameNew   Nvarchar(35)
  Declare @Price_Forosh_O Money
 Declare @Price_Sazman_O  Money
  Declare @SazmanCode Char(3)
  


 Set @Msg_Num =0
 IF (Select Count(*)  From Dbo.DrugH Where Id_Havaleh = @Id  ) > 0  And  (Select Count(*)  From Dbo.DrugHavaleh  Where Id_Havaleh = @Id) > 0
 Begin
   Begin Tran RV
   Declare @Last_Id Numeric(18,0)

   Select @KindFlag = DrugH.Kind_Flag, @Nu_ForoshNew = DrugH.Nu_Forosh, 
          @Nu_Bargasht_ForoshNew = DrugH.Nu_Bargasht_Forosh,@TransNameNew = DrugH.Trans_Name,
          @ResiveNameNew = DrugH.Resive_Name, @SenderNew = DrugHavaleh.Sender, 
          @ReciverNew = DrugHavaleh.Reciver
          From DrugH Inner Join DrugHavaleh 
          On(DrugH.Id_Havaleh = DrugHavaleh.Id_Havaleh)
          Where DrugH.Id_Havaleh = @Id
   Group By DrugH.Nu_Forosh, DrugH.Nu_Bargasht_Forosh, DrugHavaleh.Sender, 
            DrugHavaleh.Reciver , DrugH.Kind_Flag, DrugH.Trans_Name, DrugH.Resive_Name

   IF @TypeFactor = '1'  
   Begin 
     Set @Nu_Bargasht_Forosh = ''
     Set @KindFlag  = '1'
     IF @ReverseFactor = 0 
     Begin
       Set @Sender     = @SenderNew    
       Set @Reciver    = @ReciverNew
       Set @Nu_Forosh  = @Nu_ForoshNew
       Set @TransName  = @TransNameNew
       Set @ResiveName = @ResiveNameNew
     End
     Else IF @ReverseFactor = 1 
     Begin
       Set @Sender     = @ReciverNew
       Set @Reciver    = @SenderNew    
       Set @Nu_Forosh  = @Nu_Bargasht_ForoshNew
       Set @TransName  = @ResiveNameNew
       Set @ResiveName = @TransNameNew
     End
   End

   Else IF @TypeFactor = '2'  
   Begin 
     Set @Nu_Forosh = ''
     Set @KindFlag  = '2'
     IF @ReverseFactor = 0 
     Begin
       Set @Sender    = @ReciverNew
       Set @Reciver   = @SenderNew    
       Set @Nu_Bargasht_Forosh = @Nu_ForoshNew
       Set @TransName  = @ResiveNameNew
       Set @ResiveName = @TransNameNew
     End
     Else IF @ReverseFactor = 1 
     Begin
       Set @Sender    = @SenderNew    
       Set @Reciver   = @ReciverNew
       Set @Nu_Bargasht_Forosh = @Nu_Bargasht_ForoshNew
       Set @TransName  = @TransNameNew
       Set @ResiveName = @ResiveNameNew
     End
   End
     
   
   Insert Into Dbo.TmpDrugH(Kind_Flag,Nu_Forosh,Nu_Bargasht_Forosh,Del_Flag,Note_Code,Dumy_Code,Grade_Code, Nezam_No,Resive_Name,Trans_Name,Sazman_Code,Radif_Noskheh,Computer_Code,Page_No,User_Cash,TotPriceBimar,TotPriceSazman,TotPriceDiffer,TotPriceAzad,TotPriceKala, TotPriceTaghizat, HOtc,TotPriceOTC,TotPriceFaniBimar,TotPriceFaniSazman,TotPriceDaryafti,TotPriceforosh,TotPriceAfzayandeh,TotPriceKahandeh,User_id,Tarikh_Etebar,Tarikh_Noskheh,Tarikh_Paziresh, Kind_Save, Pt_Code,Par_No, NumberQueue)
                     Select @KindFlag,@Nu_Forosh,@Nu_Bargasht_Forosh,Del_Flag,Note_Code,Dumy_Code,Grade_Code, Nezam_No,@ResiveName,@TransName,Sazman_Code,Radif_Noskheh,Computer_Code,Page_No,User_Cash,TotPriceBimar,TotPriceSazman,TotPriceDiffer,TotPriceAzad,TotPriceKala, TotPriceTaghizat, HOtc,TotPriceOTC,TotPriceFaniBimar,TotPriceFaniSazman,TotPriceDaryafti,TotPriceforosh,TotPriceAfzayandeh,TotPriceKahandeh,User_id,Tarikh_Etebar,Tarikh_Noskheh,Tarikh_Paziresh, 0 , Pt_Code, Par_No, NumberQueue From DrugH
                     Where Id_Havaleh = @Id	
   IF @@Error > 0  
   Begin
     ROLLBACK TRANSACTION RV
     Set  @Msg_Num= -5  
     RETURN @Msg_Num
   End       
        
   Select @Last_Id=@@Identity
   Select  @SazmanCode = Sazman_Code From DrugH Where Id_Havaleh = @Id


   Declare TmpDrugHavaleh_Cursor Cursor LOCAL FOR
   Select  AutoId,@Sender,@Reciver,Radif,K_Code,K_Qty1,Price_Kharid, Price_Forosh,Main_Price,Status,Takhfif_Flag,Point_Flag,Serial_Flag,Hamrah_Flag,Exp_Flag,Make_Flag,Tot_Differ,Link_Like From DrugHavaleh
   Where Id_Havaleh=@Id 
   Order By AutoId, Link_Like, Link_Record

   OPEN TmpDrugHavaleh_Cursor
  
   FETCH NEXT FROM TmpDrugHavaleh_Cursor INTO @OldAutoId,@Sender,@Reciver,@Radif,@K_Code,@K_Qty1,@Price_Kharid, @Price_Forosh,@Main_Price,@Status,@Takhfif_Flag,@Point_Flag,@Serial_Flag,@Hamrah_Flag,@Exp_Flag,@Make_Flag,@Tot_Differ,@Link_Like

   WHILE @@FETCH_STATUS = 0     
   Begin 
     IF  @Status = '7 '
       Set @Link_Like =  @NewAutoId  

     IF @Status in ('7','8')
     Begin
       IF Len(@Sender) = 3
           Select  @Price_Forosh_O  = Price_Forosh  From Anbar Where A_Code = @Sender And K_Code = @K_Code
       Else 
           Select  @Price_Forosh_O  = Price_Forosh From Anbar Where A_Code = @Reciver And K_Code = @K_Code
         Select  @Price_Sazman_O  = Price From Price_Sazman Where Sazman_Code  = @SazmanCode  And K_Code = @K_Code
     End
     
     SELECT  @CtrMojodi=CtrMojodi
     FROM  AnbarId WHERE A_Code =@Sender

     If @CtrMojodi='2'  
     Begin
       SELECT  @CtrMojodi=CtrMojodi
       FROM    dbo.KalaId WHERE K_Code =@K_Code
     End

     Insert Into Dbo.TmpDrugHavaleh(Id_Havaleh,Sender,Reciver,Radif,K_Code,K_Qty1,Price_Kharid, Price_Forosh,Main_Price,Status,Takhfif_Flag,Point_Flag,Serial_Flag,Hamrah_Flag,Exp_Flag,Make_Flag,CtrMojodi,Tot_Differ,Link_Like,EditQty_Flag, Price_Forosh_O, Price_Sazman_O)
                  	    Values (@Last_Id ,@Sender,@Reciver,@Radif,@K_Code,@K_Qty1,@Price_Kharid, @Price_Forosh,@Main_Price,@Status,@Takhfif_Flag,@Point_Flag,@Serial_Flag,@Hamrah_Flag,@Exp_Flag,@Make_Flag,@CtrMojodi,@Tot_Differ,@Link_Like, '1', @Price_Forosh_O, @Price_Sazman_O)
          
        
     IF @@Error > 0  
     Begin
       ROLLBACK TRANSACTION RV
       Set  @Msg_Num= -5  
       RETURN @Msg_Num 
     End       
         
     Select @NewAutoId=@@Identity
         
         
     IF Exists( Select * From   DrugHavaleh Inner Join Over_Under_Row ON DrugHavaleh.AutoId = Over_Under_Row.AutoId Where DrugHavaleh.Id_Havaleh = @Id And Over_Under_Row.AutoId = @OldAutoId)
       Insert Into Dbo.Tmp_Over_Under_Row (AutoId,name,Code_Act,Type,Price,[Percent], Code_Over_Under)
                                   SELECT @NewAutoId,Over_Under_Row.name, Over_Under_Row.Code_Act, Over_Under_Row.Type, Over_Under_Row.Price, 
                                   Over_Under_Row.[Percent], Over_Under_Row.Code_Over_Under FROM  DrugHavaleh INNER JOIN Over_Under_Row ON DrugHavaleh.AutoId = Over_Under_Row.AutoId
                                   WHERE  (DrugHavaleh.Id_Havaleh = @Id) AND (DrugHavaleh.AutoId = @OldAutoId)           
     IF @@Error > 0  
     Begin
       ROLLBACK TRANSACTION RV
       Set  @Msg_Num= -5  
       RETURN @Msg_Num 
     End       
     FETCH NEXT FROM  TmpDrugHavaleh_Cursor INTO @OldAutoId,@Sender,@Reciver,@Radif,@K_Code,@K_Qty1,@Price_Kharid, @Price_Forosh,@Main_Price,@Status,@Takhfif_Flag,@Point_Flag,@Serial_Flag,@Hamrah_Flag,@Exp_Flag,@Make_Flag,@Tot_Differ,@Link_Like
   End --WHILE @@FETCH_STATUS = 0     

   Close TmpDrugHavaleh_Cursor
   Deallocate TmpDrugHavaleh_Cursor

   Insert Into Dbo.Tmp_Over_Under_Factor_Drug (Id_Havaleh,Code_Over_Under,Price,[Percent],Act_Code,Guy_Code,Type, Name)
                             SELECT    @Last_Id,  Over_Under_Factor_Drug.Code_Over_Under, Over_Under_Factor_Drug.Price, 
                             Over_Under_Factor_Drug.[Percent], Over_Under_Factor_Drug.Act_Code, 
                      		 Over_Under_Factor_Drug.Guy_Code, Over_Under_Factor_Drug.Type, Over_Under.Name
				             FROM   Over_Under_Factor_Drug INNER JOIN
                      		 Over_Under ON Over_Under_Factor_Drug.Code_Over_Under = Over_Under.AutoId
				             WHERE (Over_Under_Factor_Drug.Id_Havaleh = @Id)
				             And Over_Under_Factor_Drug.Price <> 0 
				             And Over_Under.Action_Code in('-1','-2','-3') 
        
   IF @@Error > 0  
   Begin
     ROLLBACK TRANSACTION RV
     Set  @Msg_Num= -5  
     RETURN @Msg_Num 
   End      

   ------------------------------------------------------------------------------------------------
   Commit Tran RV
   Select @Msg_Num=@Last_Id
   RETURN @Msg_Num
 End -- IF (Select Count(*)  From Dbo.TmpHeadH Where Id_Havaleh = @TmpId  ) > 0   And  (Select Count(*)  From Dbo.TmpHavaleh  Where Id_Havaleh = @TmpId) > 0*/
 Else
 Begin
   Set  @Msg_Num = -1 
   RETURN @Msg_Num
   Print  'Not Exists For  FactorFinal'
End
