
CREATE PROCEDURE DBO.UpdateArrivalAndDepartureGroup @ReferenceNumber INTEGER AS
SET NOCOUNT ON
/*
  Update ReservationRoomLine for whole group

  LOG:
  3.36
  20060706 JP+AV: fix for missing update of mainres if guest 1 is linked to another res
  20060426 JP: Created

  NOTES: 
*/

DECLARE
@OldLineNumber INTEGER,
@LineNumber INTEGER,
@GuestNumber INTEGER,
@UpdateMain BIT
SET @OldLineNumber = 0

DECLARE #Cursor CURSOR LOCAL FAST_FORWARD FOR

SELECT RLX.GuestNumber,RLX.LineNumber FROM ReservationRoomLineXTab RLX WITH (NOLOCK) INNER JOIN Reservations R WITH (NOLOCK) ON R.ReferenceNumber=RLX.ReferenceNumber AND R.GuestNumber=RLX.GuestNumber AND R.ReferenceNumber=@ReferenceNumber AND R.Status<7 ORDER BY RLX.GuestNumber DESC, RLX.LineNumber

OPEN #Cursor

FETCH NEXT FROM #Cursor
INTO @GuestNumber, @LineNumber

WHILE (@@FETCH_STATUS <> -1)
BEGIN
--  IF @LineNumber <> @OldLineNumber
  IF (@LineNumber <> @OldLineNumber) OR (@GuestNumber = 1)
  BEGIN
    IF @GuestNumber = 1
      SET @UpdateMain = 1
    ELSE
      SET @UpdateMain = 0
    EXEC UpdateArrivalAndDeparture2 @ReferenceNumber, @GuestNumber, @UpdateMain  -- call only for one in the room as we want to update their roomline

    SET @OldLineNumber=@LineNumber
  END

  FETCH NEXT FROM #Cursor
  INTO @GuestNumber, @LineNumber
END

CLOSE #Cursor
DEALLOCATE #Cursor

