CREATE PROCEDURE [dbo].[GetContentProduct] @Price MONEY, @PriceArrival MONEY, @DayProduct INT, @ProductNumber INT
, @StartDate DATETIME, @EndDate DATETIME, @BookedTypeNumber INT, @NumberOfRooms INT, @AdultPax INT, @Child1Pax INT, @Child2Pax INT, @TaxPercent SMALLMONEY, @FkAccountTaxGroup INT, @DiscountNettoRate FLOAT AS
SET NOCOUNT ON
/*
  NB20190702: Created
  NOTE: Calculates the Product Price and Tax grouped by AccountVatGroup
    Replaces old GetPriceProduct
  @DayProduct: 0- Overnight; 1-Day; 2-Plus
*/
DECLARE @DebugLevel TINYINT, @Msg VARCHAR(2000), @PlusProduct BIT, @ProductName VARCHAR(50), @tPre DATETIME, @tPost DATETIME 
SET @DebugLevel = 0
IF  @DayProduct>1  
  SET @PlusProduct= 1
ELSE 
  SET @PlusProduct= 0
IF @DebugLevel > 0 
BEGIN
  SET @tPre = GETDATE();
  SELECT TOP 1 @ProductName= ProductName from Products WITH (NOLOCK) where ProductNumber=@ProductNumber
  IF (@DayProduct <> 1)
  BEGIN
	SET @ProductName = 'Product Overnight = ' + ISNULL(@ProductName, '')
    IF @PlusProduct = 1
      SET @ProductName = '+ '+ ISNULL(@ProductName, '')
  END
  ELSE
    SET @ProductName = 'Product Day = ' + ISNULL(@ProductName, '')
  PRINT '---[GetContentProduct] begin: ' + @ProductName
