einvoicing 0.10.0 → 0.11.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d183c6b6825abf878fbc50cd7ccb3f719f534836a851a690b06d16aa37ba4d7f
4
- data.tar.gz: '0693f5fecd3cccb08d9044aeb981b1df6c766c027dad562ff77978a705ce3e48'
3
+ metadata.gz: ced06d0b7bf48774e2bd4c4298e3daa11e96b05e0f36d5b2e650d75587b762d5
4
+ data.tar.gz: 67fc395ecbe99741fe6b6ce630b04941e854c75a5fe2f84caf33cf0c8055362b
5
5
  SHA512:
6
- metadata.gz: 051e1202cb65ad9fca73b36d222d0475a2741c801dd94f9b73eedf8d663236baf804653409dfcf8aaf83b119fa1aad86c6daad5d6700630c05b0140da406859a
7
- data.tar.gz: 2e6a4bcf48b4d48cfcb6454db8052fd3bf5b495fc066803e612e1ff6d30ca21908b11712f140dd910a242c3a43f8fee2bbac503ea4379826bddbf3090e7b4bd7
6
+ metadata.gz: 0fb18c929a7ede65dbbeee2727e5c7f13fc18f5ad29946a29df60d71fbcf592c47384853b0ffe83e4d88a580473326810e9be9b0186727f0d418f7c997905a1a
7
+ data.tar.gz: 69cdb71c838062ce7fea312611b6f5bae61edae8c9255cacc4385b9fcbc0f69ae6e60aa09fc555453355018f9bb24fea7275889f6eeec6907a06b9c7f0d64b39
data/CHANGELOG.md CHANGED
@@ -7,6 +7,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
7
7
 
8
8
  ## [Unreleased]
9
9
 
10
+ ## [0.11.0] - 2026-12-06
11
+
12
+ ### Added
13
+ - `rounding_amount` on `Invoice` (BT-114, `ram:RoundingAmount` in CII / `cbc:PayableRoundingAmount` in UBL), positive or negative, added to the amount due (BR-CO-16: `DuePayableAmount = GrandTotal − TotalPrepaidAmount + RoundingAmount`) while the gross total stays net + VAT. VAT is computed once on the net total (BR-S-09), so some paid totals are out of reach: at 20 % a 0.02 € net gives 0.02 € and 0.03 € gives 0.04 €, and an invoice settled by a 0.03 € payment could not say so. It now states 0.02 € gross, 0.03 € prepaid, 0.01 € rounding, 0.00 € due. Both elements are left out at zero, so existing documents are unchanged byte for byte
14
+ - The FR validator accepts a prepaid amount up to `gross_total + rounding_amount`, and refuses a rounding of one currency unit or more (`rounding_amount_out_of_range`): the coarsest rounding in use, to a whole unit, never reaches one, and a larger difference is a price change that belongs in an allowance or a charge, which carry their VAT
15
+
10
16
  ## [0.10.0] - 2026-11-22
11
17
 
12
18
  ### Changed
data/README.md CHANGED
@@ -363,6 +363,42 @@ invoice.tax_total_in_tax_currency # => 1692.80 (EUR)
363
363
  The FR validator flags a declared `tax_currency` with no `tax_exchange_rate`,
364
364
  since the document could not satisfy BR-53.
365
365
 
366
+ ## Prepaid Amount and Rounding
367
+
368
+ An amount already paid (BT-113, a deposit or a *retenue de garantie*) and a
369
+ rounding (BT-114) both move the amount due without touching the VAT base or
370
+ the gross total (EN 16931 BR-CO-16):
371
+
372
+ ```text
373
+ due_amount (BT-115) = gross_total (BT-112) − prepaid_amount (BT-113) + rounding_amount (BT-114)
374
+ ```
375
+
376
+ The rounding is for a total no whole-cent net amount can reach. VAT is
377
+ computed once on the net total (BR-S-09), so at 20 % a 0.02 € net gives 0.02 €
378
+ gross and 0.03 € gives 0.04 €: an invoice for the 0.03 € a card payment
379
+ actually collected cannot be written with VAT alone. State the cent as a
380
+ rounding:
381
+
382
+ ```ruby
383
+ invoice = Einvoicing::Invoice.new(
384
+ # ... other fields ...
385
+ lines: [ Einvoicing::LineItem.new(description: "SMS", quantity: 1,
386
+ unit_price: BigDecimal("0.02"), vat_rate: 0.20) ],
387
+ prepaid_amount: BigDecimal("0.03"), # BT-113 — what was paid
388
+ rounding_amount: BigDecimal("0.01") # BT-114 — positive or negative
389
+ )
390
+
391
+ invoice.gross_total # BT-112 => 0.02 (net + VAT, unchanged)
392
+ invoice.due_amount # BT-115 => 0.00
393
+ ```
394
+
395
+ CII writes `ram:RoundingAmount` before `ram:GrandTotalAmount`, UBL
396
+ `cbc:PayableRoundingAmount` before `cbc:PayableAmount`; both are left out when
397
+ the rounding is zero, so existing documents do not change. The FR validator
398
+ accepts a prepaid amount up to `gross_total + rounding_amount`, and refuses a
399
+ rounding of one currency unit or more: a larger difference changes the price,
400
+ and belongs in an allowance or a charge, which carry their VAT.
401
+
366
402
  ## Payment Means
