Debian Codegraph @

addons/l10n_th/models/res_bank.py

raw symbols SCIP
1 # Part of Odoo. See LICENSE file for full copyright and licensing details.
2 import re
3
4 from odoo import _, api, fields, models
5 from odoo.exceptions import ValidationError
6
7
8 class ResPartnerBank(models.Model):
9 _inherit = 'res.partner.bank'
10
11 proxy_type = fields.Selection(selection_add=[('ewallet_id', 'Ewallet ID'),
12 ('merchant_tax_id', 'Merchant Tax ID'),
13 ('mobile', "Mobile Number")],
14 ondelete={'ewallet_id': 'set default', 'merchant_tax_id': 'set default', 'mobile': 'set default'})
15
16 @api.constrains('proxy_type', 'proxy_value', 'partner_id')
17 def _check_th_proxy(self):
18 tax_id_re = re.compile(r'^[0-9]{13}$')
19 mobile_re = re.compile(r'^[0-9]{10}$')
20 for bank in self.filtered(lambda b: b.country_code == 'TH'):
21 if bank.proxy_type not in ['ewallet_id', 'merchant_tax_id', 'mobile', 'none', False]:
22 raise ValidationError(_("The QR Code Type must be either Ewallet ID, Merchant Tax ID or Mobile Number to generate a Thailand Bank QR code for account number %s.", bank.acc_number))
23 if bank.proxy_type == 'merchant_tax_id' and (not bank.proxy_value or not tax_id_re.match(bank.proxy_value)):
24 raise ValidationError(_("The Merchant Tax ID must be in the format 1234567890123 for account number %s.", bank.acc_number))
25 if bank.proxy_type == 'mobile' and (not bank.proxy_value or not mobile_re.match(bank.proxy_value)):
26 raise ValidationError(_("The Mobile Number must be in the format 0812345678 for account number %s.", bank.acc_number))
27
28 @api.depends('country_code')
29 def _compute_country_proxy_keys(self):
30 bank_th = self.filtered(lambda b: b.country_code == 'TH')
31 bank_th.country_proxy_keys = 'ewallet_id,merchant_tax_id,mobile'
32 super(ResPartnerBank, self - bank_th)._compute_country_proxy_keys()
33
34 @api.depends('country_code')
35 def _compute_display_qr_setting(self):
36 bank_th = self.filtered(lambda b: b.country_code == 'TH')
37 bank_th.display_qr_setting = True
38 super(ResPartnerBank, self - bank_th)._compute_display_qr_setting()
39
40 def _get_merchant_account_info(self):
41 if self.country_code == 'TH':
42 proxy_type_mapping = {
43 'mobile': 1,
44 'merchant_tax_id': 2,
45 'ewallet_id': 3,
46 }
47 proxy_value = re.sub(r"^0", "66", self.proxy_value).zfill(13) if self.proxy_type == 'mobile' else self.proxy_value
48 vals = [
49 (0, 'A000000677010111'),
50 (proxy_type_mapping[self.proxy_type], proxy_value),
51 ]
52 return (29, ''.join([self._serialize(*val) for val in vals]))
53 return super()._get_merchant_account_info()
54
55 def _get_error_messages_for_qr(self, qr_method, debtor_partner, currency):
56 if qr_method == 'emv_qr' and self.country_code == 'TH':
57 if currency.name not in ['THB']:
58 return _("Can't generate a PayNow QR code with a currency other than THB.")
59 return None
60
61 return super()._get_error_messages_for_qr(qr_method, debtor_partner, currency)
62
63 def _check_for_qr_code_errors(self, qr_method, amount, currency, debtor_partner, free_communication, structured_communication):
64 if qr_method == 'emv_qr' and self.country_code == 'TH' and self.proxy_type not in ['ewallet_id', 'merchant_tax_id', 'mobile']:
65 return _("The PayNow Type must be either Ewallet ID, Merchant Tax ID or Mobile Number to generate a Thailand Bank QR code")
66
67 return super()._check_for_qr_code_errors(qr_method, amount, currency, debtor_partner, free_communication, structured_communication)