



CREATE PROCEDURE DBO.RecalculateResPrices @TheProductNumber INT, @OnlyNotPriceOverwritten INT=0 AS
SET NOCOUNT ON
/*
  CREATED BY: Jacob Pedersen 12-03-2002
  LAST UPDATED: 	Jacob Pedersen 30-01-2004 Fixed so AddOnIfPriceOverwritten is taking into account + Fixed serious + product bug!
			Jacob Pedersen 01-12-2003 - 02-12-2003 fixes for NameListPerPerson and OnePersonPaysForAll

  NOTE: @OnlyNotPriceOverwritten means only the reservations with the flag priceoverwritten = 0

  NOTE: This routine should be called within a transaction and no one should be using the system, because it needs to update reservations which might be open.

  NOTE: If you get a "Warning: Null value is eliminated by an aggregate or other SET operation." when running this procedure, don't be alarmed. 
              Some prices are stored as NULL where 0 should have been stored. These NULL values will not be used in SUM which don't matter as
              the value 0 wouldn't change the SUM anyways.
*/

update reservations set priceoverwritten=1 where (productnumber = 0 or productnumber is null) and (priceoverwritten=0 or priceoverwritten is null)               -- JP 110602
update reservationroomline set priceoverwritten=1 where (productnumber = 0 or productnumber is null) and (priceoverwritten=0 or priceoverwritten is null)   -- JP 110602

CREATE TABLE #Log(
  ReferenceNumber INT not null,
  GuestNumber INT not null,
  LineNumber INT,
  OldArrivalPrice MONEY,
  OldTotalPrice MONEY,
  NewArrivalPrice MONEY,
  NewTotalPrice MONEY,
  Descrip VARCHAR (50))

DECLARE @NameListReservationPerPerson BIT -- From Product - updated throughout the code

DECLARE @AddOnIfPriceOverwritten BIT
SELECT @AddOnIfPriceOverwritten=KeyValue FROM HotelSetup WITH (NOLOCK) WHERE Upper (SearchKey) = Upper ('AddOnIfPriceOverwritten')
IF (IsNull (@AddOnIfPriceOverwritten, 0)=0)
  SET @AddOnIfPriceOverwritten=0

DECLARE @HSNameListReservationPerPerson BIT -- From HotelSetup
SELECT @HSNameListReservationPerPerson=KeyValue FROM HotelSetup WITH (NOLOCK) WHERE Upper (SearchKey) = Upper ('NameListReservationPerPerson')
IF (IsNull (@HSNameListReservationPerPerson, 0)=0) or (@HSNameListReservationPerPerson=0)
  SET @HSNameListReservationPerPerson=1  -- default 1!

DECLARE @HSOnePersonPaysForAll BIT -- From HotelSetup
SELECT @HSOnePersonPaysForAll = Convert (INT, Keyvalue) FROM HotelSetup WITH (NOLOCK) WHERE Upper (SearchKey) =Upper ('OnePersonPaysForAll')
IF IsNull (@HSOnePersonPaysForAll, 0)=0
  SET @HSOnePersonPaysForAll=0

DECLARE @LinkedRes INT
DECLARE @ReferenceNumber INT
DECLARE @GuestNumber INT
DECLARE @LineNumber INT
DECLARE @SpecBookedTypeNumber INT
DECLARE @SpecNumberOfRooms INT
DECLARE @SpecArrivalDate CHAR (8)
DECLARE @SpecDepartureDate CHAR (8)
DECLARE @ArrivalDate CHAR (8)
DECLARE @DepartureDate CHAR (8)
DECLARE @TmpArrDate CHAR (8)
DECLARE @TmpDepDate CHAR (8)
DECLARE @Adults INT
DECLARE @Children1 INT
DECLARE @Children2 INT
DECLARE @SpecAdults INT
DECLARE @SpecChildren1 INT
DECLARE @SpecChildren2 INT
DECLARE @ProductNumber INT
DECLARE @SpecProductNumber INT
DECLARE @ProductNumber2 INT
DECLARE @SpecProductNumber2 INT
DECLARE @ProductNumber3 INT
DECLARE @SpecProductNumber3 INT
DECLARE @ProductNumber4 INT
DECLARE @SpecProductNumber4 INT

DECLARE @CalcTotalPrice MONEY
DECLARE @CalcArrivalPrice MONEY

DECLARE @CalcTotalPrice1 MONEY
DECLARE @CalcArrivalPrice1 MONEY
DECLARE @CalcTotalPrice2 MONEY
DECLARE @CalcArrivalPrice2 MONEY
DECLARE @CalcTotalPrice3 MONEY
DECLARE @CalcArrivalPrice3 MONEY
DECLARE @CalcTotalPrice4 MONEY
DECLARE @CalcArrivalPrice4 MONEY

DECLARE @PriceOverwritten BIT
DECLARE @SpecPriceOverwritten BIT
DECLARE @PriceOverwritten2 BIT
DECLARE @SpecPriceOverwritten2 BIT
DECLARE @PriceOverwritten3 BIT
DECLARE @SpecPriceOverwritten3 BIT
DECLARE @PriceOverwritten4 BIT
DECLARE @SpecPriceOverwritten4 BIT

DECLARE @Day2 INT
DECLARE @Day3 INT
DECLARE @Day4 INT

DECLARE @FirstPrice1 MONEY
DECLARE @FirstPrice2 MONEY
DECLARE @FirstPrice3 MONEY
DECLARE @FirstPrice4 MONEY
DECLARE @TotalPrice1 MONEY
DECLARE @TotalPrice2 MONEY
DECLARE @TotalPrice3 MONEY
DECLARE @TotalPrice4 MONEY

DECLARE @SpecArrivalPrice MONEY
DECLARE @SpecTotalPrice MONEY
DECLARE @ArrivalPrice MONEY
DECLARE @TotalPrice MONEY

DECLARE @SpecArrivalDateTime DATETIME
DECLARE @SpecDepartureDateTime DATETIME

DECLARE @TmpArrDateTime DATETIME
DECLARE @TmpDepDateTime DATETIME

DECLARE @NumberOfDays SMALLINT

DECLARE @SystemDateStr CHAR(8)
SELECT  @SystemDateStr = SYSTEMDATE FROM System  WITH (NOLOCK) WHERE NUMBER = 1  -- Only one record

Declare #RoomLineCur Cursor Forward_Only For
/*
SELECT LINENUMBER, SpecBookedTypeNumber, SpecNumberOfRooms, SpecArrivalDate, SpecDepartureDate, ArrivalDate, DepartureDate,
*/
SELECT LINENUMBER, BookedTypeNumber, SpecNumberOfRooms, SpecArrivalDate, SpecDepartureDate, ArrivalDate, DepartureDate,
Referencenumber, GuestNumber, LinkedRes, ProductNumber, SpecProductNumber FROM TMultiPurposeView WITH (NOLOCK)
WHERE ((GuestNumber = 0 and SpecArrivalDate >= @SystemDateStr) or (GuestNumber > 0 and ArrivalDate >= @SystemDateStr)) and (Status < 5 or Status is null) AND (HotelNumber=0 or HotelNumber is null)  and ReferenceNumber>0
ORDER BY LineNumber --, ReferenceNumber, GuestNumber DESC

