create FUNCTION  [dbo].[CycleAvgAnbar]  (@ACode Char(3),  @Date1 smalldatetime,@Date2 smalldatetime  , @FlagSearch Char(1))
RETURNS
@CycleAnbar TABLE
   (
   ACode   Varchar(3),
   K_Code  varchar(15),
   Name1 Nvarchar(50),
   Vorod  float,
   Sodor float,
   SumKharid Money,
   SumForosh Money,
   Mandeh float,
   PriceMin Money
   )
AS  
BEGIN 
  IF @FlagSearch = '0' 
  BEGIN
     INSERT @CycleAnbar
     Select @Acode ACode , K_Code, Name1, Sum(Vorod) Vorod , Sum(Sodor) Sodor , Sum(SumKharid) SumKharid, 
      Sum(SumForosh) SumForosh , Sum(Vorod) - Sum(Sodor) Mandeh  ,
      Sum(SumKharid) - Sum(SumForosh)  PriceMin  From (
      Select K_Code, Name1, IsNull(Sum(Vorod),0) Vorod, IsNull(Sum(Sodor),0) Sodor , IsNull(Sum(SumKharid),0) SumKharid, IsNull(Sum(SumForosh),0) SumForosh
      From (
     SELECT Kalaid.K_Code,Kind_Flag,Name1,
      Case
      When Sender =@ACode Then SUM(dbo.Havaleh.K_Qty1) 
      End As Sodor,
      Case
         When Sender  = @ACode   then  '1'
         When Reciver = @ACode   then  '2'
      End As Type,
      Case
         When Reciver = @ACode Then  SUM(dbo.Havaleh.K_Qty1) 
      End As Vorod,
      Case
        when Reciver = @ACode And headH.Kind_Flag in ('3','2') then sum (Price_KHarid* Havaleh.K_Qty1)  
        when Reciver = @ACode And headH.Kind_Flag in ('1') then sum (Avg_Price* Havaleh.K_Qty1)  
        when headH.Kind_Flag in ('0','6','8') then sum (Avg_Price* Havaleh.K_Qty1)  
      End As SumKharid,
     Case
        When HeadH.kind_flag in ('5','7','9','4')  Then SUM(dbo.Havaleh.Avg_Price * dbo.Havaleh.K_Qty1) 
        When Sender = @ACode And HeadH.kind_flag in ('1')  Then SUM(dbo.Havaleh.Avg_Price * dbo.Havaleh.K_Qty1) 
     End As SumForosh

     FROM  dbo.headH INNER JOIN dbo.Havaleh ON dbo.headH.Id_Havaleh = dbo.Havaleh.Id_Havaleh
     Inner Join Kalaid On (Havaleh.K_Code = Kalaid.K_Code)
     Where Convert(Varchar(10), Tarikh_Faktor, 111) >= @Date1
     And Convert(Varchar(10), Tarikh_Faktor, 111) <= @Date2
     And (Sender =  @ACode or Reciver = @ACode)
     Group By Kalaid.K_Code, Sender, Reciver, Kind_Flag,Name1
    ) As T1
     Group By Type, K_Code,Name1) as T1
     Group By K_Code,Name1
  END
  ElSE IF @FlagSearch = '1' 
  BEGIN
     INSERT @CycleAnbar
     Select @ACode ACode , K_Code, Name1, Sum(Vorod) Vorod , Sum(Sodor) Sodor , Sum(SumKharid) SumKharid, 
      Sum(SumForosh) SumForosh , Sum(Vorod) - Sum(Sodor) Mandeh ,
      Sum(SumKharid) - Sum(SumForosh)  PriceMin  From (
      Select K_Code, Name1, IsNull(Sum(Vorod),0) Vorod, IsNull(Sum(Sodor),0) Sodor , IsNull(Sum(SumKharid),0) SumKharid, IsNull(Sum(SumForosh),0) SumForosh
      From (
     SELECT Kalaid.K_Code,Kind_Flag, Name1,
      Case
      When Sender =@ACode Then SUM(dbo.Havaleh.K_Qty1) 
      End As Sodor,
      Case
         When Sender  = @ACode   then  '1'
         When Reciver = @ACode   then  '2'
      End As Type,
      Case
         When Reciver = @ACode Then  SUM(dbo.Havaleh.K_Qty1) 
      End As Vorod,
      Case
        when Reciver = @ACode And headH.Kind_Flag in ('3','2') then sum (Price_KHarid* Havaleh.K_Qty1)  
        when Reciver = @ACode And headH.Kind_Flag in ('1') then sum (Avg_Price* Havaleh.K_Qty1)  
        when headH.Kind_Flag in ('0','6','8') then sum (Avg_Price* Havaleh.K_Qty1)  
      End As SumKharid,
     Case
        When HeadH.kind_flag in ('5','7','9','4')  Then SUM(dbo.Havaleh.Avg_Price * dbo.Havaleh.K_Qty1) 
        When Sender = @ACode And HeadH.kind_flag in ('1')  Then SUM(dbo.Havaleh.Avg_Price * dbo.Havaleh.K_Qty1) 
     End As SumForosh
     FROM  dbo.headH INNER JOIN dbo.Havaleh ON dbo.headH.Id_Havaleh = dbo.Havaleh.Id_Havaleh
     Inner Join Kalaid On (Havaleh.K_Code = Kalaid.K_Code)
     Where Convert(Varchar(10), Tarikh_Faktor, 111) < @Date1
     And (Sender =  @ACode or Reciver = @ACode)
     Group By Kalaid.K_Code, Sender, Reciver, Kind_Flag, Name1
    ) As T1
     Group By Type, K_Code, Name1) as T1
     Group By K_Code, Name1
  END
  ElSE IF @FlagSearch = '2' 
  BEGIN
        INSERT @CycleAnbar
        Select T3.A_Code, T3.K_Code, T3.Name1, 0 vorod ,0 Sodor ,0 SumKharid , 0 SumForosh ,
       0 Mandeh , T3.Avg_Price PriceMin  From (
        SELECT  Havaleh.K_Code, Havaleh.AutoId,
        headH.Tarikh_faktor, SUM(Havaleh.K_Qty1) 
        AS SumKQty, KalaId.Name1, HeadH.Kind_Flag,
        Case  
           when kind_flag='2' then '1'    
           when kind_flag='3' then '2'    
           when kind_flag='1' then '3'    
           when kind_flag='6' then '4'    
           when kind_flag='8' then '5'    
           when kind_flag='0' then '6'    
           when kind_flag='4' then '7'    
           when kind_flag='5' then '8'    
           when kind_flag='7' then '9'    
           when kind_flag='9' then '10'   
        End as Radif,
       Case
          When Sender  <> @ACode Then Reciver
          When Reciver <> @ACode Then Sender
        End As A_Code,
       Case
          When Reciver = @ACode And Kind_Flag = '1' then Tot_Omdeh
          else
            Avg_Price
        End As Avg_Price 
        FROM headH INNER JOIN
         Havaleh ON headH.Id_Havaleh = Havaleh.Id_Havaleh INNER JOIN
         Anbar ON Havaleh.K_Code = Anbar.K_Code INNER JOIN
         KalaId ON Anbar.K_Code = KalaId.K_Code
         WHERE (Sender = @ACode Or Reciver = @ACode)
         And Convert(Varchar(10), Tarikh_Faktor, 111) >= @Date1
         And Convert(Varchar(10), Tarikh_Faktor, 111) <= @Date2
         GROUP BY Havaleh.K_Code, Havaleh.Tot_Omdeh,
          headH.Tarikh_faktor, Havaleh.Avg_Price,KalaId.Name1,Havaleh.Sender, Havaleh.Reciver,  
          HeadH.Kind_Flag, Havaleh.AutoId) As T3
          Inner Join 
           (
             ------------------------------------------------------------
	 Select MaxTarikh, K_Code, Max(Radif) Radif,Max(AutoId) Autoid From (
	 Select T1.MaxTarikh, T.K_Code, T.Radif ,AutoId  From 
	 (
	    SELECT MAX(Tarikh_Faktor) MaxTarikh, K_code
	    FROM HeadH INNER JOIN Havaleh ON HeadH.Id_Havaleh = Havaleh.Id_Havaleh
	    Where Convert(Varchar(10), Tarikh_Faktor, 111) >= @Date1
	     And Convert(Varchar(10), Tarikh_Faktor, 111) <= @Date2
	     And (Sender = @ACode Or Reciver = @ACode)
	     GROUP BY K_code) As T1
	     Inner Join 
	    ( 
	      SELECT K_code,Tarikh_Faktor,Havaleh.AutoId,
	      Case  
              when kind_flag='2' then '1'    
	          when kind_flag='3' then '2'    
	          when kind_flag='1' then '3'    
	          when kind_flag='6' then '4'    
	         when kind_flag='8' then '5'    
	         when kind_flag='0' then '6'    
	         when kind_flag='4' then '7'    
	         when kind_flag='5' then '8'    
	         when kind_flag='7' then '9'    
                       when kind_flag='9' then '10'   
	      End as RadiF  
	      FROM HeadH INNER JOIN Havaleh ON HeadH.Id_Havaleh = Havaleh.Id_Havaleh
	       Where Convert(Varchar(10), Tarikh_Faktor, 111) >= @Date1
	       And Convert(Varchar(10), Tarikh_Faktor, 111) <= @Date2
	       And (Sender = @ACode Or Reciver = @ACode)
	      GROUP BY K_code, kind_flag,Tarikh_Faktor,Havaleh.AutoId) As T
	     On (T.K_Code = T1.K_Code And T.Tarikh_Faktor = T1.MaxTarikh)) As T5
	     Group By K_Code, MaxTarikh
                    ) As T4
                    On (T3.K_Code = T4.K_Code And T3.Tarikh_faktor = T4.MaxTarikh And T3.AutoId = T4.AutoId)
                   Group By T3.A_Code, T3.K_Code, T3.Name1, T3.Avg_Price 

  ------------------------------------------------------------
  END
RETURN
END