CREATE PROCEDURE DBO.GetDiscountRate @ReferenceNumber INT, @GuestNumber SMALLINT, @AddressTypeNumber TINYINT, @DiscountTypeNumber TINYINT, @DiscountRate FLOAT OUTPUT AS
SET NOCOUNT ON

-- KJ20060117: Added PayTvDiscount (The PayTvDiscount field in ReservationAddress is added in version 3.36)
--Sidst rettet af Klaus Stenå den 9/11-2001

DECLARE @AllDiscount FLOAT
DECLARE @RoomDiscount FLOAT
DECLARE @HallDiscount FLOAT
DECLARE @MeetingDiscount FLOAT
DECLARE @PhoneDiscount FLOAT
DECLARE @PayTvDiscount FLOAT
DECLARE @ExtraDiscount FLOAT

SELECT 
  @RoomDiscount = RoomDiscountRate, 
  @HallDiscount = HallDiscountRate, 
  @MeetingDiscount = MeetingDiscountRate, 
  @PhoneDiscount = PhoneDiscountRate, 
  @AllDiscount = DiscountRate,
  @PayTvDiscount = PayTvDiscount,
  @ExtraDiscount = ExtrasDiscountRate
FROM Reservationaddress WITH (NOLOCK) 
WHERE AddressType = @AddressTypeNumber and AddressNumber IN (SELECT AddressNumber FROM ReservationAddressXTab WITH (NOLOCK) WHERE ReferenceNumber = @ReferenceNumber AND GuestNumber = @GuestNumber)

if @DiscountTypeNumber = 0
  SET @DiscountRate = @AllDiscount
ELSE IF @DiscountTypeNumber = 1
  SET @DiscountRate = @RoomDiscount
ELSE IF @DiscountTypeNumber = 2
  SET @DiscountRate = @HallDiscount
ELSE if @DiscountTypeNumber = 3
  SET @DiscountRate = @MeetingDiscount
ELSE IF @DiscountTypeNumber = 4
  SET @DiscountRate = @PhoneDiscount
ELSE IF @DiscountTypeNumber = 5
  SET @DiscountRate = @PayTvDiscount
ELSE IF @DiscountTypeNumber = 6
  SET @DiscountRate = @ExtraDiscount

IF ISNULL(@DiscountRate, 0) = 0 
  SET @DiscountRate = 0