367
403
 
368
404
  Add IBAN, BIC, and UNCL4461 payment type code to the invoice. Both CII and UBL generators emit the appropriate elements automatically.
@@ -12,6 +12,7 @@ en:
12
12
  bic_invalid: "BIC format is invalid (8 or 11 characters)"
13
13
  prepaid_amount_negative: "Prepaid amount cannot be negative"
14
14
  prepaid_amount_exceeds_total: "Prepaid amount cannot exceed the invoice total"
15
+ rounding_amount_out_of_range: "Rounding amount must stay under one unit of the invoice currency: a larger difference is an allowance or a charge, not a rounding"
15
16
  tax_exchange_rate_missing: "A VAT accounting currency (BT-6) requires tax_exchange_rate so the VAT total can be restated in it (BT-111)"
16
17
  seller:
17
18
  name_missing: "Seller name is required"
@@ -12,6 +12,7 @@ fr:
12
12
  bic_invalid: "Le format du BIC est invalide (8 ou 11 caractères)"
13
13
  prepaid_amount_negative: "Le montant déjà payé ne peut pas être négatif"
14
14
  prepaid_amount_exceeds_total: "Le montant déjà payé ne peut pas dépasser le total de la facture"
15
+ rounding_amount_out_of_range: "L'arrondi doit rester inférieur à une unité de la devise de la facture : un écart plus grand est une remise ou des frais, pas un arrondi"
15
16
  tax_exchange_rate_missing: "Une devise de comptabilisation de la TVA (BT-6) impose tax_exchange_rate pour exprimer le total de TVA dans cette devise (BT-111)"
16
17
  seller:
17
18
  name_missing: "Le nom du vendeur est requis"
@@ -228,6 +228,9 @@ module Einvoicing
228
228
  b.text("ram:TaxTotalAmount", format_amount(converted),
229
229
  "currencyID" => invoice.tax_currency)
230
230
  end
231
+ # BT-114 — sits between the VAT total and the grand total in the
232
+ # Factur-X EN16931 sequence. Omitted at zero, as before.
233
+ b.text("ram:RoundingAmount", format_amount(invoice.rounding_amount)) unless invoice.rounding_amount.zero?
231
234
  b.text("ram:GrandTotalAmount", format_amount(invoice.gross_total))
232
235
  b.text("ram:TotalPrepaidAmount", format_amount(invoice.prepaid_amount)) if invoice.prepaid_amount.positive?
233
236
  b.text("ram:DuePayableAmount", format_amount(invoice.due_amount))
@@ -207,6 +207,11 @@ module Einvoicing
207
207
  b.text("cbc:PrepaidAmount", format_amount(invoice.prepaid_amount),
208
208
  "currencyID" => invoice.currency)
209
209
  end
210
+ # BT-114 — UBL 2.1 places it between PrepaidAmount and PayableAmount.
211
+ unless invoice.rounding_amount.zero?
212
+ b.text("cbc:PayableRoundingAmount", format_amount(invoice.rounding_amount),
213
+ "currencyID" => invoice.currency)
214
+ end
210
215
  b.text("cbc:PayableAmount", format_amount(invoice.due_amount),
211
216
  "currencyID" => invoice.currency)
212
217
  end
@@ -39,7 +39,8 @@ module Einvoicing
39
39
  :document_type,
40
40
  :original_invoice_number,
41
41
  :original_invoice_date,
42
- :prepaid_amount
42
+ :prepaid_amount,
43
+ :rounding_amount
43
44
  ) do
