CREATE PROCEDURE dbo.UpdateArrivalAndDeparture @ReferenceNumber INT, @GuestNumber INT,
@GuestArrivalDate CHAR (8) OUTPUT, @GuestArrivalTime CHAR (5) OUTPUT, @GuestDepartureDate CHAR (8) OUTPUT, @GuestDepartureTime CHAR (5) OUTPUT,
@GuestAdults INT OUTPUT, @GuestChildren1 INT OUTPUT, @GuestChildren2 INT OUTPUT, @GuestPrice MONEY OUTPUT, @GuestTotalPrice Money OUTPUT,
@GuestProductNumber INT OUTPUT,
@ArrivalDate CHAR (8) OUTPUT, @ArrivalTime CHAR (5) OUTPUT, @DepartureDate CHAR (8) OUTPUT, @DepartureTime CHAR (5) OUTPUT,
@Adults INT OUTPUT, @Children1 INT OUTPUT, @Children2 INT OUTPUT, @Price MONEY OUTPUT, @TotalPrice Money OUTPUT,
@UpdateMainRes INT=1,
@WasUpdated SMALLINT OUTPUT
AS
SET NOCOUNT ON
-- DEPENDENCIES: UpdateArrivalAndDeparture2, FixReservationDates

/*
    Created by Jacob Pedersen

    Last update:
    20090324 BA: Where Clause in UPDATES where too complex. Might cause lock timeout. These are now changed to be 
                 hopefully faster and simpler for the SQL Engine to interpret.
    2008 0523 AV remember to use ISNULL else nothing is updated in the update reservations statement in the bottom
    20070904 JP: BUG: clear output params when not set
    20070829 AV: BUG: if table is cancelled then dont include it when calculationg arrival and departuredate
    20070426 JP: EXT: Fix MainRes status if arriving but inhouse or outofhouse subres
    20070314 JP: OPT: less SQL and less joins
    20070314 JP: BUG: no need to update reservations with ProductNumber = 0, use ISNULL function. Possible fix for lock timeout
    20060905 JP: BUG: do not count prices when pays cols are 0
    20050930 AV: Changed: If a Hall Line ends in the middle of the night then we should decrease departuredat with 1
    20050525 JP: Changed: if no period on mainres, then use periods of Noshow and Cancelled subres
    20050114 JP: Changed: Optimized SQL regarding min/max incl. time
    2004-05-27 JP: changed: inner selects to joins
		17-09-2003 by Jacob Pedersen changed call to SetOutOfOrderRoomAllFuture and remarked it since it locks
		15-09-2003 by Jacob Pedersen call to SetOutOfOrderRoomAllFuture
		28-08-2002 by Jacob Pedersen

    NOTE: @UpdateMainRes is default set to 1 which means that if a sub res is modified, then the main res will too. However this is not practical when saving a full namelist. Here it is better to set it to 0 for all except last reservation where we want to update the main res

                @WasUpdated is binary. 1 means res was updated. 2 means mainres was updated. 3 (1+2) means both was updated.

*/
DECLARE @Updated SMALLINT,

@LineNumber INT,
@NumberOfProducts SMALLINT,
@NumberOfPlusProducts SMALLINT,

@Arrival DATETIME,
@Departure DATETIME,

@ArrArrivalDate CHAR (8),
@ArrDepartureDate CHAR (8),
@ArrAdults INT,
@ArrChildren1 INT,
@ArrChildren2 INT,
@ArrPrice MONEY,
@ArrTotalPrice MONEY,
@ArrArrivalTime CHAR (5),
@ArrDepartureTime CHAR (5),

@DBArrivalDate CHAR(8),
@DBArrivalTime CHAR(5),
@DBDepartureDate CHAR(8),
@DBDepartureTime CHAR(5),
@DBAdults INT,
@DBChildren1 INT,
@DBChildren2 INT,
@DBPrice MONEY,
@DBTotalPrice MONEY,
@DBProductNumber INT,


@ProductNumber INT -- TODO: this should be outputted with the rest. no need as there isn't one for main res!

SET @Updated = 0