Open #RoomLineCur

Fetch Next From #RoomLineCur
Into @LINENUMBER, @SpecBookedTypeNumber, @SpecNumberOfRooms, @SpecArrivalDate, @SpecDepartureDate, @ArrivalDate, @DepartureDate, --@Adults, @Children1, @Children2, @SpecAdults, @SpecChildren1, @SpecChildren2,
@Referencenumber, @GuestNumber, @LinkedRes, @ProductNumber, @SpecProductNumber

WHILE  (@@FETCH_STATUS <> -1)
BEGIN
  SET @CalcTotalPrice =0
  SET @CalcArrivalPrice =0
  SET @CalcTotalPrice1 =0
  SET @CalcArrivalPrice1 =0
  SET @CalcTotalPrice2 =0
  SET @CalcArrivalPrice2 =0
  SET @CalcTotalPrice3 =0
  SET @CalcArrivalPrice3 =0
  SET @CalcTotalPrice4 =0
  SET @CalcArrivalPrice4 =0

  IF @GuestNumber = 0 and (@TheProductNumber = @SpecProductNumber or @TheProductNumber = -1)
  BEGIN
     SELECT @SpecPriceOverwritten=PriceOverwritten, @SpecPriceOverwritten2=PriceOverwritten2, @SpecPriceOverwritten3=PriceOverwritten3, @SpecPriceOverwritten4=PriceOverwritten4, 
                    @SpecProductNumber2=ProductNumber2, @SpecProductNumber3=ProductNumber3, @SpecProductNumber4=ProductNumber4,
                    @Day2=Day2,@Day3=Day3,@Day4=Day4,
                    @FirstPrice1=FirstPrice1,@FirstPrice2=FirstPrice2,@FirstPrice3=FirstPrice3,@FirstPrice4=FirstPrice4,
                    @TotalPrice1=TotalPrice1,@TotalPrice2=TotalPrice2,@TotalPrice3=TotalPrice3,@TotalPrice4=TotalPrice4,
                    @SpecAdults=Adults, @SpecChildren1=Children1, @SpecChildren2=Children2, 
                    @SpecArrivalPrice=Price,@SpecTotalPrice=TotalPrice FROM ReservationRoomline WITH (NOLOCK) WHERE LineNumber=@LineNumber

     if IsNull (@SpecAdults, 0) = 0
       set @SpecAdults = 0
     if IsNull (@SpecChildren1, 0) = 0
       set @SpecChildren1 = 0
     if IsNull (@SpecChildren2, 0) = 0
       set @SpecChildren2 = 0
     if IsNull (@Day2, 0) = 0
       set @Day2 = 0
     if IsNull (@Day3, 0) = 0
       set @Day3 = 0
     if IsNull (@Day4, 0) = 0
       set @Day4 = 0
     if IsNull (@SpecPriceOverwritten, 0) = 0
       set @SpecPriceOverwritten = 0
     if IsNull (@SpecPriceOverwritten2, 0) = 0
       set @SpecPriceOverwritten2 = 0
     if IsNull (@SpecPriceOverwritten3, 0) = 0
       set @SpecPriceOverwritten3 = 0
     if IsNull (@SpecPriceOverwritten4, 0) = 0
       set @SpecPriceOverwritten4 = 0

     if IsNull (@SpecProductNumber, 0) = 0
       set @SpecProductNumber = 0
     if IsNull (@SpecProductNumber2, 0) = 0
       set @SpecProductNumber2 = 0
     if IsNull (@SpecProductNumber3, 0) = 0
       set @SpecProductNumber3 = 0
     if IsNull (@SpecProductNumber4, 0) = 0
       set @SpecProductNumber4 = 0

     if IsNull (@FirstPrice1, 0) = 0




       set @FirstPrice1 = 0
     if IsNull (@FirstPrice2, 0) = 0
       set @FirstPrice2 = 0
     if IsNull (@FirstPrice3, 0) = 0
       set @FirstPrice3 = 0
     if IsNull (@FirstPrice4, 0) = 0
       set @FirstPrice4 = 0
     if IsNull (@TotalPrice1, 0) = 0
       set @TotalPrice1 = 0
     if IsNull (@TotalPrice2, 0) = 0
       set @TotalPrice2 = 0
     if IsNull (@TotalPrice3, 0) = 0
       set @TotalPrice3 = 0
     if IsNull (@TotalPrice4, 0) = 0
       set @TotalPrice4 = 0

print '-------------------------------------------------------------------------------------------'
     print 'before1:'
     if @SpecPriceOverwritten=1
       print 'with priceoverwritten!'
     print @referencenumber
     print @guestnumber
     print @linenumber
     print ' '
     print convert (varchar, @specarrivalprice)
     print convert (varchar, @spectotalprice)

   IF @SpecAdults+@SpecChildren1+@SpecChildren2 > 0
   BEGIN
     SET @TmpArrDateTime = Convert (DATETIME, @SpecArrivalDate)
     SET @TmpDepDateTime = Convert (DATETIME, @SpecDepartureDate)

     IF @Day2=0  -- not + product
     BEGIN
       IF @SpecPriceOverwritten = 0
         EXEC ProductPrices @SpecProductNumber, @SpecArrivalDate, @SpecDepartureDate, @SpecBookedTypeNumber, @SpecNumberOfRooms, @SpecAdults, @SpecChildren1, @SpecChildren2, @CalcTotalPrice OUTPUT, @CalcArrivalPrice OUTPUT
       ELSE
       BEGIN
         SET @CalcArrivalPrice = @SpecArrivalPrice

         IF @AddOnIfPriceOverwritten = 1
         BEGIN
           SET @NumberOfDays = DATEDIFF (DAY,@TmpArrDateTime,@TmpDepDateTime)
           IF @NumberOfDays = 0 SET @NumberOfDays = 1
           EXEC CalcAddonPrice @CalcArrivalPrice, @TmpArrDateTime, @NumberOfDays, @SpecAdults, @SpecChildren1, @SpecChildren2, @SpecProductNumber, @SpecBookedTypeNumber, @CalcTotalPrice OUTPUT

           SET @CalcTotalPrice = @CalcTotalPrice*@SpecNumberOfRooms
         END
         ELSE
         BEGIN
           SET @CalcTotalPrice = @CalcArrivalPrice * DATEDIFF (DAY,@TmpArrDateTime,@TmpDepDateTime) * @SpecNumberOfRooms

           /* NameListPerPerson contra OnePersonPaysForAll */
           SELECT @NameListReservationPerPerson=NameListSetting FROM Products WITH (NOLOCK) WHERE ProductNumber=@SpecProductNumber
           IF @NameListReservationPerPerson=0 OR IsNull(@NameListReservationPerPerson, 0)=0  -- follow hotelsetup
             SET @NameListReservationPerPerson=@HSNameListReservationPerPerson

           IF ((@SpecProductNumber > 0 AND @NameListReservationPerPerson=1) or (@SpecProductNumber = 0 AND @HSNameListReservationPerPerson=1)) AND (@HSOnePersonPaysForAll=0)
             SET @CalcTotalPrice = @CalcTotalPrice*(@SpecAdults+@SpecChildren1+@SpecChildren2) -- multiply with pax
         END
       END
     END
     ELSE       -- + product
     BEGIN
