--msg-num = -1  برخي از کالاها ها موجودي ندارند
--msg-num = -2 موردي براي صدور وجود ندارد
--msg-num = -5 خطاي سيستم
--msg-num = -6 فاقد اقلام داروئي
------------------------------------------------------------
--  FOROSH        = 1;
--  RETURNFOROSH  = 2;
------------------------------------------------------------

-- Kind_Flag=1     در صورتي که فرستنده يک انبار باشد و گيرنده يک شخص 
-- Kind_Flag=2     در صورتي که فرستنده يک شخص باشد و گيرنده انبار  

CREATE  PROCEDURE [dbo].[Sodor_Factor_Drug] @Msg_Num Numeric(18,0) OUTPUT,@TmpId  Numeric(18,0)
 AS
 Set @Msg_Num =0
 Declare @Par_No Varchar(9)
 Declare @Bedeh Money
 Declare @Bestan Money
 Declare @KindSave Char(1)
 Declare @Last_Id  Numeric(18,0)
 Declare @iCount   Smallint
 
 Select @Par_No = Par_No  From TmpDrugH Where Id_Havaleh = @TmpId  
 Select @iCount = Count(*) From TmpDrugHavaleh  Where Id_Havaleh = @TmpId  
 IF (Select Count(*)  From Dbo.TmpDrugH Where Id_Havaleh = @TmpId  ) > 0   
 Begin
    Select @Bedeh = Bedehkar, @Bestan = Bestankar, @Par_No = Par_No, @KindSave = Kind_Save From TmpDrugH Where Id_Havaleh = @TmpId 
    IF  @KindSave not in ('3','4')   Set @KindSave = '0'
    
    IF @KindSave <> '3'
       IF (Select Count(*) from LESS_Mojodi_Drug(@TmpId))  > 0 
       Begin
           Set  @Msg_Num= -1  
           RETURN @Msg_Num
       End
     
       IF ((@Par_No = '' Or @Par_No Is Null) And (@iCount = 0))
       Begin
          Set  @msg_num=-6
          RETURN @Msg_Num 
      End

      Begin Tran SF

      IF @Bestan > 0 
         Insert Into Dbo.DrugH(Kind_Flag,Kind_Save,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,TotPriceOTC,HOTC, TotPriceFaniBimar,TotPriceFaniSazman,TotPriceDaryafti,TotPriceforosh,TotPriceAfzayandeh,TotPriceKahandeh,User_id,Tarikh_Etebar,Tarikh_Noskheh,Tarikh_Paziresh,OverFlagBimeh, HospitalFlag,Pt_Code, Bedehkar, Bestankar, Par_No , NumberQueue,CheckU)
                                Select Kind_Flag,@KindSave,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,0,0,0,0,0,0,0,0, 0,0,TotPriceDaryafti,0,0,0,User_id,Tarikh_Etebar,Tarikh_Noskheh,Tarikh_Paziresh,OverFlagBimeh, HospitalFlag, Pt_Code, Bedehkar, Bestankar, Par_No, NumberQueue,CheckU From TmpDrugH
                                Where Id_Havaleh=@TmpId	
      Else
         Insert Into Dbo.DrugH(Kind_Flag,Kind_Save,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,TotPriceOTC,HOTC, TotPriceFaniBimar,TotPriceFaniSazman,TotPriceDaryafti,TotPriceforosh,TotPriceAfzayandeh,TotPriceKahandeh,User_id,Tarikh_Etebar,Tarikh_Noskheh,Tarikh_Paziresh,OverFlagBimeh, HospitalFlag,Pt_Code, Bedehkar, Bestankar, Par_No,NumberQueue,CheckU)
                                Select Kind_Flag,@KindSave, 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,TotPriceOTC,HOTC, TotPriceFaniBimar,TotPriceFaniSazman,TotPriceDaryafti,TotPriceforosh,TotPriceAfzayandeh,TotPriceKahandeh,User_id,Tarikh_Etebar,Tarikh_Noskheh,Tarikh_Paziresh,OverFlagBimeh, HospitalFlag, Pt_Code, Bedehkar, Bestankar, Par_No,NumberQueue,CheckU From TmpDrugH
                                Where Id_Havaleh=@TmpId	
       IF @@Error > 0  
       Begin
            Set  @Msg_Num= -5  
            ROLLBACK TRANSACTION SF
            RETURN @Msg_Num 
       End
    
       Select  @Last_Id = Scope_Identity()
    
       IF  @iCount > 0
       Begin
            Insert Into Dbo.DrugHavaleh(Id_Havaleh,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,CtrMojodi,Tot_forosh,Tot_Differ,Link_Like, Link_Record, EditQty_Flag, BimarPercent)
                                          Select  @Last_Id ,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,CtrMojodi,Tot_forosh,Tot_Differ,Link_Like,Link_Record, EditQty_Flag,BimarPercent 
                                          From TmpDrugHavaleh Where Id_Havaleh=@TmpId  
       
           IF @@Error > 0  
           Begin
                Set  @Msg_Num= -5  
                ROLLBACK TRANSACTION SF
                RETURN @Msg_Num 
           End
       
