SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[OnlineCheckInCurrentReservation]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[OnlineCheckInCurrentReservation]
GO

CREATE PROCEDURE dbo.OnlineCheckInCurrentReservation  @AKind TINYINT, @AReferenceNumber INT, @AGuestNumber INT, @ErrorLevelCheckin INT OUTPUT, @ErrorLevelRoomAllocate INT OUTPUT, @ErrorLevel INT OUTPUT AS

--Created AV 20080107 Used Online. Only if namelist is created

--Last Changed: AV 20080109 errorlevels for this proc used
/*
  Error Levels from ReservationCheckIn Stored procedure:
 -01 : Reservation already inhouse
  00 : No errors - CheckIn succesfully
  01 : Reservation is NOX
  02 : Reservation is not arriving today
  03 : A room has not been allocated
  04 : The allocated room is not clean


  Error Levels from ReservationAllocate Stored procedure:
 -01 : Reservation already allocated
  00 : No errors - Allocation succesfully
  01 : Reservation is not arriving
  02 : No room is vacant and meeting our requirements

*/
/*
This Procs Errorlevel
  00 : No errors - CheckIn succesfully
  01:  Error: Either Checkin or  RoomAllocate has failed
  02:  Error: More than one room exists and no namelist is created
*/


/*
  Kind
 1 check alle reservationer ind
 2 check hoved res + første ind
 
*/


  DECLARE @GuestNumber INT
  DECLARE @RoomNumber INT
  DECLARE @LineNumber INT
  DECLARE @SubTypes VARCHAR(255)
  DECLARE @AllocatedRoomNumber INTEGER
  DECLARE @FRoomNumber INTEGER
  DECLARE @RoomCountWithOutNameList INT
  DECLARE @RoomCount INT

  SET @SubTypes = ''
  SET @ErrorLevelCheckin = 0
  SET @ErrorLevelRoomAllocate = 0
  SET @ErrorLevel = 0
 
  IF @GuestNumber = 0
  BEGIN
    SELECT @RoomCount = Count(*) FROM ReservationRoomLineXTab WITH (NOLOCK) WHERE ReferenceNumber = @AReferenceNumber
    IF ISNULL(@RoomCount, 0) > 0 -- we know that there are more than one room on the res
    BEGIN
      SELECT @RoomCountWithOutNameList = Count(*) FROM ReservationRoomLineXTab WITH (NOLOCK) WHERE ReferenceNumber = @AReferenceNumber AND GuestNumber = 0
      IF ISNULL(@RoomCountWithOutNameList, 0 ) > 0 -- we know that there is not created namelist for at least one roomline.
        SET @ErrorLevel = 2
      RETURN
    END
  END

  DECLARE UpdateCur Cursor Forward_Only For
  SELECT DISTINCT Reservations.GuestNumber, ISNULL(ReservationRoomLine.RoomNumber, 0), ReservationRoomLineXTab.LineNumber FROM Reservations WITH (NOLOCK)
      INNER JOIN ReservationRoomLineXTab WITH (NOLOCK) ON Reservations.ReferenceNumber = ReservationRoomLineXTab.ReferenceNumber AND Reservations.GuestNumber = ReservationRoomLineXTab.GuestNumber
  INNER JOIN ReservationRoomLine WITH (NOLOCK) ON ReservationRoomLineXTab.LineNumber = ReservationRoomLine.LineNumber
  WHERE Reservations.ReferenceNumber = @AReferenceNumber AND 
  --Reservations.GuestNumber > 0 AND 
    Reservations.Status < 5 AND ((@AKind = 1) OR (@AKind = 2 AND Reservations.GuestNumber = @AGuestNumber))
  OPEN UpdateCur
  FETCH NEXT FROM UpdateCur INTO @GuestNumber, @RoomNumber, @LineNumber
  WHILE  (@@FETCH_STATUS <> -1)
  BEGIN
    SET @ErrorLevelCheckin = 0
    SET @ErrorLevelRoomAllocate = 0
    IF @RoomNumber = 0    
      EXEC dbo.ReservationAllocate @LineNumber, @SubTypes, @AllocatedRoomNumber OUTPUT, @FRoomNumber OUTPUT, @ErrorLevelRoomAllocate OUTPUT
    IF @ErrorLevelRoomAllocate <> 0
    BEGIN
      SET @ErrorLevel = 1
      BREAK
    END
    EXEC dbo.ReservationCheckIn @AReferenceNumber, @GuestNumber, @ErrorLevelCheckin OUTPUT
    IF @ErrorLevelCheckin <> 0 AND @ErrorLevelCheckin <> -1
    BEGIN
      SET @ErrorLevel = 1
      BREAK
    END
--       PRINT 'guest'+ convert(varchar(1),@GuestNumber)
    FETCH NEXT FROM UpdateCur INTO @GuestNumber, @RoomNumber, @LineNumber
  END
  CLOSE UpdateCur
  DEALLOCATE UpdateCur
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

