CREATE FUNCTION [dbo].GetConvertedCurrencyValue (@APrice FLOAT (53), @FromCurrencyRate FLOAT (53), @ToCurrencyRate FLOAT (53)) RETURNS MONEY AS
/*
  Created by Roy Nielsen october 2014.
  Used for calculating currency convertion values

  --
  
  Changes:
*/
BEGIN
  DECLARE @Res MONEY

  IF @APrice <= 0
    SET @Res = 0
  ELSE
  BEGIN
    DECLARE @NumberOfDecimals INT

    SELECT @NumberOfDecimals = KeyValue FROM HotelSetup WITH (NOLOCK) WHERE SearchKey = 'NumberOfDecimalsInPrices'
    SET @NumberOfDecimals = ISNULL(@NumberOfDecimals, 2)

	SET @Res = ((@APrice * 100.0) / @FromCurrencyRate) -- Convert back to @FromCurrency
    SET @Res = ((@Res * @FromCurrencyRate) / @ToCurrencyRate) -- Convert from @FromCurrency to @ToCurrency
    SET @Res = ((@Res * @ToCurrencyRate) / 100.0) -- Convert to local currency
  END
  RETURN @Res
END
GO


