CREATE PROCEDURE [dbo].[OnlineUpdateRoomLine] @ReferenceNumber INT, @AProductNumber INT, @RoomLineNumber INT, @TotalPrice MONEY, @ArrivePrice MONEY, @RoomTypeNumber INT, @NumberOfRooms INT, 
@Adult INT, @Child1 INT, @Child2 INT, @CardNumber VARCHAR(128), @ExpireDate DATETIME,  @ACustomerNumber INT = 0, @PriceAsMoneyChanged BIT =1, @GenerateArrangement BIT=1 AS
SET NOCOUNT ON
/*
    Created by Troels Buch Juni 2004

    Changes: N/A
	TBU20170910: Changed GenerateArrangementFromNameList to ReCreateArrangement
	TBU20170221: Added @GenerateArrangement
	TBU20140818: Added With (Nolock)
    	TB20140813: Added @PriceAsMoneyChanged
    	AV 20060614: HotelNumber selected from ReservationMain Table and transferred to Arrangement stored procedure
	TB20060330:  Removed Begin tran / rollback tran
	JP20041217: Chaned CreditCardNumber to 128 chars
	TB20041006: Cleaned up
	TB20041129: Add Creditcardnumber and expiredate to reservation
	TB20041206: Call GenerateArrangementFromNameList  to add package to roomlines

    NOTES: 	- 

*/
DECLARE @ErrorLevel INT
SET @ErrorLevel = 00

DECLARE @ArrivalDate DATETIME
DECLARE @DepartureDate DATETIME

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

--BEGIN TRAN

DECLARE @ReservationNumber INT

DECLARE @HotelNumber INT
DECLARE @DominoEnabled BIT

SELECT @DominoEnabled=CONVERT(SMALLINT,KeyValue) FROM HotelSetup WITH (NOLOCK) WHERE SearchKey='CentralBookingType'
  IF ISNULL(@DominoEnabled, 0) = 0
    SET @DominoEnabled = 0

IF @DominoEnabled = 1
  SELECT @HotelNumber = HotelNumber FROM ReservationMain WITH (NOLOCK) WHERE ReferenceNumber = @ReferenceNumber
ELSE
  SET @HotelNumber = -1


UPDATE ReservationRoomLine SET ProductNumber = @AProductNumber, Price = @ArrivePrice, TotalPrice = @TotalPrice WHERE LineNumber = @RoomLineNumber

SELECT @ArrivalDate=arrivaldate, @DepartureDate=Departuredate FROM ReservationRoomLine WITH (NOLOCK) inner join ReservationRoomLinextab WITH (NOLOCK) on ReservationRoomLine.linenumber = ReservationRoomLinextab.linenumber WHERE ReservationRoomLinextab.Linenumber = @RoomLineNumber

IF @GenerateArrangement =1
BEGIN
	-- [ReCreateArrangement] @refno int, @AHotelNumber int = -1
	EXEC ReCreateArrangement @ReferenceNumber, @HotelNumber
	--EXEC GenerateArrangementFromNameList @AProductNumber, @AProductNumber, 1, @ArrivalDate, @ArrivalDate, @DepartureDate, @DepartureDate, @ArrivalDate,
	--@DepartureDate, ReferenceNumber, 0, @Adult, @Adult,  @Child1, @Child1,  @Child2, @Child2, @NumberOfRooms, @NumberOfRooms, 0, 0, 0, @RoomLineNumber,@RoomTypeNumber,0, @HotelNumber
END

-- Add Creditcardnumber and expiredate to reservation : TB20041129
IF @CardNumber <> '' 
BEGIN
  UPDATE Reservations SET Creditcardnumber = @CardNumber, [ExpireDate] = @ExpireDate   WHERE  referencenumber = @ReferenceNumber
END