IF @GuestNumber > 0
BEGIN
  SELECT @LineNumber=LineNumber FROM dbo.ReservationRoomlineXtab WITH (NOLOCK) WHERE ReferenceNumber=@ReferenceNumber and GuestNumber=@GuestNumber

  SELECT @Arrival = MIN (CONVERT(DATETIME, ArrivalDate, 112)+CONVERT(DATETIME, ArrivalTime, 108)),
                 @Departure = MAX (CONVERT(DATETIME, DepartureDate, 112)+CONVERT(DATETIME, DepartureTime, 108)),
                 @Adults = SUM (Reservations.Adults),
                 @Children1 = SUM (Reservations.Children1),
                 @Children2 = SUM (Reservations.Children2),
                 @ProductNumber = MAX (ISNULL(dbo.Reservations.ProductNumber, 0))
  FROM dbo.Reservations WITH (NOLOCK) WHERE Status < 7 and ReferenceNumber=@ReferenceNumber and GuestNumber in
  (SELECT GuestNumber FROM dbo.ReservationRoomLineXTab WITH (NOLOCK) WHERE LineNumber=@LineNumber)

  SELECT @Price = SUM (Reservations.Price),
                 @TotalPrice = SUM (Reservations.Totalprice)
  FROM dbo.Reservations WITH (NOLOCK) WHERE Status < 7 and PaysAdults+PaysChildren1+PaysChildren2 > 0 and ReferenceNumber=@ReferenceNumber and GuestNumber in
  (SELECT GuestNumber FROM dbo.ReservationRoomLineXTab WITH (NOLOCK) WHERE LineNumber=@LineNumber)

  SET @ArrivalDate=CONVERT(CHAR(8), @Arrival, 112)
  SET @ArrivalTime=CONVERT(CHAR(5), @Arrival, 108)
  SET @DepartureDate=CONVERT(CHAR(8), @Departure, 112)
  SET @DepartureTime=CONVERT(CHAR(5), @Departure, 108)

  -- Count the different products on the linked res
  SELECT @NumberOfProducts=COUNT (DISTINCT ISNULL(ProductNumber, 0)) FROM dbo.Reservations WITH (NOLOCK) WHERE Status < 7 and ReferenceNumber=@ReferenceNumber and GuestNumber in
  (SELECT GuestNumber FROM dbo.ReservationRoomLineXTab WITH (NOLOCK) WHERE LineNumber=@LineNumber)

  SELECT @NumberOfPlusProducts=COUNT (DISTINCT ISNULL(ProductNumber, 0)) FROM dbo.Reservations WITH (NOLOCK) WHERE Day2>0 AND Status < 7 and ReferenceNumber=@ReferenceNumber and GuestNumber in
  (SELECT GuestNumber FROM dbo.ReservationRoomLineXTab WITH (NOLOCK) WHERE LineNumber=@LineNumber)

  IF @NumberOfPlusProducts > 0
  BEGIN
    IF @NumberOfPlusProducts <> @NumberOfProducts  -- not all products are + or not all + are the same! TODO: this method is not 100% as it assumes that +products are the same if first product is the same
      SET @ProductNumber=-2  -- **** in reception
  END
  ELSE IF @NumberOfProducts > 1  -- different products
    SET @ProductNumber=-2 -- **** in reception
  ELSE IF @NumberOfProducts < 1
    SET @ProductNumber=NULL

-- moved up  SELECT @LineNumber=LineNumber FROM ReservationRoomlineXtab WITH (NOLOCK) WHERE ReferenceNumber=@ReferenceNumber and GuestNumber=@GuestNumber


  IF not @ArrivalDate is null and not @DepartureDate is null and not (@ArrivalDate='18991230') and not (@DepartureDate='18991230')
  BEGIN
    -- BA/20090323 ************************

    SELECT @DBArrivalDate = ArrivalDate, @DBArrivalTime = ArrivalTime, 
           @DBDepartureDate = DepartureDate, @DBDepartureTime = DepartureTime, 
           @DBAdults = Adults, @DBChildren1 = Children1, @DBChildren2 = Children2, 
           @DBPrice = Price, @DBTotalPrice = TotalPrice, @DBProductNumber = ProductNumber
    FROM ReservationRoomLine WITH (NOLOCK)
    WHERE LineNumber = @LineNumber

    IF @DBArrivalDate <> @ArrivalDate or @DBArrivalTime <> @ArrivalTime OR 
       @DBDepartureDate <> @DepartureDate OR @DBDepartureTime <> @DepartureTime OR 
       @DBAdults <> @Adults OR @DBChildren1 <> @Children1 OR @DBChildren2 <> @Children2 OR
       @DBPrice <> @Price OR @DBTotalPrice <> @TotalPrice OR @DBProductNumber <> @ProductNumber
    BEGIN
      UPDATE dbo.ReservationRoomLine SET ArrivalDate=@ArrivalDate, ArrivalTime=@ArrivalTime, DepartureDate=@DepartureDate, DepartureTime=@DepartureTime, Adults=@Adults, Children1=@Children1, Children2=@Children2, Price=@Price, TotalPrice=@TotalPrice, ProductNumber=@ProductNumber 
      WHERE  LineNumber=@LineNumber -- AND
