CREATE PROCEDURE dbo.PostTheDay @ReferenceNumber INT, @GuestNumber INT, @HotelCountryNumber INT, @HotelCustomerNumber INT, @ProductNumber INT, @BookedTypeNumber INT, @NumberOfRooms INT, @Adults INT, @Children1 INT, @Children2 INT, 
  @StayStartDate DATETIME, @StayEndDate DATETIME, @PostDate DATETIME, @PostToDate DATETIME OUTPUT, @BillTextProductDepartureAddon VARCHAR(50) OUTPUT, @DepartureAddon MONEY OUTPUT, @POSTProduct BIT OUTPUT, @ProductWareLinkNumber INT OUTPUT,
  @Addon MONEY OUTPUT, @BillTextProduct VARCHAR(50) OUTPUT, @BillTextProductAddon VARCHAR(50) OUTPUT, @PriceProduct MONEY OUTPUT, @PacketNumber INT OUTPUT, @EnviromentFeeProduct MONEY OUTPUT,
  @SpecifiedDepartureAddOn BIT OUTPUT, @EnviromentFeeType FLOAT OUTPUT, @GetVATFrom SMALLINT, @CityTaxDoPost BIT, @Price MONEY, @WareNumber INT OUTPUT, @StayBalanceAccountNumber INT OUTPUT, @StayResultAccountNumber INT OUTPUT, @StayVATAccountNumber INT OUTPUT,
  @VatValueProduct FLOAT OUTPUT, @WareText VARCHAR(150) OUTPUT, @PriceOverWritten BIT, @PackageDebAccount INT, @AddonIfPriceOverwritten BIT, @SplitBillNumber INT, @UserIDNumber INT, @FK_LogiTurnoverDepartment INT, @TotalDiscountValueForPackagesplit FLOAT, @PackagePriceProduct MONEY OUTPUT,
  @VatByPacketValueProduct MONEY OUTPUT, @PackagePriceProductDiscount MONEY OUTPUT AS
/*
  Created by Kent Johansen, September 2022

  Changes:
  KJ 20230306: Bug: Fix for "Departure package posted multiple times when precharge".
  KJ 20230222: Bug: Fix for "Departure package not posted when precharge". Case #CR229027
  KJ 20230220: Bug: Fix for "Departure package posted twice". Case #CR228926
*/

DECLARE @ShowPrint BIT
SET @ShowPrint = 0

IF @ShowPrint = 1
  PRINT 'PostTheDay'

DECLARE @DummyIsFixedPriceProduct BIT
DECLARE @DummyPriceProduct MONEY
DECLARE @DummyBillTextProduct VARCHAR(50)
DECLARE @VATProduct BIT
DECLARE @DummyPacketNumber INT
DECLARE @DummyEnviromentFeeProduct INT
DECLARE @DummyVATProduct BIT
DECLARE @DummySpecifiedDepartureAddOn BIT
DECLARE @DummyEnviromentFeeType FLOAT
DECLARE @LogiWareIndex TINYINT
DECLARE @DummyPriceProductCityTax MONEY
DECLARE @DummyDepartureAddon MONEY
DECLARE @NumberOfChargedDeparturePackaged INT
DECLARE @TempVatByPacketValueProduct MONEY
DECLARE @TempPackagePriceProduct MONEY
DECLARE @TempPackagePriceProductDiscount MONEY
DECLARE @NumberOfChargedPackaged INT

IF @StayEndDate = @PostToDate+1
  SET @PostToDate = @StayEndDate
  
SET @BillTextProductDepartureAddon = ''
SET @DepartureAddon = 0
  
IF (@StayEndDate <> @PostDate) OR (@StayEndDate = @StayStartDate)
  EXEC GetNextNumber 'PRODUCTWARELINKIDENTITY', 1, @ProductWareLinkNumber OUTPUT

IF @ShowPrint = 1
BEGIN
  PRINT '@ProductWareLinkNumber'
  PRINT @ProductWareLinkNumber
  PRINT '@SplitBillNumber'
  PRINT @SplitBillNumber
END

SET @POSTProduct = 1 --@POSTProduct1

EXEC ProductPricesDay @ProductNumber, @StayStartDate, @StayEndDate, @PostDate, @BookedTypeNumber, @NumberOfRooms, @Adults, @Children1, @Children2, @Addon OUTPUT, @BillTextProduct OUTPUT, @BillTextProductAddon OUTPUT, @PriceProduct OUTPUT, @PacketNumber OUTPUT, @EnviromentFeeProduct OUTPUT, @DummyIsFixedPriceProduct OUTPUT, @VATProduct OUTPUT, @SpecifiedDepartureAddOn OUTPUT, -1, 0, @EnviromentFeeType OUTPUT
  