print 'PLUS product1:'
       SET @TmpArrDateTime = Convert (DATETIME, @SpecArrivalDate)
--       SET @TmpDepDateTime = Convert (DATETIME, @SpecDepartureDate)+@Day2-1

         IF @Day2=0
           SET @TmpDepDateTime = Convert (DATETIME, @SpecDepartureDate)
         ELSE
           SET @TmpDepDateTime = Convert (DATETIME, @SpecArrivalDate)+@Day2-1

print 'row1'
       IF @SpecPriceOverwritten=0
         EXEC ProductPrices @SpecProductNumber, @TmpArrDateTime, @TmpDepDateTime, @SpecBookedTypeNumber, @SpecNumberOfRooms, @SpecAdults, @SpecChildren1, @SpecChildren2, @CalcTotalPrice1 OUTPUT, @CalcArrivalPrice1 OUTPUT  -- JP 100602
       ELSE
       BEGIN
         SET @CalcArrivalPrice1 = @FirstPrice1

         IF @AddOnIfPriceOverwritten = 1
         BEGIN
           SET @NumberOfDays = DATEDIFF (DAY,@TmpArrDateTime,@TmpDepDateTime)
           IF @NumberOfDays = 0 SET @NumberOfDays = 1
           EXEC CalcAddonPrice @CalcArrivalPrice1, @TmpArrDateTime, @NumberOfDays, @SpecAdults, @SpecChildren1, @SpecChildren2, @SpecProductNumber, @SpecBookedTypeNumber, @CalcTotalPrice1 OUTPUT

           SET @CalcTotalPrice1 = @CalcTotalPrice1*@SpecNumberOfRooms
         END
         ELSE
         BEGIN
           SET @CalcTotalPrice1 = @CalcArrivalPrice1 * DATEDIFF (DAY,@TmpArrDateTime,@TmpDepDateTime) *@SpecNumberOfRooms

           /* NameListPerPerson contra OnePersonPaysForAll */
           SELECT @NameListReservationPerPerson=NameListSetting FROM ReservationRoomLine WITH (NOLOCK) WHERE LineNumber=@LineNumber
           IF @NameListReservationPerPerson=0 OR IsNull(@NameListReservationPerPerson, 0)=0  -- follow hotelsetup
             SET @NameListReservationPerPerson=@HSNameListReservationPerPerson

           IF ((@SpecProductNumber > 0 AND @NameListReservationPerPerson=1) or (@SpecProductNumber = 0 AND @HSNameListReservationPerPerson=1)) AND (@HSOnePersonPaysForAll=0)
             SET @CalcTotalPrice1 = @CalcTotalPrice1*(@SpecAdults+@SpecChildren1+@SpecChildren2) -- multiply with pax
         END
       END

       IF @Day2>0
       BEGIN
         SET @TmpArrDateTime = Convert (DATETIME, @SpecArrivalDate)+@Day2-1
         IF @Day3=0
           SET @TmpDepDateTime = Convert (DATETIME, @SpecDepartureDate)
         ELSE
           SET @TmpDepDateTime = Convert (DATETIME, @SpecArrivalDate)+@Day3-1

print 'row2'
         IF @SpecPriceOverwritten2=0
           EXEC ProductPrices @SpecProductNumber2, @TmpArrDateTime, @TmpDepDateTime, @SpecBookedTypeNumber, @SpecNumberOfRooms, @SpecAdults, @SpecChildren1, @SpecChildren2, @CalcTotalPrice2 OUTPUT, @CalcArrivalPrice2 OUTPUT  -- JP 100602
         ELSE
         BEGIN
           SET @CalcArrivalPrice2 = @FirstPrice2

           IF @AddOnIfPriceOverwritten = 1
           BEGIN
             SET @NumberOfDays = DATEDIFF (DAY,@TmpArrDateTime,@TmpDepDateTime)
             IF @NumberOfDays = 0 SET @NumberOfDays = 1
             EXEC CalcAddonPrice @CalcArrivalPrice2, @TmpArrDateTime, @NumberOfDays, @SpecAdults, @SpecChildren1, @SpecChildren2, @SpecProductNumber2, @SpecBookedTypeNumber, @CalcTotalPrice2 OUTPUT

             SET @CalcTotalPrice2 = @CalcTotalPrice2*@SpecNumberOfRooms
           END
           ELSE
           BEGIN
             SET @CalcTotalPrice2 = @CalcArrivalPrice2 * DATEDIFF (DAY,@TmpArrDateTime,@TmpDepDateTime) *@SpecNumberOfRooms

             /* NameListPerPerson contra OnePersonPaysForAll */
             SELECT @NameListReservationPerPerson=NameListSetting FROM ReservationRoomLine WITH (NOLOCK) WHERE LineNumber=@LineNumber
             IF @NameListReservationPerPerson=0 OR IsNull(@NameListReservationPerPerson, 0)=0  -- follow hotelsetup
               SET @NameListReservationPerPerson=@HSNameListReservationPerPerson

             IF ((@SpecProductNumber2 > 0 AND @NameListReservationPerPerson=1) or (@SpecProductNumber2 = 0 AND @HSNameListReservationPerPerson=1)) AND (@HSOnePersonPaysForAll=0)
               SET @CalcTotalPrice2 = @CalcTotalPrice2*(@SpecAdults+@SpecChildren1+@SpecChildren2) -- multiply with pax
           END
         END
       END
 
       IF @Day3>0
       BEGIN
         SET @TmpArrDateTime = Convert (DATETIME, @SpecArrivalDate)+@Day3-1
         IF @Day4=0
           SET @TmpDepDateTime = Convert (DATETIME, @SpecDepartureDate)
         ELSE
           SET @TmpDepDateTime = Convert (DATETIME, @SpecArrivalDate)+@Day4-1

