CREATE PROCEDURE [dbo].[OnlineAvailableHallPax] @AHallTypeNumber INT
	,@AStartDateTime DATETIME
	,@APax INT
	,@AMinutesBeforeScheduleIsClosing INT = 0
	,@minutesToEarliestBooking INT = 0
	,@FixedStartTimes VarChar(MAX) = '' --Optional. Fixed starttimes separated with comma (Eg.: '10:00,10:10,10:35,11:00;11:30,12:35')
     ,@langInt int = 0                   --Optional. If 0 or translation not found, get name from ConferenceRooms
AS
-- Calls: GetClimbingSchedulePaxInfo
--        GetSchedulerExpressDataWithPAX2
-- 2014-04-10/SG: Based on / replaces OnlineAvailableWares. Used in GoHigh, when HotelSetup.HallClimbingUseClimbingAvailability is set

-- 20170928/SG: Use translated name
-- 20160718/SG: Optional param. @FixedStartTimes overrules the calculation of starttimes
-- 20150105/SG: Use (new) Hall.OnlineBookingTimeInterval as BookingTimeInterval when pr. hall value is needed

BEGIN
	DECLARE @HallNumber INT;
	DECLARE @HallName VARCHAR(50);
	DECLARE @TimeInterval INT;
	DECLARE @TimeIntervalFromHall INT;
	DECLARE @BookingInterval INT;
	DECLARE @SchedulerFound BIT;
	DECLARE @TempPax INT;
	DECLARE @OpeningDateTime DATETIME;
	DECLARE @CloseDateTime DATETIME;
	DECLARE @TempPax2 INT;
	DECLARE @FirstBookingTime DATETIME;
	DECLARE @TripDuration INT --minutes
	
	-- Only one hall per HallType is allowed
	SELECT @HallNumber = ConferenceRoomNumber, @HallName = IsNull(t.Name, h.Name), @TimeIntervalFromHall = OnlineBookingTimeInterval
	  FROM ConferenceRooms h With (NOLOCK)
       left outer join Translation t With (Nolock) on h.ConferenceRoomNumber = t.ObjectNumber and t.Type=4 and t.CountryNumber = @langInt
	  WHERE AllowFilterPicassoOnline = 1 AND ConferenceRoomTypeNumber = @AHallTypeNumber;
	  --AND cr.Istable is NULL ?? (input parameter?)

	SELECT @TripDuration = KeyValue FROM HotelSetup(NOLOCK) WHERE SearchKey = 'hallClimbingAvailabilityCheckInMinutes'; --Replaces parameter 'TripDuration';
     SET @TripDuration = ISNULL(@TripDuration, 210); --Default 210 min
    
	CREATE TABLE #TempAvailabilityTable (
		AvailablePax INT
		,ArrivalDateTime DATETIME
		,DepartureDateTime DATETIME
		,HallName VARCHAR(50)
		,HallNumber INT
		);
     --Holds all possible starttimes comming from parameter or calculated
	CREATE TABLE #TempStartTimes (
		ArrivalDateTime DateTime
		);


	 -- 1: Find åbningstid i scheduler
	EXEC dbo.GetSchedulerExpressDataWithPAX2 
	   @AStartDateTime
	   ,@AHallTypeNumber
	   ,@SchedulerFound OUTPUT
	   ,@TempPax OUTPUT
	   ,@OpeningDateTime OUTPUT
	   ,@CloseDateTime OUTPUT
	   ,@TempPax2 OUTPUT;
		
	IF @SchedulerFound = 1
	BEGIN
       IF @FixedStartTimes <> ''
	  BEGIN
	    CREATE TABLE #TempStartTimesStr (
		  ArrivalDateTimeStr VarChar(30)
		  );
	    --tilføje bet. for OpeningDateTime og CloseDateTime
	    INSERT INTO #TempStartTimesStr (ArrivalDateTimeStr) SELECT * FROM SplitListMax(@FixedStartTimes)
	    Declare @StartDate VarChar(20)
	    SET @StartDate = Convert(VarChar(20), @AStartDateTime, 112); --yyyymmdd
	    
	    INSERT INTO #TempStartTimes (ArrivalDateTime) 
	      Select Convert(DateTime, @StartDate + ' ' + ArrivalDateTimeStr) 
		 FROM #TempStartTimesStr
		 WHERE Convert(DateTime, @StartDate + ' ' + ArrivalDateTimeStr) BETWEEN @OpeningDateTime AND @CloseDateTime;
	    
	    DROP TABLE #TempStartTimesStr;
	  END ELSE
	  BEGIN --standard intervaller
	    IF ISNULL(@TimeIntervalFromHall, 0) = 0
	    BEGIN
		 SELECT @TimeInterval = KeyValue FROM HotelSetup WITH (NOLOCK) WHERE SearchKey = 'OnlineBookingTimeInterval' 
		 IF ISNULL(@TimeInterval, 0) = 0
		 BEGIN
		   SET @TimeInterval = 30; --default
		 END
	    END ELSE
	    BEGIN
		 SET @TimeInterval = @TimeIntervalFromHall
	    END   
	
	    -- 1: Find åbningstid i scheduler (ovenfor)
	    -- 2: Korriger for @AMinutesBeforeScheduleIsClosing og @minutesToEarliestBooking
	    -- 3: Loop med @TimeInterval
	    --         Indsæt i #TempStartTimes
	 
	    print '@TempPax, @OpeningDateTime, @CloseDateTime, @TempPax2: ' + convert(char(2),@TempPax) + ', ' + convert(char(25),@OpeningDateTime) + ', ' + convert(char(25),@CloseDateTime) + ', ' + convert(char(2),@TempPax2);
	    -- 2: Korriger for @AMinutesBeforeScheduleIsClosing og @minutesToEarliestBooking
	    -- The starting time must be incremented with @TimeInterval until @minutesToEarliestBooking is fullfilled in order to get the correct time.
	    SET @FirstBookingTime = DateAdd(minute, @minutesToEarliestBooking, GetDate());

	    DECLARE @TempOpeningTime DATETIME;
	    DECLARE @TempClosingTime DATETIME;
        
	    SET @TempOpeningTime = @OpeningDateTime;

	    WHILE @TempOpeningTime < @FirstBookingTime
	    BEGIN
		 SET @TempOpeningTime = DATEADD(minute, @TimeInterval, @TempOpeningTime);
	    END;
	    SET @OpeningDateTime = @TempOpeningTime;
	    SET @TempClosingTime = DATEADD(minute, - 1 * @AMinutesBeforeScheduleIsClosing, @CloseDateTime);

	    -- 3: Loop med @TimeInterval  
	    DECLARE @TempDateTime DATETIME;
	    DECLARE @AvailableConcurrentPax INT;
	    DECLARE @DummyPax1 INT;
	    DECLARE @DummyPax2 INT;
	    DECLARE @AvailableTeamPax INT;

	    SET @TempDateTime = @OpeningDateTime;

	    WHILE @TempDateTime <= @TempClosingTime
	    BEGIN
		 INSERT INTO #TempStartTimes (ArrivalDateTime) Values (@TempDateTime);
		 SET @TempDateTime = DATEADD(minute, @TimeInterval, @TempDateTime);
	    END; --WHILE
       END; --@FixedStartTimes = ''
	END; --IF @SchedulerFound = 1 

	--Common
	    --         Gennemløbe #TempStartTimes
	    --         GetClimbingSchedulePaxInfo(@TimeInterval)
	    --         GetClimbingSchedulePaxInfo(@TripDuration)
	    --         Hvis MIN(@AvailableTeamPax, @AvailableConcurrentPax)
	    --            Tilføje tid til temp tabel
	    -- 4: SELECT temp tabel
     SELECT @BookingInterval = KeyValue FROM HotelSetup(NOLOCK) WHERE SearchKey = 'HallClimbingBookingIntervalInMinutes'; --”Booking interval in minutes” angiver en periode for ”samtidig” start. Det er altså indenfor denne periode, der checkes i forhold til Pax2.
	SET @TempDateTime = cast('1753-1-1' as datetime); --MinDateTime
	WHILE (@TempDateTime IS Not NULL)
	BEGIN
	  SET @TempDateTime = (SELECT TOP 1 ArrivalDateTime FROM #TempStartTimes WHERE ArrivalDateTime > @TempDateTime ORDER BY ArrivalDateTime)
	  IF @TempDateTime IS Not NULL
	  BEGIN
		
		  DECLARE @TempAvailPax INT;
		  DECLARE @DepartureDateTime DATETIME;

            EXEC dbo.GetClimbingSchedulePaxInfo
                @HallNumber
               ,0                         --@AHallLineID ??
               ,@TempDateTime             --@AStartDateTime
               ,@TripDuration            
               ,@AvailableConcurrentPax OUTPUT
               ,@DummyPax1 OUTPUT;

            EXEC dbo.GetClimbingSchedulePaxInfo
                @HallNumber
               ,0                         --@AHallLineID ??
               ,@TempDateTime             --@AStartDateTime
               ,@BookingInterval
               ,@DummyPax2 OUTPUT
               ,@AvailableTeamPax OUTPUT;

--PRINT '@TempDateTime: ' + CAST(@TempDateTime as char(20))
--PRINT '@AvailableConcurrentPax: ' + CAST(@AvailableConcurrentPax as char(2)) + ' @AvailableTeamPax: ' + CAST(@AvailableTeamPax as Char(2))

		  --Both values > Pax?
		  IF @AvailableConcurrentPax >= @APax AND @AvailableTeamPax >= @APax 
		  BEGIN
			 SET @DepartureDateTime = DATEADD(minute, @TripDuration, @TempDateTime);

			 INSERT INTO #TempAvailabilityTable (
				    AvailablePax
				    ,ArrivalDateTime
				    ,DepartureDateTime
				    ,HallName
				    ,HallNumber
				    )
			 VALUES (
				    0
				    ,@TempDateTime
				    ,@DepartureDateTime
				    ,@HallName
				    ,--Same name and number for all records
				    @HallNumber
				    );
		  END;
        END; --If @TempDateTime IS Not NULL
			
    END; --While
    
    DROP TABLE #TempStartTimes;
    -- 4: SELECT temp tabel
	SELECT *
	FROM #TempAvailabilityTable WITH (NOLOCK)

	DROP TABLE #TempAvailabilityTable
END;
