CREATE PROCEDURE [dbo].[OwnerAddToFolio] @ReferenceNumber INT, @GuestNumber INT, @OwnerCustomerNumber INT, @HotelNumber INT, @ARoomTypeNumber INT,
  @OwnerWareNumber INT, @OwnerWareText VARCHAR(50), @AUserIdNumber INT, @NettoPrice MONEY, 
  @OwnerAdditionalWareNumber INT, @OwnerAdditionalWareText VARCHAR(50), @NettoPriceAdditional MONEY, 
  @OwnerAdditional2WareNumber INT, @OwnerAdditional2WareText VARCHAR(50), @NettoPriceAdditional2 MONEY, 
  @OwnerEnvironmentWareNumber INT, @OwnerEnvironmentWareText VARCHAR(80), @NettoPriceEnvironment MONEY,
  @OwnerCleaningWareNumber INT, @OwnerCleaningWareText VARCHAR(80), @NettoPriceCleaning MONEY, @ARoomNumber INT = -1, 
  @AUsageFeeWareNumber INT, @AUsageFeeWareText VARCHAR(80), @AUsageFeePrice MONEY,
  @ATicketFeeWareNumber INT, @ATicketFeeWareText VARCHAR(80), @ATicketFeePrice MONEY, @ATicketFeePieces INT, 
  @ACreditCardFeeWareNumber INT, @ACreditCardFeeWareText VARCHAR(80), @ACreditCardFeePrice MONEY,  
  @AProvisionWareNumber INT, @AProvisionWareText VARCHAR(80), @AProvisionPrice MONEY, 
  @AFolioNumber INT OUTPUT AS

/*
  Created by Kent Johansen december 2006

  Last Changed:
    KJ 20240214: Added timeZone
    KJ 20210326: Ext: Added rounding of owner part amount (Req. #3965)
    KJ 20210217: Bug: Fix for "TPB fee added on owner folio - should be subtracted". Case# CR205148
    KJ 20210201: Bug: Fix for "Provision not posted"
    KJ 20190822: Ext: Added provision owner part. Emmaferie
    KJ 20170215: Ext: Added additional ware 2 
    KJ 20150908: New: Alpen travel owner calculation req #2725
--nb20140721 FolioIdentity discontinued
    KJ 20130506: Bug: Fix for "Wrong account incl/excl VAT". Case #CR055013
    KJ 20120403: Bug: PostVAT = 1
    KJ 20111013: Bug: Fix for "Some owner part is not posted". Case #CR034894
    KJ 20110516: Bug: PostVAT = 1
    KJ 20110118: Ext: @OwnerNewFolioDate hotel setting is now per hotel.
    KJ 20101004: Bug: Fix for "Usage fee and ticket fee not posted for all rooms"
    KJ 20100920: Added AOwnertype (Logi, OwnerElectricity, OwnerCleaning, OwnerAdditional)
    KJ 20100831: Ext: Added AFolioNumber as output parameter
    KJ 20100825: Ext: Post of ticket fee added
    KJ 20100817: Ext: Post of brugsret
    KJ 20100809: Bug: Fix for "Not posting on new folio"
    KJ 20100728: Ext: New folio on specific date of month
    KJ 20100112: Ext: Added Owner calculation per room number
    KJ 20071112: ShowPrint = 0
    KJ 20071112: Department rewrite to @FK_DepartmentNumber
    KJ 20071109: Added OwnerCleaning stuff
    KJ 20070618: Do not mix up reservation and owner folios. 
    KJ 20070510: Bug: PostDiscount = 0
    KJ 20070507: Bug: Do not add guestnumber to note field
    KJ 20070503: Bug: Fix for Pices should be -
    KJ 20070501: Chaged the get folionumber
    KJ 20070103: Added Environment
    KJ 20070103: Added Additional
*/

DECLARE @ShowPrint BIT
SET @ShowPrint = 0

