CREATE PROCEDURE [dbo].[OnlineAddRoomToCourse] @AReferenceNumber INT, @ANumberOfRooms SMALLINT, @AAdults SMALLINT, @AChildren1 SMALLINT, @AChildren2 SMALLINT, @AStartDate DATETIME, @AStartTime VARCHAR(5), @AEndDate DATETIME, @AEndTime VARCHAR(5), @ARoomTypeNumber INT, @AProductNumber INT , @LineNumber INT, @RoomLineNumber INT OUTPUT,  @ACustomerNumber INT = 0, @Return_Message VARCHAR(1024) = ''  OUTPUT AS
SET NOCOUNT ON
/*
    	Created by Troels Buch 2016

    	Changes: N/A
    	
    	NOTES: 	

*/

DECLARE @ErrorCode int
DECLARE @ErrorStep varchar(200)
DECLARE @RoomCount int

SELECT @ErrorCode = @@ERROR

BEGIN TRY

  BEGIN TRAN

	SELECT @ErrorStep = 'Error in OnlineAddRoomLine';

	--set @ANumberOfRooms = 'gr'

    EXEC OnlineAddRoomLine @AReferenceNumber, @ANumberOfRooms, @AAdults, @AChildren1, @AChildren2, @AStartDate, @AStartTime, @AEndDate, @AEndTime, @ARoomTypeNumber, @AProductNumber , @LineNumber , @RoomLineNumber OUTPUT,  @ACustomerNumber 

	SELECT @RoomCount=NumberOfRooms FROM RESERVATIONS res WITH (NOLOCK)
	INNER JOIN RESERVATIONROOMLINEXTAB resxtab WITH (NOLOCK) ON res.ReferenceNumber = resxtab.ReferenceNumber 
	INNER JOIN RESERVATIONROOMLINE resline WITH (NOLOCK) ON resxtab.LINENUMBER = resline.LINENUMBER
	WHERE res.ReferenceNumber = (SELECT FK_Referencenumber FROM ReservationReservationXtab WITH (NOLOCK)
	WHERE FK_ReferenceNumbersub = @AReferenceNumber)

	SET @RoomCount = @RoomCount-1
	
	SELECT @ErrorStep = 'Error in UPDATE RESERVATIONROOMLINE';	

	UPDATE RESERVATIONROOMLINE set NumberOfRooms = @RoomCount WHERE linenumber=(SELECT resline.Linenumber FROM RESERVATIONS res  WITH (NOLOCK)
	INNER JOIN RESERVATIONROOMLINEXTAB resxtab WITH (NOLOCK) ON res.ReferenceNumber = resxtab.ReferenceNumber 
	INNER JOIN RESERVATIONROOMLINE resline WITH (NOLOCK) ON resxtab.LINENUMBER = resline.LINENUMBER
	WHERE res.ReferenceNumber = (SELECT FK_Referencenumber FROM ReservationReservationXtab WITH (NOLOCK)
	WHERE FK_ReferenceNumbersub = @AReferenceNumber))  

  COMMIT TRAN

  RETURN @ErrorCode

END TRY

BEGIN CATCH
  IF @@TRANCOUNT > 0 
    ROLLBACK

  SELECT @ErrorCode = ERROR_NUMBER()
        , @Return_Message = @ErrorStep + ' '
        + cast(ERROR_NUMBER() as varchar(20)) + ' line: '
        + cast(ERROR_LINE() as varchar(20)) + ' ' 
        + ERROR_MESSAGE() + ' > ' 
        + ERROR_PROCEDURE()

	
  RETURN @ErrorCode  
END CATCH