print 'row3'
         IF @SpecPriceOverwritten3=0
           EXEC ProductPrices @SpecProductNumber3, @TmpArrDateTime, @TmpDepDateTime, @SpecBookedTypeNumber, @SpecNumberOfRooms, @SpecAdults, @SpecChildren1, @SpecChildren2, @CalcTotalPrice3 OUTPUT, @CalcArrivalPrice3 OUTPUT  -- JP 100602
         ELSE
         BEGIN
           SET @CalcArrivalPrice3 = @FirstPrice3

           IF @AddOnIfPriceOverwritten = 1
           BEGIN
             SET @NumberOfDays = DATEDIFF (DAY,@TmpArrDateTime,@TmpDepDateTime)
             IF @NumberOfDays = 0 SET @NumberOfDays = 1
             EXEC CalcAddonPrice @CalcArrivalPrice3, @TmpArrDateTime, @NumberOfDays, @SpecAdults, @SpecChildren1, @SpecChildren2, @SpecProductNumber3, @SpecBookedTypeNumber, @CalcTotalPrice3 OUTPUT

             SET @CalcTotalPrice3 = @CalcTotalPrice3*@SpecNumberOfRooms
           END
           ELSE
           BEGIN
             SET @CalcTotalPrice3 = @CalcArrivalPrice3 * DATEDIFF (DAY,@TmpArrDateTime,@TmpDepDateTime) *@SpecNumberOfRooms

             /* NameListPerPerson contra OnePersonPaysForAll */
             SELECT @NameListReservationPerPerson=NameListSetting FROM ReservationRoomLine WITH (NOLOCK) WHERE LineNumber=@LineNumber
             IF @NameListReservationPerPerson=0 OR IsNull(@NameListReservationPerPerson, 0)=0  -- follow hotelsetup
               SET @NameListReservationPerPerson=@HSNameListReservationPerPerson

             IF ((@SpecProductNumber3 > 0 AND @NameListReservationPerPerson=1) or (@SpecProductNumber3 = 0 AND @HSNameListReservationPerPerson=1)) AND (@HSOnePersonPaysForAll=0)
               SET @CalcTotalPrice3 = @CalcTotalPrice3*(@SpecAdults+@SpecChildren1+@SpecChildren2) -- multiply with pax
           END
         END
       END

       IF @Day4>0
       BEGIN
         SET @TmpArrDateTime = Convert (DATETIME, @SpecArrivalDate)+@Day4-1
         SET @TmpDepDateTime = Convert (DATETIME, @SpecDepartureDate)
print 'row4'
         IF @SpecPriceOverwritten4=0
           EXEC ProductPrices @SpecProductNumber4, @TmpArrDateTime, @TmpDepDateTime, @SpecBookedTypeNumber, @SpecNumberOfRooms, @SpecAdults, @SpecChildren1, @SpecChildren2, @CalcTotalPrice4 OUTPUT, @CalcArrivalPrice4 OUTPUT  -- JP 100602
         ELSE
         BEGIN
           SET @CalcArrivalPrice4 = @FirstPrice4

           IF @AddOnIfPriceOverwritten = 1
           BEGIN
             SET @NumberOfDays = DATEDIFF (DAY,@TmpArrDateTime,@TmpDepDateTime)
             IF @NumberOfDays = 0 SET @NumberOfDays = 1
             EXEC CalcAddonPrice @CalcArrivalPrice4, @TmpArrDateTime, @NumberOfDays, @SpecAdults, @SpecChildren1, @SpecChildren2, @SpecProductNumber4, @SpecBookedTypeNumber, @CalcTotalPrice4 OUTPUT

             SET @CalcTotalPrice4 = @CalcTotalPrice4*@SpecNumberOfRooms
           END
           ELSE
           BEGIN
             SET @CalcTotalPrice4 = @CalcArrivalPrice4 * DATEDIFF (DAY,@TmpArrDateTime,@TmpDepDateTime) *@SpecNumberOfRooms

             /* NameListPerPerson contra OnePersonPaysForAll */
             SELECT @NameListReservationPerPerson=NameListSetting FROM ReservationRoomLine WITH (NOLOCK) WHERE LineNumber=@LineNumber
             IF @NameListReservationPerPerson=0 OR IsNull(@NameListReservationPerPerson, 0)=0  -- follow hotelsetup
               SET @NameListReservationPerPerson=@HSNameListReservationPerPerson

             IF ((@SpecProductNumber4 > 0 AND @NameListReservationPerPerson=1) or (@SpecProductNumber4 = 0 AND @HSNameListReservationPerPerson=1)) AND (@HSOnePersonPaysForAll=0)
               SET @CalcTotalPrice4 = @CalcTotalPrice4*(@SpecAdults+@SpecChildren1+@SpecChildren2) -- multiply with pax
           END
         END
       END
       print '+product calced prices:'
       print convert (varchar, @CalcArrivalPrice1)
       print convert (varchar, @CalcTotalPrice1)
       print convert (varchar, @CalcArrivalPrice2)
       print convert (varchar, @CalcTotalPrice2)
       print convert (varchar, @CalcArrivalPrice3)
       print convert (varchar, @CalcTotalPrice3)
       print convert (varchar, @CalcArrivalPrice4)
       print convert (varchar, @CalcTotalPrice4)


       SET @CalcArrivalPrice=@CalcArrivalPrice1
       SET @CalcTotalPrice=@CalcTotalPrice1+@CalcTotalPrice2+@CalcTotalPrice3+@CalcTotalPrice4

        IF @OnlyNotPriceOverwritten=0 or (@PriceOverwritten=0 and @OnlyNotPriceOverwritten=1)
        BEGIN
          UPDATE ReservationRoomLine SET FirstPrice1=@CalcArrivalPrice1, TotalPrice1=@CalcTotalPrice1, FirstPrice2=@CalcArrivalPrice2, TotalPrice2=@CalcTotalPrice2, FirstPrice3=@CalcArrivalPrice3, TotalPrice3=@CalcTotalPrice3, FirstPrice4=@CalcArrivalPrice4, TotalPrice4=@CalcTotalPrice4 WHERE LineNumber = @LineNumber

         if @FirstPrice1<>@CalcArrivalPrice1 or @TotalPrice1<>@CalcTotalPrice1
           INSERT INTO #Log (ReferenceNumber, GuestNumber, LineNumber, OldArrivalPrice, OldTotalPrice, NewArrivalPrice, NewTotalPrice, Descrip) VALUES (@ReferenceNumber, @GuestNumber, @LineNumber, @FirstPrice1, @TotalPrice1, @CalcArrivalPrice1, @CalcTotalPrice1, 'RoomLine.+Product Row1')
         if @FirstPrice2<>@CalcArrivalPrice2 or @TotalPrice2<>@CalcTotalPrice2
           INSERT INTO #Log (ReferenceNumber, GuestNumber, LineNumber, OldArrivalPrice, OldTotalPrice, NewArrivalPrice, NewTotalPrice, Descrip) VALUES (@ReferenceNumber, @GuestNumber, @LineNumber, @FirstPrice2, @TotalPrice2, @CalcArrivalPrice2, @CalcTotalPrice2, 'RoomLine.+Product Row2')
         if @FirstPrice3<>@CalcArrivalPrice3 or @TotalPrice3<>@CalcTotalPrice3
           INSERT INTO #Log (ReferenceNumber, GuestNumber, LineNumber, OldArrivalPrice, OldTotalPrice, NewArrivalPrice, NewTotalPrice, Descrip) VALUES (@ReferenceNumber, @GuestNumber, @LineNumber, @FirstPrice3, @TotalPrice3, @CalcArrivalPrice3, @CalcTotalPrice3, 'RoomLine.+Product Row3')
         if @FirstPrice4<>@CalcArrivalPrice4 or @TotalPrice4<>@CalcTotalPrice4
           INSERT INTO #Log (ReferenceNumber, GuestNumber, LineNumber, OldArrivalPrice, OldTotalPrice, NewArrivalPrice, NewTotalPrice, Descrip) VALUES (@ReferenceNumber, @GuestNumber, @LineNumber, @FirstPrice4, @TotalPrice4, @CalcArrivalPrice4, @CalcTotalPrice4, 'RoomLine.+Product Row4')
        END
     END
   END

     print 'after1:'
     print convert (varchar, @CalcArrivalPrice)
     print convert (varchar, @CalcTotalPrice)
    
     IF @CalcArrivalPrice<>@specarrivalprice or @CalcTotalPrice<>@spectotalprice
     BEGIN
       IF @OnlyNotPriceOverwritten=0 or (@SpecPriceOverwritten=0 and @OnlyNotPriceOverwritten=1)
       BEGIN
         print '!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!DIFFERENT 1 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!'
         UPDATE ReservationRoomLine SET Price = @CalcArrivalPrice, TotalPrice = @CalcTotalPrice WHERE LineNumber = @LineNumber
         INSERT INTO #Log (ReferenceNumber, GuestNumber, LineNumber, OldArrivalPrice, OldTotalPrice, NewArrivalPrice, NewTotalPrice, Descrip) VALUES (@ReferenceNumber, @GuestNumber, @LineNumber, @SpecArrivalPrice, @SpecTotalPrice, @CalcArrivalPrice, @CalcTotalPrice, 'RoomLine')
       END
     END
  END
  ELSE IF @GuestNumber > 0 and (@TheProductNumber = @ProductNumber or @TheProductNumber = -1)
  BEGIN
     SELECT @PriceOverwritten = PriceOverwritten, @PriceOverwritten2=PriceOverwritten2, @PriceOverwritten3=PriceOverwritten3, @PriceOverwritten4=PriceOverwritten4,
                    @ProductNumber2=ProductNumber2, @ProductNumber3=ProductNumber3, @ProductNumber4=ProductNumber4,
                    @Day2=Day2,@Day3=Day3,@Day4=Day4,
                    @FirstPrice1=FirstPrice1,@FirstPrice2=FirstPrice2,@FirstPrice3=FirstPrice3,@FirstPrice4=FirstPrice4,
                    @TotalPrice1=TotalPrice1,@TotalPrice2=TotalPrice2,@TotalPrice3=TotalPrice3,@TotalPrice4=TotalPrice4,
                    @Adults=PaysAdults, @Children1=PaysChildren1, @Children2=PaysChildren2, 
                    @arrivalprice=price, @totalprice = totalprice from reservations WITH (nolock) where referencenumber=@referencenumber and guestnumber=@guestnumber

     -- IF Namelist IS NOT per person THEN dont use paysadults etc for calculation