IF @ShowPrint = 1
BEGIN
  PRINT 'Add to folio for: '
  PRINT @ReferenceNumber
END

DECLARE @CentralBookingType INT
SELECT @CentralBookingType = CONVERT(INT, KeyValue) FROM HotelSetup WITH (NOLOCK) WHERE SearchKey = 'CentralBookingType'
IF ISNULL(@CentralBookingType, 0) = 0 
  SET @CentralBookingType = 0

DECLARE @HotelCountryNumber INT
SELECT @HotelCountryNumber = CONVERT(INT, KeyValue) FROM HotelSetUp WITH (NOLOCK) WHERE SearchKey = 'CountryCode'
IF @@ROWCOUNT = 0
  SELECT TOP 1 @HotelCountryNumber = CountryNumber FROM Countries WITH (NOLOCK)

DECLARE @PerRoomNumber SMALLINT
SELECT @PerRoomNumber = CONVERT(SMALLINT, KeyValue) FROM HotelSetup WITH (NOLOCK) WHERE SearchKey = 'OwnerInvoicePerRoom'
IF ISNULL(@PerRoomNumber, 0) = 0 
  SET @PerRoomNumber = 0

DECLARE @TransactionDatetime DATETIME
DECLARE @P1 INT
DECLARE @HotelCustomerNumber INT
DECLARE @EnvironmentWareNumberTmp INT
DECLARE @CleaningWareNumberTmp INT
DECLARE @AdditionalWareNumberTmp INT
DECLARE @Additional2WareNumberTmp INT

IF @CentralBookingType IN (3,7)
  SELECT @HotelCustomerNumber = CustomerNumber FROM Customer WITH (NOLOCK) WHERE HotelNumber = @HotelNumber AND CustomerTypeNumber = 7
ELSE
  SET @HotelCustomerNumber = -1

DECLARE @OwnerNewFolioDate INT
  SELECT @OwnerNewFolioDate = dbo.GetHotelSetupKeyValue ('OwnerNewFolioDate', @HotelCustomerNumber)
SET @OwnerNewFolioDate = ISNULL(@OwnerNewFolioDate, 0)

EXEC SystemDateTime '', @TransactionDatetime OUTPUT

DECLARE @AddressNumber INT
DECLARE @TheFolioNumber INT
DECLARE @TheNote VARCHAR(10)

SET @AddressNumber = 0
SET @TheFolioNumber = 0

IF @ShowPrint = 1
BEGIN
  PRINT '@OwnerCustomerNumber'
  PRINT @OwnerCustomerNumber
END

SELECT @TheFolioNumber = FolioNumber FROM FOLIOS WITH (NOLOCK)
INNER JOIN ReservationAddress WITH (NOLOCK) ON ReservationAddress.AddressNumber = Folios.ReservationAddressNumber 
  AND Link = @OwnerCustomerNumber
  AND ((@PerRoomNumber = 0) OR (ReservationAddress.FK_RoomNumber = @ARoomNumber))
WHERE OpenFolio=1 
AND HotelCustomerNumber = @HotelCustomerNumber 
AND ISNULL(Folios.ReferenceNumber, 0) = 0

IF ISNULL(@TheFolioNumber, 0) > 0 AND (@OwnerNewFolioDate > 0)
BEGIN
  -- check to see if new folio is needed
  DECLARE @LastTranDate CHAR(8) 
  DECLARE @ADate CHAR(8)
  DECLARE @Year CHAR(4)
  DECLARE @Month CHAR(2)
  DECLARE @Day CHAR(2)

  SELECT TOP 1 @LastTranDate = CONVERT(CHAR(8), DateStamp, 112) FROM Transactions WITH (NOLOCK)
  WHERE FolioNumber = @TheFolioNumber
  ORDER BY DateStamp
  
  SET @Year = DATEPART(YEAR, @TransactionDatetime)
  SET @Month = RIGHT('00'+CONVERT(VARCHAR(2), DATEPART(MONTH, @TransactionDatetime)),2)
  SET @Day = RIGHT('00'+CONVERT(VARCHAR(2), @OwnerNewFolioDate),2)
  
  SET @ADate = @Year+@Month+@Day
  
  IF @LastTranDate < @ADate
    SET @TheFolioNumber = -1
