From 62fa76a044aa5a43fd1e0f21e1e5910deca7b0ae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?T=C3=A9o=20Goddet?= Date: Mon, 20 Jun 2022 09:23:12 +0000 Subject: [PATCH] [IMP] l10n_ch: allow to use Creditor Reference in swiss payment QR-Bill Creditor Reference is one of the 3 allowed references type in swiss QR-Bills (with None and QR-Reference) It is used with normal IBAN (QR-Reference must be used only with special QR-IBAN) Fixes #71578 Closes #81269 closes odoo/odoo#95518 X-original-commit: 8e1d86dcb10aebc1be1a318126492573a4b74331 Signed-off-by: Olivier Colson (oco) --- addons/l10n_ch/models/account_invoice.py | 8 ++++++++ addons/l10n_ch/models/res_bank.py | 17 +++++++++++++++- addons/l10n_ch/report/swissqr_report.xml | 25 +++++++++++++++++++++--- 3 files changed, 46 insertions(+), 4 deletions(-) diff --git a/addons/l10n_ch/models/account_invoice.py b/addons/l10n_ch/models/account_invoice.py index 1cc742ac7ae..fa7611e79c5 100644 --- a/addons/l10n_ch/models/account_invoice.py +++ b/addons/l10n_ch/models/account_invoice.py @@ -331,6 +331,14 @@ class AccountMove(models.Model): i -= 5 return spaced_qrr_ref + @api.model + def space_scor_reference(self, iso11649_ref): + """ Makes the provided SCOR reference human-friendly, spacing its elements + by blocks of 5 from right to left. + """ + + return ' '.join(iso11649_ref[i:i + 4] for i in range(0, len(iso11649_ref), 4)) + def l10n_ch_action_print_qr(self): ''' Checks that all invoices can be printed in the QR format. diff --git a/addons/l10n_ch/models/res_bank.py b/addons/l10n_ch/models/res_bank.py index 1d1fffce6a2..1ef1e85f123 100644 --- a/addons/l10n_ch/models/res_bank.py +++ b/addons/l10n_ch/models/res_bank.py @@ -2,6 +2,7 @@ # Part of Odoo. See LICENSE file for full copyright and licensing details. import re +from stdnum.util import clean from odoo import api, fields, models, _ from odoo.addons.base.models.res_bank import sanitize_account_number @@ -255,6 +256,9 @@ class ResPartnerBank(models.Model): reference_type = 'QRR' reference = structured_communication acc_number = sanitize_account_number(self.l10n_ch_qr_iban) + elif self._is_iso11649_reference(structured_communication): + reference_type = 'SCOR' + reference = structured_communication.replace(' ', '') currency = currency or self.currency_id or self.company_id.currency_id @@ -331,11 +335,22 @@ class ResPartnerBank(models.Model): and re.match('\d+$', reference) \ and reference == mod10r(reference[:-1]) + @api.model + def _is_iso11649_reference(self, reference): + """ Checks whether the given reference is a ISO11649 (SCOR) reference. + """ + return reference \ + and len(reference) >= 5 \ + and len(reference) <= 25 \ + and reference.startswith('RF') \ + and int(''.join(str(int(x, 36)) for x in clean(reference[4:] + reference[:4], ' -.,/:').upper().strip())) % 97 == 1 + # see https://github.com/arthurdejong/python-stdnum/blob/master/stdnum/iso11649.py + def _eligible_for_qr_code(self, qr_method, debtor_partner, currency, raises_error=True): if qr_method == 'ch_qr': error_messages = [_("The QR code could not be generated for the following reason(s):")] if self.acc_type != 'iban': - error_messages.append(_("The account type isn't QR-IBAN.")) + error_messages.append(_("The account type isn't QR-IBAN or IBAN.")) if self.partner_id.country_id.code != 'CH': error_messages.append(_("Your company isn't located in Switzerland.")) if not debtor_partner or debtor_partner.country_id.code != 'CH': diff --git a/addons/l10n_ch/report/swissqr_report.xml b/addons/l10n_ch/report/swissqr_report.xml index 1a813117efe..8419cc1ccc7 100644 --- a/addons/l10n_ch/report/swissqr_report.xml +++ b/addons/l10n_ch/report/swissqr_report.xml @@ -22,6 +22,9 @@ + + +

QR-bill for invoice

@@ -53,15 +56,23 @@
- +
Reference
+
+


+ +
+
+
+
+
Payable by @@ -157,17 +168,25 @@
- +
Reference
+
+


+ +
+
+
+
+
- +
Additional information