CREATE PROCEDURE dbo.OnlineAvailabilityRoomNumber @ARoomnumber INT, @AStart DATETIME, @AEnd DATETIME, @AHotelnumber INT, @FinalRoomNumber INT = 0  AS
SET NOCOUNT ON
/*
    Created by Troels Buch Mar 2008

    Changes: N/A
	TB20080401:  Added Check on @FinalRoomNumber

    NOTES: 	- 

*/

DECLARE @DominoEnabled BIT

SELECT @DominoEnabled=CONVERT(SMALLINT,KeyValue) FROM HotelSetup WITH (NOLOCK) WHERE SearchKey='CentralBookingType'
  IF ISNULL(@DominoEnabled, 0) = 0
    SET @DominoEnabled = 0

IF @FinalRoomNumber <> 0
BEGIN
   IF @DominoEnabled = 0
	SELECT @ARoomnumber=RoomNumber FROM rooms WITH (NOLOCK) INNER JOIN roomtypes WITH (NOLOCK) ON Rooms.RoomTypeNumber =  roomtypes.RoomTypeNumber WHERE Number = @FinalRoomNumber
   ELSE
 	SELECT @ARoomnumber=RoomNumber FROM rooms WITH (NOLOCK) INNER JOIN roomtypes WITH (NOLOCK) ON Rooms.RoomTypeNumber =  roomtypes.RoomTypeNumber WHERE Number = @FinalRoomNumber AND Rooms.HotelNumber = @AHotelnumber
END

print @ARoomnumber

SELECT COUNT(*) AS CNT, (SELECT rooms.RoomTypeNumber FROM rooms WITH (NOLOCK) INNER JOIN roomtypes WITH (NOLOCK) ON Rooms.RoomTypeNumber =  roomtypes.RoomTypeNumber WHERE RoomNumber = @ARoomnumber) AS RoomTypeNumber 
FROM ReservationRoomLine RL WITH (NOLOCK)
INNER JOIN ReservationRoomLineXTab RLX WITH (NOLOCK) ON RL.LineNumber=RLX.LineNumber AND RoomNumber=@ARoomnumber
AND CONVERT(DATETIME,ArrivalDate,112)+CONVERT(DATETIME,ArrivalTime,108)<=@AEnd
AND CONVERT(DATETIME,DepartureDate,112)+CONVERT(DATETIME,DepartureTime,108)>=@AStart
INNER JOIN Reservations R WITH (NOLOCK) ON RLX.ReferenceNumber=R.ReferenceNumber AND RLX.GuestNumber=R.GuestNumber AND RLX.ReferenceNumber<>-2
AND R.Status<6