END

SET @TheNote = CONVERT(VARCHAR(10), @ReferenceNumber)
IF @GuestNumber > 0
  SET @TheNote = @TheNote + '/' + CAST(@GuestNumber AS VARCHAR)

IF @ShowPrint = 1
BEGIN
  PRINT 'FolioNumber BEFORE'
  PRINT @TheFolioNumber
  PRINT @HotelCustomerNumber
  PRINT @TheNote
  PRINT @OwnerWareNumber
END

IF ISNULL(@TheFolioNumber, 0) <= 0
  EXEC InvoiceCreate @OwnerCustomerNumber, 1, @HotelCustomerNumber, @AddressNumber OUTPUT, @TheFolioNumber OUTPUT, @ARoomNumber

SET @AFolioNumber = @TheFolioNumber

IF @ShowPrint = 1
BEGIN
  PRINT 'FolioNumber AFTER'
  PRINT @TheFolioNumber
END
  
DECLARE @tmpWareNumber INT
DECLARE @RoundRule VARCHAR(50)
SELECT @RoundRule = KeyValue FROM Hotelsetup With (NOLOCK) WHERE Searchkey = 'OwnerPartRoundRule'

SET @RoundRule = ISNULL(@RoundRule, 0)
  
-- Logi
IF ISNULL(@NettoPrice, 0) > 0
BEGIN
  IF @RoundRule = 1 -- Nearst
    SET @NettoPrice = ROUND(@NettoPrice, 0)
  ELSE IF @RoundRule = 2 -- Down
    SET @NettoPrice = FLOOR(@NettoPrice)
  ELSE IF @RoundRule = 3 -- Up
    SET @NettoPrice = CEILING(@NettoPrice)

  EXEC Postering  @Note = @TheNote, @PostForDatetime = @TransactionDatetime, @PostDiscount = 0, @PostVAT = 1, @PostOnReferenceNumber = 0,
    @PostOnGuestNumber = 0, @RoomNumber = 0, @UserIDNumber = @AUserIdNumber, @FolioNumber = @TheFolioNumber, @TransferToReferenceNumber = 0, 
    @TransferToGuestNumber = 0, @TransferToFolioNumber = 0, @TransferToAddressTypeNumber = 0, @WareTextOverWrite = @OwnerWareText, @Pieces = -1, 
    @Price = @NettoPrice, @CountryNumber = @HotelCountryNumber, @PaxAdult = 0, @PaxChild1 = 0, @PaxChild2 = 0, @MovedFolioNumber = 0, 
    @FK_DepartmentNumber = 0, @WareNumber = @OwnerWareNumber, @Payment = 0, @GlobalTransactionNumber = 0, @MovedTransactionNumber = 0, 
    @Correction = 0, @DepositFollowupNumber = 0, @TransNrShowOnBillOnAdd = @P1 output, @FirstAddressNumber = NULL,
    @TakeAccountsFromWareNumber = 0, @AutoReceivedPayment = 0, @FirstGlobaltransactionNumber = 0, @HotelCustomerNumber = @HotelCustomerNumber, @ChargedWithCard = 0
END

