CREATE PROCEDURE [dbo].[GA_FillTempTables] @SpecifyOnRoomTypeSegPos BIT, @FormatedStartDate CHAR(8), @FormatedEndDate CHAR(8), @RealSystemDateStr CHAR(8), @TheHotelNumber INT AS
SET NOCOUNT ON

/* Setup Variables */
DECLARE
@HotelNumber INT,
@LineNumber INT,
@BookedTypeNumber INT,
@RoomNumberID INT,
@NumberOfRooms SMALLINT,
@SpecArrivalDate CHAR(8),
@SpecDepartureDate CHAR(8),
@ArrivalDate CHAR(8),
@DepartureDate CHAR(8),
@Adults SMALLINT,
@Children1 SMALLINT,
@Children2 SMALLINT,
@SpecAdults SMALLINT,
@SpecChildren1 SMALLINT,
@SpecChildren2 SMALLINT,
@ReferenceNumber INT,
@GuestNumber INT,
@Status TINYINT,
@LinkedRes BIT,
@ArrivalTime CHAR(5),
@DepartureTime CHAR(5),
@SpecArrivalTime CHAR(5),
@SpecDepartureTime CHAR(5),
@SegmentNumber INT,
@PointOfSaleNumber INT,
@SoldPrice MONEY,

@OldLineNumber INT,
@AllocatedTypeNumber INT,
@NumberOfConfirmed SMALLINT,
@NumberOfProvisionals SMALLINT,
@NumberOfGuaranteed SMALLINT,
@NumberOfWaitinglists SMALLINT,
@NumberOfTentative SMALLINT,
@NumberOfDayRooms SMALLINT,
@NumberOfOutOfOrder SMALLINT,
@AllotNumberOfRooms SMALLINT,
@AllotAdults SMALLINT,
@AllotChildren1 SMALLINT,
@AllotChildren2 SMALLINT,
@AllotmentNumber INT,
@ReservationType TINYINT,
@CurrentDate DATETIME,
@CurrentDateStr CHAR(8),
@OriginalArrDate CHAR(8),
@OriginalDepDate CHAR(8),
@StoreAllotmentNumber INT,
@StoreNumberOfRooms SMALLINT,
@StoreNumberOfDayRooms SMALLINT,
@PaxMulitiplyRooms SMALLINT,
@IsDayRoom BIT,
@SingleSegmentNumber INT,
@SinglePointOfSaleNumber INT

/* Fill Variables */
DECLARE @DayroomsLaterThanTime CHAR(5)
SELECT @DayroomsLaterThanTime=KeyValue FROM HotelSetup WITH (NOLOCK) WHERE Upper (SearchKey)=Upper('DayroomsLaterThanTime')
IF @DayroomsLaterThanTime = NULL
  SET @DayroomsLaterThanTime = '23:59' -- default

DECLARE @IncludeTentative BIT
SELECT @IncludeTentative=KeyValue FROM HotelSetup WITH (NOLOCK) WHERE Upper (SearchKey)=Upper('IncludeTentative')
IF IsNull (@IncludeTentative, 0) = 0
  SET @IncludeTentative = 0

DECLARE @IncludeDayroomsInSoldRooms BIT
SELECT @IncludeDayroomsInSoldRooms=KeyValue FROM HotelSetup WITH (NOLOCK) WHERE Upper (SearchKey)=Upper('IncludeDayroomsInSoldRooms')
IF IsNull (@IncludeDayroomsInSoldRooms, 0) = 0
  SET @IncludeDayroomsInSoldRooms = 0

SELECT TOP 1 @SingleSegmentNumber = Number FROM Segments WITH (NOLOCK) WHERE Number>0 and (MarkAsDeleted='0') OR (MarkAsDeleted is null)  -- Get any SEG to put it on. Only used when checking for overbook
SELECT TOP 1 @SinglePointOfSaleNumber = Number FROM PointOfSales WITH (NOLOCK) WHERE Number>0 and (MarkAsDeleted='0') OR (MarkAsDeleted is null) -- Get any POS to put it on. Only used when checking for overbook

/* Select, Loop and Update/Insert */

