CREATE PROCEDURE [dbo].[GetPriceProductOnline] @Price MONEY OUTPUT, @PriceArrival MONEY OUTPUT, @Tax MONEY OUTPUT, @TaxArrival MONEY OUTPUT
  , @SegmentNumber INT, @PointofSaleNumber INT, @DayProduct INT, @ProductNumber INT, @StartDate DATETIME, @EndDate DATETIME
  , @BookedTypeNumber INT, @NumberOfRooms INT, @AdultPax INT, @Child1Pax INT, @Child2Pax INT, @VatPercent REAL AS
SET NOCOUNT ON
/*
  NB20200921 Replaced earlier by GetContentProduct, now morphed into an online friendly version
  NB20050622: Created
  NOTE: Calculates the Product prices and Vat amount of those  
  @DayProduct: 0- Overnight; 1-Day; 2-Plus
*/
DECLARE @DebugLevel TINYINT--, @Msg VARCHAR(2000)
SET @DebugLevel = 0
IF @DebugLevel >= 1 
  PRINT '--[GetPriceProduct] begin'
DECLARE @ProductContentInt BIT
IF OBJECT_ID('tempdb..#ProductContent') IS NULL
BEGIN
  CREATE TABLE #ProductContent
  (  
    Price MONEY null,
    Tax MONEY null,
    Kind TINYINT not null, --1: package arrival; 2: package stay and departure 3: product arrival; 4: product stay and departure; 5: addon;
    FkAccountTaxGroup INT null,
    ProductNumber INT null, --null used temporary as flag 4 filtering done from undone packet records (SUM = PackagePrice and PackagePriceArrival
    LineNumber INT null
  )
  SET @ProductContentInt = 1
END
ELSE
  SET @ProductContentInt = 0
EXEC [dbo].[GetContentProduct] @Price, @PriceArrival, @DayProduct, @ProductNumber, @StartDate, @EndDate, @BookedTypeNumber, 
  @NumberOfRooms, @AdultPax, @Child1Pax, @Child2Pax, @VatPercent, NULL, 0
--1: package arrival; 2: package stay and departure 3: product arrival; 4: product stay and departure; 5: addon 
DECLARE @PriceOverwrite MONEY = @PriceArrival
IF @DayProduct = 1
  SELECT @PriceArrival = SUM(Price), @TaxArrival = SUM(Tax) from #ProductContent (nolock) where Kind in (1, 3) 
ELSE
  SELECT @PriceArrival = SUM(Price), @TaxArrival = SUM(Tax) from #ProductContent (nolock) where Kind in (3) 
SELECT @Price = SUM(Price), @Tax = SUM(Tax) from #ProductContent (nolock) where Kind in (1,2,4,5)
IF ISNULL(@PriceOverwrite, 0) > 0
BEGIN 
  IF @DayProduct = 1
  BEGIN
    DECLARE @Pax INT = @AdultPax + @Child1Pax + @Child2Pax
    if @PriceArrival <> 0
    SET @PriceArrival = @PriceArrival / @Pax
    if @TaxArrival <> 0
    SET @TaxArrival = @TaxArrival / @Pax
  END
  BEGIN
    DECLARE @DayCount INT = datediff(dy, convert(datetime, @StartDate, 122), convert(datetime, @EndDate, 122))
    IF ISNULL(@DayCount, 0) = 0 SET @DayCount = 1
    BEGIN
      SET @Tax = @Tax*@DayCount
      SET @Price = @Price*@DayCount
    END
  END
END
IF @DebugLevel = 1
BEGIN
--SELECT * FROM #ProductContent
  PRINT '@PriceOverwrite='+ISNULL(CONVERT(VARCHAR, @PriceOverwrite), '')
  PRINT '--[GetPriceProduct] end'
END
IF @ProductContentInt = 1
  DROP TABLE #ProductContent