-- Additional
IF ISNULL(@NettoPriceAdditional, 0) > 0
BEGIN
  -- Get warenumber for Additional
  SELECT @AdditionalWareNumberTmp = Ware.WareNumber
  FROM Ware WITH (NOLOCK) 
  INNER JOIN WareRoomTypeXTab WITH (NOLOCK) ON WareRoomTypeXTab.FK_WareNumber = Ware.WareNumber
    AND WareRoomTypeXTab.FK_RoomTypeNumber = @ARoomTypeNumber
  INNER JOIN WareDepartmentsXtab WITH (NOLOCK) ON WareDepartmentsXtab.WareNumber = Ware.WareNumber
    AND WareDepartmentsXtab.Type = 1 -- Cashier Department
  INNER JOIN Departments WITH (NOLOCK) ON Departments.PK_Department = WareDepartmentsXtab.FK_Department
    AND Departments.HotelNumber = @HotelNumber
  WHERE Ware.Type = 9  --wtOwnerAdditional
  AND ISNULL(Ware.MarkAsDeleted, 0) = 0

  IF ISNULL(@AdditionalWareNumberTmp, 0) > 0
     SET @OwnerAdditionalWareNumber = @AdditionalWareNumberTmp

  IF @RoundRule = 1 -- Nearst
    SET @NettoPriceAdditional = ROUND(@NettoPriceAdditional, 0)
  ELSE IF @RoundRule = 2 -- Down
    SET @NettoPriceAdditional = FLOOR(@NettoPriceAdditional)
  ELSE IF @RoundRule = 3 -- Up
    SET @NettoPriceAdditional = CEILING(@NettoPriceAdditional)

  EXEC Postering  @Note = @TheNote, @PostForDatetime = @TransactionDatetime, @PostDiscount = 0, @PostVAT = 1, @PostOnReferenceNumber = 0,
    @PostOnGuestNumber = 0, @RoomNumber = 0, @UserIDNumber = @AUserIdNumber, @FolioNumber = @TheFolioNumber, @TransferToReferenceNumber = 0, 
    @TransferToGuestNumber = 0, @TransferToFolioNumber = 0, @TransferToAddressTypeNumber = 0, @WareTextOverWrite = @OwnerAdditionalWareText, @Pieces = -1, 
    @Price = @NettoPriceAdditional, @CountryNumber = @HotelCountryNumber, @PaxAdult = 0, @PaxChild1 = 0, @PaxChild2 = 0, @MovedFolioNumber = 0, 
    @FK_DepartmentNumber = 0, @WareNumber = @OwnerAdditionalWareNumber, @Payment = 0, @GlobalTransactionNumber = 0, @MovedTransactionNumber = 0, 
    @Correction = 0, @DepositFollowupNumber = 0, @TransNrShowOnBillOnAdd = @P1 output, @FirstAddressNumber = NULL,
    @TakeAccountsFromWareNumber = 0, @AutoReceivedPayment = 0, @FirstGlobaltransactionNumber = 0, @HotelCustomerNumber = @HotelCustomerNumber, @ChargedWithCard = 0
END    