--------------------------------------------------------------------------------------------------------------------
         -- در صورت وجود كاهنده و افزاينده روي فاكتور اطلاعات آن از تمپ به جدول اصلي منتقل مي شود
           IF (SELECT     Count(*)    FROM    dbo.Tmp_Over_Under_Factor_Drug   WHERE     (Id_Havaleh = @TmpId)) > 0 
               Insert Into dbo.Over_Under_Factor_Drug(Id_Havaleh, Code_Over_Under ,Type ,Price ,[Percent] , Act_Code , Guy_Code)
                           Select  @Last_Id, Code_Over_Under ,Type ,Price , [Percent] , Act_Code , Guy_Code
                           From Tmp_Over_Under_factor_Drug  Where (Id_Havaleh=@TmpId And Price > 0) 
       
           IF @@Error > 0  
           Begin
                Set  @Msg_Num= -5  
                ROLLBACK TRANSACTION SF
                RETURN @Msg_Num 
           End
----------------------------------------------------------------------------------------------------------------------
           IF (SELECT     Count(*)    FROM    dbo.Tmp_Over_Under_Factor_Cash   WHERE     (Id_Havaleh = @TmpId)) > 0 
               Insert Into dbo.Over_Under_Factor_Drug(Id_Havaleh, Code_Over_Under ,Type ,Price ,[Percent] , Act_Code, Guy_Code)
                           Select  @Last_Id, Code_Over_Under ,Tmp_Over_Under_Factor_Cash.Type ,Price , [Percent] , Tmp_Over_Under_Factor_Cash.Act_Code, '-4'
                           From Tmp_Over_Under_factor_Cash  inner join Over_Under on Over_Under.AutoId = Code_Over_Under
                           Where (Id_Havaleh=@TmpId And Price > 0) 
               
           IF @@Error > 0  
           Begin
                Set  @Msg_Num= -5  
                ROLLBACK TRANSACTION SF
                RETURN @Msg_Num 
           End
----------------------------------------------------------------------------------------------------------------------
       -- در صورت وجود موارد كاهنده و افزاينده روي تك تك اقلام اطلاعات آن از تمپ به جدول اصلي منتقل مي شود 
              Insert Into Dbo.Over_Under_Row (AutoId,name,Code_Act,Type,Price,[Percent],Code_Over_Under)
              SELECT  Tmp_Over_Under_Row.AutoId, Tmp_Over_Under_Row.name, Tmp_Over_Under_Row.Code_Act, 
                            Tmp_Over_Under_Row.Type, Tmp_Over_Under_Row.Price, Tmp_Over_Under_Row.[Percent], 
                            Tmp_Over_Under_Row.Code_Over_Under FROM  TmpDrugHavaleh INNER JOIN
                            Tmp_Over_Under_Row ON TmpDrugHavaleh.AutoId = Tmp_Over_Under_Row.AutoId
                            WHERE  TmpDrugHavaleh.Id_Havaleh = @TmpId And Price > 0 
           IF @@Error > 0  
           Begin
                Set  @Msg_Num= -5  
                ROLLBACK TRANSACTION SF
                RETURN @Msg_Num 
           End
---------------------------------------------------------------------------------------------------------------------------------------
            Exec sp_CreateParByReferDay @TmpId
            
            Delete From dbo.TmpDrugH   where  Id_Havaleh=@TmpId 
            Delete From dbo.Tmp_Over_Under_Factor_Cash where  Id_Havaleh=@TmpId 
   
      
            Select @Msg_Num=@Last_Id
            Commit Tran SF
            RETURN @Msg_Num
      End  -- IF  (@iCount) > 0
      Else
      Begin
           Set  @msg_num=10
           Delete From dbo.TmpDrugH   where  Id_Havaleh=@TmpId 
           Commit Tran SF
           RETURN @msg_num
      End
 End  -- IF (Select Count(*)  From Dbo.TmpDrugH Where Id_Havaleh = @TmpId  ) > 0   
 Else
 Begin
    Set  @msg_num=-2 
    RETURN @msg_num
    Print  'Not Exists For Sodor Factor'
 End
