From d874be99f57f0cc79734be1e5dd1186e30a3ddb5 Mon Sep 17 00:00:00 2001 From: Jonas Peschla Date: Thu, 20 Feb 2025 15:27:30 +0100 Subject: [PATCH] Add support for vertical VAT calculation --- lib/secretariat/constants.rb | 4 ++++ lib/secretariat/invoice.rb | 13 ++++++++++--- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/lib/secretariat/constants.rb b/lib/secretariat/constants.rb index 3a40a82..47b7a27 100644 --- a/lib/secretariat/constants.rb +++ b/lib/secretariat/constants.rb @@ -54,6 +54,10 @@ module Secretariat :EXPORT => 'Export outside the EU' } + # For the background of vertical and horizontal tax calculation see https://hilfe.pacemo.de/de-form/articles/3489851-rundungsfehler-bei-rechnungen + # The idea of introducing an unknown value is that this could be inferred from the given invoice total and line items by probing both variants and selecting the matching one - or reporting a taxation error if neither matches. + TAX_CALCULATION_METHODS = %i[HORIZONTAL VERTICAL UNKNOWN].freeze + UNIT_CODES = { :PIECE => "C62", :DAY => "DAY", diff --git a/lib/secretariat/invoice.rb b/lib/secretariat/invoice.rb index 585c964..6ace2ba 100644 --- a/lib/secretariat/invoice.rb +++ b/lib/secretariat/invoice.rb @@ -41,7 +41,7 @@ module Secretariat :grand_total_amount, :due_amount, :paid_amount, - + :tax_calculation_method, keyword_init: true ) do @@ -69,7 +69,14 @@ def taxes taxes[line_item.tax_percent].tax_amount += BigDecimal(line_item.tax_amount) taxes[line_item.tax_percent].base_amount += BigDecimal(line_item.net_amount) * line_item.quantity end - taxes.values + if tax_calculation_method == :VERTICAL + taxes.values.map do |tax| + tax.tax_amount = (tax.base_amount * tax.tax_percent / 100).round(2) + tax + end + else + taxes.values + end end def payment_code @@ -170,7 +177,7 @@ def to_xml(version: 1, validate: true) xml.text(issue_date.strftime("%Y%m%d")) end end - + end transaction = by_version(version, 'SpecifiedSupplyChainTradeTransaction', 'SupplyChainTradeTransaction') xml['rsm'].send(transaction) do