--      (ArrivalDate <> @ArrivalDate or ArrivalTime <> @ArrivalTime OR DepartureDate<>@DepartureDate OR DepartureTime<>@DepartureTime OR Adults<>@Adults OR Children1<>@Children1 OR Children2<>@Children2 OR Price<>@Price OR TotalPrice<>@TotalPrice OR ProductNumber<>@ProductNumber)
    END

    IF @@rowcount > 0
    BEGIN
      PRINT '1!:'+Convert(VARCHAR (20), @LineNumber)
      SET @Updated = 1
    END
  END

-- todo select distinct product from reservations!  count > 1 then ****

  SET @GuestArrivalDate = @ArrivalDate
  SET @GuestArrivalTime = @ArrivalTime
  SET @GuestDepartureDate = @DepartureDate
  SET @GuestDepartureTime = @DepartureTime
  SET @GuestAdults = @Adults
  SET @GuestChildren1 = @Children1
  SET @GuestChildren2 = @Children2
  SET @GuestPrice = @Price
  SET @GuestTotalPrice = @TotalPrice
  SET @GuestProductNumber = ISNULL(@ProductNumber, 0)
END
ELSE -- Clear output params
BEGIN
  SET @GuestArrivalDate = ''
  SET @GuestArrivalTime = ''
  SET @GuestDepartureDate = ''
  SET @GuestDepartureTime = ''
  SET @GuestAdults = 0
  SET @GuestChildren1 = 0
  SET @GuestChildren2 = 0
  SET @GuestPrice = 0
  SET @GuestTotalPrice = 0
  SET @GuestProductNumber = 0
END

