CREATE PROCEDURE  [dbo].[AvgPriceAnbar] (@ACode varchar(5),  @Date1 smalldatetime,@Date2 smalldatetime , @K_Code Varchar(20)) AS

 Declare @KCode  Varchar(15)
 Declare @Radif  Smallint
 Declare @Price  Money
 DEclare @IdHavaleh Bigint

 IF @K_Code = '' 
 Begin
	 Declare Cursor_Insert Cursor For
	 SELECT Havaleh.K_Code 
	 FROM headH INNER JOIN  Havaleh ON headH.Id_Havaleh =Havaleh.Id_Havaleh 
	 Where ((Havaleh.Sender =@ACode) or (Havaleh.Reciver = @ACode)) 
	 And (headH.Tarikh_faktor >=  @Date1) 
	 And (HeadH.Tarikh_Faktor <=  @Date2 )   
	 Group By Havaleh.K_Code 
	 Open Cursor_Insert
	 Fetch Next From Cursor_Insert Into @KCode
	 While @@Fetch_Status = 0 
	 Begin
	   /************************************************************************/
		Insert Into ##TempPriceAvg(K_Code, Price, Id_Havaleh, Radif)
		Select K_code,Price,Id_Havaleh,
		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 (  
			SELECT Havaleh.K_Code,Tarikh_Faktor Tarikh,HeadH.Id_Havaleh,Kind_Flag,
					  Case 
				When Kind_Flag = '1' And Havaleh.Reciver = @ACode Then Tot_Omdeh
				Else Avg_Price
					  End as Price
		  FROM headH INNER JOIN  Havaleh ON headH.Id_Havaleh =Havaleh.Id_Havaleh 
		  WHERE (Havaleh.K_Code= @KCode) 
		  And((Havaleh.Sender =@ACode) or (Havaleh.Reciver = @ACode)) 
		  And (headH.Tarikh_faktor >=  @Date1) 
		  And (HeadH.Tarikh_Faktor <=  @Date2 )   
		 group by Havaleh.K_Code,Tarikh_Faktor ,HeadH.Id_Havaleh, 
		 Tot_Omdeh, Kind_Flag, Sender ,reciver, Avg_Price
		   ) Table1  
		 Order by Tarikh ,Radif 

	   Insert Into ##AvgPrice(K_Code , Price, A_Code)
	   Select Top 1 K_code , Price , @ACode From ##TempPriceAvg
	   Order By AutoId Desc

	   Truncate Table ##TempPriceAvg
	   /************************************************************************/
	   Fetch Next From Cursor_Insert Into @KCode
	 End
	 Close Cursor_Insert
	 Deallocate Cursor_Insert
 End
 Else
 Begin
	 Declare Cursor_Insert Cursor For
	 SELECT Havaleh.K_Code 
	 FROM headH INNER JOIN  Havaleh ON headH.Id_Havaleh =Havaleh.Id_Havaleh 
	 Where ((Havaleh.Sender =@ACode) or (Havaleh.Reciver = @ACode)) 
	 And (headH.Tarikh_faktor >=  @Date1) 
	 And (HeadH.Tarikh_Faktor <=  @Date2 ) 
	 And Havaleh.K_Code = @K_Code  
	 Group By Havaleh.K_Code 
	 Open Cursor_Insert
	 Fetch Next From Cursor_Insert Into @KCode
	 While @@Fetch_Status = 0 
	 Begin
	   /************************************************************************/
		Insert Into ##TempPriceAvg(K_Code, Price, Id_Havaleh, Radif)
		Select K_code,Price,Id_Havaleh,
		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 (  
			SELECT Havaleh.K_Code,Tarikh_Faktor Tarikh,HeadH.Id_Havaleh,Kind_Flag,
					  Case 
				When Kind_Flag = '1' And Havaleh.Reciver = @ACode Then Tot_Omdeh
				Else Avg_Price
					  End as Price
		  FROM headH INNER JOIN  Havaleh ON headH.Id_Havaleh =Havaleh.Id_Havaleh 
		  WHERE (Havaleh.K_Code= @KCode) 
		  And((Havaleh.Sender =@ACode) or (Havaleh.Reciver = @ACode)) 
		  And (headH.Tarikh_faktor >=  @Date1) 
		  And (HeadH.Tarikh_Faktor <=  @Date2 )   
		 group by Havaleh.K_Code,Tarikh_Faktor ,HeadH.Id_Havaleh, 
		 Tot_Omdeh, Kind_Flag, Sender ,reciver, Avg_Price
		   ) Table1  
		 Order by Tarikh ,Radif 

	   Insert Into ##AvgPrice(K_Code , Price, A_Code)
	   Select Top 1 K_code , Price , @ACode From ##TempPriceAvg
	   Order By AutoId Desc

	   Truncate Table ##TempPriceAvg
	   /************************************************************************/
	   Fetch Next From Cursor_Insert Into @KCode
	 End
	 Close Cursor_Insert
	 Deallocate Cursor_Insert
 End