Repository navigation
fix(peppol): report recorded payments as PrepaidAmount in the UBL - #860
Merged
Merged
Conversation
The Peppol UBL set PayableAmount (BT-115) to the invoice total and never emitted PrepaidAmount (BT-113), so an invoice with a recorded payment was sent or exported asking the buyer for the full total. The Factur-X/CII export already subtracts amount_paid. Emit PrepaidAmount when payments are recorded and set PayableAmount to TaxInclusiveAmount - PrepaidAmount (BR-CO-16). As in the CII export, the amount due is never negative; here the prepaid amount is capped at the total so BR-CO-16 holds, and an overpayment is therefore not shown in full in PrepaidAmount.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What is wrong
build_peppol_ubl_invoice_xmlwrites the invoice total intoPayableAmount(BT-115) and never emitsPrepaidAmount(BT-113). When payments are already recorded on the invoice (a deposit, a partial payment, or a resend after a payment), the UBL that is sent through the access point or downloaded from the UBL export (/invoices/<id>/export/ubl) asks the buyer to pay the full total.The Factur-X/ZUGFeRD export (
app/utils/cii_invoice.py) already handles this: it writesamount_paidasTotalPrepaidAmountandDuePayableAmount = total - amount_paid(seetest_cii_prepaid_amountintests/test_cii_invoice.py). So the same invoice gets two different payable amounts depending on the format:amount_paidDuePayableAmountPayableAmount(develop)PayableAmount(this change)PrepaidAmount50.00)PrepaidAmount242.00)EN 16931 rule BR-CO-16 defines the payable amount as
TaxInclusiveAmount - PrepaidAmount + PayableRoundingAmount.What this changes
app/integrations/peppol.py: whenamount_paidis above zero,PrepaidAmountis added toLegalMonetaryTotal(beforePayableAmount, as the UBL 2.1 sequence requires), andPayableAmountis the total minus the prepaid amount.PayableAmount0.00). The Factur-X export still writes the full payment asTotalPrepaidAmount; here the prepaid amount is capped at the total instead, so that BR-CO-16 holds in the UBL (300.00 paid on 242.00 givesPrepaidAmount242.00 andPayableAmount0.00). The other UBL option, the full payment asPrepaidAmountwith a negativePayableAmount, also satisfies BR-CO-16; if you would rather have that, it is a one-line change and I can switch it.PrepaidAmount,PayableAmount= total).Unchanged: the Factur-X/CII export, the invoice totals, and the send/export routes.
Tests
New file
tests/test_peppol_ubl_totals.py:developtest_unpaid_invoice_payable_is_the_total_without_prepaidtest_missing_amount_paid_is_treated_as_unpaidtest_partially_paid_invoice_reports_prepaid_and_the_balance(50.00 of 242.00 → payable 192.00)PrepaidAmount)test_prepaid_amount_precedes_payable_amountPrepaidAmount)test_fully_paid_invoice_has_nothing_payablePrepaidAmount)test_overpaid_invoice_has_nothing_payable_and_stays_balanced(300.00 of 242.00)PrepaidAmount)test_peppol_attachments.py,test_peppol_bridge.py,test_peppol_identifiers.py,test_peppol_service.py,test_peppol_ubl_totals.py,test_invoice_validators.py,test_invoice_pdf_postprocess.py,test_zugferd.py,test_cii_invoice.py,test_invoices.py): 173 passed, 1 skipped with the change; ondevelopthe same files give 169 passed, 1 skipped and only the 4 new tests failing.developgives 2676 passed and the same 5 failures (test_time_format_display.py::test_parse_user_time_input_compact_hhmmand four tests intests/test_utils/test_webhook_service.py), so nothing new fails.