-- Additional 2
IF ISNULL(@NettoPriceAdditional2, 0) > 0
BEGIN
  -- Get warenumber for Additional
  SELECT @Additional2WareNumberTmp = Ware.WareNumber
  FROM Ware WITH (NOLOCK) 
  INNER JOIN WareRoomTypeXTab WITH (NOLOCK) ON WareRoomTypeXTab.FK_WareNumber = Ware.WareNumber
    AND WareRoomTypeXTab.FK_RoomTypeNumber = @ARoomTypeNumber
  INNER JOIN WareDepartmentsXtab WITH (NOLOCK) ON WareDepartmentsXtab.WareNumber = Ware.WareNumber
    AND WareDepartmentsXtab.Type = 1 -- Cashier Department
  INNER JOIN Departments WITH (NOLOCK) ON Departments.PK_Department = WareDepartmentsXtab.FK_Department
    AND Departments.HotelNumber = @HotelNumber
  WHERE Ware.Type = 9  --wtOwnerAdditional
  AND ISNULL(Ware.MarkAsDeleted, 0) = 0

  IF ISNULL(@Additional2WareNumberTmp, 0) > 0
     SET @OwnerAdditional2WareNumber = @Additional2WareNumberTmp

  IF @RoundRule = 1 -- Nearst
    SET @NettoPriceAdditional2 = ROUND(@NettoPriceAdditional2, 0)
  ELSE IF @RoundRule = 2 -- Down
    SET @NettoPriceAdditional2 = FLOOR(@NettoPriceAdditional2)
  ELSE IF @RoundRule = 3 -- Up
    SET @NettoPriceAdditional2 = CEILING(@NettoPriceAdditional2)

  EXEC Postering  @Note = @TheNote, @PostForDatetime = @TransactionDatetime, @PostDiscount = 0, @PostVAT = 1, @PostOnReferenceNumber = 0,
    @PostOnGuestNumber = 0, @RoomNumber = 0, @UserIDNumber = @AUserIdNumber, @FolioNumber = @TheFolioNumber, @TransferToReferenceNumber = 0, 
    @TransferToGuestNumber = 0, @TransferToFolioNumber = 0, @TransferToAddressTypeNumber = 0, @WareTextOverWrite = @OwnerAdditional2WareText, @Pieces = -1, 
    @Price = @NettoPriceAdditional2, @CountryNumber = @HotelCountryNumber, @PaxAdult = 0, @PaxChild1 = 0, @PaxChild2 = 0, @MovedFolioNumber = 0, 
    @FK_DepartmentNumber = 0, @WareNumber = @OwnerAdditional2WareNumber, @Payment = 0, @GlobalTransactionNumber = 0, @MovedTransactionNumber = 0, 
    @Correction = 0, @DepositFollowupNumber = 0, @TransNrShowOnBillOnAdd = @P1 output, @FirstAddressNumber = NULL,
    @TakeAccountsFromWareNumber = 0, @AutoReceivedPayment = 0, @FirstGlobaltransactionNumber = 0, @HotelCustomerNumber = @HotelCustomerNumber, @ChargedWithCard = 0
END    

-- Environment
IF ISNULL(@NettoPriceEnvironment, 0) > 0
BEGIN
  -- Get warenumber for Environment
  SELECT @EnvironmentWareNumberTmp = Ware.WareNumber
  FROM Ware WITH (NOLOCK) 
  INNER JOIN WareRoomTypeXTab WITH (NOLOCK) ON WareRoomTypeXTab.FK_WareNumber = Ware.WareNumber
    AND WareRoomTypeXTab.FK_RoomTypeNumber = @ARoomTypeNumber
  INNER JOIN WareDepartmentsXtab WITH (NOLOCK) ON WareDepartmentsXtab.WareNumber = Ware.WareNumber
    AND WareDepartmentsXtab.Type = 1 -- Cashier Department
  INNER JOIN Departments WITH (NOLOCK) ON Departments.PK_Department = WareDepartmentsXtab.FK_Department
    AND Departments.HotelNumber = @HotelNumber
  WHERE Ware.Type = 7  --wtOwnerElectricity
  AND ISNULL(Ware.MarkAsDeleted, 0) = 0
    
  IF ISNULL(@EnvironmentWareNumberTmp, 0) > 0
    SET @OwnerEnvironmentWareNumber = @EnvironmentWareNumberTmp

  IF @RoundRule = 1 -- Nearst
    SET @NettoPriceEnvironment = ROUND(@NettoPriceEnvironment, 0)
  ELSE IF @RoundRule = 2 -- Down
    SET @NettoPriceEnvironment = FLOOR(@NettoPriceEnvironment)
  ELSE IF @RoundRule = 3 -- Up
    SET @NettoPriceEnvironment = CEILING(@NettoPriceEnvironment)

  EXEC Postering  @Note = @TheNote, @PostForDatetime = @TransactionDatetime, @PostDiscount = 0, @PostVAT = 1, @PostOnReferenceNumber = 0,
    @PostOnGuestNumber = 0, @RoomNumber = 0, @UserIDNumber = @AUserIdNumber, @FolioNumber = @TheFolioNumber, @TransferToReferenceNumber = 0,
    @TransferToGuestNumber = 0, @TransferToFolioNumber = 0, @TransferToAddressTypeNumber = 0, @WareTextOverWrite = @OwnerEnvironmentWareText, @Pieces = -1,
    @Price = @NettoPriceEnvironment, @CountryNumber = @HotelCountryNumber, @PaxAdult = 0, @PaxChild1 = 0, @PaxChild2 = 0, @MovedFolioNumber = 0,
    @FK_DepartmentNumber = 0, @WareNumber = @OwnerEnvironmentWareNumber, @Payment = 0, @GlobalTransactionNumber = 0, @MovedTransactionNumber = 0,
    @Correction = 0, @DepositFollowupNumber = 0, @TransNrShowOnBillOnAdd = @P1 output, @FirstAddressNumber = NULL,
    @TakeAccountsFromWareNumber = 0, @AutoReceivedPayment = 0, @FirstGlobaltransactionNumber = 0, @HotelCustomerNumber = @HotelCustomerNumber, @ChargedWithCard = 0