--     SELECT @NameListReservationPerPerson = Convert (INT, Keyvalue) FROM HotelSetup (NOLOCK) WHERE Upper (SearchKey) =Upper ('NameListReservationPerPerson')
--     IF @HSNameListReservationPerPerson <> 1
--       SELECT @Adults=Adults, @Children1=Children1, @Children2=Children2 FROM Reservations (NOLOCK) WHERE ReferenceNumber=@ReferenceNumber and GuestNumber=@GuestNumber

    -- read PAX from reservation if NOT namelist per person... read Period if namelist per person since it's linked!
    IF @Day2=0 -- not plus product
      IF @ProductNumber > 0
         SELECT @NameListReservationPerPerson=NameListSetting FROM Products WITH (NOLOCK) WHERE ProductNumber=@SpecProductNumber         
      ELSE
         SET @NameListReservationPerPerson = @HSNameListReservationPerPerson
    ELSE
       SELECT @NameListReservationPerPerson=NameListSetting FROM ReservationRoomLine WITH (NOLOCK) WHERE LineNumber=@LineNumber

     IF IsNull(@NameListReservationPerPerson, 0) = 0
       SET @NameListReservationPerPerson = @HSNameListReservationPerPerson 

     IF @NameListReservationPerPerson <> 1  -- NOT per person
       SELECT @Adults=Adults, @Children1=Children1, @Children2=Children2 FROM Reservations WITH (NOLOCK) WHERE ReferenceNumber=@ReferenceNumber and GuestNumber=@GuestNumber
     ELSE
       SELECT @ArrivalDate=ArrivalDate, @DepartureDate=DepartureDate FROM ReservationRoomLine WITH (NOLOCK) WHERE LineNumber=@LineNumber

     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 (@Day2, 0) = 0
       set @Day2 = 0
     if IsNull (@Day3, 0) = 0
       set @Day3 = 0
     if IsNull (@Day4, 0) = 0
       set @Day4 = 0
     if IsNull (@PriceOverwritten, 0) = 0
       set @PriceOverwritten = 0
     if IsNull (@PriceOverwritten2, 0) = 0
       set @PriceOverwritten2 = 0
     if IsNull (@PriceOverwritten3, 0) = 0
       set @PriceOverwritten3 = 0
     if IsNull (@PriceOverwritten4, 0) = 0
       set @PriceOverwritten4 = 0

     if IsNull (@ProductNumber, 0) = 0
       set @ProductNumber = 0
     if IsNull (@ProductNumber2, 0) = 0
       set @ProductNumber2 = 0
     if IsNull (@ProductNumber3, 0) = 0
       set @ProductNumber3 = 0
     if IsNull (@ProductNumber4, 0) = 0
       set @ProductNumber4 = 0

     if IsNull (@FirstPrice1, 0) = 0
       set @FirstPrice1 = 0
     if IsNull (@FirstPrice2, 0) = 0
       set @FirstPrice2 = 0
     if IsNull (@FirstPrice3, 0) = 0
       set @FirstPrice3 = 0
     if IsNull (@FirstPrice4, 0) = 0
       set @FirstPrice4 = 0
     if IsNull (@TotalPrice1, 0) = 0
       set @TotalPrice1 = 0
     if IsNull (@TotalPrice2, 0) = 0
       set @TotalPrice2 = 0
     if IsNull (@TotalPrice3, 0) = 0
       set @TotalPrice3 = 0
     if IsNull (@TotalPrice4, 0) = 0
       set @TotalPrice4 = 0

print '-------------------------------------------------------------------------------------------'
     print 'before2:'
     if @PriceOverwritten=1
       print 'with priceoverwritten!'
     print @referencenumber
     print @guestnumber
     print @linenumber
     print ' '
     print convert (varchar, @arrivalprice)
     print convert (varchar, @totalprice)