44
45
  def initialize(invoice_number:, issue_date:, seller:, buyer:, lines:,
45
46
  allowances: [], charges: [],
@@ -48,7 +49,7 @@ module Einvoicing
48
49
  payment_reference: nil, note: nil,
49
50
  payment_means_code: nil, iban: nil, bic: nil,
50
51
  document_type: :invoice, original_invoice_number: nil, original_invoice_date: nil,
51
- prepaid_amount: BigDecimal(0))
52
+ prepaid_amount: BigDecimal(0), rounding_amount: BigDecimal(0))
52
53
  allowances = allowances || []
53
54
  charges = charges || []
54
55
  computed_breakdown = tax_breakdown || compute_tax_breakdown(lines, allowances, charges)
@@ -73,7 +74,11 @@ module Einvoicing
73
74
  document_type: document_type,
74
75
  original_invoice_number: original_invoice_number,
75
76
  original_invoice_date: original_invoice_date,
76
- prepaid_amount: prepaid_amount.nil? ? BigDecimal(0) : BigDecimal(prepaid_amount.to_s)
77
+ prepaid_amount: prepaid_amount.nil? ? BigDecimal(0) : BigDecimal(prepaid_amount.to_s),
78
+ # Held to the cent like every amount the documents state: a sub-cent
79
+ # rounding would print as 0.01 while the amount due kept 0.005, and
80
+ # the two would no longer add up (BR-CO-16).
81
+ rounding_amount: rounding_amount.nil? ? BigDecimal(0) : BigDecimal(rounding_amount.to_s).round(2, :half_up)
77
82
  )
78
83
  end
79
84
 
@@ -129,11 +134,13 @@ module Einvoicing
129
134
  (net_total + tax_total).round(2, :half_up)
130
135
  end
131
136
 
132
- # Amount due after deducting any retained/prepaid amount (BT-113).
133
- # VAT remains due on the full gross_total — only the payable balance is reduced
134
- # (EN 16931 BR-CO-16: DuePayableAmount = GrandTotal − TotalPrepaidAmount).
137
+ # Amount due for payment (BT-115) — EN 16931 BR-CO-16:
138
+ # GrandTotal − TotalPrepaidAmount (BT-113) + RoundingAmount (BT-114).
139
+ # VAT remains due on the full gross_total: a prepaid amount or a rounding
140
+ # only moves the payable balance. The rounding is what makes a paid total
141
+ # reachable when no whole-cent net amount gives it (0.03 € at 20 % VAT).
135
142
  def due_amount
136
- gross_total - prepaid_amount
143
+ gross_total - prepaid_amount + rounding_amount
137
144
  end
138
145
 
139
146
  # BT-6 is set and differs from the invoice currency (BT-5), so BR-53 requires
@@ -206,10 +206,21 @@ module Einvoicing
206
206
  if invoice.prepaid_amount.negative?
207
207
  errors << { field: :prepaid_amount, error: :prepaid_amount_negative,
208
208
  message: Einvoicing::I18n.t("errors.invoice.prepaid_amount_negative") }
209
- elsif invoice.prepaid_amount > invoice.gross_total
209
+ elsif invoice.prepaid_amount > invoice.gross_total + invoice.rounding_amount
210
+ # With a rounding (BT-114) the prepaid amount may settle the rounded
211
+ # total: 0.03 € paid on a 0.02 € gross total rounded up by 0.01 €.
210
212
  errors << { field: :prepaid_amount, error: :prepaid_amount_exceeds_total,
211
213
  message: Einvoicing::I18n.t("errors.invoice.prepaid_amount_exceeds_total") }
212
214
  end
215
+ # A rounding absorbs what is left below the smallest unit a payment
216
+ # can be rounded to; the coarsest rounding in use, to a whole unit of
217
+ # the currency, never reaches one. Anything larger changes the price,
218
+ # and belongs in an allowance or a charge (BG-20 / BG-21), which carry
219
+ # their VAT, rather than in an amount outside the VAT base.
220
+ if invoice.rounding_amount.abs >= 1
221
+ errors << { field: :rounding_amount, error: :rounding_amount_out_of_range,
222
+ message: Einvoicing::I18n.t("errors.invoice.rounding_amount_out_of_range") }
223
+ end
213
224
  errors
214
225
  end
215
226
  private_class_method :validate_invoice_fields
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Einvoicing
4
- VERSION = "0.10.0"
4
+ VERSION = "0.11.0"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: einvoicing
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.10.0
4
+ version: 0.11.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nathan Le Ray