END

-- Cleaning
IF ISNULL(@NettoPriceCleaning, 0) > 0
BEGIN
  -- Get warenumber for Cleaning
  SELECT @CleaningWareNumberTmp = Ware.WareNumber
  FROM Ware WITH (NOLOCK)
  INNER JOIN WareRoomTypeXTab WITH (NOLOCK) ON WareRoomTypeXTab.FK_WareNumber = Ware.WareNumber
    AND WareRoomTypeXTab.FK_RoomTypeNumber = @ARoomTypeNumber
  INNER JOIN WareDepartmentsXtab WITH (NOLOCK) ON WareDepartmentsXtab.WareNumber = Ware.WareNumber
    AND WareDepartmentsXtab.Type = 1 -- Cashier Department
  INNER JOIN Departments WITH (NOLOCK) ON Departments.PK_Department = WareDepartmentsXtab.FK_Department
    AND Departments.HotelNumber = @HotelNumber
  WHERE Ware.Type = 8  --wtOwnerCleaning
  AND ISNULL(Ware.MarkAsDeleted, 0) = 0

  IF ISNULL(@CleaningWareNumberTmp, 0) > 0
    SET @OwnerCleaningWareNumber = @CleaningWareNumberTmp

  IF @RoundRule = 1 -- Nearst
    SET @NettoPriceCleaning = ROUND(@NettoPriceCleaning, 0)
  ELSE IF @RoundRule = 2 -- Down
    SET @NettoPriceCleaning = FLOOR(@NettoPriceCleaning)
  ELSE IF @RoundRule = 3 -- Up
    SET @NettoPriceCleaning = CEILING(@NettoPriceCleaning)

  EXEC Postering  @Note = @TheNote, @PostForDatetime = @TransactionDatetime, @PostDiscount = 0, @PostVAT = 1, @PostOnReferenceNumber = 0,
    @PostOnGuestNumber = 0, @RoomNumber = 0, @UserIDNumber = @AUserIdNumber, @FolioNumber = @TheFolioNumber, @TransferToReferenceNumber = 0,
    @TransferToGuestNumber = 0, @TransferToFolioNumber = 0, @TransferToAddressTypeNumber = 0, @WareTextOverWrite = @OwnerCleaningWareText, @Pieces = -1,
    @Price = @NettoPriceCleaning, @CountryNumber = @HotelCountryNumber, @PaxAdult = 0, @PaxChild1 = 0, @PaxChild2 = 0, @MovedFolioNumber = 0,
    @FK_DepartmentNumber = 0, @WareNumber = @OwnerCleaningWareNumber, @Payment = 0, @GlobalTransactionNumber = 0, @MovedTransactionNumber = 0,
    @Correction = 0, @DepositFollowupNumber = 0, @TransNrShowOnBillOnAdd = @P1 output, @FirstAddressNumber = NULL,
    @TakeAccountsFromWareNumber = 0, @AutoReceivedPayment = 0, @FirstGlobaltransactionNumber = 0, @HotelCustomerNumber = @HotelCustomerNumber, @ChargedWithCard = 0