print @ArrivalDate
print @departureDate
--print @bookedtypenumber
--print @adults
--print @children1
--print @children2
   IF @Adults+@Children1+@Children2 > 0
   BEGIN
     SET @TmpArrDateTime = Convert (DATETIME, @ArrivalDate)
     SET @TmpDepDateTime = Convert (DATETIME, @DepartureDate)

     IF @Day2=0  -- not + product
     BEGIN
       IF @PriceOverwritten = 0
begin
print @productnumber
print @adults
print @children1
print @children2
         EXEC ProductPrices @ProductNumber, @TmpArrDateTime, @TmpDepDateTime, @SpecBookedTypeNumber, 1, @Adults, @Children1, @Children2, @CalcTotalPrice OUTPUT, @CalcArrivalPrice OUTPUT
end
       ELSE
       BEGIN
         SET @CalcArrivalPrice = @ArrivalPrice

         IF @AddOnIfPriceOverwritten = 1
         BEGIN
           SET @NumberOfDays = DATEDIFF (DAY,@TmpArrDateTime,@TmpDepDateTime)
           IF @NumberOfDays = 0 SET @NumberOfDays = 1
           EXEC CalcAddonPrice @CalcArrivalPrice, @TmpArrDateTime, @NumberOfDays, @Adults, @Children1, @Children2, @ProductNumber, @SpecBookedTypeNumber, @CalcTotalPrice OUTPUT

--           SET @CalcTotalPrice = @CalcTotalPrice*@SpecNumberOfRooms
         END
         ELSE
         BEGIN
          SET @CalcTotalPrice = @CalcArrivalPrice * DATEDIFF (DAY,@TmpArrDateTime,@TmpDepDateTime) 

          /* NameListPerPerson contra OnePersonPaysForAll */
          SELECT @NameListReservationPerPerson=NameListSetting FROM Products WITH (NOLOCK) WHERE ProductNumber=@ProductNumber
          IF @NameListReservationPerPerson=0 OR IsNull(@NameListReservationPerPerson, 0)=0  -- follow hotelsetup
            SET @NameListReservationPerPerson=@HSNameListReservationPerPerson

          IF ((@ProductNumber > 0 AND @NameListReservationPerPerson=1) or (@ProductNumber = 0 AND @HSNameListReservationPerPerson=1)) AND (@HSOnePersonPaysForAll=0)
            SET @CalcTotalPrice = @CalcTotalPrice*(@Adults+@Children1+@Children2) -- multiply with pax
         END
       END 
     END
     ELSE        -- + product
     BEGIN
print '+product2:'
       SET @TmpArrDateTime = @TmpArrDateTime
--       SET @TmpDepDateTime = @TmpArrDateTime+@Day2-1
       IF @Day2=0
         SET @TmpDepDateTime = Convert (DATETIME, @TmpDepDateTime)
       ELSE
         SET @TmpDepDateTime = Convert (DATETIME, @TmpArrDateTime)+@Day2-1

       IF @PriceOverwritten=0
         EXEC ProductPrices @ProductNumber, @TmpArrDateTime, @TmpDepDateTime, @SpecBookedTypeNumber, 1, @Adults, @Children1, @Children2, @CalcTotalPrice1 OUTPUT, @CalcArrivalPrice1 OUTPUT   -- JP 100602
       ELSE
       BEGIN
         SET @CalcArrivalPrice1 = @FirstPrice1

         IF @AddOnIfPriceOverwritten = 1
         BEGIN
           SET @NumberOfDays = DATEDIFF (DAY,@TmpArrDateTime,@TmpDepDateTime)
           IF @NumberOfDays = 0 SET @NumberOfDays = 1
           EXEC CalcAddonPrice @CalcArrivalPrice1, @TmpArrDateTime, @NumberOfDays, @Adults, @Children1, @Children2, @ProductNumber, @SpecBookedTypeNumber, @CalcTotalPrice1 OUTPUT

--           SET @CalcTotalPrice1 = @CalcTotalPrice1*@SpecNumberOfRooms
         END
         ELSE
         BEGIN
           SET @CalcTotalPrice1 = @CalcArrivalPrice1 * DATEDIFF (DAY,@TmpArrDateTime,@TmpDepDateTime)

            /* NameListPerPerson contra OnePersonPaysForAll */
            SELECT @NameListReservationPerPerson=NameListSetting FROM Products WITH (NOLOCK) WHERE ProductNumber=@ProductNumber
            IF @NameListReservationPerPerson=0 OR IsNull(@NameListReservationPerPerson, 0)=0  -- follow hotelsetup
              SET @NameListReservationPerPerson=@HSNameListReservationPerPerson

            IF ((@ProductNumber > 0 AND @NameListReservationPerPerson=1) or (@ProductNumber = 0 AND @HSNameListReservationPerPerson=1)) AND (@HSOnePersonPaysForAll=0)
              SET @CalcTotalPrice1 = @CalcTotalPrice1*(@Adults+@Children1+@Children2) -- multiply with pax
         END
       END

       IF @Day2>0
       BEGIN
         SET @TmpArrDateTime = Convert (DATETIME, @ArrivalDate)+@Day2-1
         IF @Day3=0
           SET @TmpDepDateTime = Convert (DATETIME, @DepartureDate)
         ELSE
           SET @TmpDepDateTime = Convert (DATETIME, @ArrivalDate)+@Day3-1

         IF @PriceOverwritten2=0
           EXEC ProductPrices @ProductNumber2, @TmpArrDateTime, @TmpDepDateTime, @SpecBookedTypeNumber, 1, @Adults, @Children1, @Children2, @CalcTotalPrice2 OUTPUT, @CalcArrivalPrice2 OUTPUT   -- JP 100602
         ELSE
         BEGIN
           SET @CalcArrivalPrice2 = @FirstPrice2

           IF @AddOnIfPriceOverwritten = 1
           BEGIN
             SET @NumberOfDays = DATEDIFF (DAY,@TmpArrDateTime,@TmpDepDateTime)
             IF @NumberOfDays = 0 SET @NumberOfDays = 1
             EXEC CalcAddonPrice @CalcArrivalPrice2, @TmpArrDateTime, @NumberOfDays, @Adults, @Children1, @Children2, @ProductNumber2, @SpecBookedTypeNumber, @CalcTotalPrice2 OUTPUT