/* -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
IF @UpdateMainRes = 1  -- JP 120602
BEGIN
  -- No need to do products because we don't display a union of all the products on all roomlines!

  -- Rooms --
  SELECT @Arrival = MIN (CONVERT(DATETIME, ArrivalDate, 112)+CONVERT(DATETIME, ArrivalTime, 108)),
                 @Departure = MAX (CONVERT(DATETIME, DepartureDate, 112)+CONVERT(DATETIME, DepartureTime, 108)),
                 @Adults = SUM (reservationroomline.Adults),
                 @Children1 = SUM (reservationroomline.Children1),
                 @Children2 = SUM (reservationroomline.Children2),
                 @Price = SUM (ISNULL(reservationroomline.Price,0)),
                 @TotalPrice = SUM (ISNULL(reservationroomline.TotalPrice, 0))
  from dbo.reservationroomline WITH (nolock) where linenumber in (
  select distinct linenumber from dbo.reservationroomlinextab WITH (nolock) inner join dbo.reservations WITH (nolock) on reservations.referencenumber=reservationroomlinextab.referencenumber and reservations.guestnumber=reservationroomlinextab.guestnumber and reservationroomlinextab.referencenumber=@referencenumber and status < 7)

  SET @ArrivalDate=CONVERT(CHAR(8), @Arrival, 112)
  SET @ArrivalTime=CONVERT(CHAR(5), @Arrival, 108)
  SET @DepartureDate=CONVERT(CHAR(8), @Departure, 112)
  SET @DepartureTime=CONVERT(CHAR(5), @Departure, 108)

  -- Halls --
  SELECT @Arrival = MIN (CONVERT(DATETIME, ArrivalDate, 112)+CONVERT(DATETIME, ArrivalTime, 108)),
                 @Departure = MAX (CONVERT(DATETIME, DBO.GetArrDepartureDate(DepartureDate,DepartureTime), 112)+CONVERT(DATETIME, DepartureTime, 108)), --AV 20050930
                 @ArrAdults = SUM (Adults),
                 @ArrChildren1 = SUM (Children1),
                 @ArrPrice = SUM (ISNULL(Price, 0)),
                 @ArrTotalPrice = SUM (ISNULL(TotalPrice, 0))
  FROM dbo.ReservationConferenceLine WITH (nolock) WHERE ReferenceNumber=@ReferenceNumber
  AND ((TableBooking = 1 AND ISNULL(TableStatus,0) < 7)  OR (ISNULL(TableBooking,0) = 0))

  SET @ArrArrivalDate=CONVERT(CHAR(8), @Arrival, 112)
  SET @ArrArrivalTime=CONVERT(CHAR(5), @Arrival, 108)
  SET @ArrDepartureDate=CONVERT(CHAR(8), @Departure, 112)
  SET @ArrDepartureTime=CONVERT(CHAR(5), @Departure, 108)

  if IsNull (@ArrAdults, 0) = 0
    Set @ArrAdults = 0
  if IsNull (@ArrChildren1, 0) = 0
    Set @ArrChildren1 = 0
  if IsNull (@ArrPrice, 0) = 0
    Set @ArrPrice = 0
  if IsNull (@ArrTotalPrice, 0) = 0
    Set @ArrTotalPrice = 0

  IF @ArrArrivalDate < @ArrivalDate or @ArrivalDate is null -- no roomlines
  BEGIN
    SET @ArrivalDate = @ArrArrivalDate
    SET @ArrivalTime = @ArrArrivalTime
  END
  ELSE IF @ArrivalDate = @ArrArrivalDate
  BEGIN
    IF @ArrArrivalTime < @ArrivalTime
      SET @ArrivalTime = @ArrArrivalTime
  END

  IF @ArrDepartureDate > @DepartureDate or @DepartureDate is null -- no roomlines
  BEGIN
    SET @DepartureDate = @ArrDepartureDate
    SET @DepartureTime = @ArrDepartureTime
  END
  ELSE IF @DepartureDate = @ArrDepartureDate
  BEGIN
    IF @ArrDepartureTime > @DepartureTime
      SET @DepartureTime = @ArrDepartureTime
  END

  --AV 101002 (the whole block) if we do not do this and the variables just under is null then we can not add value to a null value
  if IsNull (@Adults, 0) = 0 
    set @Adults = 0 
  if IsNull (@Children1, 0) = 0
    set @Children1 = 0
  if IsNull (@Price, 0) = 0
    set @Price = 0
  if IsNull (@TotalPrice, 0) = 0
    set @TotalPrice = 0

  SET @Adults = @Adults+@ArrAdults
  SET @Children1 = @Children1+@ArrChildren1
  SET @Price = @Price+@ArrPrice
  SET @TotalPrice=@TotalPrice+@ArrTotalPrice

  -- Fix MainRes status if arriving but inhouse or outofhouse subres
  DECLARE @StatusResMain TINYINT
  SELECT @StatusResMain = Status FROM dbo.Reservations WITH (NOLOCK) WHERE ReferenceNumber=@ReferenceNumber AND GuestNumber=0
  IF @StatusResMain < 5
  BEGIN
    DECLARE @SubResInhouse INT
    SELECT @SubResInhouse = COUNT(*) FROM dbo.Reservations WITH (NOLOCK) WHERE ReferenceNumber=@ReferenceNumber AND GuestNumber>0 AND Status=5

    IF @SubResInhouse > 0
      UPDATE dbo.Reservations SET Status = 5 WHERE ReferenceNumber=@ReferenceNumber AND GuestNumber=0
    ELSE
    BEGIN
      DECLARE @SubResOutOfHouse INT
      SELECT @SubResOutOfHouse = COUNT(*) FROM dbo.Reservations WITH (NOLOCK) WHERE ReferenceNumber=@ReferenceNumber AND GuestNumber>0 AND Status=6

      IF @SubResOutOfHouse > 0
        UPDATE dbo.Reservations SET Status = 6 WHERE ReferenceNumber=@ReferenceNumber AND GuestNumber=0
    END
  END

  IF @GuestNumber=0 AND (@ArrivalDate is null OR @DepartureDate is null OR @ArrivalDate='18991230' OR @DepartureDate='18991230') -- Nothing found for main res. use Noshow and Cancelled subres info
  BEGIN
     -- get cancelled periods from subres
    SELECT @Arrival = MIN (CONVERT(DATETIME, ArrivalDate, 112)+CONVERT(DATETIME, ArrivalTime, 108)),
                 @Departure = MAX (CONVERT(DATETIME, DepartureDate, 112)+CONVERT(DATETIME, DepartureTime, 108)),
                 @Adults = SUM (Reservations.Adults),
                 @Children1 = SUM (Reservations.Children1),
                 @Children2 = SUM (Reservations.Children2),
                 @Price = SUM (Reservations.Price),
                 @TotalPrice = SUM (Reservations.Totalprice),
                 @ProductNumber = MAX (Reservations.ProductNumber)
    FROM dbo.Reservations WITH (NOLOCK) WHERE Status >= 7 and ReferenceNumber=@ReferenceNumber and GuestNumber in
    (SELECT A.GuestNumber FROM dbo.ReservationRoomLineXTab A WITH (NOLOCK) INNER JOIN dbo.ReservationRoomLineXTab B WITH (NOLOCK) ON A.LineNumber=B.LineNumber WHERE B.ReferenceNumber=@ReferenceNumber AND B.GuestNumber>0)

    SET @ArrivalDate=CONVERT(CHAR(8), @Arrival, 112)
    SET @ArrivalTime=CONVERT(CHAR(5), @Arrival, 108)
    SET @DepartureDate=CONVERT(CHAR(8), @Departure, 112)
    SET @DepartureTime=CONVERT(CHAR(5), @Departure, 108)

  SELECT @Arrival = MIN (CONVERT(DATETIME, ArrivalDate, 112)+CONVERT(DATETIME, ArrivalTime, 108)),
                 @Departure = MAX (CONVERT(DATETIME, DepartureDate, 112)+CONVERT(DATETIME, DepartureTime, 108)),
                 @ArrAdults = SUM (reservationroomline.Adults),
                 @ArrChildren1 = SUM (reservationroomline.Children1),
                 @ArrChildren2 = SUM (reservationroomline.Children2),
                 @ArrPrice = SUM (reservationroomline.Price),
                 @ArrTotalPrice = SUM (reservationroomline.TotalPrice)                 
  from dbo.reservationroomline WITH (nolock) where linenumber in (
  select distinct linenumber from dbo.reservationroomlinextab WITH (nolock) inner join dbo.reservations WITH (nolock) on reservations.referencenumber=reservationroomlinextab.referencenumber and reservations.guestnumber=reservationroomlinextab.guestnumber and reservationroomlinextab.referencenumber=@referencenumber and reservationroomlinextab.Guestnumber=0)

  SET @ArrArrivalDate=CONVERT(CHAR(8), @Arrival, 112)
  SET @ArrArrivalTime=CONVERT(CHAR(5), @Arrival, 108)
  SET @ArrDepartureDate=CONVERT(CHAR(8), @Departure, 112)
  SET @ArrDepartureTime=CONVERT(CHAR(5), @Departure, 108)

  IF @ArrArrivalDate < @ArrivalDate or @ArrivalDate is null -- no roomlines
  BEGIN
    SET @ArrivalDate = @ArrArrivalDate
    SET @ArrivalTime = @ArrArrivalTime
  END
  ELSE IF @ArrivalDate = @ArrArrivalDate
  BEGIN
    IF @ArrArrivalTime < @ArrivalTime
      SET @ArrivalTime = @ArrArrivalTime
  END

  IF @ArrDepartureDate > @DepartureDate or @DepartureDate is null -- no roomlines
  BEGIN
    SET @DepartureDate = @ArrDepartureDate
    SET @DepartureTime = @ArrDepartureTime
  END
  ELSE IF @DepartureDate = @ArrDepartureDate
  BEGIN
    IF @ArrDepartureTime > @DepartureTime
      SET @DepartureTime = @ArrDepartureTime
  END

  if IsNull (@Adults, 0) = 0 
    set @Adults = 0 
  if IsNull (@Children1, 0) = 0
    set @Children1 = 0
  if IsNull (@Price, 0) = 0
    set @Price = 0
  if IsNull (@TotalPrice, 0) = 0
    set @TotalPrice = 0

  SET @Adults = @Adults+@ArrAdults
  SET @Children1 = @Children1+@ArrChildren1
  SET @Price = @Price+@ArrPrice
  SET @TotalPrice=@TotalPrice+@ArrTotalPrice

  END

  IF not @ArrivalDate is null and not @DepartureDate is null and not (@ArrivalDate='18991230') and not (@DepartureDate='18991230')
  BEGIN
    if IsNull (@Adults, 0) = 0
      Set @Adults = 0
    if IsNull (@Children1, 0) = 0
      Set @Children1 = 0
    if IsNull (@Children2, 0) = 0
      Set @Children2 = 0
    if IsNull (@Price, 0) = 0
      Set @Price = 0
    if IsNull (@TotalPrice, 0) = 0
      Set @TotalPrice = 0

/*
    UPDATE dbo.Reservations SET ArrivalDate=@ArrivalDate, ArrivalTime=@ArrivalTime, DepartureDate=@DepartureDate, DepartureTime=@DepartureTime, Adults=@Adults, Children1=@Children1, Children2=@Children2, Price=@Price, TotalPrice=@TotalPrice WHERE ReferenceNumber=@ReferenceNumber AND GuestNumber=0 AND
                                                 (ArrivalDate <> @ArrivalDate or ArrivalTime <> @ArrivalTime OR DepartureDate<>@DepartureDate OR DepartureTime<>@DepartureTime OR Adults<>@Adults OR Children1<>@Children1 OR Children2<>@Children2 OR Price<>@Price OR TotalPrice<>@TotalPrice)
*/--Removed AV 2080523 remember to use ISNULL else nothing is updated

    -- BA/20090323 ***********************
    SELECT @DBArrivalDate = ArrivalDate, @DBArrivalTime = ArrivalTime, 
           @DBDepartureDate = DepartureDate, @DBDepartureTime = DepartureTime, 
           @DBAdults = Adults, @DBChildren1 = Children1, @DBChildren2 = Children2, 
           @DBPrice = Price, @DBTotalPrice = TotalPrice, @DBProductNumber = ProductNumber
    FROM Reservations WITH (NOLOCK)
    WHERE ReferenceNumber = @ReferenceNumber AND GuestNumber = 0


    IF (ISNULL(@DBArrivalDate, '') <> @ArrivalDate or ISNULL(@DBArrivalTime, '') <> @ArrivalTime OR 
        ISNULL(@DBDepartureDate,'') <> ISNULL(@DepartureDate,'') OR ISNULL(@DBDepartureTime,'') <> @DepartureTime OR 
        ISNULL(@DBAdults,0) <> @Adults OR ISNULL(@DBChildren1,0) <> @Children1 OR ISNULL(@DBChildren2, 0) <> @Children2 OR 
        ISNULL(@DBPrice,0) <> @Price OR ISNULL(@DBTotalPrice,0) <> @TotalPrice)
    BEGIN
      UPDATE dbo.Reservations SET ArrivalDate=@ArrivalDate, ArrivalTime=@ArrivalTime, DepartureDate=@DepartureDate, DepartureTime=@DepartureTime, Adults=@Adults, Children1=@Children1, Children2=@Children2, Price=@Price, TotalPrice=@TotalPrice 
      WHERE ReferenceNumber = @ReferenceNumber AND GuestNumber = 0 -- AND
