CREATE PROCEDURE [dbo].[OnlineAddRoomLine] @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, @AArrivalPrice MONEY = 0,
@ATotalPrice MONEY = 0 AS 
SET NOCOUNT ON
/*
    Created by Troels Buch Juni 2004

    Changes:
	PHJ20211025: Added ScreenLine number to roomline.
	TBU20161213: Added @AArrivalPrice - @ATotalPrice - KUN 8.3+
    SG 2016-08-17: PriceOverWritten logic added. 
    TBU20150709: Changed EnableReservationGroup
    TBU20150709: Added FK_ReservationGroup
    TB20060330: removed begin tran...
    TB20060228: Added @LineNumber as parameter. Call GetNextNumber outside this SP
    TB20050513: Change call to update Tabel 'ReservationIdentity' to be called from a procedure
    TB20041006: Cleaned up

    NOTES: 	- 

*/
DECLARE @ErrorLevel INT
SET @ErrorLevel = 00

/*
  Error Levels:
  00 : No errors
  01 : Not available anymore

*/

--DECLARE @LineNumber INT
--EXEC GetNextNumber 'RESERVATIONROOMLINEIDENTITY', 1, @LineNumber OUTPUT

--BEGIN TRAN

DECLARE @AddressNumber INT,
@ArrivalTime CHAR (5),
@DepartureTime CHAR (5),
@ReservationNumber INT, @DefaultReservetionId INT, @UseReservationGroup BIT 

SET @ArrivalTime= CONVERT(CHAR, @AStartTime, 112)
SET @DepartureTime = CONVERT(CHAR, @AEndTime, 112)
SET @ReservationNumber = @AReferenceNumber

SET @DefaultReservetionId = NULL
SELECT @UseReservationGroup =  keyvalue FROM Hotelsetup WITH (NOLOCK) WHERE Searchkey = 'EnableReservationGroup'
IF @UseReservationGroup > 0
	SELECT @DefaultReservetionId = IsNull(ID, 0) from Reservationgroup (nolock) where DefaultGroup = 1
IF IsNull(@DefaultReservetionId, 0) < 1
	SET @DefaultReservetionId = NULL
--PriceOverWrite?
Declare @HotelSetupCheckInAlwaysSetPriceOverwrite char(5);
Declare @ProductAlwaysSetPriceOverride char(5);
Declare @PriceOverwrite int;

SET @HotelSetupCheckInAlwaysSetPriceOverwrite = dbo.GetHotelSetupKeyValue('CheckInAlwaysSetPriceOverwrite', -1);
SELECT @ProductAlwaysSetPriceOverride = ProductExtended.AlwaysSetPriceOverride FROM ProductExtended WHERE FK_ProductNumber = @AProductNumber
IF @HotelSetupCheckInAlwaysSetPriceOverwrite = '1' OR @ProductAlwaysSetPriceOverride = '1'
BEGIN
  SET @PriceOverwrite = 1;
END ELSE
BEGIN
  SET @PriceOverwrite = 0;
END

declare @screenlineNo int = 0;
select @screenlineNo = count(*) from ReservationRoomLine with (nolock) inner 
join RESERVATIONROOMLINEXTAB with (nolock) on RESERVATIONROOMLINEXTAB.LINENUMBER = RESERVATIONROOMLINE.LINENUMBER and referencenumber = @AReferenceNumber

set @screenlineNo = @screenlineNo + 1

--Price = @ArrivePrice, TotalPrice = @TotalPrice
INSERT INTO ReservationRoomLine (LineNumber, BookedTypeNumber, NumberOfRooms, ArrivalDate, ArrivalTime, DepartureDate, DepartureTime, 
                                 Adults, Children1, Children2, ProductNumber, FK_ReservationGroup, PriceOverwritten, Price, TotalPrice, screenline)
                         VALUES (@LineNumber, @ARoomTypeNumber, @ANumberOfRooms, CONVERT(CHAR, @AStartDate, 112), @ArrivalTime, CONVERT(CHAR, @AEndDate, 112), @DepartureTime, 
					        @AAdults, @AChildren1, @AChildren2, @AProductNumber, @DefaultReservetionId, @PriceOverwrite, @AArrivalPrice, @ATotalPrice, @screenlineNo)

--UPDATE ReservationIdentity SET @ReservationNumber = IdentityField = IdentityField + 1  -- New referencenumber

INSERT INTO ReservationRoomLineXTab (ReferenceNumber, GuestNumber, LineNumber) VALUES (@ReservationNumber, 0, @LineNumber)

SET @RoomLineNumber = @LineNumber

--SET @ErrorLevel = 1 -- testing!

/*
IF @ErrorLevel > 00
BEGIN
  SET @ReservationNumber = 0
  --SET @Price = 0
  ROLLBACK TRAN
  RETURN
END*/

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

EXEC OnlineUpdateFollowUp @AReferenceNumber

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

--COMMIT TRAN