IF @SpecifiedDepartureAddOn = 0 AND @StayEndDate = @PostDate+1
  EXEC ProductPricesDay @ProductNumber, @StayStartDate, @StayEndDate, @StayEndDate, @BookedTypeNumber, @NumberOfRooms, @Adults, @Children1, @Children2, @DepartureAddon OUTPUT, @DummyBillTextProduct OUTPUT, @BillTextProductDepartureAddon OUTPUT, @DummyPriceProduct OUTPUT, @DummyPacketNumber OUTPUT, @DummyEnviromentFeeProduct OUTPUT, @DummyIsFixedPriceProduct OUTPUT, @DummyVATProduct OUTPUT, @DummySpecifiedDepartureAddOn OUTPUT, -1, 0, @DummyEnviromentFeeType OUTPUT
ELSE IF @SpecifiedDepartureAddOn = 0 AND @StayEndDate = @PostDate AND @StayEndDate <> @StayStartDate
BEGIN
  SET @BillTextProductAddon = ''
  SET @Addon = 0
END
  
IF @GetVATFrom = 4
BEGIN
  SET @LogiWareIndex = @VATProduct+1
  EXEC GetVatValueAndAccountnumbers 0, @GetVatFrom, @LogiWareIndex, @WareNumber OUTPUT, @StayBalanceAccountNumber OUTPUT, @StayResultAccountNumber OUTPUT, @StayVATAccountNumber OUTPUT, @VatValueProduct OUTPUT, @WareText OUTPUT
END
  
IF @StayEndDate = @PostDate+1 -- pakke debitering afrejse dagen før
BEGIN
  EXEC PostGetCityTax @CityTaxDoPost, @GuestNumber, @Price, @PriceOverWritten, @ProductNumber, @StayEndDate, @StayEndDate, @StayEndDate, @BookedTypeNumber, @NumberOfRooms, @Adults, @Children1, @Children2, @DummyPriceProductCityTax OUTPUT
  EXEC ProductPricesDay @ProductNumber, @StayStartDate, @StayEndDate, @StayEndDate, @BookedTypeNumber, @NumberOfRooms, @Adults, @Children1, @Children2, @DummyDepartureAddon OUTPUT, @DummyBillTextProduct OUTPUT, @BillTextProductDepartureAddon OUTPUT, @DummyPriceProduct OUTPUT, @DummyPacketNumber OUTPUT, @DummyEnviromentFeeProduct OUTPUT, @DummyIsFixedPriceProduct OUTPUT, @DummyVATProduct OUTPUT, @DummySpecifiedDepartureAddOn OUTPUT, -1, 0, @DummyEnviromentFeeType OUTPUT
  EXEC GetNumberOfChargedDeparturePackage @ProductNumber, @DummyPacketNumber, @ReferenceNumber, @GuestNumber, @PackageDebAccount, @StayEndDate, @NumberOfChargedDeparturePackaged OUTPUT

  IF @ShowPrint = 1
  BEGIN
    PRINT '@NumberOfChargedDeparturePackaged111111111111111111'
    PRINT @NumberOfChargedDeparturePackaged
  END

  IF @DummyPacketNumber > 0 AND @NumberOfChargedDeparturePackaged = 0
  BEGIN
    IF @PriceOverWritten = 1 AND @AddonIfPriceOverwritten = 0
      SET @DummyPriceProduct = 0
    IF @DummySpecifiedDepartureAddOn = 1
      SET @DummyPriceProduct = @DummyDepartureAddon
    ELSE
      SET @DummyPriceProduct = 0
    EXEC PostPackage -1, @ReferenceNumber, @GuestNumber, @SplitBillNumber, @StayResultAccountNumber, 1, 0, 1, @BookedTypeNumber, @UserIDNumber, @DummyPacketNumber, @ProductNumber, @NumberOfRooms, @PostDate, @Adults, @Children1, @Children2, @DummyPriceProduct, @FK_LogiTurnoverDepartment, @TotalDiscountValueForPackagesplit, @PackagePriceProduct OUTPUT, @VatByPacketValueProduct OUTPUT, @PackagePriceProductDiscount OUTPUT, @ProductWareLinkNumber, @HotelCountryNumber, @HotelCustomerNumber, @APostCityTax = @CityTaxDoPost, @ALogiVATValue = @VatValueProduct, @ARoomTotalPrice = @DummyPriceProductCityTax
  END
  ELSE
  BEGIN
    SET @PackagePriceProduct = 0
    SET @VatByPacketValueProduct = 0
    SET @PackagePriceProductDiscount = 0
  END
END
  
IF @PriceOverWritten = 1 OR @ProductNumber = 0
BEGIN
  IF (@StayEndDate <> @PostDate) OR (@StayEndDate = @StayStartDate)
  BEGIN
    IF ISNULL(@Price, 0) = 0 
      SET @Price = 0
    IF @GuestNumber = 0 AND @NumberOfRooms > 0
      SET @PriceProduct = @Price * @NumberOfRooms
    ELSE 
      SET @PriceProduct = @Price
  END
  IF @PriceOverWritten = 1
  BEGIN
    IF @AddonIfPriceOverwritten = 0
    BEGIN
      SET @Addon = 0
      SET @DepartureAddon = 0
      SET @BillTextProductDepartureAddon = ''
    END
  END
