CREATE PROCEDURE dbo.ReservationCheckInCourt @ReferenceNumber INTEGER, @GuestNumber INTEGER OUTPUT, @ErrorLevel INTEGER OUTPUT AS
SET NOCOUNT ON
/*
  Stored procedure which checks in court-reservation if possible. Returns errorlevel if not and the GuestNumber which has the problem

  20071004 : MF CREATED

  NOTES: This routine is  only used for Online checkins of Court reservations, hence it does only support Court reservations. Also log is created for Online user

*/
/*
  Error Levels:
 -01 : Reservation already inhouse
  00 : No errors - CheckIn succesfully
  01 : Reservation is NOX
  02 : Reservation is not arriving today
*/

-- if guestnumber > 0 then checkin guestnumber 0. if no errors (or already checkedin) then continue
IF @GuestNumber > 0
BEGIN
  EXEC dbo.ReservationCheckIn @ReferenceNumber, 0, @ErrorLevel OUTPUT
  IF @ErrorLevel > 00
  BEGIN
    SET @GuestNumber = 0
    RETURN
  END
END

SET @ErrorLevel = 00

-- Status < 5
DECLARE
@Status TINYINT,
@ArrivalDate CHAR(8)
SELECT @Status=Status, @ArrivalDate=ArrivalDate FROM Reservations WITH (NOLOCK) WHERE ReferenceNumber=@ReferenceNumber AND GuestNumber=@GuestNumber
IF @Status = 5
BEGIN
  SET @ErrorLevel = -01
  RETURN
END
ELSE IF @Status > 5
BEGIN
  SET @ErrorLevel = 01
  RETURN
END

-- Arrival = Today (systemdate)
DECLARE
@SystemDate CHAR(8)
SELECT @SystemDate=SystemDate FROM System WITH (NOLOCK) WHERE Number=1

--- Test
--SET @SystemDate = convert(varchar(8),convert(datetime,'2006',112), 112)
--SET @SystemDate = '20071201'
---

IF @ArrivalDate <> @SystemDate
BEGIN
  SET @ErrorLevel = 02
  RETURN
END

-- Check in now that all criteria is met
UPDATE Reservations SET Status=5 WHERE ReferenceNumber=@ReferenceNumber AND GuestNumber=@GuestNumber -- TODO: update arrivaltime if hotelsetting is set

-- FOLLOWUP ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

DECLARE @NewFollowUpNumber INT,
@SystemDateTime DATETIME,
@Text VARCHAR(96),
@UserIDNumber INT

EXEC SystemDateTime '', @SystemDateTime OUTPUT

-- Find the UserIDNumber for PicassoOnline
SELECT @UserIDNumber=UserIDNumber FROM Personel WITH (NOLOCK) WHERE UserTypeNumber=5

-- Create log
SET @Text = 'Court Checked In'
INSERT INTO FollowUp ([Date], Type, Priority, State, Note, CreatedDate, CreatedBy, SubType, SubType2)
VALUES (@SystemDateTime, 5, 12, 3, @Text, @SystemDateTime, @UserIDNumber, 1, 4)
SELECT @NewFollowUpNumber = @@identity
INSERT INTO ReservationFollowUpXTab (ReferenceNumber, GuestNumber, FollowUpNumber) VALUES (@ReferenceNumber, 0, @NewFollowUpNumber)

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

EXEC OnlineUpdateFollowUp @ReferenceNumber