CREATE PROCEDURE [dbo].[Sp_SleepKalaAvg]  @DataBase VARCHAR(6), @KindF VARCHAR(50), @KindBF VARCHAR(50),
                                                                              @StrAnbar  VARCHAR (100) , @GroupAnbar VARCHAR(100) , 
 					        @KolStrDate VARCHAR(1500), @StrCase VARCHAR (120), 
                                                                              @Qty VARCHAR (4), @StrMojodi VARCHAR(100), @StrFieldMojodi VARCHAR(50), 
					       @StrLink VARCHAR(100), @CheckLevelLink CHAR (1), @Str_Anbar1 VARCHAR(90), @Str_Anbar2 VARCHAR(90), @CodeAnbar VARCHAR(90), @CodeForoshandeh VARCHAR(5)= NULL 
AS 

DECLARE @Str VARCHAR(8000)
DECLARE @TmpStr VARCHAR(2000)

SET @TmpStr = '  if Exists (select * from [Tempdb].dbo.sysobjects where Name = N''##Avg'') 
                              drop table ##Avg          
                              Create Table ##Avg (K_Code Varchar(15) null, Name1 Nvarchar(100) Null, Name2 Nvarchar(80) Null, 
							  GuyCode Varchar(5) Null, Name NVarchar(90) Null,
                               Mojodi Money Null, KalaMin Real Null, Avg_Forosh_z Money Null,
                               Avg_Forosh_y Money Null, Sood Money Null,Price_Kharid Money,
                              NameUnit2 Nvarchar(30) Null, Qty_Forosh Real null
                              ,Note Nvarchar(60) Null , NameH1 Nvarchar(30) Null,  
                              NameL1 Nvarchar(30) Null, Namel2 Nvarchar(30) Null,
                               SumKolKharid Money Null, SumKolForosh Money Null,AssetpriceKharid Money Null
                              ,AssetpriceForosh Money Null, AssetAvgpriceKharid Money  Null, 
                              QtyDay Int Null, R_Unit1_2 Real)'