END

-- Usage fee
IF ISNULL(@AUsageFeePrice, 0) > 0
BEGIN
  IF @RoundRule = 1 -- Nearst
    SET @AUsageFeePrice = ROUND(@AUsageFeePrice, 0)
  ELSE IF @RoundRule = 2 -- Down
    SET @AUsageFeePrice = FLOOR(@AUsageFeePrice)
  ELSE IF @RoundRule = 3 -- Up
    SET @AUsageFeePrice = CEILING(@AUsageFeePrice)

  EXEC Postering  @Note = '', @PostForDatetime = @TransactionDatetime, @PostDiscount = 0, @PostVAT = 1, @PostOnReferenceNumber = 0,
    @PostOnGuestNumber = 0, @RoomNumber = 0, @UserIDNumber = @AUserIdNumber, @FolioNumber = @TheFolioNumber, @TransferToReferenceNumber = 0,
    @TransferToGuestNumber = 0, @TransferToFolioNumber = 0, @TransferToAddressTypeNumber = 0, @WareTextOverWrite = @AUsageFeeWareText, @Pieces = 1,
    @Price = @AUsageFeePrice, @CountryNumber = @HotelCountryNumber, @PaxAdult = 0, @PaxChild1 = 0, @PaxChild2 = 0, @MovedFolioNumber = 0,
    @FK_DepartmentNumber = 0, @WareNumber = @AUsageFeeWareNumber, @Payment = 0, @GlobalTransactionNumber = 0, @MovedTransactionNumber = 0,
    @Correction = 0, @DepositFollowupNumber = 0, @TransNrShowOnBillOnAdd = @P1 output, @FirstAddressNumber = NULL,
    @TakeAccountsFromWareNumber = 0, @AutoReceivedPayment = 0, @FirstGlobaltransactionNumber = 0, @HotelCustomerNumber = @HotelCustomerNumber, @ChargedWithCard = 0
END

-- Ticket Fee
IF ISNULL(@ATicketFeePrice, 0) > 0
BEGIN
  IF @RoundRule = 1 -- Nearst
    SET @ATicketFeePrice = ROUND(@ATicketFeePrice, 0)
  ELSE IF @RoundRule = 2 -- Down
    SET @ATicketFeePrice = FLOOR(@ATicketFeePrice)
  ELSE IF @RoundRule = 3 -- Up
    SET @ATicketFeePrice = CEILING(@ATicketFeePrice)

  EXEC Postering  @Note = '', @PostForDatetime = @TransactionDatetime, @PostDiscount = 0, @PostVAT = 1, @PostOnReferenceNumber = 0,
    @PostOnGuestNumber = 0, @RoomNumber = 0, @UserIDNumber = @AUserIdNumber, @FolioNumber = @TheFolioNumber, @TransferToReferenceNumber = 0,
    @TransferToGuestNumber = 0, @TransferToFolioNumber = 0, @TransferToAddressTypeNumber = 0, @WareTextOverWrite = @ATicketFeeWareText, @Pieces = @ATicketFeePieces,
    @Price = @ATicketFeePrice, @CountryNumber = @HotelCountryNumber, @PaxAdult = 0, @PaxChild1 = 0, @PaxChild2 = 0, @MovedFolioNumber = 0,
    @FK_DepartmentNumber = 0, @WareNumber = @ATicketFeeWareNumber, @Payment = 0, @GlobalTransactionNumber = 0, @MovedTransactionNumber = 0,
    @Correction = 0, @DepositFollowupNumber = 0, @TransNrShowOnBillOnAdd = @P1 output, @FirstAddressNumber = NULL,
    @TakeAccountsFromWareNumber = 0, @AutoReceivedPayment = 0, @FirstGlobaltransactionNumber = 0, @HotelCustomerNumber = @HotelCustomerNumber, @ChargedWithCard = 0