END
  
IF @ShowPrint = 1
BEGIN
  PRINT '111111111111111111111111111111111111111111111111111'
  PRINT '@StayStartDate'
  PRINT @StayStartDate
  PRINT '@StayEndDate'
  PRINT @StayEndDate
  PRINT '@PostDate'
  PRINT @PostDate
END

IF (@StayEndDate <> @PostDate) OR (@StayEndDate = @StayStartDate)
BEGIN
  IF @ShowPrint = 1
    PRINT 'aaaaaaaaaaaaaaaaaaaaaaaaaaaa'
  IF @StayEndDate = @PostDate+1
  BEGIN
    SET @TempVatByPacketValueProduct = @VatByPacketValueProduct
    SET @TempPackagePriceProduct = @PackagePriceProduct
    SET @TempPackagePriceProductDiscount = @PackagePriceProductDiscount
  END
  
  EXEC GetNumberOfChargedDeparturePackage @ProductNumber, @PacketNumber, @ReferenceNumber, @GuestNumber, @PackageDebAccount, @PostDate, @NumberOfChargedPackaged OUTPUT

  IF @PacketNumber > 0 AND @NumberOfChargedPackaged = 0
    EXEC PostPackage -1, @ReferenceNumber, @GuestNumber, @SplitBillNumber, @StayResultAccountNumber, 1, 1, @PriceOverWritten, @BookedTypeNumber, @UserIDNumber, @PacketNumber, @ProductNumber, @NumberOfRooms, @PostDate, @Adults, @Children1, @Children2, @PriceProduct, @FK_LogiTurnoverDepartment, @TotalDiscountValueForPackagesplit, @PackagePriceProduct OUTPUT, @VatByPacketValueProduct OUTPUT, @PackagePriceProductDiscount OUTPUT, @ProductWareLinkNumber, @HotelCountryNumber, @HotelCustomerNumber, @APostCityTax = @CityTaxDoPost, @ALogiVATValue = @VatValueProduct, @ARoomTotalPrice = @PriceProduct
  ELSE
  BEGIN
    SET @PackagePriceProduct = 0
    SET @VatByPacketValueProduct = 0
    SET @PackagePriceProductDiscount = 0
  END
  IF @StayEndDate = @PostDate+1
  BEGIN
    SET @VatByPacketValueProduct = @VatByPacketValueProduct + @TempVatByPacketValueProduct
    SET @PackagePriceProduct =  @PackagePriceProduct + @TempPackagePriceProduct
    SET @PackagePriceProductDiscount =  @PackagePriceProductDiscount + @TempPackagePriceProductDiscount
  END
END
ELSE
BEGIN
  IF @ShowPrint = 1
  BEGIN
    PRINT 'Departure package'

    PRINT '@ProductNumber'
    PRINT @ProductNumber
    PRINT '@DummyPacketNumber'
    PRINT @DummyPacketNumber
    PRINT '@ReferenceNumber'
    PRINT @ReferenceNumber
    PRINT '@GuestNumber'
    PRINT @GuestNumber
    PRINT '@PackageDebAccount'
    PRINT @PackageDebAccount
    PRINT '@StayEndDate'
    PRINT @StayEndDate
  END

  IF @PacketNumber > 0
      EXEC GetNumberOfChargedDeparturePackage @ProductNumber, @PacketNumber, @ReferenceNumber, @GuestNumber, @PackageDebAccount, @StayEndDate, @NumberOfChargedDeparturePackaged OUTPUT
  IF @PacketNumber > 0 AND @NumberOfChargedDeparturePackaged = 0
  BEGIN
    IF @SpecifiedDepartureAddOn = 1
      SET @DummyPriceProduct = @Addon
    ELSE
      SET @DummyPriceProduct = 0
    IF @DummyPriceProduct = 0 
      SET @DummyPriceProduct = @PriceProduct
    EXEC PostPackage -1, @ReferenceNumber, @GuestNumber, @SplitBillNumber, @StayResultAccountNumber, 0, 1, 1, @BookedTypeNumber, @UserIDNumber, @PacketNumber, @ProductNumber, @NumberOfRooms, @PostDate, @Adults, @Children1, @Children2, @DummyPriceProduct, @FK_LogiTurnoverDepartment, @TotalDiscountValueForPackagesplit, @PackagePriceProduct OUTPUT, @VatByPacketValueProduct OUTPUT, @PackagePriceProductDiscount OUTPUT, @ProductWareLinkNumber, @HotelCountryNumber, @HotelCustomerNumber, @APostCityTax = @CityTaxDoPost, @ALogiVATValue = @VatValueProduct, @ARoomTotalPrice = @DummyPriceProductCityTax
  END
  ELSE
  BEGIN
    SET @PackagePriceProduct = 0
    SET @VatByPacketValueProduct = 0
    SET @PackagePriceProductDiscount = 0
  END
END