CREATE PROCEDURE [dbo].[Sodor_AnbarGA_Resid] @AutoId   Smallint, @Msg_Num   Int  OutPut , @TarikhFaKtor  DateTime, @User Smallint, @AnbarCode  Varchar(3), @TypePrice Char
AS

  Declare @AccCode  Varchar(20)
  Declare @Sender    Varchar(10)
  Declare  @Reciver   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 @Radif Smallint
  Declare @Code Varchar(6)
  Declare @Avg_price   Money
  Declare @Price             Money



  Set @Radif     = 1

  Select @AccCode = Acc_code From Classify Where AutoId = @AutoId
  IF @AccCode <>  ''  
  begin
        Select @Code = Code From Acc_Link Where Acc_Code = @AccCode
        IF  @Code  =  ''
        begin
              print 'كد شخص ندارد'
              Set @msg_num = -1
              Return @msg_num
        end
        else
        begin
             Set  @Sender = @Code
            Set @Reciver = @AnbarCode
        end
  end
  else
  begin  
      print 'كد حسابداري ندارد'
      Set @msg_num = -2
      Return @msg_num
  end  

    if (SELECT Count(*)   FROM  Stock INNER JOIN
    KalaId ON Stock.Kala_Code = KalaId.K_Code
    WHERE (Stock.Anbar_Code = @AnbarCode) AND (Stock.FinalCount is not null) And FinalCount - Mojodi > 0 ) =  0 
   begin
       Set @msg_num = -4
       Return @msg_num
   end

    begin Tran T1
    Delete From TmpHeadh Where Kind_Flag = '3'
    Insert into TmpHeadh (Kind_Flag , User_Id, Tarikh_Faktor) Values ('3', @User,@TarikhFaktor)
    If @@Error > 0 
    begin
       Set  @msg_num= -5  --msg-num = -5 خطاي سيستم
       ROLLBACK TRANSACTION T1
       RETURN @msg_num 
    end 
 
    Select @IdHavaleh = @@Identity
    
    Declare Anbar_Ga Cursor LOCAL FOR

    SELECT @idHavaleh , Stock.Kala_Code, @sender , @Reciver ,
    FinalCount - Mojodi , KalaId.R_Unit1_2, KalaId.Point_Flag, KalaId.Serial_flag, 
    KalaId.Exp_Flag, KalaId.Make_Flag, KalaId.Hamrah_Flag, 
    Stock.Price_Kharid, Stock.price_Forosh,  Stock.Avg_Price
    FROM  Stock INNER JOIN
    KalaId ON Stock.Kala_Code = KalaId.K_Code   INNER JOIN  AnbarId ON Stock.Anbar_Code = AnbarId.A_Code
    WHERE (Stock.Anbar_Code = @AnbarCode) AND (Stock.FinalCount is not null) And (FinalCount - Mojodi) > 0 

    OPEN Anbar_Ga
    FETCH NEXT FROM Anbar_Ga INTO @idHavaleh, @KalaCode, @sender , @Reciver , @KQty, 
                                  @R_Unit1_2, @Point_Flag, @Serial_flag, @Exp_Flag, @Make_Flag, 
                                  @Hamrah_Flag, @Price_Kharid, @price_Forosh, @Avg_Price

    WHILE @@FETCH_STATUS = 0     
    BEGIN 
         IF @TypePrice = '0' 
            Set @Price = @Price_Kharid
         Else
              IF @TypePrice = '1' 
              Set @Price = @price_Forosh
         Else
            IF @TypePrice = '2' 
              Set @Price = @Avg_Price

        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, Radif)

         Values (@idHavaleh, @KalaCode, @Sender, @Reciver , @KQty, @R_Unit1_2,  @Point_Flag, @Serial_flag, @Exp_Flag, @Make_Flag, 
                             @Hamrah_Flag, @Price, @price_Forosh, @Radif)
      If @@Error > 0 
      begin
         Set  @msg_num= -5  --msg-num = -5 خطاي سيستم
         ROLLBACK TRANSACTION T1
         RETURN @msg_num 
      end 

                           
         FETCH NEXT FROM Anbar_Ga INTO @idHavaleh, @KalaCode, @sender , @Reciver , @KQty,
                                      @R_Unit1_2, @Point_Flag, @Serial_flag, @Exp_Flag, @Make_Flag, 
                                      @Hamrah_Flag, @Price_Kharid, @price_Forosh, @Avg_Price
        Set @Radif = @Radif + 1



    End
    Close Anbar_Ga
    Deallocate Anbar_Ga

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

