CREATE PROCEDURE dbo.OnlineRebookReservation @AReferenceNumber INT, @ErrorLevel INT OUTPUT AS
SET NOCOUNT ON
/*
    Created by Jacob Pedersen July 2004

    Changes:
    TB20070511: Delete ReservationRoomLineXTab before roomline
    AV 2006 1113 ReservationConferenceLine Added
    TB20050626: Changed State to be readed from hotelsetup
    20041004 JP: fixed cancelled status (8->7)

    NOTES: 	- 

*/
/*
  Error Levels:
  00 : No errors
  01 : Reservation was not cancelled so it could not be rebooked
*/
SET @ErrorLevel = 00
DECLARE @Status TINYINT
DECLARE @TempState INT

SELECT @TempState=KeyValue FROM HotelSetup WITH (NOLOCK) WHERE Upper (SearchKey)=Upper('PicassoOnlineTempState')
SELECT @Status=Status FROM Reservations WITH (NOLOCK) WHERE ReferenceNumber=@AReferenceNumber AND GuestNumber=0

IF @Status<>7 /* Cancelled */
BEGIN
  SET @ErrorLevel=01
  RETURN
END


UPDATE Reservations SET Status=@TempState   WHERE ReferenceNumber=@AReferenceNumber AND GuestNumber=0

DELETE FROM ReservationRoomLineXTab WHERE ReferenceNumber=@AReferenceNumber AND GuestNumber=0

DELETE FROM ReservationRoomLine WHERE LineNumber IN (SELECT LineNumber FROM ReservationRoomLineXTab WITH (NOLOCK) WHERE ReferenceNumber=@AReferenceNumber AND GuestNumber=0)

DELETE FROM ReservationConferenceLine WHERE ReferenceNumber = @AReferenceNumber

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

EXEC OnlineUpdateFollowUp @AReferenceNumber

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