EXEC(@TmpStr)

 SET @Str= '  Declare @Id1 Smallint
              Select @id1 = Max(Qtyday) From (
	          SELECT K_Code, Count(K_code) QtyDay
               FROM (  
                        SELECT [' + @DataBase + '].Dbo.havaleh.k_code,
                        COUNT([' + @DataBase  + '].Dbo.havaleh.k_qty1) AS Qtyday
                        FROM [' + @DataBase   + '].Dbo.havaleh 
                        INNER JOIN [' + @DataBase + '].Dbo.Headh ON [' + @DataBase + '].Dbo.havaleh.id_havaleh = [' + 
                       @DataBase + '].Dbo.headh.id_havaleh  
	                      Where ([' + @DataBase + '].Dbo.HeadH.Kind_Flag ' + @KindF  +
                        '  GROUP BY [' + @DataBase + '].Dbo.Havaleh.k_code, 
                           Convert(Varchar(10), [' + @DataBase + '].Dbo.HeadH.Tarikh_Faktor, 111)  
                       ) AS Table1 GROUP BY K_Code) As T

                      Insert  Into ##Avg(K_Code, Name1, Name2, GuyCode, Name,QtyDay, Qty_Forosh, Price_Kharid,Mojodi, KalaMin,  Avg_Forosh_z, 
                    Avg_Forosh_y, SumKolKharid,  SumKolForosh,  Sood, AssetpriceKharid,   AssetpriceForosh,AssetAvgpriceKharid,
                   NameH1, NameL1, Namel2, NameUnit2, Note,R_Unit1_2)  

                    Select RepTotal.K_Code, Name1, Name2,GuyCode, Name,QtyDay, Qty_Forosh, RepTotal.Price_Kharid,Mojodi, 0  KalaMin, 
                    Case       
                       When QtyDay = 0   then 0       
                       When QtyDay <> 0  then Avg_Forosh
                     End As Avg_Forosh_z, 
                    0 As Avg_Forosh_y,
                    SumKolKharid,  SumKolForosh, SumKolForosh - SumKolKharid Sood , AssetpriceKharid,   AssetpriceForosh,AssetAvgpriceKharid  , '
                   IF @CheckLevelLink = '1' 
                      Set @Str = @Str +  ' NameH1, NameL1, Namel2,'
                   Else
                       Set @Str = @Str +  ' '''' NameH1,  '''' NameL1,  '''' Namel2,' 

                  Set @Str = @Str + 
	   ' Case       
	    When NameUnit2 Is Null Then NameUnit1        
	     When NameUnit2 Is Not Null Then NameUnit2   
	  End as NameUnit2,  
	   Case        ' +
	  '  When R_Unit1_2 <> 1 Then (''هر ''  + NameUnit2 + ''معادل'' + Cast(R_Unit1_2 as Varchar(10)) + '' '' + NameUnit1 + ''است'')   '  +
	  ' End as Note,  R_Unit1_2
	    From (
	       Select K_Code, Name1, Name2, GuyCode, Name, QtyDay, Qty_Forosh, TotalQuery.Price_Kharid,Avg_Forosh, Mojodi, Kala_Min,
	       R_Unit1_2,SumKolKharid,  SumKolForosh,
	       NameUnit1, NameUnit2,AssetpriceKharid,  AssetpriceForosh,    AssetAvgpriceKharid '
	    IF @CheckLevelLink = '1' 
                     Set @Str = @Str +  ', NameH1, NameL1, Namel2'
	     Else
	          Set @Str = @Str +  ',''''  NameH1, '''' NameL1, '''' Namel2'
	     Set @Str = @Str + 
	     ' From ( 
	       SELECT PublicForosh.K_Code, PublicForosh.Name1, PublicForosh.Name2, PublicForosh.GuyCode,PublicForosh.Name,PublicForosh.QtyDay, Anbar.Kala_Min,
	       PublicForosh.Qty_Forosh,  SUM(Price_Kharid)/COUNT(Price_Kharid) Price_Kharid,PublicForosh.Avg_Forosh, SUM(Anbar.Mojodi) AS Mojodi,KalaId.R_Unit1_2, 
	       Unit1.Name NameUnit1, Unit2.Name NameUnit2,PublicForosh.SumKolKharid, PublicForosh.SumKolForosh,  
	       SUM(Anbar.Mojodi * Anbar.Price_Kharid) AS AssetpriceKharid,  
	       SUM(Anbar.Mojodi * Anbar.Price_Forosh) AS AssetpriceForosh,  
	       SUM(Anbar.Mojodi * Anbar.Avg_Price_Kharid) AS AssetAvgpriceKharid  ' 
                   IF @CheckLevelLink = '1' 
	       Set @Str = @Str +  ', Level_Header.Name AS NameH1, Level_Detail1.Name AS NameL1, 
                               Level_Detail2.Name AS Namel2
   		                       FROM    Level_Link INNER JOIN
				 Level_Header ON Level_Link.L_Code = Level_Header.L_Code LEFT OUTER JOIN
				 Level_Detail1 ON Level_Link.L_Code1 = Level_Detail1.Ch_Code AND Level_Header.L_Code = Level_Detail1.Fa_Code LEFT OUTER JOIN
				 Level_Detail2 ON Level_Link.L_Code2 = Level_Detail2.Ch_Code AND Level_Detail1.Ch_Code = Level_Detail2.Fa_Code RIGHT OUTER JOIN ('
                 Else
                   Set @Str = @Str +   ' From ('
	       Set @Str = @Str + 
	      ' SELECT K_Code, Name1, Name2,Table1.GuyCode, Table1.Name, Count(K_code) QtyDay, SUM(Qty_Forosh) Qty_Forosh, 
	      SUM(Qty_Forosh) /@id1   Avg_Forosh  ,Sum(SumKolKharid) SumKolKharid,  
	      Sum(SumKolForosh) SumKolForosh 
	      FROM (  
	      SELECT [' + @DataBase+ '].Dbo.havaleh.k_code, [' + @DataBase + '].Dbo.kalaid.Name1, [' + @DataBase + '].Dbo.kalaid.Name2, [' + @DataBase + '].Dbo.RegForoshKala.Sender GuyCode,[' + @DataBase + '].Dbo.Guy.Name,
	      COUNT([' + @DataBase + '].Dbo.havaleh.k_qty1) AS Qtyday, 
	      SUM([' + @DataBase   + '].Dbo.havaleh.k_qty1) AS Qty_forosh,  
	      Sum([' + @DataBase   + '].Dbo.Havaleh.Price_Kharid * [' + @DataBase + '].Dbo.Havaleh.K_Qty1) SumKolKharid, 
	      Sum([' + @DataBase   + '].Dbo.Havaleh.Price_Forosh * [' + @DataBase + '].Dbo.Havaleh.K_Qty1) SumKolForosh 
	      FROM [' + @DataBase  + '].Dbo.havaleh INNER JOIN [' + @DataBase + '].Dbo.kalaid  
	      ON [' + @DataBase    + '].Dbo.havaleh.k_code = [' + @DataBase + '].Dbo.kalaid.k_code   
          LEFT OUTER JOIN ['   + @DataBase + '].Dbo.RegForoshKala ON [' + @DataBase + '].dbo.RegForoshKala.K_Code = ['   + @DataBase + '].Dbo.havaleh.K_Code
          LEFT OUTER JOIN ['   + @DataBase + '].Dbo.Guy ON [' + @DataBase + '].dbo.guy.Guy_Code = ['   + @DataBase + '].Dbo.RegForoshKala.Sender'  

	       IF LTrim(RTrim(@StrLink)) <> '0 '
	           Set @Str = @Str + @StrLink  + @Str_Anbar1

	       Set @Str = @Str  + ' INNER JOIN [' + @DataBase + '].Dbo.Headh ON [' + @DataBase + '].Dbo.havaleh.id_havaleh = [' + @DataBase + '].Dbo.headh.id_havaleh ' +
	        ' Where ([' + @DataBase + '].Dbo.HeadH.Kind_Flag ' + @KindF 

                     IF LTrim(RTrim(@StrAnbar)) <> '0 '
                         Set @Str = @Str + @StrAnbar  

          Set @Str = @Str + ' GROUP BY [' + @DataBase + '].Dbo.kalaid.name1, [' + @DataBase + '].Dbo.kalaid.name2,[' + @DataBase + '].Dbo.RegForoshKala.Sender, [' + @DataBase + '].Dbo.Guy.Name,
          [' + @DataBase + '].Dbo.Havaleh.k_code, Convert(Varchar(10), ['  + @DataBase + '].Dbo.HeadH.Tarikh_Faktor, 111)'
                    

                    IF LTrim(RTrim(@GroupAnbar)) <>  '0 '
                         Set @Str = @Str + @GroupAnbar 

                     Set @Str = @Str +   ' UNION ALL ' +	 	


	       'SELECT [' + @DataBase + '].Dbo.havaleh.k_code, [' + @DataBase + '].Dbo.kalaid.Name1, [' + @DataBase + '].Dbo.kalaid.Name2,[' + @DataBase + '].Dbo.RegForoshKala.Sender GuyCode,[' + @DataBase + '].Dbo.Guy.Name,' +					   
	       ' 0 AS Qtyday,  - SUM([' + @DataBase + '].Dbo.havaleh.k_qty1) AS qty_forosh,  '                      +
	       ' Sum([' + @DataBase + '].Dbo.Havaleh.Price_Kharid * [' + @DataBase + '].Dbo.Havaleh.K_Qty1) SumKolKharid,  ' +
	       ' Sum([' + @DataBase + '].Dbo.Havaleh.Price_Forosh * [' + @DataBase + '].Dbo.Havaleh.K_Qty1) SumKolForosh ' +
	       ' FROM [' + @DataBase + '].Dbo.havaleh INNER JOIN [' + @DataBase + '].Dbo.kalaid  '   +
	       ' ON [' + @DataBase + '].Dbo.havaleh.k_code = [' + @DataBase + '].Dbo.kalaid.k_code   '  +
           ' LEFT OUTER JOIN [' + @DataBase + '].Dbo.RegForoshKala ON [' + @DataBase + '].dbo.RegForoshKala.K_Code = ['   + @DataBase + '].Dbo.havaleh.K_code' +
           ' LEFT OUTER JOIN [' + @DataBase + '].Dbo.Guy ON [' + @DataBase + '].dbo.RegForoshKala.Sender = ['   + @DataBase + '].Dbo.Guy.Guy_code'  
	       
	       IF LTrim(RTrim(@StrLink)) <> '0 '
	          Set @Str = @Str + @StrLink + @Str_Anbar2
	
	       Set @Str = @Str +   ' INNER JOIN [' + @DataBase + '].Dbo.headh ON [' + @DataBase + '].Dbo.havaleh.id_havaleh = [' + @DataBase + '].Dbo.headh.id_havaleh ' +
			              '  Where ([' + @DataBase + '].Dbo.HeadH.Kind_Flag ' + @KindBF  

	      IF LTrim(RTrim(@StrAnbar ))<> '0'
	         Set @Str = @Str + @StrAnbar  
	
	     Set @Str = @Str +  '  GROUP BY [' + @DataBase + '].Dbo.kalaid.name1, [' + @DataBase + '].Dbo.kalaid.name2,[' + @DataBase + '].Dbo.havaleh.k_code,[' + @DataBase + '].Dbo.RegForoshKala.Sender, [' + @DataBase + '].Dbo.Guy.Name'     

                   IF LTrim(RTrim(@GroupAnbar)) <>  '0 '
                      Set @Str = @Str + @GroupAnbar 

                   Set @Str = @Str +   ') AS Table1 
    	     GROUP BY K_Code, Name1,Name2, Table1.GuyCode,Table1.Name
	         ) PublicForosh INNER JOIN Anbar 
	         ON PublicForosh.K_Code = Anbar.K_Code INNER JOIN KalaId 
	         ON Anbar.K_Code = KalaId.K_Code INNER JOIN Unit1 
	         ON KalaId.U_Code1 = Unit1.U_Code1 '
                  IF @CheckLevelLink = '1' 
                      Set @Str = @Str + ' ON Level_Link.K_Code = PublicForosh.K_Code '  
                   Set @Str =  @Str + ' LEFT OUTER JOIN Unit2 ON KalaId.U_Code2 = Unit2.U_Code2 '							          
                   IF @CodeAnbar <> ''
                       Set @Str =  @Str +@CodeAnbar 
              Set @Str = @Str +  
			'GROUP BY PublicForosh.K_Code, PublicForosh.Name1, PublicForosh.Name2,
			PublicForosh.QtyDay, PublicForosh.Qty_Forosh, 
			PublicForosh.Avg_Forosh, PublicForosh.SumKolKharid, PublicForosh.SumKolForosh, PublicForosh.GuyCode,PublicForosh.Name,
			KalaId.R_Unit1_2, Unit1.Name, Unit2.Name, Anbar.Kala_Min'
            IF @CheckLevelLink = '1' 
                Set @Str = @Str + ', Level_Header.Name, Level_Detail1.Name, Level_Detail2.Name'
           Set @Str  = @Str +  
          ') as TotalQuery'	 +									   
         ' ) as RepTotal'
         if @CodeForoshandeh <> ''
           Set @Str = @Str + ' Left Outer join (
		                                   Select K_Code From [' + @DataBase +'].Dbo.RegForoshKala
										   WHERE Sender = IsNull(' + @CodeForoshandeh + ',Sender)' + 
										   ') As T on T.k_code = RepTotal.K_Code'
                              
Exec(@Str) 

 Set @Str='Declare @Id2 Smallint 
                    Select @id2 = Max(Qtyday) From (
                    SELECT K_Code, Count(K_code) QtyDay
                     FROM (  
                    SELECT [' + @DataBase + '].Dbo.havaleh.k_code, 
	       COUNT([' + @DataBase + '].Dbo.havaleh.k_qty1) AS Qtyday
	        FROM [' + @DataBase + '].Dbo.havaleh 
	       INNER JOIN [' + @DataBase + '].Dbo.Headh ON [' + @DataBase + '].Dbo.havaleh.id_havaleh = [' + @DataBase + '].Dbo.headh.id_havaleh  
	       Where ([' + @DataBase + '].Dbo.HeadH.Kind_Flag ' + @KindF  + @KolStrDate+
	       'GROUP BY [' + @DataBase + '].Dbo.Havaleh.k_code, 
	       Convert(Varchar(10), ['+ @DataBase + '].Dbo.HeadH.Tarikh_Faktor, 111)
	       ) AS Table1  
	       GROUP BY K_Code
	       ) As T
                   Insert  Into ##Avg(K_Code, Name1, Name2,GuyCode, Name,QtyDay, Qty_Forosh, Price_Kharid,Mojodi, KalaMin,  Avg_Forosh_z, 
                   Avg_Forosh_y, SumKolKharid,  SumKolForosh,  Sood, AssetpriceKharid,   AssetpriceForosh,AssetAvgpriceKharid,
                   NameH1, NameL1, Namel2, NameUnit2, Note)
                   Select RepTotal.K_Code, Name1, Name2,GuyCode, Name,QtyDay, 0 Qty_Forosh, Price_Kharid,Mojodi,  Kala_Min/26 KalaMin, 
                   0 As Avg_Forosh_z, 
                   Avg_Forosh As Avg_Forosh_y,
                   SumKolKharid,  SumKolForosh, 0 Sood , AssetpriceKharid,   AssetpriceForosh,AssetAvgpriceKharid  , '
                   IF @CheckLevelLink = '1' 
                      Set @Str = @Str +  ' NameH1, NameL1, Namel2,'
                   Else
                       Set @Str = @Str +  ' '''' NameH1,  '''' NameL1,  '''' Namel2,' 

                  Set @Str = @Str + 
	  ' Case       
	    When NameUnit2 Is Null Then NameUnit1        
	    When NameUnit2 Is Not Null Then NameUnit2   
	  End as NameUnit2,   
	   Case 
	      When R_Unit1_2 <> 1 Then (''هر ''  + NameUnit2 + ''معادل'' + Cast(R_Unit1_2 as Varchar(10)) + '' '' + NameUnit1 + ''است'')   
	   End as Note  
	   From (
			   Select K_Code, Name1, Name2,GuyCode, Name,QtyDay, Qty_Forosh, Avg_Forosh, Mojodi, Kala_Min,
			   R_Unit1_2,SumKolKharid,  SumKolForosh, TotalQuery.Price_Kharid,
			   NameUnit1, NameUnit2,AssetpriceKharid,  AssetpriceForosh,    AssetAvgpriceKharid'
	    IF @CheckLevelLink = '1' 
                     Set @Str = @Str +  ', NameH1, NameL1, Namel2'
	     Else
	          Set @Str = @Str +  ',''''  NameH1, '''' NameL1, '''' Namel2'
	     Set @Str = @Str + 
	     ' From ( 
	      SELECT PublicForosh.K_Code, PublicForosh.Name1, PublicForosh.Name2, PublicForosh.GuyCode,PublicForosh.Name,PublicForosh.QtyDay, Anbar.Kala_Min,
	      PublicForosh.Qty_Forosh,  PublicForosh.Avg_Forosh, 0 AS Mojodi,KalaId.R_Unit1_2, 0 as Price_Kharid,
	      Unit1.Name NameUnit1, Unit2.Name NameUnit2,PublicForosh.SumKolKharid, PublicForosh.SumKolForosh,  
	      0 AS AssetpriceKharid,  
	      0 AS AssetpriceForosh,  
	      0 AS AssetAvgpriceKharid '
          IF @CheckLevelLink = '1' 
	       Set @Str = @Str +  ', Level_Header.Name AS NameH1, Level_Detail1.Name AS NameL1, 
                               Level_Detail2.Name AS Namel2
   		                       FROM    Level_Link INNER JOIN
				 Level_Header ON Level_Link.L_Code = Level_Header.L_Code LEFT OUTER JOIN
				 Level_Detail1 ON Level_Link.L_Code1 = Level_Detail1.Ch_Code AND Level_Header.L_Code = Level_Detail1.Fa_Code LEFT OUTER JOIN
				 Level_Detail2 ON Level_Link.L_Code2 = Level_Detail2.Ch_Code AND Level_Detail1.Ch_Code = Level_Detail2.Fa_Code RIGHT OUTER JOIN (
                     Else'
                         Set @Str = @Str +   ' From ('
	       Set @Str = @Str + 
	      ' SELECT K_Code, Name1, Name2,Table1.GuyCode, Table1.Name,0 QtyDay, SUM(Qty_Forosh) Qty_Forosh,  
	       SUM(Qty_Forosh) /@id2  Avg_Forosh  ,0 SumKolKharid,  
	      0 SumKolForosh 
	       FROM ( 
	       SELECT [' + @DataBase + '].Dbo.havaleh.k_code, [' + @DataBase + '].Dbo.kalaid.Name1, [' + @DataBase + '].Dbo.kalaid.Name2, [' + @DataBase + '].Dbo.RegForoshKala.Sender GuyCode,[' + @DataBase + '].Dbo.Guy.Name,
	       0 AS Qtyday, 
	       SUM([' + @DataBase + '].Dbo.havaleh.k_qty1) AS Qty_forosh,  
	       Sum([' + @DataBase + '].Dbo.Havaleh.Price_Kharid * [' + @DataBase + '].Dbo.Havaleh.K_Qty1) SumKolKharid, 
	       Sum([' + @DataBase + '].Dbo.Havaleh.Price_Forosh * [' + @DataBase + '].Dbo.Havaleh.K_Qty1) SumKolForosh  
	       FROM [' + @DataBase + '].Dbo.havaleh INNER JOIN [' + @DataBase + '].Dbo.kalaid  
	       ON [' + @DataBase + '].Dbo.havaleh.k_code = [' + @DataBase + '].Dbo.kalaid.k_code 
           LEFT OUTER JOIN [' + @DataBase + '].Dbo.RegForoshKala ON [' + @DataBase + '].dbo.RegForoshKala.K_Code = ['   + @DataBase + '].Dbo.havaleh.K_code
           LEFT OUTER JOIN [' + @DataBase + '].Dbo.Guy ON [' + @DataBase + '].dbo.RegForoshKala.Sender = ['   + @DataBase + '].Dbo.Guy.Guy_code'  

	       IF LTrim(RTrim(@StrLink)) <> '0 '
	           Set @Str = @Str + @StrLink  + @Str_Anbar1

	       Set @Str = @Str  + ' INNER JOIN [' + @DataBase + '].Dbo.Headh ON [' + @DataBase + '].Dbo.havaleh.id_havaleh = [' + @DataBase + '].Dbo.headh.id_havaleh ' +
	        ' Where ([' + @DataBase + '].Dbo.HeadH.Kind_Flag ' + @KindF  + @KolStrDate

                     IF LTrim(RTrim(@StrAnbar)) <> '0 '
                         Set @Str = @Str + @StrAnbar  

          Set @Str = @Str + ' GROUP BY [' + @DataBase + '].Dbo.kalaid.name1, [' + @DataBase + '].Dbo.kalaid.name2,[' + @DataBase + '].Dbo.RegForoshKala.Sender, [' + @DataBase + '].Dbo.Guy.Name,
          [' + @DataBase + '].Dbo.Havaleh.k_code, Convert(Varchar(10), ['  + @DataBase + '].Dbo.HeadH.Tarikh_Faktor, 111)'

           IF LTrim(RTrim(@GroupAnbar)) <>  '0 '
              Set @Str = @Str + @GroupAnbar 

            Set @Str = @Str +   ' UNION ALL 

	       SELECT [' + @DataBase + '].Dbo.havaleh.k_code, [' + @DataBase + '].Dbo.kalaid.Name1, [' + @DataBase + '].Dbo.kalaid.Name2, [' + @DataBase + '].Dbo.RegForoshKala.Sender GuyCode,[' + @DataBase + '].Dbo.Guy.Name,
	        0 AS Qtyday,  - SUM([' + @DataBase + '].Dbo.havaleh.k_qty1) AS qty_forosh,  
	        Sum([' + @DataBase + '].Dbo.Havaleh.Price_Kharid * [' + @DataBase + '].Dbo.Havaleh.K_Qty1) SumKolKharid, 
	        Sum([' + @DataBase + '].Dbo.Havaleh.Price_Forosh * [' + @DataBase + '].Dbo.Havaleh.K_Qty1) SumKolForosh 
	        FROM [' + @DataBase + '].Dbo.havaleh INNER JOIN [' + @DataBase + '].Dbo.kalaid  
	        ON [' + @DataBase + '].Dbo.havaleh.k_code = [' + @DataBase + '].Dbo.kalaid.k_code  
            LEFT OUTER JOIN [' + @DataBase + '].Dbo.RegForoshKala ON [' + @DataBase + '].dbo.RegForoshKala.K_Code = ['   + @DataBase + '].Dbo.havaleh.K_code
            LEFT OUTER JOIN [' + @DataBase + '].Dbo.Guy ON [' + @DataBase + '].dbo.RegForoshKala.Sender = ['   + @DataBase + '].Dbo.Guy.Guy_code'  
	       
	       IF LTrim(RTrim(@StrLink)) <> '0 '
	          Set @Str = @Str + @StrLink + @Str_Anbar2
	
	       Set @Str = @Str +  ' INNER JOIN [' + @DataBase + '].Dbo.headh ON [' + @DataBase + '].Dbo.havaleh.id_havaleh = [' + @DataBase + '].Dbo.headh.id_havaleh 
			                    Where ([' + @DataBase + '].Dbo.HeadH.Kind_Flag ' + @KindBF   + @KolStrDate

	      IF LTrim(RTrim(@StrAnbar ))<> '0'
	         Set @Str = @Str + @StrAnbar  
	
	       SET @Str = @Str +  '  GROUP BY [' + @DataBase + '].Dbo.kalaid.name1,' + '[' + @DataBase + '].Dbo.kalaid.name2, [' + @DataBase + '].Dbo.havaleh.k_code,[' + @DataBase + '].Dbo.RegForoshKala.Sender, [' + @DataBase + '].Dbo.Guy.Name'     

                   IF LTrim(RTrim(@GroupAnbar)) <>  '0 '
                      Set @Str = @Str + @GroupAnbar 

                   Set @Str = @Str +   ') AS Table1 
    				GROUP BY K_Code, Name1,Name2,Table1.GuyCode,Table1.Name
				   ) PublicForosh INNER JOIN Anbar 
				   ON PublicForosh.K_Code = Anbar.K_Code INNER JOIN KalaId 
				   ON Anbar.K_Code = KalaId.K_Code INNER JOIN Unit1 
				   ON KalaId.U_Code1 = Unit1.U_Code1 '
                  IF @CheckLevelLink = '1' 
                      Set @Str = @Str + ' ON Level_Link.K_Code = PublicForosh.K_Code     '  
                   Set @Str =  @Str + ' LEFT OUTER JOIN Unit2 ON KalaId.U_Code2 = Unit2.U_Code2  '							          
                   IF @CodeAnbar <> ''
                       Set @Str =  @Str +@CodeAnbar 
              Set @Str = @Str +  
				'GROUP BY PublicForosh.K_Code, PublicForosh.Name1,PublicForosh.Name2,PublicForosh.GuyCode,PublicForosh.Name, 
				PublicForosh.QtyDay, PublicForosh.Qty_Forosh, 
				PublicForosh.Avg_Forosh, PublicForosh.SumKolKharid, PublicForosh.SumKolForosh, 
				KalaId.R_Unit1_2, Unit1.Name, Unit2.Name, Anbar.Kala_Min'							          
            IF @CheckLevelLink = '1' 
                Set @Str = @Str + ', Level_Header.Name, Level_Detail1.Name, Level_Detail2.Name'
           Set @Str  = @Str +  
          ') as TotalQuery'	 +									   
         ' ) as RepTotal'	
         if @CodeForoshandeh <> ''
           Set @Str = @Str + ' Left Outer join (
		                                   Select K_Code From [' + @DataBase +'].Dbo.RegForoshKala
										   WHERE Sender = IsNull(' + @CodeForoshandeh + ',Sender)' + 
										   ') As T on T.k_code = RepTotal.K_Code'
         
Exec(@Str)