END
IF @DebugLevel > 1 
BEGIN
  SET @Msg = 'DECLARE @Price MONEY, @PriceArrival MONEY
  , @DayProduct INT, @ProductNumber INT, @FkAccountTaxGroup INT, @StartDate DATETIME, @EndDate DATETIME
  , @BookedTypeNumber INT, @NumberOfRooms INT, @AdultPax INT, @Child1Pax INT, @Child2Pax INT, @TaxPercent REAL, @DiscountNettoRate FLOAT'+CHAR(13)
  IF ISNULL( @Price, -1) = -1 
    SET @Msg = @Msg+'SET @Price = NULL'+CHAR(13)
  ELSE
    SET @Msg = @Msg + 'SET @Price = '+CONVERT(VARCHAR,@Price)+CHAR(13)
  IF ISNULL( @PriceArrival, -1) = -1 
    SET @Msg = @Msg+'SET @PriceArrival = NULL'+CHAR(13)
  ELSE
    SET @Msg = @Msg + 'SET @PriceArrival = '+CONVERT(VARCHAR,@PriceArrival)+CHAR(13)
  IF ISNULL( @ProductNumber, -1) = -1 
    SET @Msg = @Msg+'SET @ProductNumber = NULL'+CHAR(13)
  ELSE
    SET @Msg = @Msg + 'SET @ProductNumber = '+CONVERT(VARCHAR,@ProductNumber)+CHAR(13)
  SET @Msg = @Msg + 'SET @StartDate = CONVERT(DATETIME, '''+CONVERT(VARCHAR, ISNULL(@StartDate, -1), 112)+''' , 112)'+CHAR(13)
  SET @Msg = @Msg + 'SET @EndDate = CONVERT(DATETIME, '''+CONVERT(VARCHAR, ISNULL(@EndDate, -1), 112)+''' , 112)'+CHAR(13)
  IF ISNULL( @BookedTypeNumber, -1) = -1 
    SET @Msg = @Msg+'SET @BookedTypeNumber = NULL'+CHAR(13)
  ELSE
    SET @Msg = @Msg+'SET @BookedTypeNumber = '+CONVERT(VARCHAR, @BookedTypeNumber)+CHAR(13)
  IF ISNULL( @NumberOfRooms, -1) = -1 
    SET @Msg = @Msg+'SET @NumberOfRooms = NULL'+CHAR(13)
  ELSE
    SET @Msg = @Msg + 'SET @NumberOfRooms = '+CONVERT(VARCHAR,@NumberOfRooms)+CHAR(13)
  IF ISNULL( @AdultPax, -1) = -1 
    SET @Msg = @Msg+'SET @AdultPax = NULL'+CHAR(13)
  ELSE
    SET @Msg = @Msg + 'SET @AdultPax = '+CONVERT(VARCHAR,@AdultPax)+CHAR(13)
  IF ISNULL( @Child1Pax, -1) = -1 
    SET @Msg = @Msg+'SET @Child1Pax = NULL'+CHAR(13)
  ELSE
    SET @Msg = @Msg + 'SET @Child1Pax = '+CONVERT(VARCHAR,@Child1Pax)+CHAR(13)
  IF ISNULL( @Child2Pax, -1) = -1 
    SET @Msg = @Msg+'SET @Child2Pax = NULL'+CHAR(13)
  ELSE
    SET @Msg = @Msg + 'SET @Child2Pax = '+CONVERT(VARCHAR,@Child2Pax)+CHAR(13)
  IF ISNULL( @TaxPercent, -1) = -1 
    SET @Msg = @Msg+'SET @TaxPercent = NULL'+CHAR(13)
  ELSE
    SET @Msg = @Msg + 'SET @TaxPercent = '+CONVERT(VARCHAR,@TaxPercent)+CHAR(13)
  IF ISNULL( @DayProduct, -1) = -1 
    SET @Msg = @Msg+'SET @DayProduct = NULL'+CHAR(13)
  ELSE
    SET @Msg = @Msg + 'SET @DayProduct = '+CONVERT(VARCHAR,@DayProduct)+CHAR(13)
  IF ISNULL( @FkAccountTaxGroup, -1) = -1 
    SET @Msg = @Msg+'SET @FkAccountTaxGroup = NULL'+CHAR(13)
  ELSE
    SET @Msg = @Msg+'SET @FkAccountTaxGroup = '+CONVERT(VARCHAR, @FkAccountTaxGroup)+CHAR(13)
  IF ISNULL( @DiscountNettoRate, -1) = -1 
    SET @Msg = @Msg+'SET @DiscountNettoRate = NULL'+CHAR(13)
  ELSE
    SET @Msg = @Msg + 'SET @DiscountNettoRate = '+CONVERT(VARCHAR,@DiscountNettoRate)+CHAR(13)

  SET @Msg = @Msg+'EXEC GetContentProduct @Price, @PriceArrival, @DayProduct, @ProductNumber, @StartDate, @EndDate
  , @BookedTypeNumber, @NumberOfRooms, @AdultPax, @Child1Pax, @Child2Pax,  @TaxPercent, @FkAccountTaxGroup'+ CHAR(13)
  SET @Msg = @Msg + 'PRINT CONVERT(varchar, @PriceArrival) --' + CONVERT(varchar, ISNULL(@PriceArrival, -1)) + CHAR(13)
  SET @Msg = @Msg + 'PRINT CONVERT(varchar, @Price) --' + CONVERT(varchar, ISNULL(@Price, -1)) + CHAR(13)
  PRINT @Msg
  PRINT 'Call SP GetContentProduct end --------------------------------'
END
DECLARE @DiscountPercentPackage REAL
, @PriceProductAddon MONEY 
, @TaxProductAddon MONEY
, @PricePackage MONEY
, @PricePackageArrival MONEY
, @PriceOverwrite MONEY 
, @TempPriceOverwrite MONEY 
, @AddonIfPriceOverwritten BIT
, @TaxArrival MONEY
, @Kind TINYINT
, @TaxPackage MONEY
, @TaxPackageArrival MONEY
, @Tax MONEY

SET @DiscountPercentPackage = 0 --TODO: Where do you get the value from?
set @PriceOverwrite = @PriceArrival
set @TempPriceOverwrite = ISNULL(@PriceArrival, 0)

IF (@DayProduct <> 1)
BEGIN
  SET @PricePackage = @Price
  IF @Price > 0
  BEGIN
    SET @PriceArrival = @PricePackage 
    SET @Price = @PricePackage * @NumberOfRooms
  END
  IF @DebugLevel > 1
  BEGIN
    PRINT '@PriceOverwrite='+ISNULL(CONVERT(VARCHAR, @PriceOverwrite), '')
    PRINT '@Price='+ISNULL(CONVERT(VARCHAR, @Price), '')
    PRINT '@PriceArrival='+ISNULL(CONVERT(VARCHAR, @PriceArrival), '')
  END
  EXEC ProductPrices @ProductNumber, @StartDate, @EndDate, @BookedTypeNumber, @NumberOfRooms, @AdultPax, @Child1Pax, @Child2Pax
    , @Price OUTPUT, @PriceArrival OUTPUT, @PriceProductAddon OUTPUT, @TaxProductAddon OUTPUT, @PlusProduct
  IF @PricePackage > 0
  BEGIN
    SET @PriceArrival = @PricePackage 
    SET @Price = @PricePackage * @NumberOfRooms
    SET @PricePackage = @Price 
  END
  IF @DebugLevel > 1
  BEGIN
    PRINT '@PricePackage='+ISNULL(CONVERT(VARCHAR, @PricePackage), '')
    PRINT '@Price='+ISNULL(CONVERT(VARCHAR, @Price), '')
    PRINT '@PriceArrival='+ISNULL(CONVERT(VARCHAR, @PriceArrival), '')
    PRINT '@DayProduct='+ISNULL(CONVERT(VARCHAR, @DayProduct), '')
  END
  --Get Package Part of the Product
  EXEC dbo.GetProductPackagePart @ProductNumber, @StartDate, @EndDate, @BookedTypeNumber, @NumberOfRooms, @AdultPax, @Child1Pax
  , @Child2Pax, @DiscountPercentPackage, @PricePackage
  SET @PriceProductAddon = @PriceProductAddon*@NumberOfRooms
  IF NOT @PriceOverwrite IS NULL
  BEGIN 
    DECLARE @DayCount INT = datediff(dy, convert(datetime, @StartDate, 122), convert(datetime, @EndDate, 122))
	  IF ISNULL(@DayCount, 0) = 0 SET @DayCount = 1
	  SET @PriceArrival = @PriceOverwrite
    IF @DayProduct > 1 --CR187937 4 PlusProduct periods TotalPrice is used therfore do not multiply with @DayCount
      SET @Price = @PriceOverwrite*@NumberOfRooms
    ELSE
      SET @Price = @PriceOverwrite*@DayCount*@NumberOfRooms
    SELECT @AddonIfPriceOverwritten = dbo.GetHotelSetupKeyValue('AddonIfPriceOverwritten', -1)
    IF ISNULL(@AddonIfPriceOverwritten,0)=0 SET @AddonIfPriceOverwritten=0
    IF (@AddonIfPriceOverwritten = 1)
      SET @Price = @Price + @PriceProductAddon
  END
  SELECT @PricePackage = SUM(Price), @TaxPackage = SUM(Tax) FROM #ProductContent (nolock) WHERE Kind in (1,2) AND ProductNumber IS NULL
  SELECT @PricePackageArrival = SUM(Price)/@NumberOfRooms, @TaxPackageArrival = SUM(Tax)/@NumberOfRooms FROM #ProductContent (nolock) WHERE Kind = 1 AND ProductNumber IS NULL
  IF ISNULL(@PricePackage, 0) = 0 SET @PricePackage = 0
  IF ISNULL(@TaxPackage, 0) = 0 SET @TaxPackage = 0
  IF ISNULL(@PricePackageArrival, 0) = 0 SET @PricePackageArrival = 0
  IF ISNULL(@TaxPackageArrival, 0) = 0 SET @TaxPackageArrival = 0
  if ISNULL(@DayProduct, 0) <> 2  --0: not plus product; 3: first period in Plus product
    SET @Kind = 3
  ELSE
    SET @Kind = 4
  IF @DebugLevel > 1 --AND @PriceOverwrite > 0
    PRINT '@Kind='+ISNULL(CONVERT(VARCHAR, @Kind), '')
  if @Kind = 3
  BEGIN    
    IF @PricePackageArrival < @PriceArrival 
      SET @TaxArrival= (@PriceArrival-@PricePackageArrival)*(1- 1/convert(float,(@TaxPercent/100 +1))) + @TaxPackageArrival
    ELSE IF @Price > 0 
      SET @TaxArrival= @TaxPackageArrival 
	ELSE 
	  SET @TaxArrival= 0
    INSERT INTO #ProductContent (Price, Tax, FkAccountTaxGroup, Kind, ProductNumber) VALUES (@PriceArrival, @TaxArrival, @FkAccountTaxGroup, @Kind, @ProductNumber) 
    SET @Kind = 4
  END
  IF @Price > 0 
    SET @Tax = (@Price-@PricePackage)*(1- 1/convert(float,(@TaxPercent/100 +1)))
  ELSE
    SET @Tax = -@TaxPackage
  IF @DebugLevel > 1 
    PRINT '@Price='+ISNULL(CONVERT(VARCHAR, @Price), '')
  SET @Price = @Price - @PricePackage
  IF @DebugLevel > 1
  BEGIN 
    PRINT '@Price='+ISNULL(CONVERT(VARCHAR, @Price), '')
    PRINT '@PricePackage='+ISNULL(CONVERT(VARCHAR, @PricePackage), '')
    PRINT '@PricePackageArrival='+ISNULL(CONVERT(VARCHAR, @PricePackageArrival), '')

  END
  IF @Kind = 4 and @Price <> 0 
    INSERT INTO #ProductContent (Price, Tax, FkAccountTaxGroup, Kind, ProductNumber) 
    VALUES (@Price, @Tax, @FkAccountTaxGroup, 4, @ProductNumber) 
  IF @TaxProductAddon > 0
	INSERT INTO #ProductContent (Price, Tax, FkAccountTaxGroup, Kind, ProductNumber) 
	VALUES (@PriceProductAddon, @TaxProductAddon, @FkAccountTaxGroup, 5, @ProductNumber) --TODO: fake, get real @FkAccountTaxGroup value 
  UPDATE #ProductContent SET ProductNumber = @ProductNumber WHERE ProductNumber IS NULL
  IF (SELECT COUNT(*) FROM #ProductContent WHERE Kind = 4 and ProductNumber= @ProductNumber) = 0
    INSERT INTO #ProductContent (Price, Tax, FkAccountTaxGroup, Kind, ProductNumber) 
      VALUES (0, 0, 0, 4, @ProductNumber) 
END
ELSE
BEGIN
  DECLARE 
    @BillTextProduct VARCHAR(50)
  , @BillTextAddOn VARCHAR(50)
  , @PackageNumber INT
  , @EnvironmentFee MONEY
  , @FixedPrice BIT
  , @VATProductDummy BIT
  , @SpecifiedDepartureAddOn BIT
  , @CurrentDate DATETIME
  , @PriceProductDay MONEY
  , @PricePackageDay MONEY
  , @TaxPackageDay MONEY
  SET @CurrentDate = @StartDate
  --SET @PricePackage = @Price
  SET @Price = 0  
  SET @PriceArrival = 0
  SET @PricePackage = 0
  SET @PricePackageArrival = 0
  WHILE @CurrentDate <= @EndDate
  BEGIN
    SET @PriceProductAddon= 0
    SET @PriceProductDay= 0
    IF ISNULL(@ProductNumber, 0)>0 --and @PriceOverwrite = 0 
      EXEC ProductPricesDay @ProductNumber, @StartDate, @EndDate, @CurrentDate, @BookedTypeNumber, @NumberOfRooms, @AdultPax, @Child1Pax, @Child2Pax
	  , @PriceProductAddon OUTPUT, @BillTextProduct OUTPUT, @BillTextAddOn OUTPUT, @TempPriceOverwrite OUTPUT, @PackageNumber OUTPUT, @EnvironmentFee OUTPUT
	  , @FixedPrice OUTPUT, @VATProductDummy OUTPUT,  @SpecifiedDepartureAddOn OUTPUT
    SET @PricePackageDay= ISNULL(@PricePackageDay, 0)
    SET @TaxPackageDay= ISNULL(@TaxPackageDay, 0)
    IF @PriceOverwrite > 0 
      SET @PriceProductDay = ISNULL(@PriceOverwrite, 0)
    ELSE
      SET @PriceProductDay = ISNULL(@TempPriceOverwrite, 0)
    IF @PackageNumber > 0 
    BEGIN
IF @DebugLevel > 2
BEGIN
  PRINT '---GetPackageContent begin'
  SET @Msg = 'DECLARE @PricePackage MONEY, @DiscountPercentPackage MONEY
  , @PackageNumber INT, @ProductNumber INT, @CurrentDate DATETIME, @StartDate DATETIME
  , @BookedTypeNumber INT, @NumberOfRooms INT, @AdultPax INT, @Child1Pax INT, @Child2Pax INT,  @VatPercent REAL'+CHAR(13)
  SET @Msg = @Msg + 'SET @CurrentDate = CONVERT(DATETIME, '''+CONVERT(VARCHAR, ISNULL(@CurrentDate, -1), 112)+''' , 112)'+CHAR(13)
  SET @Msg = @Msg + 'SET @StartDate = CONVERT(DATETIME, '''+CONVERT(VARCHAR, ISNULL(@StartDate, -1), 112)+''' , 112)'+CHAR(13)
  
  IF ISNULL( @ProductNumber, -1) = -1 
    SET @Msg = @Msg+'SET @PackageNumber = NULL'+CHAR(13)
  ELSE
    SET @Msg = @Msg + 'SET @PackageNumber = '+CONVERT(VARCHAR,@PackageNumber)+CHAR(13)
  IF ISNULL( @ProductNumber, -1) = -1 
    SET @Msg = @Msg+'SET @ProductNumber = NULL'+CHAR(13)
  ELSE
    SET @Msg = @Msg + 'SET @ProductNumber = '+CONVERT(VARCHAR,@ProductNumber)+CHAR(13)
  IF ISNULL( @BookedTypeNumber, -1) = -1 
    SET @Msg = @Msg+'SET @BookedTypeNumber = NULL'+CHAR(13)
  ELSE
    SET @Msg = @Msg+'SET @BookedTypeNumber = '+CONVERT(VARCHAR, @BookedTypeNumber)+CHAR(13)
  IF ISNULL( @NumberOfRooms, -1) = -1 
    SET @Msg = @Msg+'SET @NumberOfRooms = NULL'+CHAR(13)
  ELSE
    SET @Msg = @Msg + 'SET @NumberOfRooms = '+CONVERT(VARCHAR,@NumberOfRooms)+CHAR(13)
  IF ISNULL( @AdultPax, -1) = -1 
    SET @Msg = @Msg+'SET @AdultPax = NULL'+CHAR(13)
  ELSE
    SET @Msg = @Msg + 'SET @AdultPax = '+CONVERT(VARCHAR,@AdultPax)+CHAR(13)
  IF ISNULL( @Child1Pax, -1) = -1 
    SET @Msg = @Msg+'SET @Child1Pax = NULL'+CHAR(13)
  ELSE
    SET @Msg = @Msg + 'SET @Child1Pax = '+CONVERT(VARCHAR,@Child1Pax)+CHAR(13)
  IF ISNULL( @Child2Pax, -1) = -1 
    SET @Msg = @Msg+'SET @Child2Pax = NULL'+CHAR(13)
  ELSE
    SET @Msg = @Msg + 'SET @Child2Pax = '+CONVERT(VARCHAR,@Child2Pax)+CHAR(13)
  IF ISNULL(@DiscountPercentPackage, -1) = -1 
    SET @Msg = @Msg+'SET @DiscountPercentPackage = NULL'+CHAR(13)
  ELSE
    SET @Msg = @Msg + 'SET @DiscountPercentPackage = '+CONVERT(VARCHAR,@DiscountPercentPackage)+CHAR(13)
  IF ISNULL(@PricePackage, -1) = -1 
    SET @Msg = @Msg+'SET @PricePackage = NULL'+CHAR(13)
  ELSE
    SET @Msg = @Msg + 'SET @PricePackage = '+CONVERT(VARCHAR,@PricePackage)+CHAR(13)

  SET @Msg = @Msg+'SELECT Price, Tax, 1, FkAccountTaxGroup, NULL, NULL FROM dbo.GetPackageContent(@PackageNumber, @CurrentDate, @ProductNumber, @BookedTypeNumber, @NumberOfRooms'+
  ', @AdultPax, @Child1Pax, @Child2Pax, @DiscountPercentPackage, @PricePackage)'+ CHAR(13)
  PRINT @Msg
  PRINT '---GetPackageContent end'
END
      IF @PriceProductDay = @PriceOverwrite
	    SET @Kind = 2
	  ELSE
  	    SET @Kind = 1
      IF @CurrentDate = @StartDate --AND @PacketType = 0
        INSERT INTO #ProductContent 
          SELECT Price, Tax, @Kind, FkAccountTaxGroup, NULL, NULL FROM dbo.GetPackageContent(@PackageNumber, @CurrentDate, @ProductNumber, @BookedTypeNumber, @NumberOfRooms, @AdultPax, @Child1Pax, @Child2Pax, @DiscountPercentPackage, @PricePackage)
      SET @Kind = 2
      IF @CurrentDate > @StartDate --AND @CurrentDate < @EndDate AND @PacketType = 1 or @CurrentDate = @EndDate AND @PacketType = 2
        INSERT INTO #ProductContent 
          SELECT Price, Tax, @Kind, FkAccountTaxGroup, NULL, NULL FROM dbo.GetPackageContent(@PackageNumber, @CurrentDate, @ProductNumber, @BookedTypeNumber, @NumberOfRooms, @AdultPax, @Child1Pax, @Child2Pax, @DiscountPercentPackage, @PricePackage)
      SELECT @PricePackageDay = SUM(Price), @TaxPackageDay = SUM(Tax) FROM #ProductContent WHERE ProductNumber IS NULL AND LineNumber IS NULL
      UPDATE #ProductContent SET ProductNumber = @ProductNumber WHERE ProductNumber IS NULL AND LineNumber IS NULL
    END
    IF @CurrentDate = @StartDate
    BEGIN
      IF (SELECT TOP 1 FixedPrice FROM Products WITH (NOLOCK) WHERE ProductNumber=@ProductNumber) = 1 OR @AdultPax + @Child1Pax + @Child2Pax = 0
      BEGIN
        SET @PriceArrival = @PriceProductDay/@NumberOfRooms
        SET @PricePackageArrival = @PricePackageDay/@NumberOfRooms
        SET @TaxPackageArrival = @TaxPackageDay/@NumberOfRooms
      END
      ELSE IF @PriceOverwrite <> 0 
      BEGIN
        SET @PriceArrival = @PriceProductDay --/ (@AdultPax + @Child1Pax + @Child2Pax)
        SET @PricePackageArrival = @PricePackageDay / (@AdultPax + @Child1Pax + @Child2Pax)
        SET @TaxPackageArrival = @TaxPackageDay / (@AdultPax + @Child1Pax + @Child2Pax)
      END
      ELSE 
      BEGIN
        SET @PriceArrival = @PriceProductDay 
        SET @PricePackageArrival = @PricePackageDay 
        SET @TaxPackageArrival = @TaxPackageDay 
      END
    END
IF @DebugLevel > 1 --AND @PriceOverwrite > 0
BEGIN
  PRINT 'Init:'
  PRINT '@PriceOverwrite='+ CONVERT(VARCHAR, @PriceOverwrite)
  PRINT '@PriceProductDay='+ CONVERT(VARCHAR, @PriceProductDay)
  PRINT '@NumberOfRooms='+ CONVERT(VARCHAR, @NumberOfRooms)
  PRINT '@PackageNumber='+ ISNULL(CONVERT(VARCHAR, @PackageNumber), '')
  PRINT '@PricePackageDay= '+CONVERT(VARCHAR,@PricePackageDay)
  PRINT '@TaxPackageDay= '+CONVERT(VARCHAR,@TaxPackageDay)
END
    IF (SELECT TOP 1 FixedPrice FROM Products WITH (NOLOCK) WHERE ProductNumber=@ProductNumber) = 1
      OR @AdultPax + @Child1Pax + @Child2Pax = 0 OR  @PriceOverwrite = 0 
    BEGIN
      SET @Price = @Price + @PriceProductDay
      SET @PricePackage = @PricePackage + @PricePackageDay
    END
    ELSE
    BEGIN
      SET @Price = @Price + @PriceProductDay*(@AdultPax + @Child1Pax + @Child2Pax)
      IF (SELECT TOP 1 FixedPrice FROM Products WITH (NOLOCK) WHERE ProductNumber=@ProductNumber) <> 1
        OR @AdultPax + @Child1Pax + @Child2Pax = 0 OR  @PriceOverwrite <> 0 
        SET @PricePackage = @PricePackage + @PricePackageDay
      ELSE
        SET @PricePackage = @PricePackage + @PricePackageDay*(@AdultPax + @Child1Pax + @Child2Pax)
    END
    SET @CurrentDate = DATEADD(Day, 1, @CurrentDate)
  END  
  IF NOT @PricePackageDay IS NULL
    SELECT @TaxPackage = SUM(Tax) FROM #ProductContent (nolock) WHERE Kind in (1,2) AND LineNumber IS NULL
  ELSE
  BEGIN
    SELECT @PricePackage = SUM(Price), @TaxPackage = SUM(Tax) FROM #ProductContent (nolock) WHERE Kind in (1,2) AND LineNumber IS NULL
    SELECT @PricePackageArrival = SUM(Price), @TaxPackageArrival = SUM(Tax) FROM #ProductContent (nolock) WHERE Kind = 1 AND ProductNumber IS NULL
  END
  IF ISNULL(@PricePackage, 0) = 0
    SET @PricePackage = 0
  IF ISNULL(@TaxPackage, 0) = 0
    SET @TaxPackage = 0
  SET @PricePackageArrival = ISNULL(@PricePackageArrival, 0)
  SET @TaxPackageArrival = ISNULL(@TaxPackageArrival, 0)
  SET @TaxArrival= (@PriceArrival-@PricePackageArrival) *(1-(1/convert(float,((@TaxPercent/100)+1)))) + @TaxPackageArrival
  SET @Tax= (@Price-@PricePackage) *(1-(1/convert(float,((@TaxPercent/100)+1)))) --+ @TaxPackage -- @TaxPackageArrival
  IF @PriceProductDay = @PriceOverwrite 
    INSERT INTO #ProductContent (Price, Tax, FkAccountTaxGroup, Kind, ProductNumber) 
      VALUES (@PricePackageArrival, @TaxPackageArrival, @FkAccountTaxGroup, 3, @ProductNumber) 

  SET @PriceArrival = ISNULL(@PriceArrival, 0)
  SET @TaxArrival = ISNULL(@TaxArrival, 0)
  IF ISNULL(@DiscountNettoRate, 0) > 0
  BEGIN
    SET @Price = @Price - @Price*@DiscountNettoRate/100
    SET @Tax = @Tax - @Tax*@DiscountNettoRate/100
    SET @PriceArrival = @PriceArrival - @PriceArrival*@DiscountNettoRate/100
    SET @TaxArrival = @TaxArrival - @TaxArrival*@DiscountNettoRate/100
  END

  INSERT INTO #ProductContent (Price, Tax, FkAccountTaxGroup, Kind, ProductNumber) 
    VALUES (@PriceArrival - @PricePackageArrival, @TaxArrival - @TaxPackageArrival, @FkAccountTaxGroup, 3, @ProductNumber) 

  INSERT INTO #ProductContent (Price, Tax, FkAccountTaxGroup, Kind, ProductNumber) 
    VALUES ((ISNULL(@Price, 0)-ISNULL(@PricePackage, 0)), ISNULL(@Tax, 0), @FkAccountTaxGroup, 4, @ProductNumber) 
END
UPDATE #ProductContent SET ProductNumber = @ProductNumber WHERE ProductNumber IS NULL AND LineNumber IS NULL
IF @DebugLevel > 1 --AND @Price > 0
BEGIN
  PRINT 'Product:'
  PRINT '@Price='+ISNULL(CONVERT(VARCHAR, @Price), '')
  PRINT '@Tax='+ISNULL(CONVERT(VARCHAR, @Tax), '')
  PRINT '@PriceArrival='+ISNULL(CONVERT(VARCHAR, @PriceArrival), '')
  PRINT '@TaxArrival='+ISNULL(CONVERT(VARCHAR, @TaxArrival), '')
  PRINT '@PriceProductAddon='+ISNULL(CONVERT(VARCHAR, @PriceProductAddon), '')
  PRINT '@TaxProductAddon='+ISNULL(CONVERT(VARCHAR, @TaxProductAddon), '')
  PRINT '@TaxPercent='+ISNULL(CONVERT(VARCHAR, @TaxPercent), '')
  PRINT 'Package:'
  PRINT '@PricePackage='+ISNULL(CONVERT(VARCHAR, @PricePackage), '')
  PRINT '@TaxPackage='+ISNULL(CONVERT(VARCHAR, @TaxPackage), '')
  PRINT '@PricePackageArrival='+ISNULL(CONVERT(VARCHAR, @PricePackageArrival), '')
  PRINT '@TaxPackageArrival='+ISNULL(CONVERT(VARCHAR, @TaxPackageArrival), '')
  PRINT '@ProductNumber='+ISNULL(CONVERT(VARCHAR, @ProductNumber), '')
  PRINT '@PlusProduct='+ISNULL(CONVERT(VARCHAR, @PlusProduct), '')
  PRINT '@PriceOverwrite='+ISNULL(CONVERT(VARCHAR, @PriceOverwrite), '') 
  IF @DebugLevel > 2
  BEGIN
    PRINT 'Other:'
    PRINT '@DiscountNettoRate='+ISNULL(CONVERT(VARCHAR, @DiscountNettoRate), '') 
    PRINT '@PricePackageDay='+ISNULL(CONVERT(VARCHAR, @PricePackageDay), '') 
  END
END
IF @DebugLevel > 0
BEGIN
  SET @tPost = GETDATE();
  PRINT 'Time SP GetContentProduct: ' + Convert(VARCHAR, DATEDIFF(millisecond, @tPre, @tPost))
END
  