END

-- Credit card Fee
IF ISNULL(@ACreditCardFeePrice, 0) > 0
BEGIN
  IF @RoundRule = 1 -- Nearst
    SET @ACreditCardFeePrice = ROUND(@ACreditCardFeePrice, 0)
  ELSE IF @RoundRule = 2 -- Down
    SET @ACreditCardFeePrice = FLOOR(@ACreditCardFeePrice)
  ELSE IF @RoundRule = 3 -- Up
    SET @ACreditCardFeePrice = CEILING(@ACreditCardFeePrice)

  EXEC Postering  @Note = '', @PostForDatetime = @TransactionDatetime, @PostDiscount = 0, @PostVAT = 1, @PostOnReferenceNumber = 0,
    @PostOnGuestNumber = 0, @RoomNumber = 0, @UserIDNumber = @AUserIdNumber, @FolioNumber = @TheFolioNumber, @TransferToReferenceNumber = 0,
    @TransferToGuestNumber = 0, @TransferToFolioNumber = 0, @TransferToAddressTypeNumber = 0, @WareTextOverWrite = @ACreditCardFeeWareText, @Pieces = 1,
    @Price = @ACreditCardFeePrice, @CountryNumber = @HotelCountryNumber, @PaxAdult = 0, @PaxChild1 = 0, @PaxChild2 = 0, @MovedFolioNumber = 0,
    @FK_DepartmentNumber = 0, @WareNumber = @ACreditCardFeeWareNumber, @Payment = 0, @GlobalTransactionNumber = 0, @MovedTransactionNumber = 0,
    @Correction = 0, @DepositFollowupNumber = 0, @TransNrShowOnBillOnAdd = @P1 output, @FirstAddressNumber = NULL,
    @TakeAccountsFromWareNumber = 0, @AutoReceivedPayment = 0, @FirstGlobaltransactionNumber = 0, @HotelCustomerNumber = @HotelCustomerNumber, @ChargedWithCard = 0
END

IF ISNULL(@AProvisionPrice , 0) > 0
BEGIN
  IF @RoundRule = 1 -- Nearst
    SET @AProvisionPrice = ROUND(@AProvisionPrice, 0)
  ELSE IF @RoundRule = 2 -- Down
    SET @AProvisionPrice = FLOOR(@AProvisionPrice)
  ELSE IF @RoundRule = 3 -- Up
    SET @AProvisionPrice = CEILING(@AProvisionPrice)

  EXEC Postering  @Note = @TheNote, @PostForDatetime = @TransactionDatetime, @PostDiscount = 0, @PostVAT = 1, @PostOnReferenceNumber = 0,
    @PostOnGuestNumber = 0, @RoomNumber = 0, @UserIDNumber = @AUserIdNumber, @FolioNumber = @TheFolioNumber, @TransferToReferenceNumber = 0,
    @TransferToGuestNumber = 0, @TransferToFolioNumber = 0, @TransferToAddressTypeNumber = 0, @WareTextOverWrite = @AProvisionWareText, @Pieces = 1,
    @Price = @AProvisionPrice, @CountryNumber = @HotelCountryNumber, @PaxAdult = 0, @PaxChild1 = 0, @PaxChild2 = 0, @MovedFolioNumber = 0,
    @FK_DepartmentNumber = 0, @WareNumber = @AProvisionWareNumber, @Payment = 0, @GlobalTransactionNumber = 0, @MovedTransactionNumber = 0,
    @Correction = 0, @DepositFollowupNumber = 0, @TransNrShowOnBillOnAdd = @P1 output, @FirstAddressNumber = NULL,
    @TakeAccountsFromWareNumber = 0, @AutoReceivedPayment = 0, @FirstGlobaltransactionNumber = 0, @HotelCustomerNumber = @HotelCustomerNumber, @ChargedWithCard = 0
END