IF (@TheHotelNumber = 0)
BEGIN
  DECLARE #RoomLineCur CURSOR LOCAL FAST_FORWARD FOR
  SELECT HotelNumber, LineNumber, SpecBookedTypeNumber, SpecRoomNumberID, SpecNumberOfRooms, SpecArrivalDate, SpecDepartureDate, ArrivalDate, DepartureDate, Adults, Children1, Children2, SpecAdults, SpecChildren1, SpecChildren2, ReferenceNumber, GuestNumber, Status, LinkedRes,
                 ArrivalTime, DepartureTime, SpecArrivalTime, SpecDepartureTime, MarketSegment, PointOfSale, Price
  FROM TGenerateAvailabilityView WITH (NOLOCK)
  WHERE (SpecArrivalDate <= @FormatedEndDate AND SpecDepartureDate >= @FormatedStartDate AND ReferenceNumber>0
                 and (Status < 7 or Status is null) AND (HotelNumber=0 or HotelNumber is null))
                 or (ReferenceNumber = 0 and SpecArrivalDate <= @FormatedEndDate AND SpecDepartureDate >= @FormatedStartDate AND SpecArrivalDate<>SpecDepartureDate) -- outoforder. same res for all hotels!
  ORDER BY LineNumber, DepartureDate DESC, DepartureTime DESC
END
ELSE if @TheHotelNumber = -1
BEGIN
  DECLARE #RoomLineCur CURSOR LOCAL FAST_FORWARD FOR
  SELECT HotelNumber, LINENUMBER, SpecBookedTypeNumber, SpecRoomNumberID, SpecNumberOfRooms, SpecArrivalDate, SpecDepartureDate, ArrivalDate, DepartureDate, Adults, Children1, Children2, SpecAdults, SpecChildren1, SpecChildren2, ReferenceNumber, GuestNumber, Status, LinkedRes,
                 ArrivalTime, DepartureTime, SpecArrivalTime, SpecDepartureTime, MarketSegment, PointOfSale, Price
  FROM TGenerateAvailabilityView WITH (NOLOCK)
  WHERE (SpecArrivalDate <= @FormatedEndDate AND SpecDepartureDate >= @FormatedStartDate AND ReferenceNumber>0
                 and (Status < 7 or Status is null))
                 or (ReferenceNumber = 0 and SpecArrivalDate <= @FormatedEndDate AND SpecDepartureDate >= @FormatedStartDate AND SpecArrivalDate<>SpecDepartureDate) -- outoforder. same res for all hotels!
  ORDER BY LineNumber, DepartureDate DESC, DepartureTime DESC
END
ELSE
BEGIN
  DECLARE #RoomLineCur CURSOR LOCAL FAST_FORWARD FOR
  SELECT HotelNumber, LINENUMBER, SpecBookedTypeNumber, SpecRoomNumberID, SpecNumberOfRooms, SpecArrivalDate, SpecDepartureDate, ArrivalDate, DepartureDate, Adults, Children1, Children2, SpecAdults, SpecChildren1, SpecChildren2, ReferenceNumber, GuestNumber, Status, LinkedRes,
                 ArrivalTime, DepartureTime, SpecArrivalTime, SpecDepartureTime, MarketSegment, PointOfSale, Price
  FROM TGenerateAvailabilityView WITH (NOLOCK)
  WHERE (SpecArrivalDate <= @FormatedEndDate AND SpecDepartureDate >= @FormatedStartDate AND ReferenceNumber>0
                 and (Status < 7 or Status is null)  AND (HotelNumber=@TheHotelNumber OR HotelNumber IS NULL))
                 or (ReferenceNumber = 0 and SpecArrivalDate <= @FormatedEndDate AND SpecDepartureDate >= @FormatedStartDate AND SpecArrivalDate<>SpecDepartureDate) -- outoforder. same res for all hotels!
  ORDER BY LineNumber, DepartureDate DESC, DepartureTime DESC
END

OPEN #RoomLineCur

FETCH NEXT FROM #RoomLineCur
INTO @HotelNumber, @LINENUMBER, @BookedTypeNumber, @RoomNumberID, @NumberOfRooms, @SpecArrivalDate, @SpecDepartureDate, @ArrivalDate, @DepartureDate, @Adults, @Children1, @Children2,
@SpecAdults, @SpecChildren1, @SpecChildren2, @ReferenceNumber, @GuestNumber, @Status, @LinkedRes, @ArrivalTime, @DepartureTime, @SpecArrivalTime, @SpecDepartureTime, @SegmentNumber, @PointOfSaleNumber, @SoldPrice

SET @OldLineNumber = 0
--SET @RoomArrived = 0

