CREATE FUNCTION FuncRepGolKish  (@Kind Varchar(10), @Type_Price Char(1), 
                                 @AzCode Varchar(20), @TaCode Varchar(20), 
                                 @ACode Varchar(3), @DefaultPrice Money)
RETURNS
   @RepGolKish TABLE
   (
     K_Code Varchar(15), 
     BarCode Varchar(40), 
     Name1 nvarchar (50) , 
     Resive_Fa Real, 
     Exit_Fa Real, 
     Mojodi Money, 
     Price_Kharid Money, 
     Kala_Max Real, 
     TypePrice Money, 
     EndEnterTarikh DateTime, 
     EndExitTarikh DateTime,
     EndEnterKqty Float, 
     EndExitKqty Float, 
     DarsadSood Float 
   )
AS  
BEGIN 
  Declare @Kind1 Char(1)
  Declare @Kind2 Char(1)
  Declare @Kind3 Char(1)
  Declare @Kind4 Char(1)
  Declare @Kind5 Char(1)
  Declare @Kind6 Char(1)
  Declare @Kind7 Char(1)
  Declare @Kind8 Char(1)
  Declare @Kind9 Char(1)
  Declare @Kind10 Char(1)
  Declare @PriceKala_Max Money
  Declare @PriceKala_Max1 Money
  Declare @PriceKala_Max2 Money
  Declare @PriceKala_Max3 Money

  Set @kind1  = Substring(@Kind,1,1)
  Set @kind2  = Substring(@Kind,2,1)
  Set @kind3  = Substring(@Kind,3,1)
  Set @kind4  = Substring(@Kind,4,1)
  Set @kind5  = Substring(@Kind,5,1)
  Set @kind6  = Substring(@Kind,6,1)
  Set @kind7  = Substring(@Kind,7,1)
  Set @kind8  = Substring(@Kind,8,1)
  Set @kind9  = Substring(@Kind,9,1)
  Set @kind10 = Substring(@Kind,10,1)

  Set @PriceKala_Max = @DefaultPrice * 120/100
  Set @PriceKala_Max1 = @DefaultPrice * 115/100
  Set @PriceKala_Max2 = @DefaultPrice * 110/100
  Set @PriceKala_Max3 = @DefaultPrice * 105/100


  IF @Type_Price = '0'
  Begin
    IF @TaCode = '' 
      Set @TaCode = 'ZZZZZZZZZZZZZ'
    INSERT @RepGolKish
    Select K_Code, BarCode, Name1, Resive_Fa, Exit_Fa, Mojodi, Price_Kharid, Kala_Max, TypePrice, 
    Cast(Sum(EndEnterTarikh) as DateTime) EndEnterTarikh, 
    Cast(Sum(EndExitTarikh) As DateTime) EndExitTarikh,
    Sum(EndEnterKqty) EndEnterKqty , 
    Sum(EndExitKqty) EndExitKqty, DarsadSood  
    From (

    Select K_Code, BarCode, Name1, Resive_Fa, Exit_Fa, Mojodi, Price_Kharid, Kala_Max, TypePrice, 
    Case 
      When TypeFactor = 1  then Cast(MaxTarikh as Float)
    End As EndEnterTarikh,
    Case 
      When TypeFactor = 2  then Cast(MaxTarikh as Float)
    End As EndExitTarikh,
 
    Case 
      When TypeFactor = 1  then SumKQty
    End As EndEnterKqty,
    Case 
      When TypeFactor = 2  then SumKQty
    End As EndExitKqty, DarsadSood
    From (
    --------------------------------(مقادير ورودي)----------------------------------------
    Select 1 as TypeFactor,Table1.K_Code, Table2.Barcode, Table2.Name1, 
    Table2.Price_Kharid, Table2.Kala_Max, Table2.Price_Omdeh TypePrice, 
    Table2.SumKQty ,Table1.MaxTarikh , Table2.Mojodi, 
    Table2.Resive_Fa, Table2.Exit_Fa, Table2.DarsadSood
    From (
    Select Max(dbo.HeadH.Tarikh_Faktor) MaxTarikh , Havaleh.K_Code From Havaleh
    Inner Join HeadH on (Havaleh.Id_Havaleh = HeadH.Id_Havaleh)
    WHERE (headH.Kind_Flag IN (@kind1, @kind2, @kind3, @kind4,@kind5, @kind6)) 
    AND (Havaleh.Reciver = @ACode) AND 
    (Havaleh.K_Code >=@AzCode And Havaleh.K_Code <=@TaCode )
    Group By Havaleh.K_Code) As Table1

    Inner Join (

    SELECT KalaId.K_Code, KalaId.Barcode, KalaId.Name1, Anbar.Price_Kharid, 
    Anbar.Kala_Max, Anbar.Price_Omdeh, Anbar.Resive_fa, Anbar.Exit_Fa,
    SUM(Havaleh.K_Qty1) SumKQty, HeadH.Tarikh_Faktor, Anbar.Mojodi, 
    Case   
      When Anbar.Price_Kharid = 0  And (Anbar.Kala_Max Not in (5,10,15,20) Or Anbar.Kala_Max Is Null) then ((Anbar.Price_Omdeh - (Anbar.Price_Kharid * @DefaultPrice))/ (@DefaultPrice * 1)) * 100    
      When Anbar.Price_Kharid <> 0 And (Anbar.Kala_Max Not in (5,10,15,20) Or Anbar.Kala_Max Is Null) then ((Anbar.Price_Omdeh - (Anbar.Price_Kharid * @DefaultPrice))/ (@DefaultPrice * Anbar.Price_Kharid)) * 100   
      When Anbar.Price_Kharid = 0  And Anbar.Kala_Max = 20  then ((Anbar.Price_Omdeh - (Anbar.Price_Kharid * @PriceKala_Max))/ (@PriceKala_Max * 1)) * 100    
      When Anbar.Price_Kharid <> 0 And Anbar.Kala_Max = 20  then ((Anbar.Price_Omdeh - (Anbar.Price_Kharid * @PriceKala_Max))/ (@PriceKala_Max * Anbar.Price_Kharid)) * 100   
      When Anbar.Price_Kharid = 0  And Anbar.Kala_Max = 15  then ((Anbar.Price_Omdeh - (Anbar.Price_Kharid * @PriceKala_Max1))/ (@PriceKala_Max1 * 1)) * 100    
      When Anbar.Price_Kharid <> 0 And Anbar.Kala_Max = 15  then ((Anbar.Price_Omdeh - (Anbar.Price_Kharid * @PriceKala_Max1))/ (@PriceKala_Max1 * Anbar.Price_Kharid)) * 100   
      When Anbar.Price_Kharid = 0  And Anbar.Kala_Max = 10  then ((Anbar.Price_Omdeh - (Anbar.Price_Kharid * @PriceKala_Max2))/ (@PriceKala_Max2 * 1)) * 100    
      When Anbar.Price_Kharid <> 0 And Anbar.Kala_Max = 10  then ((Anbar.Price_Omdeh - (Anbar.Price_Kharid * @PriceKala_Max2))/ (@PriceKala_Max2 * Anbar.Price_Kharid)) * 100   
      When Anbar.Price_Kharid = 0  And Anbar.Kala_Max = 5  then ((Anbar.Price_Omdeh - (Anbar.Price_Kharid * @PriceKala_Max3))/ (@PriceKala_Max3 * 1)) * 100    
      When Anbar.Price_Kharid <> 0 And Anbar.Kala_Max = 5  then ((Anbar.Price_Omdeh - (Anbar.Price_Kharid * @PriceKala_Max3))/ (@PriceKala_Max3 * Anbar.Price_Kharid)) * 100   


    End As DarsadSood
    FROM Anbar INNER JOIN KalaId ON Anbar.K_Code = KalaId.K_Code INNER JOIN
    Havaleh ON Anbar.A_Code = Havaleh.Reciver AND Anbar.K_Code = Havaleh.K_Code INNER JOIN
    headH ON Havaleh.Id_Havaleh = headH.Id_Havaleh
    WHERE (headH.Kind_Flag IN (@kind1, @kind2, @kind3, @kind4,@kind5, @kind6)) AND (Anbar.A_Code = @ACode) 
    And (KalaId.K_Code >=@AzCode And KalaId.K_Code <=@TACode)
    GROUP BY KalaId.K_Code, KalaId.Barcode, KalaId.Name1, Anbar.Price_Kharid, 
    Anbar.Kala_Max, Anbar.Price_Omdeh,HeadH.Tarikh_Faktor,
    Anbar.Resive_fa, Anbar.Exit_Fa,Anbar.Mojodi) as Table2
    On(Table1.K_Code = Table2.K_Code and Table1.MaxTarikh = Table2.Tarikh_Faktor)
    --------------------------------(مقادير ورودي)----------------------------------------

    Union 

    --------------------------------(مقادير خروجي)----------------------------------------
    Select 2 as TypeFactor,Table1.K_Code, Table2.Barcode, Table2.Name1, 
    Table2.Price_Kharid, Table2.Kala_Max, Table2.Price_Omdeh TypePrice, 
    Table2.SumKQty ,Table1.MaxTarikh , Table2.Mojodi, 
    Table2.Resive_Fa, Table2.Exit_Fa, Table2.DarsadSood
    From (
    Select Max(HeadH.Tarikh_Faktor) MaxTarikh , Havaleh.K_Code From Havaleh
    Inner Join HeadH on (Havaleh.Id_Havaleh = HeadH.Id_Havaleh)
    WHERE (headH.Kind_Flag IN (@kind7, @kind8, @kind9, @kind10)) 
    AND (Havaleh.Sender = @ACode) AND 
    (Havaleh.K_Code >=@AzCode And Havaleh.K_Code <=@TACode)
    Group By Havaleh.K_Code) As Table1

    Inner Join (

    SELECT KalaId.K_Code, KalaId.Barcode, KalaId.Name1, Anbar.Price_Kharid, 
    Anbar.Kala_Max, Anbar.Price_Omdeh, Anbar.Resive_fa, Anbar.Exit_Fa,
    SUM(Havaleh.K_Qty1) SumKQty, HeadH.Tarikh_Faktor, Anbar.Mojodi, 
    Case   
      When Anbar.Price_Kharid = 0  And (Anbar.Kala_Max Not in (5,10,15,20) Or Anbar.Kala_Max Is Null) then ((Anbar.Price_Omdeh - (Anbar.Price_Kharid * @DefaultPrice))/ (@DefaultPrice * 1)) * 100    
      When Anbar.Price_Kharid <> 0 And (Anbar.Kala_Max Not in (5,10,15,20) Or Anbar.Kala_Max Is Null) then ((Anbar.Price_Omdeh - (Anbar.Price_Kharid * @DefaultPrice))/ (@DefaultPrice * Anbar.Price_Kharid)) * 100   
      When Anbar.Price_Kharid = 0  And Anbar.Kala_Max = 20  then ((Anbar.Price_Omdeh - (Anbar.Price_Kharid * @PriceKala_Max))/ (@PriceKala_Max * 1)) * 100    
      When Anbar.Price_Kharid <> 0 And Anbar.Kala_Max = 20  then ((Anbar.Price_Omdeh - (Anbar.Price_Kharid * @PriceKala_Max))/ (@PriceKala_Max * Anbar.Price_Kharid)) * 100   
      When Anbar.Price_Kharid = 0  And Anbar.Kala_Max =15  then ((Anbar.Price_Omdeh - (Anbar.Price_Kharid * @PriceKala_Max1))/ (@PriceKala_Max1 * 1)) * 100    
      When Anbar.Price_Kharid <> 0 And Anbar.Kala_Max = 15  then ((Anbar.Price_Omdeh - (Anbar.Price_Kharid * @PriceKala_Max1))/ (@PriceKala_Max1 * Anbar.Price_Kharid)) * 100   
      When Anbar.Price_Kharid = 0  And Anbar.Kala_Max = 10  then ((Anbar.Price_Omdeh - (Anbar.Price_Kharid * @PriceKala_Max2))/ (@PriceKala_Max2 * 1)) * 100    
      When Anbar.Price_Kharid <> 0 And Anbar.Kala_Max = 10  then ((Anbar.Price_Omdeh - (Anbar.Price_Kharid * @PriceKala_Max2))/ (@PriceKala_Max2 * Anbar.Price_Kharid)) * 100   
      When Anbar.Price_Kharid = 0  And Anbar.Kala_Max =5  then ((Anbar.Price_Omdeh - (Anbar.Price_Kharid * @PriceKala_Max3))/ (@PriceKala_Max3 * 1)) * 100    
      When Anbar.Price_Kharid <> 0 And Anbar.Kala_Max = 5  then ((Anbar.Price_Omdeh - (Anbar.Price_Kharid * @PriceKala_Max3))/ (@PriceKala_Max3 * Anbar.Price_Kharid)) * 100   

    end As DarsadSood
    FROM Anbar INNER JOIN KalaId ON Anbar.K_Code = KalaId.K_Code INNER JOIN
    Havaleh ON Anbar.A_Code = Havaleh.Sender AND Anbar.K_Code = Havaleh.K_Code INNER JOIN
    headH ON Havaleh.Id_Havaleh = headH.Id_Havaleh
    WHERE (headH.Kind_Flag IN (@kind7, @kind8, @kind9, @kind10)) AND (Anbar.A_Code = @ACode) 
    And (KalaId.K_Code >=@AzCode And KalaId.K_Code <=@TACode)
    GROUP BY KalaId.K_Code, KalaId.Barcode, KalaId.Name1, Anbar.Price_Kharid, 
    Anbar.Kala_Max, Anbar.Price_Omdeh,HeadH.Tarikh_Faktor,
    Anbar.Resive_fa, Anbar.Exit_Fa,Anbar.Mojodi) as Table2
    On(Table1.K_Code = Table2.K_Code and Table1.MaxTarikh = Table2.Tarikh_Faktor)) As PublicTable) As Rasool
    Group By K_Code, BarCode, Name1, Resive_Fa, Exit_Fa, Mojodi, Price_Kharid, Kala_Max, TypePrice, DarsadSood
    Order By K_Code
    --------------------------------(مقادير خروجي)----------------------------------------
  End -- @If Type_Price = '0'
  Else
  Begin
    IF @TaCode = '' 
      Set @TaCode = 'ZZZZZZZZZZZZZ'

    INSERT @RepGolKish
    Select K_Code, BarCode, Name1, Resive_Fa, Exit_Fa, Mojodi, Price_Kharid, Kala_Max, TypePrice, 
    Cast(Sum(EndEnterTarikh) as DateTime) EndEnterTarikh, 
    Cast(Sum(EndExitTarikh) As DateTime) EndExitTarikh,
    Sum(EndEnterKqty) EndEnterKqty , 
    Sum(EndExitKqty) EndExitKqty, DarsadSood  
    From (

    Select K_Code, BarCode, Name1, Resive_Fa, Exit_Fa, Mojodi, Price_Kharid, Kala_Max, TypePrice, 
    Case 
      When TypeFactor = 1  then Cast(MaxTarikh as Float)
    End As EndEnterTarikh,
    Case 
      When TypeFactor = 2  then Cast(MaxTarikh as Float)
    End As EndExitTarikh,
 
    Case 
      When TypeFactor = 1  then SumKQty
    End As EndEnterKqty,
    Case 
      When TypeFactor = 2  then SumKQty
    End As EndExitKqty, DarsadSood
    From (
    --------------------------------(مقادير ورودي)----------------------------------------
    Select 1 as TypeFactor,Table1.K_Code, Table2.Barcode, Table2.Name1, 
    Table2.Price_Kharid, Table2.Kala_Max, Table2.Price_Forosh TypePrice, 
    Table2.SumKQty ,Table1.MaxTarikh , Table2.Mojodi, 
    Table2.Resive_Fa, Table2.Exit_Fa, Table2.DarsadSood
    From (
    Select Max(dbo.HeadH.Tarikh_Faktor) MaxTarikh , Havaleh.K_Code From Havaleh
    Inner Join HeadH on (Havaleh.Id_Havaleh = HeadH.Id_Havaleh)
    WHERE (headH.Kind_Flag IN (@kind1, @kind2, @kind3, @kind4,@kind5, @kind6)) 
    AND (Havaleh.Reciver = @ACode) AND (Havaleh.K_Code >=@AzCode And Havaleh.K_Code <=@TaCode )
    Group By Havaleh.K_Code) As Table1

    Inner Join (

    SELECT KalaId.K_Code, KalaId.Barcode, KalaId.Name1, Anbar.Price_Kharid, 
    Anbar.Kala_Max, Anbar.Price_Forosh, Anbar.Resive_fa, Anbar.Exit_Fa,
    SUM(Havaleh.K_Qty1) SumKQty, HeadH.Tarikh_Faktor, Anbar.Mojodi, 
    Case   
      When Anbar.Price_Kharid = 0  And (Anbar.Kala_Max Not in (5,10,15,20) Or Anbar.Kala_Max Is Null) then ((Anbar.Price_Forosh - (Anbar.Price_Kharid * @DefaultPrice))/ (@DefaultPrice * 1)) * 100    
      When Anbar.Price_Kharid <> 0 And (Anbar.Kala_Max Not in (5,10,15,20) Or Anbar.Kala_Max Is Null) then ((Anbar.Price_Forosh - (Anbar.Price_Kharid * @DefaultPrice))/ (@DefaultPrice * Anbar.Price_Kharid)) * 100   
      When Anbar.Price_Kharid = 0  And Anbar.Kala_Max = 20  then ((Anbar.Price_Forosh - (Anbar.Price_Kharid * @PriceKala_Max))/ (@PriceKala_Max * 1)) * 100    
      When Anbar.Price_Kharid <> 0 And Anbar.Kala_Max = 20  then ((Anbar.Price_Forosh - (Anbar.Price_Kharid * @PriceKala_Max))/ (@PriceKala_Max * Anbar.Price_Kharid)) * 100   
      When Anbar.Price_Kharid = 0  And Anbar.Kala_Max =  15 then ((Anbar.Price_Forosh - (Anbar.Price_Kharid * @PriceKala_Max1))/ (@PriceKala_Max1 * 1)) * 100    
      When Anbar.Price_Kharid <> 0 And Anbar.Kala_Max = 15  then ((Anbar.Price_Forosh - (Anbar.Price_Kharid * @PriceKala_Max1))/ (@PriceKala_Max1* Anbar.Price_Kharid)) * 100   
      When Anbar.Price_Kharid = 0  And Anbar.Kala_Max = 10  then ((Anbar.Price_Forosh - (Anbar.Price_Kharid * @PriceKala_Max2))/ (@PriceKala_Max2 * 1)) * 100    
      When Anbar.Price_Kharid <> 0 And Anbar.Kala_Max = 10  then ((Anbar.Price_Forosh - (Anbar.Price_Kharid * @PriceKala_Max2))/ (@PriceKala_Max2 * Anbar.Price_Kharid)) * 100   
      When Anbar.Price_Kharid = 0  And Anbar.Kala_Max =  5 then ((Anbar.Price_Forosh - (Anbar.Price_Kharid * @PriceKala_Max3))/ (@PriceKala_Max3 * 1)) * 100    
      When Anbar.Price_Kharid <> 0 And Anbar.Kala_Max = 5  then ((Anbar.Price_Forosh - (Anbar.Price_Kharid * @PriceKala_Max3))/ (@PriceKala_Max3* Anbar.Price_Kharid)) * 100   

    End As DarsadSood
    FROM Anbar INNER JOIN KalaId ON Anbar.K_Code = KalaId.K_Code INNER JOIN
    Havaleh ON Anbar.A_Code = Havaleh.Reciver AND Anbar.K_Code = Havaleh.K_Code INNER JOIN
    headH ON Havaleh.Id_Havaleh = headH.Id_Havaleh
    WHERE (headH.Kind_Flag IN (@kind1, @kind2, @kind3, @kind4,@kind5, @kind6)) AND (Anbar.A_Code = @ACode) 
    And (KalaId.K_Code >=@AzCode And KalaId.K_Code <=@TACode)
    GROUP BY KalaId.K_Code, KalaId.Barcode, KalaId.Name1, Anbar.Price_Kharid, 
    Anbar.Kala_Max, Anbar.Price_Forosh,HeadH.Tarikh_Faktor,
    Anbar.Resive_fa, Anbar.Exit_Fa,Anbar.Mojodi) as Table2
    On(Table1.K_Code = Table2.K_Code and Table1.MaxTarikh = Table2.Tarikh_Faktor)
    --------------------------------(مقادير ورودي)----------------------------------------

    Union 

    --------------------------------(مقادير خروجي)----------------------------------------
    Select 2 as TypeFactor,Table1.K_Code, Table2.Barcode, Table2.Name1, 
    Table2.Price_Kharid, Table2.Kala_Max, Table2.Price_Forosh TypePrice, 
    Table2.SumKQty ,Table1.MaxTarikh , Table2.Mojodi, 
    Table2.Resive_Fa, Table2.Exit_Fa, Table2.DarsadSood
    From (
    Select Max(HeadH.Tarikh_Faktor) MaxTarikh , Havaleh.K_Code From Havaleh
    Inner Join HeadH on (Havaleh.Id_Havaleh = HeadH.Id_Havaleh)
    WHERE (headH.Kind_Flag IN (@kind7, @kind8, @kind9, @kind10)) 
    AND (Havaleh.Sender = @ACode) AND 
    (Havaleh.K_Code >=@AzCode And Havaleh.K_Code <=@TACode)
    Group By Havaleh.K_Code) As Table1

    Inner Join (

    SELECT KalaId.K_Code, KalaId.Barcode, KalaId.Name1, Anbar.Price_Kharid, 
    Anbar.Kala_Max, Anbar.Price_Forosh, Anbar.Resive_fa, Anbar.Exit_Fa,
    SUM(Havaleh.K_Qty1) SumKQty, HeadH.Tarikh_Faktor, Anbar.Mojodi, 
    Case   
      When Anbar.Price_Kharid = 0  And (Anbar.Kala_Max Not in(5,10,15,20) Or Anbar.Kala_Max Is Null) then ((Anbar.Price_Forosh - (Anbar.Price_Kharid * @DefaultPrice))/ (@DefaultPrice * 1)) * 100    
      When Anbar.Price_Kharid <> 0 And (Anbar.Kala_Max Not in(5,10,15,20) Or Anbar.Kala_Max Is Null) then ((Anbar.Price_Forosh - (Anbar.Price_Kharid * @DefaultPrice))/ (@DefaultPrice * Anbar.Price_Kharid)) * 100   
      When Anbar.Price_Kharid = 0  And Anbar.Kala_Max = 20  then ((Anbar.Price_Forosh - (Anbar.Price_Kharid * @PriceKala_Max))/ (@PriceKala_Max * 1)) * 100    
      When Anbar.Price_Kharid <> 0 And Anbar.Kala_Max = 20  then ((Anbar.Price_Forosh - (Anbar.Price_Kharid * @PriceKala_Max))/ (@PriceKala_Max * Anbar.Price_Kharid)) * 100   
      When Anbar.Price_Kharid = 0  And Anbar.Kala_Max = 15  then ((Anbar.Price_Forosh - (Anbar.Price_Kharid * @PriceKala_Max1))/ (@PriceKala_Max1 * 1)) * 100    
      When Anbar.Price_Kharid <> 0 And Anbar.Kala_Max = 15  then ((Anbar.Price_Forosh - (Anbar.Price_Kharid * @PriceKala_Max1))/ (@PriceKala_Max1 * Anbar.Price_Kharid)) * 100   
      When Anbar.Price_Kharid = 0  And Anbar.Kala_Max = 10  then ((Anbar.Price_Forosh - (Anbar.Price_Kharid * @PriceKala_Max2))/ (@PriceKala_Max2 * 1)) * 100    
      When Anbar.Price_Kharid <> 0 And Anbar.Kala_Max = 10  then ((Anbar.Price_Forosh - (Anbar.Price_Kharid * @PriceKala_Max2))/ (@PriceKala_Max2 * Anbar.Price_Kharid)) * 100   
      When Anbar.Price_Kharid = 0  And Anbar.Kala_Max = 5  then ((Anbar.Price_Forosh - (Anbar.Price_Kharid * @PriceKala_Max3))/ (@PriceKala_Max3 * 1)) * 100    
      When Anbar.Price_Kharid <> 0 And Anbar.Kala_Max =5  then ((Anbar.Price_Forosh - (Anbar.Price_Kharid * @PriceKala_Max3))/ (@PriceKala_Max3 * Anbar.Price_Kharid)) * 100   

    End As DarsadSood
    FROM Anbar INNER JOIN KalaId ON Anbar.K_Code = KalaId.K_Code INNER JOIN
    Havaleh ON Anbar.A_Code = Havaleh.Sender AND Anbar.K_Code = Havaleh.K_Code INNER JOIN
    headH ON Havaleh.Id_Havaleh = headH.Id_Havaleh
    WHERE (headH.Kind_Flag IN (@kind7, @kind8, @kind9, @kind10)) AND (Anbar.A_Code = @ACode) 
    And (KalaId.K_Code >=@AzCode And KalaId.K_Code <=@TACode)
    GROUP BY KalaId.K_Code, KalaId.Barcode, KalaId.Name1, Anbar.Price_Kharid, 
    Anbar.Kala_Max, Anbar.Price_Forosh,HeadH.Tarikh_Faktor,
    Anbar.Resive_fa, Anbar.Exit_Fa,Anbar.Mojodi) as Table2
    On(Table1.K_Code = Table2.K_Code and Table1.MaxTarikh = Table2.Tarikh_Faktor)) As PublicTable) As Rasool
    Group By K_Code, BarCode, Name1, Resive_Fa, Exit_Fa, Mojodi, Price_Kharid, Kala_Max, TypePrice, DarsadSood
    Order By K_Code
    --------------------------------(مقادير خروجي)----------------------------------------
  End
  RETURN
END
