Debian Codegraph @

addons/account_qr_code_sepa/models/res_bank.py

raw symbols SCIP
1 # -*- coding: utf-8 -*-
2
3 from odoo import models, fields, api, _
4 from odoo.addons.account.tools import is_valid_structured_reference, sanitize_structured_reference
5
6
7 class ResPartnerBank(models.Model):
8 _inherit = 'res.partner.bank'
9
10 def _get_qr_vals(self, qr_method, amount, currency, debtor_partner, free_communication, structured_communication):
11 if qr_method == 'sct_qr':
12 if structured_communication and is_valid_structured_reference(structured_communication):
13 structured_communication = sanitize_structured_reference(structured_communication)
14 comment = ''
15 else:
16 structured_communication = ''
17 comment = free_communication or ''
18
19 qr_code_vals = [
20 'BCD', # Service Tag
21 '002', # Version
22 '1', # Character Set
23 'SCT', # Identification Code
24 self.bank_bic or '', # BIC of the Beneficiary Bank
25 (self.acc_holder_name or self.partner_id.name)[:71], # Name of the Beneficiary
26 self.sanitized_acc_number, # Account Number of the Beneficiary
27 currency.name + str(amount), # Currency + Amount of the Transfer in EUR
28 '', # Purpose of the Transfer
29 structured_communication, # Remittance Information (Structured)
30 comment[:141], # Remittance Information (Unstructured) (can't be set if there is a structured one)
31 '', # Beneficiary to Originator Information
32 ]
33 return qr_code_vals
34 return super()._get_qr_vals(qr_method, amount, currency, debtor_partner, free_communication, structured_communication)
35
36 def _get_qr_code_generation_params(self, qr_method, amount, currency, debtor_partner, free_communication, structured_communication):
37 if qr_method == 'sct_qr':
38 return {
39 'barcode_type': 'QR',
40 'quiet': 0,
41 'width': 128,
42 'height': 128,
43 'humanreadable': 1,
44 'value': '\n'.join(self._get_qr_vals(qr_method, amount, currency, debtor_partner, free_communication, structured_communication)),
45 }
46 return super()._get_qr_code_generation_params(qr_method, amount, currency, debtor_partner, free_communication, structured_communication)
47
48 def _get_error_messages_for_qr(self, qr_method, debtor_partner, currency):
49 if qr_method == 'sct_qr':
50 # Some countries share the same IBAN country code
51 # (e.g. Åland Islands and Finland IBANs are 'FI', but Åland Islands' code is 'AX').
52 sepa_country_codes = self.env.ref('base.sepa_zone').country_ids.mapped('code')
53 non_iban_codes = {'AX', 'NC', 'YT', 'TF', 'BL', 'RE', 'MF', 'GP', 'PM', 'PF', 'GF', 'MQ', 'JE', 'GG', 'IM'}
54 sepa_iban_codes = {code for code in sepa_country_codes if code not in non_iban_codes}
55 error_messages = []
56 if currency.name != 'EUR':
57 error_messages.append(_("Can't generate a SEPA QR Code with the %s currency.", currency.name))
58 if self.acc_type != 'iban':
59 error_messages.append(_("Can't generate a SEPA QR code if the account type isn't IBAN."))
60 if not (self.sanitized_acc_number and self.sanitized_acc_number[:2] in sepa_iban_codes):
61 error_messages.append(_("Can't generate a SEPA QR code with a non SEPA iban."))
62 if len(error_messages) > 0:
63 return '\r\n'.join(error_messages)
64 return None
65 return super()._get_error_messages_for_qr(qr_method, debtor_partner, currency)
66
67 def _check_for_qr_code_errors(self, qr_method, amount, currency, debtor_partner, free_communication, structured_communication):
68 if qr_method == 'sct_qr':
69 if not self.acc_holder_name and not self.partner_id.name:
70 return _("The account receiving the payment must have an account holder name or partner name set.")
71
72 return super()._check_for_qr_code_errors(qr_method, amount, currency, debtor_partner, free_communication, structured_communication)
73
74 @api.model
75 def _get_available_qr_methods(self):
76 rslt = super()._get_available_qr_methods()
77 rslt.append(('sct_qr', _("SEPA Credit Transfer QR"), 20))
78 return rslt