--             SET @CalcTotalPrice2 = @CalcTotalPrice2 * @SpecNumberOfRooms
           END
           ELSE
           BEGIN
             SET @CalcTotalPrice2 = @CalcArrivalPrice2 * DATEDIFF(DAY, @TmpArrDateTime, @TmpDepDateTime)

            /* NameListPerPerson contra OnePersonPaysForAll */
            SELECT @NameListReservationPerPerson=NameListSetting FROM Products WITH (NOLOCK) WHERE ProductNumber=@ProductNumber
            IF @NameListReservationPerPerson=0 OR IsNull(@NameListReservationPerPerson, 0)=0  -- follow hotelsetup
              SET @NameListReservationPerPerson=@HSNameListReservationPerPerson

            IF ((@ProductNumber > 0 AND @NameListReservationPerPerson=1) or (@ProductNumber = 0 AND @HSNameListReservationPerPerson=1)) AND (@HSOnePersonPaysForAll=0)
              SET @CalcTotalPrice2 = @CalcTotalPrice2*(@Adults+@Children1+@Children2) -- multiply with pax
           END
         END
       END
 
       IF @Day3>0
       BEGIN
         SET @TmpArrDateTime = Convert (DATETIME, @ArrivalDate)+@Day3-1
         IF @Day4=0
           SET @TmpDepDateTime = Convert (DATETIME, @DepartureDate)
         ELSE
           SET @TmpDepDateTime = Convert (DATETIME, @ArrivalDate)+@Day4-1

         IF @PriceOverwritten3=0
           EXEC ProductPrices @ProductNumber3, @TmpArrDateTime, @TmpDepDateTime, @SpecBookedTypeNumber, 1, @Adults, @Children1, @Children2, @CalcTotalPrice3 OUTPUT, @CalcArrivalPrice3 OUTPUT  -- JP 100602
         ELSE
         BEGIN
           SET @CalcArrivalPrice3 = @FirstPrice3

           IF @AddOnIfPriceOverwritten = 1
           BEGIN
             SET @NumberOfDays = DATEDIFF (DAY,@TmpArrDateTime,@TmpDepDateTime)
             IF @NumberOfDays = 0 SET @NumberOfDays = 1
             EXEC CalcAddonPrice @CalcArrivalPrice3, @TmpArrDateTime, @NumberOfDays, @Adults, @Children1, @Children2, @ProductNumber3, @SpecBookedTypeNumber, @CalcTotalPrice3 OUTPUT

--             SET @CalcTotalPrice3 = @CalcTotalPrice3 * @SpecNumberOfRooms
           END
           ELSE
           BEGIN
             SET @CalcTotalPrice3 = @CalcArrivalPrice3 * DATEDIFF (DAY,@TmpArrDateTime,@TmpDepDateTime)

            /* NameListPerPerson contra OnePersonPaysForAll */
            SELECT @NameListReservationPerPerson=NameListSetting FROM Products WITH (NOLOCK) WHERE ProductNumber=@ProductNumber
            IF @NameListReservationPerPerson=0 OR IsNull(@NameListReservationPerPerson, 0)=0  -- follow hotelsetup
              SET @NameListReservationPerPerson=@HSNameListReservationPerPerson

            IF ((@ProductNumber > 0 AND @NameListReservationPerPerson=1) or (@ProductNumber = 0 AND @HSNameListReservationPerPerson=1)) AND (@HSOnePersonPaysForAll=0)
              SET @CalcTotalPrice3 = @CalcTotalPrice3*(@Adults+@Children1+@Children2) -- multiply with pax
           END
         END
       END

       IF @Day4>0
       BEGIN
         SET @TmpArrDate = @ArrivalDate+@Day4-1
         SET @TmpDepDate = @DepartureDate

         SET @TmpArrDateTime = Convert (DATETIME, @TmpArrDate)
         SET @TmpDepDateTime = Convert (DATETIME, @TmpDepDate)


         IF @PriceOverwritten4=0
           EXEC ProductPrices @ProductNumber3, @TmpArrDateTime, @TmpDepDateTime, @SpecBookedTypeNumber, 1, @Adults, @Children1, @Children2, @CalcTotalPrice4 OUTPUT, @CalcArrivalPrice4 OUTPUT  -- JP 100602
         ELSE
         BEGIN
           SET @CalcArrivalPrice4 = @FirstPrice4

           IF @AddOnIfPriceOverwritten = 1
           BEGIN
             SET @NumberOfDays = DATEDIFF (DAY,@TmpArrDateTime,@TmpDepDateTime)
             IF @NumberOfDays = 0 SET @NumberOfDays = 1
             EXEC CalcAddonPrice @CalcArrivalPrice4, @TmpArrDateTime, @NumberOfDays, @Adults, @Children1, @Children2, @ProductNumber4, @SpecBookedTypeNumber, @CalcTotalPrice4 OUTPUT

--             SET @CalcTotalPrice4 = @CalcTotalPrice4 * @SpecNumberOfRooms
           END
           ELSE
           BEGIN
             SET @CalcTotalPrice4 = @CalcArrivalPrice4 * DATEDIFF (DAY,@TmpArrDateTime,@TmpDepDateTime)

            /* NameListPerPerson contra OnePersonPaysForAll */
            SELECT @NameListReservationPerPerson=NameListSetting FROM Products WITH (NOLOCK) WHERE ProductNumber=@ProductNumber
            IF @NameListReservationPerPerson=0 OR IsNull(@NameListReservationPerPerson, 0)=0  -- follow hotelsetup
              SET @NameListReservationPerPerson=@HSNameListReservationPerPerson

            IF ((@ProductNumber > 0 AND @NameListReservationPerPerson=1) or (@ProductNumber = 0 AND @HSNameListReservationPerPerson=1)) AND (@HSOnePersonPaysForAll=0)
              SET @CalcTotalPrice4 = @CalcTotalPrice4*(@Adults+@Children1+@Children2) -- multiply with pax
           END
         END
       END

       print '+product calced prices:'
       print convert (varchar, @CalcArrivalPrice1)
       print convert (varchar, @CalcTotalPrice1)
       print convert (varchar, @CalcArrivalPrice2)
       print convert (varchar, @CalcTotalPrice2)
       print convert (varchar, @CalcArrivalPrice3)
       print convert (varchar, @CalcTotalPrice3)
       print convert (varchar, @CalcArrivalPrice4)
       print convert (varchar, @CalcTotalPrice4)

       SET @CalcArrivalPrice=@CalcArrivalPrice1
       SET @CalcTotalPrice=@CalcTotalPrice1+@CalcTotalPrice2+@CalcTotalPrice3+@CalcTotalPrice4
       IF @OnlyNotPriceOverwritten=0 or (@PriceOverwritten=0 and @OnlyNotPriceOverwritten=1)
       BEGIN
         UPDATE Reservations SET FirstPrice1=@CalcArrivalPrice1, TotalPrice1=@CalcTotalPrice1, FirstPrice2=@CalcArrivalPrice2, TotalPrice2=@CalcTotalPrice2, FirstPrice3=@CalcArrivalPrice3, TotalPrice3=@CalcTotalPrice3, FirstPrice4=@CalcArrivalPrice4, TotalPrice4=@CalcTotalPrice4 WHERE ReferenceNumber = @ReferenceNumber AND GuestNumber = @GuestNumber

         if @FirstPrice1<>@CalcArrivalPrice1 or @TotalPrice1<>@CalcTotalPrice1
           INSERT INTO #Log (ReferenceNumber, GuestNumber, LineNumber, OldArrivalPrice, OldTotalPrice, NewArrivalPrice, NewTotalPrice, Descrip) VALUES (@ReferenceNumber, @GuestNumber, @LineNumber, @FirstPrice1, @TotalPrice1, @CalcArrivalPrice1, @CalcTotalPrice1, 'Reservation.+Product Row1')
         if @FirstPrice2<>@CalcArrivalPrice2 or @TotalPrice2<>@CalcTotalPrice2
           INSERT INTO #Log (ReferenceNumber, GuestNumber, LineNumber, OldArrivalPrice, OldTotalPrice, NewArrivalPrice, NewTotalPrice, Descrip) VALUES (@ReferenceNumber, @GuestNumber, @LineNumber, @FirstPrice2, @TotalPrice2, @CalcArrivalPrice2, @CalcTotalPrice2, 'Reservation.+Product Row2')
         if @FirstPrice3<>@CalcArrivalPrice3 or @TotalPrice3<>@CalcTotalPrice3
           INSERT INTO #Log (ReferenceNumber, GuestNumber, LineNumber, OldArrivalPrice, OldTotalPrice, NewArrivalPrice, NewTotalPrice, Descrip) VALUES (@ReferenceNumber, @GuestNumber, @LineNumber, @FirstPrice3, @TotalPrice3, @CalcArrivalPrice3, @CalcTotalPrice3, 'Reservation.+Product Row3')
         if @FirstPrice4<>@CalcArrivalPrice4 or @TotalPrice4<>@CalcTotalPrice4
           INSERT INTO #Log (ReferenceNumber, GuestNumber, LineNumber, OldArrivalPrice, OldTotalPrice, NewArrivalPrice, NewTotalPrice, Descrip) VALUES (@ReferenceNumber, @GuestNumber, @LineNumber, @FirstPrice4, @TotalPrice4, @CalcArrivalPrice4, @CalcTotalPrice4, 'Reservation.+Product Row4')
       END
     END
   END

     print 'after2:'
     print convert (varchar, @CalcArrivalPrice)
     print convert (varchar, @CalcTotalPrice)