WHILE  (@@FETCH_STATUS <> -1)
BEGIN
  IF (@ReferenceNumber = 0) -- read hotelnumber from room/roomtype
  BEGIN
    IF @RoomNumberID > 0
      SELECT @HotelNumber = HotelNumber FROM Rooms WITH (NOLOCK) WHERE RoomNumber = @RoomNumberID
    ELSE
      SELECT @HotelNumber = HotelNumber FROM RoomTypes WITH (NOLOCK) WHERE RoomTypeNumber = @BookedTypeNumber
  END

  IF IsNull (@HotelNumber, 0) = 0
    SET @HotelNumber = 0
  IF IsNull (@Adults, 0) = 0
    SET @Adults = 0
  IF IsNull (@Children1, 0) = 0
    SET @Children1 = 0
  IF IsNull (@Children2, 0) = 0
    SET @Children2= 0
  SET @IsDayroom = 0

  IF @Status < 7  --  rsCancelled
  BEGIN
    SET @OriginalArrDate = ''
    SET @OriginalDepDate = ''
    IF @GuestNumber = 0
    BEGIN
      SET @ArrivalDate = @SpecArrivalDate
      SET @DepartureDate = @SpecDepartureDate
      SET @ArrivalTime = @SpecArrivalTime
      SET @DepartureTime = @SpecDepartureTime
      SET @Adults = @SpecAdults
      SET @Children1 = @SpecChildren1
      SET @Children2 = @SpecChildren2

      IF @ReferenceNumber = 0 and @ArrivalDate=@DepartureDate  -- compare dates for backward compatibility. it would be better to test for 23:59 because of future compatibilty but nothing to do about that now :(
      BEGIN
        SET @DepartureTime = '00:00'
        SET @DepartureDate = CONVERT (CHAR (8), CONVERT (DATETIME, @DepartureDate)+1, 112)
      END
    END
    ELSE IF @LinkedRes > 1
    BEGIN
      IF @OldLineNumber = @LineNumber
        SET @NumberOfRooms = 0
      ELSE
      BEGIN
        SET @OriginalArrDate = @ArrivalDate
        SET @OriginalDepDate = @DepartureDate
        SET @ArrivalDate = @SpecArrivalDate
        SET @DepartureDate = @SpecDepartureDate  -- THERE IS ONE PROBLEM WITH THIS! If there is a hole in the stay we cannot compare against inhouse lists. Example guest 1 from 1st to 2nd in the month and guest 2 from 3rd to 4th in the month. There is no one in the room the 2nd day but it is still counted! KA has said OK for this
      END
    END

    IF @RoomNumberID > 0
      SELECT @AllocatedTypeNumber = RoomTypeNumber FROM Rooms WITH (NOLOCK) WHERE RoomNumber = @RoomNumberID
    ELSE
      SET @AllocatedTypeNumber = @BookedTypeNumber

    SET @NumberOfConfirmed = 0
    SET @NumberOfProvisionals = 0
    SET @NumberOfGuaranteed = 0
    SET @NumberOfWaitinglists = 0
    SET @NumberOfTentative = 0

    IF (@ReferenceNumber > 0)
    BEGIN
      SELECT @AllotmentNumber=AllotmentNumber, @ReservationType=ReservationType FROM ReservationMain WITH (NOLOCK) WHERE ReferenceNumber=@ReferenceNumber
      IF IsNull (@AllotmentNumber, 0) = 0
        SET @AllotmentNumber = 0

      IF (@Status = 0)  -- rsConfirmed
        SET @NumberOfConfirmed = @NumberOfRooms
      ELSE IF (@Status = 1)  -- rsProvisional
        SET @NumberOfProvisionals = @NumberOfRooms
      ELSE IF (@Status = 2)  -- rsGuaranteed
        SET @NumberOfGuaranteed = @NumberOfRooms
      ELSE IF (@Status = 3)  -- rsWaitinglist
        SET @NumberOfWaitinglists = @NumberOfRooms
      ELSE IF (@Status = 4)  -- rsTentative
        SET @NumberOfTentative = @NumberOfRooms

      IF (@Status = 3) OR (@Status = 4 AND @IncludeTentative = 0) -- rsWaitinglist or rsTentative
      BEGIN
        SET @NumberOfRooms = 0  -- Reservation wont be counted
        SET @Adults = 0
        SET @Children1 = 0
        SET @Children2 = 0
      END
    END  -- IF @ReferenceNumber > 0
    ELSE
      SET @AllotmentNumber=0

    IF @ArrivalDate < @FormatedStartDate
      SET @CurrentDateStr = @FormatedStartDate
    ELSE
      SET @CurrentDateStr = @ArrivalDate
    SET @CurrentDate = Convert (DATETIME, @CurrentDateStr)

    SET @StoreAllotmentNumber = @AllotmentNumber  -- Makes sure that reservations more than one day is divided correctly!
    SET @StoreNumberOfRooms = @NumberOfRooms

--- W H I L E ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

    WHILE (@AllocatedTypeNumber > 0 and @CurrentDateStr <= @FormatedEndDate) and ((@ArrivalDate = @DepartureDate and @CurrentDateStr = @DepartureDate) or (@ArrivalDate <> @DepartureDate and @CurrentDateStr = @DepartureDate and (@DepartureTime > @DayroomsLaterThanTime and @DayroomsLaterThanTime<'23:59')) or (@CurrentDateStr < @DepartureDate)) -- fix for dayroom problem regarding departuredate
    BEGIN
      IF @AllotmentNumber = 0
      BEGIN
        SET @PaxMulitiplyRooms = @NumberOfRooms
        IF (@OriginalDepDate <> '') AND ((@CurrentDateStr >= @OriginalDepDate) OR (@CurrentDateStr < @OriginalArrDate)) and (@IsDayRoom = 0)
          SET @PaxMulitiplyRooms = 0
        ELSE IF @PaxMulitiplyRooms = 0
          SET @PaxMulitiplyRooms = 1
      END
      ELSE
      BEGIN
        SET @PaxMulitiplyRooms = @StoreNumberOfRooms  -- section moved up from below. numberofrooms made multiplypax wrong since it was 0
        IF (@OriginalDepDate <> '') AND ((@CurrentDateStr >= @OriginalDepDate) OR (@CurrentDateStr < @OriginalArrDate)) and (@IsDayRoom = 0)
          SET @PaxMulitiplyRooms = 0
        ELSE IF @PaxMulitiplyRooms = 0
          SET @PaxMulitiplyRooms = 1
      END

      IF @CurrentDateStr >= @FormatedStartDate
      BEGIN
        IF @AllotmentNumber = 0  -- No allotmentnumber, have to insert the number of rooms on any given allotment. This doesnt affect the number
        BEGIN
          SET @AllotmentNumber = -1
          SET @AllotNumberOfRooms = 0
          SET @AllotAdults = 0
          SET @AllotChildren1 = 0
          SET @AllotChildren2 = 0
          SET @NumberOfRooms = @StoreNumberOfRooms
          -- Only update TotalRoomsBooked in AllotmentLines
        END
        ELSE
        BEGIN
          SET @AllotNumberOfRooms = @StoreNumberOfRooms  -- We have a res against allotment. Only subtract from allotment
          SET @NumberOfRooms = 0  -- and not from availability

          IF @AllotNumberOfRooms = 0
          BEGIN
            SET @AllotAdults = @Adults
            SET @AllotChildren1 = @Children1
            SET @AllotChildren2 = @Children2
          END
          BEGIN
            SET @AllotAdults = @Adults*@AllotNumberOfRooms			
            SET @AllotChildren1 = @Children1*@AllotNumberOfRooms			
            SET @AllotChildren2 = @Children2*@AllotNumberOfRooms			
          END
        END

        IF 0 = 0
        BEGIN
          IF IsNull (@SegmentNumber, 0) = 0
            SET @SegmentNumber = @SingleSegmentNumber
          IF IsNull (@PointOfSaleNumber, 0) = 0
            SET @PointOfSaleNumber = @SinglePointOfSaleNumber

          SET @NumberOfOutOfOrder = 0
          IF @ReferenceNumber=0 
          BEGIN
            IF @CurrentDateStr<@DepartureDate
              SET @NumberOfOutOfOrder = @NumberOfRooms
            SET @NumberOfRooms = 0
            SET @NumberOfDayRooms = 0
          END

          SET @StoreNumberOfDayRooms = @NumberOfDayRooms
          IF @NumberOfDayrooms > 0 AND @ArrivalDate <> @DepartureDate AND @CurrentDateStr < @DepartureDate
            SET @NumberOfDayRooms = 0

          IF @NumberOfDayrooms > 0 AND @AllotNumberOfRooms > 0 AND @ArrivalDate <> @DepartureDate AND @CurrentDateStr = @DepartureDate  -- JP 20040331 fix for allotment on depdate
          BEGIN
            IF @NumberOfRooms = 0
              SET @NumberOfRooms = @AllotNumberOfRooms
            SET @AllotNumberOfRooms = 0
          END

          IF (((@SpecArrivalDate = @SpecDepartureDate) or (@SpecArrivalDate <> @SpecDepartureDate AND @SpecDepartureDate = @CurrentDateStr))) and (@ReferenceNumber>0)
          BEGIN
            SET @IsDayroom = 1
            SET @NumberOfDayrooms = @NumberOfRooms + @AllotNumberOfRooms

            IF (@IncludeDayroomsInSoldRooms = 0) or (@CurrentDateStr >= @RealSystemDateStr) -- DO NOT COUNT DayRooms
            BEGIN
              SET @PaxMulitiplyRooms = 0
              SET @NumberOfRooms = 0
              SET @AllotNumberOfRooms = 0
            END
          END

          IF (@ReferenceNumber > 0) AND (@AllotNumberOfRooms > 0) AND (@CurrentDateStr >=  CONVERT (CHAR (8), @ArrivalDate)) and (@CurrentDateStr < CONVERT (CHAR (8), @DepartureDate))
          BEGIN
            UPDATE #AllotmentLines SET
              CurrentCount = CurrentCount + @AllotNumberOfRooms  -- Number of reservations against this allotment
            WHERE RoomTypeNumber = @AllocatedTypeNumber AND DateStamp=@CurrentDateStr and AllotmentNumber = @AllotmentNumber OPTION (KEEP PLAN)
            IF @@rowcount = 0
              INSERT INTO #AllotmentLines (AllotmentNumber,RoomTypeNumber,DateStamp,CurrentCount)
              VALUES (@AllotmentNumber, @AllocatedTypeNumber, @CurrentDateStr, @AllotNumberOfRooms)
          END

          IF @SpecifyOnRoomTypeSegPos = 1
          BEGIN
--if @tint = 0
--  print  'RS_START: '+convert(varchar, getdate (),121)
            UPDATE #RoomBookingStatus SET
              SoldRooms = SoldRooms+@NumberOfRooms,  -- sold not on allotment
              SoldPrice = SoldPrice+@SoldPrice,
              Adults = Adults+@Adults*@PaxMulitiplyRooms,
              Children1= Children1+@Children1*@PaxMulitiplyRooms,
              Children2 = Children2+@Children2*@PaxMulitiplyRooms, 
              NumberOfConfirmed = NumberOfConfirmed+@NumberOfConfirmed,
              NumberOfProvisionals = NumberOfProvisionals+@NumberOfProvisionals,
              NumberOfGuaranteed = NumberOfGuaranteed+@NumberOfGuaranteed,
              NumberOfTentative = NumberOfTentative+@NumberOfTentative, 
              NumberOfWaitinglists = NumberOfWaitinglists+@NumberOfWaitinglists,
              NumberOfDayrooms = NumberOfDayrooms + @NumberOfDayrooms,
              NumberOfOutOfOrder = NumberOfOutOfOrder + @NumberOfOutOfOrder,
              PaxAllotment = PaxAllotment + (@AllotAdults+@AllotChildren1+@AllotChildren2) /** @PaxMulitiplyRooms*/
            WHERE RoomTypeNumber = @AllocatedTypeNumber AND DateStamp=@CurrentDateStr and SegmentNumber=@SegmentNumber and PointOfSaleNumber = @PointOfSaleNumber and HotelNumber = @HotelNumber OPTION (KEEP PLAN)

            IF @@rowcount = 0 -- Nothing was updated -> insert row!
              INSERT INTO #RoomBookingStatus (DateStamp,RoomTypeNumber,SegmentNumber,PointOfSaleNumber,HotelNumber,SoldRooms,Adults,Children1,Children2,NumberOfConfirmed,NumberOfProvisionals,NumberOfGuaranteed,NumberOfTentative,NumberOfWaitinglists,NumberOfDayrooms,FreeRoomsAllotment,TotalRoomsAllotment,PaxAllotment,NumberOfRoomsSoldToday, NumberOfOutOfOrder, SoldPrice/*, NumberOfOutOfHouseDayRooms*/)
              VALUES (@CurrentDateStr, @AllocatedTypeNumber, @SegmentNumber, @PointOfSaleNumber, @HotelNumber, @NumberOfRooms, @Adults*@PaxMulitiplyRooms, @Children1*@PaxMulitiplyRooms, @Children2*@PaxMulitiplyRooms, @NumberOfConfirmed, @NumberOfProvisionals, @NumberOfGuaranteed, @NumberOfTentative, @NumberOfWaitinglists, @NumberOfDayrooms, 0, 0, (@AllotAdults+@AllotChildren1+@AllotChildren2) /** @PaxMulitiplyRooms*/, 0, @NumberOfOutOfOrder, @SoldPrice/*, @NumberOfOutOfHouseDayRooms*/)

--if @tint = 0
--  print  'RS_END  : '+convert(varchar, getdate (),121)
          END
          ELSE -- NOT @SpecifyOnRoomTypeSegPos = 1
          BEGIN
            UPDATE #RoomBookingStatus2 SET
              SoldRooms = SoldRooms+@NumberOfRooms,  -- sold not on allotment
              SoldPrice = SoldPrice+@SoldPrice,
              Adults = Adults+@Adults*@PaxMulitiplyRooms,
              Children1= Children1+@Children1*@PaxMulitiplyRooms,
              Children2 = Children2+@Children2*@PaxMulitiplyRooms, 
              NumberOfConfirmed = NumberOfConfirmed+@NumberOfConfirmed,
              NumberOfProvisionals = NumberOfProvisionals+@NumberOfProvisionals,
              NumberOfGuaranteed = NumberOfGuaranteed+@NumberOfGuaranteed,
              NumberOfTentative = NumberOfTentative+@NumberOfTentative, 
              NumberOfWaitinglists = NumberOfWaitinglists+@NumberOfWaitinglists,
              NumberOfDayrooms = NumberOfDayrooms + @NumberOfDayrooms,
              NumberOfOutOfOrder = NumberOfOutOfOrder + @NumberOfOutOfOrder,
              PaxAllotment = PaxAllotment + (@AllotAdults+@AllotChildren1+@AllotChildren2) /** @PaxMulitiplyRooms*/
            WHERE RoomTypeNumber = @AllocatedTypeNumber AND DateStamp=@CurrentDateStr and HotelNumber = @HotelNumber
            IF @@rowcount = 0 -- Nothing was updated -> insert row!
              INSERT INTO #RoomBookingStatus2 (DateStamp,RoomTypeNumber,HotelNumber,SoldRooms,Adults,Children1,Children2,NumberOfConfirmed,NumberOfProvisionals,NumberOfGuaranteed,NumberOfTentative,NumberOfWaitinglists,NumberOfDayrooms,FreeRoomsAllotment,TotalRoomsAllotment,NumberOfRoomsSoldToday, NumberOfOutOfOrder/*, NumberOfOutOfHouseDayRooms*/, PaxAllotment, SoldPrice)
              VALUES (@CurrentDateStr, @AllocatedTypeNumber, @HotelNumber, @NumberOfRooms, @Adults*@PaxMulitiplyRooms, @Children1*@PaxMulitiplyRooms, @Children2*@PaxMulitiplyRooms, @NumberOfConfirmed, @NumberOfProvisionals, @NumberOfGuaranteed, @NumberOfTentative, @NumberOfWaitinglists, @NumberOfDayrooms, 0, 0, 0, @NumberOfOutOfOrder/*, @NumberOfOutOfHouseDayRooms*/ , (@AllotAdults+@AllotChildren1+@AllotChildren2), @SoldPrice /** @PaxMulitiplyRooms*/  )
          END
          SET @NumberOfDayRooms = @StoreNumberOfDayRooms  -- needed because we read from arrival to departure and not the other way
        END
      END -- end if
      SET @AllotmentNumber = @StoreAllotmentNumber -- Makes sure that reservations with more than one day is divided correctly!
      SET @CurrentDate = @CurrentDate +1
      SET @CurrentDateStr = Convert (Char (8), @CurrentDate, 112)
    END  -- End While
  END
  SET @OldLineNumber = @LineNumber 

  FETCH NEXT FROM #RoomLineCur
  INTO @HotelNumber, @LINENUMBER, @BookedTypeNumber, @RoomNumberID, @NumberOfRooms, @SpecArrivalDate, @SpecDepartureDate, @ArrivalDate, @DepartureDate, @Adults, @Children1, @Children2,
  @SpecAdults, @SpecChildren1, @SpecChildren2, @ReferenceNumber, @GuestNumber, @Status, @LinkedRes, @ArrivalTime, @DepartureTime, @SpecArrivalTime, @SpecDepartureTime, @SegmentNumber, @PointOfSaleNumber, @SoldPrice
--    set @tint =  @tint + 1
END -- END WHILE

CLOSE #RoomLineCur
DEALLOCATE #RoomLineCur






