Create PROCEDURE [dbo].[AddOverUnderTmpFactor] @IdHavaleh BIGINT , @ActionCode SMALLINT 
AS 

DECLARE @GuyCode VARCHAR(5)
SELECT @GuyCode = 
CASE
  WHEN LEN(Sender) = 5 THEN Sender
  ELSE Reciver
END FROM dbo.TmpHavaleh WHERE Id_Havaleh = @IdHavaleh

DELETE FROM dbo.Tmp_Over_Under_Factor WHERE  Id_Havaleh=@IdHavaleh

 INSERT INTO dbo.Tmp_Over_Under_Factor
 (
     Id_Havaleh,
     Name,
     Code_Over_Under,
     Type,
     Act_Code,
     Price_Row,
     Max_Price,
     Max_percent,
	 Guy_Code,
     ShowPrice
 )
 SELECT @IdHavaleh, Name, AutoId, type,Act_Code,Case
When (Formula is Null Or Formula = '') Then 0   
    Else                                                       
    Formula                                                
End as Price_Row  ,Max_Price,Max_Percent,@GuyCode,ShowPrice 

FROM dbo.Over_Under 
WHERE 
Action_Code = @ActionCode AND 
Active = 1 AND CAST(GETDATE() AS DATE) BETWEEN AzTarikh AND TaTarikh
ORDER BY SortDisplay

