CREATE PROCEDURE [dbo].[spSavePDAOrder]   @Msg_Num   Int  OutPut , @OrderAutoId bigint , @User Smallint, @AnbarCode  Varchar(3) , @FactType smallInt , @PriceType smallInt
AS
  Declare @Guy_Code      Varchar(10)
  Declare @IdHavaleh    Numeric(9)
  Declare @KalaCode     Varchar(15)
  Declare @Kqty         Real
  Declare @R_Unit1_2    Real
  Declare @Point_Flag   bit
  Declare @Serial_flag  bit
  Declare @Exp_Flag     bit
  Declare @Make_Flag    bit
  Declare @Hamrah_Flag  bit
  Declare @Price_Kharid Money
  Declare @price_Forosh Money
  Declare @Price_Omdeh  Money 
  Declare @Radif        Smallint
  Declare @Price        Money
  Declare @PDANo        Varchar(10) 
  Set @Radif = 1

  Select @Guy_Code = tblHOrder.CustomerCode , @PDANo = tblHOrder.PdaAutoId
  From tblHOrder
  Inner Join tblDOrder On tblDOrder.AutoId = tblHOrder.AutoId
  Where tblDOrder.AutoId = @OrderAutoId 
  Group By tblHOrder.CustomerCode , tblHOrder.PdaAutoId
  Having ISNULL(COUNT(tblDOrder.AutoId),0) > 0   
  
   Begin Tran T1
   
   Insert into TmpHeadh (Kind_Flag , User_Id, Note ,Tarikh_Faktor) Values (@FactType , @User,N'- سفارش PDA شماره ' + @PDANo ,GETDATE())
    If @@Error > 0 
    begin
       Set  @msg_num= -5  --msg-num = -5 خطاي سيستم
       ROLLBACK TRANSACTION T1
       RETURN @msg_num 
    end 

    Select @IdHavaleh = @@Identity
  
    Declare curPDAOrder Cursor LOCAL FOR
    
    SELECT @idHavaleh , tblDOrder.StockCode , @AnbarCode , @Guy_Code ,
    tblDOrder.Qty , KalaId.R_Unit1_2, KalaId.Point_Flag, KalaId.Serial_flag, 
    KalaId.Exp_Flag, KalaId.Make_Flag, KalaId.Hamrah_Flag, 
    Anbar.Price_Kharid , case when @PriceType = 1  then tblDORder.Price else Anbar.Price_Forosh end , 
    case when @PriceType = 1  then tblDORder.Price else Anbar.Price_Forosh end
    FROM  tblDOrder 
    INNER JOIN  KalaId  ON tblDOrder.StockCode  = KalaId.K_Code   
    Inner Join  Anbar   On Anbar.K_Code         = KalaId.K_Code AND Anbar.A_Code =  @AnbarCode
    INNER JOIN  AnbarId ON Anbar.A_Code         = AnbarId.A_Code
    WHERE (AnbarId.A_Code = @AnbarCode ) And tblDOrder.AutoId = @OrderAutoId
        
    OPEN curPDAOrder
    FETCH NEXT FROM curPDAOrder INTO @idHavaleh, @KalaCode, @AnbarCode , @Guy_Code , @KQty, 
                                  @R_Unit1_2, @Point_Flag, @Serial_flag, @Exp_Flag, @Make_Flag, 
                                  @Hamrah_Flag, @Price_Kharid, @price_Forosh ,@Price_Omdeh

    WHILE @@FETCH_STATUS = 0     
    BEGIN 
         Insert Into TmpHavaleh (Id_Havaleh, K_Code, Sender, Reciver, K_Qty1, R_Unit1_2, Point_Flag, Serial_flag, Exp_Flag, Make_Flag, 
                                Hamrah_Flag, Price_Kharid, Price_Forosh, Price_Omdeh, Radif)
          Values (@idHavaleh, @KalaCode, @AnbarCode, @Guy_Code , @KQty, @R_Unit1_2,  @Point_Flag, @Serial_flag, @Exp_Flag, @Make_Flag, 
                       @Hamrah_Flag, @Price_Kharid, @price_Forosh , @Price_Omdeh  , @Radif)
      If @@Error > 0 
      begin
         Set  @msg_num= -5  --msg-num = -5 خطاي سيستم
         ROLLBACK TRANSACTION T1
         RETURN @msg_num 
      end 
                             
       FETCH NEXT FROM curPDAOrder 
       INTO @idHavaleh, @KalaCode, @AnbarCode , @Guy_Code , @KQty, @R_Unit1_2, @Point_Flag, @Serial_flag, 
       @Exp_Flag, @Make_Flag, @Hamrah_Flag, @Price_Kharid, @price_Forosh  , @Price_Omdeh
       
       Set @Radif = @Radif + 1

    End
    Close curPDAOrder
    Deallocate curPDAOrder
    
    Update tblHOrder Set KindFlag = 1 where AutoId = @OrderAutoId

    Commit Tran T1
    Set  @msg_num= @IdHavaleh
    RETURN @msg_num

