CREATE PROCEDURE dbo.OnlineAddHallLine @AReferenceNumber INT, @AAdults SMALLINT, @AChildren1 SMALLINT, @AChildren2 SMALLINT, @AStartDate DATETIME, @AStartTime VARCHAR(5), @AEndDate DATETIME, @AEndTime VARCHAR(5), 
                                       @AHallNumber INT, @ATableSetup INT, @AProductNumber INT, @ALineNumber INT, @ATableBooking INT, @AGuestNumber INT = 0 
									   AS 
SET NOCOUNT ON

-- Created by AV 20060614
-- 2022-02-15/SG: Using HotelSetup.OnlineSegmentTable and HotelSetup.OnlinePOSTable
-- 2022-01-27/SG: Added GuestNumber in ReservationConferenceLine
-- 2022-01-11/SG: HallNumber set to NULL in tableBooking (previously this was cared for in Online)
-- 2016-08-17/SG: PriceOverWritten logic added. 
-- NLB 20070124: Added segment to reservation
-- Sidst rettet 2006 0711 Added search for HallTypeNumber
-- Sidst rettet 2007 0425 Added GuestNumber
-- @ATableBooking=0: Hallbooking, @ATableBooking=1: Bordbooking
---Sidst rettet 2009 0610 Also fill out the tablestatus column if we are dealing with a tablebooking

--@ATableBooking:
--0  Not table booking
--1  Only table booking
--2  Table booked together with a room or a hall


DECLARE @TableStatus INT

IF @ATableBooking IN (1, 2)
  SELECT @TableStatus = Status FROM Reservations WITH (NOLOCK) WHERE ReferenceNumber = @AReferenceNumber  AND GuestNumber = 0
ELSE
  SET @TableStatus = NULL

IF (@ATableSetup=0)
   SET @ATableSetup = NULL

DECLARE @HallTypeNumber INT
SELECT @HallTypeNumber = ConferenceRoomTypeNumber FROM ConferenceRooms WITH (NOLOCK) WHERE ConferenceRoomNumber = @AHallNumber
IF @ATableBooking IN (1, 2)
BEGIN
  SET @AHallNumber = NULL --Tablebooking does not book on a hall, only on a hallType 
END

--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 @TableBooking int
If @ATableBooking in (1,2)
Begin
  Set @TableBooking = 1
End Else
Begin
  Set @TableBooking = 0
End
INSERT INTO ReservationConferenceLine (ReservationConfLineNumber, BookedConferenceRoomTypeNumber, NumberOfRooms, ArrivalDate, ArrivalTime, DepartureDate, DepartureTime, 
        ConferenceRoomSetupNumber, ConferenceRoomNumber, Adults, Children1, Children2, ReferenceNumber, TableBooking, TableStatus, PriceOverWritten,
		GuestNo)
VALUES (@ALineNumber, @HallTypeNumber, 1, CONVERT(VARCHAR(8),@AStartDate,112), @AStartTime, CONVERT(VARCHAR(8),@AEndDate,112), @AEndTime, 
        @ATableSetup, @AHallNumber, @AAdults, @AChildren1, @AChildren2, @AReferenceNumber, @TableBooking, @TableStatus, @PriceOverwrite,
		@AGuestNumber)

-- Update segment
DECLARE @SegmentNumberHall INT
DECLARE @SegmentNumberTable INT
DECLARE @POSNumberTable INT

SELECT @SegmentNumberHall = IsNull(dbo.GetHotelSetupKeyValue('PicassoOnlineSegmentNumberHalls', -1), 0)
SELECT @SegmentNumberTable = IsNull(dbo.GetHotelSetupKeyValue('OnlineSegmentTable', -1), 0)
SELECT @POSNumberTable = IsNull(dbo.GetHotelSetupKeyValue('OnlinePOSTable', -1), 0)

If @ATableBooking = 0 and @SegmentNumberHall <> 0
Begin
  UPDATE RESERVATIONS SET MarketSegment = @SegmentNumberHall WHERE ReferenceNumber = @AReferenceNumber AND GuestNumber = @AGuestNumber 
End
If @ATableBooking = 1
Begin
  if @SegmentNumberTable > 0
  Begin
    UPDATE RESERVATIONS SET MarketSegment = @SegmentNumberTable WHERE ReferenceNumber = @AReferenceNumber AND GuestNumber = @AGuestNumber 
  End Else
  Begin
    --To be backward compatible 
    UPDATE RESERVATIONS SET MarketSegment = @SegmentNumberHall WHERE ReferenceNumber = @AReferenceNumber AND GuestNumber = @AGuestNumber 
  End
  if @POSNumberTable > 0
  Begin
    UPDATE RESERVATIONS SET PointOfSale = @POSNumberTable WHERE ReferenceNumber = @AReferenceNumber AND GuestNumber = @AGuestNumber 
  End
End
--@ATableBooking = 2, do nothing

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

EXEC OnlineUpdateFollowUp @AReferenceNumber