print @PriceOverwritten

     IF convert (varchar, @CalcArrivalPrice) <> convert (varchar, @arrivalprice) or convert (varchar, @CalcTotalPrice) <> convert (varchar, @totalprice)
     BEGIN
       IF @OnlyNotPriceOverwritten=0 or (@PriceOverwritten=0 and @OnlyNotPriceOverwritten=1)
       BEGIN
         print '!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!DIFFERENT 2 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!'
         UPDATE Reservations SET Price = @CalcArrivalPrice, TotalPrice = @CalcTotalPrice WHERE ReferenceNumber = @ReferenceNumber AND GuestNumber = @GuestNumber
         INSERT INTO #Log (ReferenceNumber, GuestNumber, LineNumber, OldArrivalPrice, OldTotalPrice, NewArrivalPrice, NewTotalPrice, Descrip) VALUES (@ReferenceNumber, @GuestNumber, @LineNumber, @ArrivalPrice, @TotalPrice, @CalcArrivalPrice, @CalcTotalPrice, 'Reservation')
       END
     END

     IF @GuestNumber > 0
     BEGIN
       SELECT @CalcArrivalPrice = SUM (Price), @CalcTotalPrice = SUM (TotalPrice)  FROM Reservations WHERE ReferenceNumber = @ReferenceNumber AND GuestNumber > 0 AND Status < 7
print 'update main res:'
     print convert (varchar, @CalcArrivalPrice)
     print convert (varchar, @CalcTotalPrice)

       SELECT @ArrivalPrice = Price, @TotalPrice = TotalPrice FROM Reservations WITH (NOLOCK) WHERE ReferenceNumber = @ReferenceNumber AND GuestNumber = 0  -- for log

       if IsNull (@ArrivalPrice, 0) = 0
         SET @ArrivalPrice = 0
       if IsNull (@TotalPrice, 0) = 0
         SET @TotalPrice = 0

       IF @CalcArrivalPrice<>@ArrivalPrice OR @CalcTotalPrice<>@TotalPrice
       BEGIN
         IF @OnlyNotPriceOverwritten=0 or (@PriceOverwritten=0 and @OnlyNotPriceOverwritten=1)
         BEGIN
           UPDATE Reservations SET Price = @CalcArrivalPrice, TotalPrice = @CalcTotalPrice WHERE ReferenceNumber = @ReferenceNumber AND GuestNumber = 0
           INSERT INTO #Log (ReferenceNumber, GuestNumber, LineNumber, OldArrivalPrice, OldTotalPrice, NewArrivalPrice, NewTotalPrice, Descrip) VALUES (@ReferenceNumber, 0, @LineNumber, @ArrivalPrice, @TotalPrice, @CalcArrivalPrice, @CalcTotalPrice, 'Reservation0')
         END
       END

       SELECT @CalcArrivalPrice = SUM (Price), @CalcTotalPrice = SUM (TotalPrice)  FROM Reservations WHERE ReferenceNumber = @ReferenceNumber AND GuestNumber IN (SELECT GuestNumber FROM ReservationRoomLineXTab WITH (NOLOCK) WHERE LineNumber=@LineNumber) AND Status < 7
       SELECT @ArrivalPrice = Price, @TotalPrice = TotalPrice FROM ReservationRoomLine WITH (NOLOCK) WHERE LineNumber = @LineNumber  -- for log

       if IsNull (@ArrivalPrice, 0) = 0
         SET @ArrivalPrice = 0
       if IsNull (@TotalPrice, 0) = 0
         SET @TotalPrice = 0

       IF @CalcArrivalPrice<>@ArrivalPrice OR @CalcTotalPrice<>@TotalPrice
       BEGIN
print 'update roomline:'
print @linenumber
     print convert (varchar, @CalcArrivalPrice)
     print convert (varchar, @CalcTotalPrice)
         IF @OnlyNotPriceOverwritten=0 or (@PriceOverwritten=0 and @OnlyNotPriceOverwritten=1) 
         BEGIN
           UPDATE ReservationRoomLine SET Price = @CalcArrivalPrice, TotalPrice = @CalcTotalPrice WHERE LineNumber = @LineNumber
           INSERT INTO #Log (ReferenceNumber, GuestNumber, LineNumber, OldArrivalPrice, OldTotalPrice, NewArrivalPrice, NewTotalPrice, Descrip) VALUES (@ReferenceNumber, 0, @LineNumber, @ArrivalPrice, @TotalPrice, @CalcArrivalPrice, @CalcTotalPrice, 'RoomLine0')
         END
       END
     END
  END 

  Fetch Next From #RoomLineCur
  Into @LINENUMBER, @SpecBookedTypeNumber, @SpecNumberOfRooms, @SpecArrivalDate, @SpecDepartureDate, @ArrivalDate, @DepartureDate, --@Adults, @Children1, @Children2, @SpecAdults, @SpecChildren1, @SpecChildren2,
  @Referencenumber, @GuestNumber, @LinkedRes, @ProductNumber, @SpecProductNumber
END

Close #RoomLineCur
Deallocate #RoomLineCur

SELECT * FROM #Log ORDER BY ReferenceNumber, GuestNumber, LineNumber