--          (ISNULL(ArrivalDate, '') <> @ArrivalDate or ISNULL(ArrivalTime, '') <> @ArrivalTime OR 
--           ISNULL(DepartureDate,'') <> ISNULL(@DepartureDate,'') OR ISNULL(DepartureTime,'')<>@DepartureTime OR 
--           ISNULL(Adults,0)<>@Adults OR ISNULL(Children1,0)<>@Children1 OR ISNULL(Children2, 0)<>@Children2 OR 
--           ISNULL(Price,0)<>@Price OR ISNULL(TotalPrice,0)<>@TotalPrice)
    END
    IF @@rowcount > 0
    BEGIN
      print 'updated'
      PRINT '2!:'+Convert(VARCHAR (20), @ReferenceNumber)+'/'+Convert(VARCHAR (20), @GuestNumber)
      IF @Updated = 1
        SET @Updated = 1+2
      ELSE
        SET @Updated = 2
    END
  END

END

SET @WasUpdated = @Updated

-- Set Rooms outoforder /Skallerup --

--THIS IS A PROBLEM SINCE ALL THE ROOMS ON THE RESERVATION WILL BE LOCKED FOR THE DURATION OF THE TRANSACTION SO CALL HAS BEEN MOVED TO DELPHI CODE WITH OWN CONNECTION

--EXEC SetOutOfOrderRoomAllFutureAllRooms @ReferenceNumber, @GuestNumber

