| 1 |
|
| 2 |
import re |
| 3 |
import time |
| 4 |
|
| 5 |
from odoo import _, api, fields, models |
| 6 |
from odoo.exceptions import ValidationError |
| 7 |
|
| 8 |
|
| 9 |
class ResPartnerBank(models.Model): |
| 10 |
_inherit = 'res.partner.bank' |
| 11 |
|
| 12 |
proxy_type = fields.Selection( |
| 13 |
selection_add=[ |
| 14 |
('bakong_id_solo', "Bakong Account ID (Solo Merchant)"), |
| 15 |
('bakong_id_merchant', "Bakong Account ID (Corporate Merchant)"), |
| 16 |
], |
| 17 |
ondelete={'bakong_id_solo': 'set default', 'bakong_id_merchant': 'set default'}, |
| 18 |
) |
| 19 |
l10n_kh_merchant_id = fields.Char("Merchant ID") |
| 20 |
|
| 21 |
@api.constrains('proxy_type', 'proxy_value') |
| 22 |
def _check_kh_proxy(self): |
| 23 |
bakong_id_re = re.compile(r"^[a-zA-Z0-9_].*@[a-zA-Z0-9_].*$") |
| 24 |
for bank in self.filtered(lambda b: b.country_code == 'KH'): |
| 25 |
if bank.proxy_type not in ['bakong_id_solo', 'bakong_id_merchant', 'none', False]: |
| 26 |
raise ValidationError(_("The proxy type must be Bakong Account ID")) |
| 27 |
if bank.proxy_type in ['bakong_id_solo', 'bakong_id_merchant'] and (not bank.proxy_value or not bakong_id_re.match(bank.proxy_value) or len(bank.proxy_value) > 32): |
| 28 |
raise ValidationError(_("Please enter a valid Bakong Account ID.")) |
| 29 |
if bank.proxy_type == 'bakong_id_merchant' and not bank.l10n_kh_merchant_id: |
| 30 |
raise ValidationError(_("Merchant ID is missing.")) |
| 31 |
|
| 32 |
def _get_qr_code_vals_list(self, qr_method, amount, currency, debtor_partner, free_communication, structured_communication): |
| 33 |
res = super()._get_qr_code_vals_list(qr_method, amount, currency, debtor_partner, free_communication, structured_communication) |
| 34 |
if self.country_code == 'KH': |
| 35 |
|
| 36 |
timestamp = str(time.time_ns() // 1000000) |
| 37 |
|
| 38 |
|
| 39 |
expiry_timestamp = str((time.time_ns() // 1000000) + (1000 * 60 * 60 * 24 * 30)) |
| 40 |
res.append( |
| 41 |
(99, f"00{len(timestamp)}{timestamp}01{len(expiry_timestamp)}{expiry_timestamp}"), |
| 42 |
) |
| 43 |
return res |
| 44 |
|
| 45 |
def _compute_display_qr_setting(self): |
| 46 |
bank_kh = self.filtered(lambda b: b.country_code == 'KH') |
| 47 |
bank_kh.display_qr_setting = self.env.company.qr_code |
| 48 |
super(ResPartnerBank, self - bank_kh)._compute_display_qr_setting() |
| 49 |
|
| 50 |
@api.depends('country_code') |
| 51 |
def _compute_country_proxy_keys(self): |
| 52 |
bank_kh = self.filtered(lambda b: b.country_code == 'KH') |
| 53 |
bank_kh.country_proxy_keys = 'bakong_id_solo,bakong_id_merchant' |
| 54 |
super(ResPartnerBank, self - bank_kh)._compute_country_proxy_keys() |
| 55 |
|
| 56 |
def _get_merchant_account_info(self): |
| 57 |
if self.country_code == 'KH': |
| 58 |
if self.proxy_type == 'bakong_id_solo': |
| 59 |
return 29, self._serialize(*(0, self.proxy_value)) |
| 60 |
elif self.proxy_type == 'bakong_id_merchant': |
| 61 |
merchant_account_info = ''.join([self._serialize(*val) for val in [ |
| 62 |
(0, self.proxy_value), |
| 63 |
(1, self.l10n_kh_merchant_id), |
| 64 |
(2, self.bank_id.name), |
| 65 |
]]) |
| 66 |
return 30, merchant_account_info |
| 67 |
return super()._get_merchant_account_info() |
| 68 |
|
| 69 |
def _get_additional_data_field(self, comment): |
| 70 |
if self.country_code == 'KH': |
| 71 |
return self._serialize(1, comment) |
| 72 |
return super()._get_additional_data_field(comment) |
| 73 |
|
| 74 |
def _get_merchant_category_code(self): |
| 75 |
if self.country_code == 'KH': |
| 76 |
return '0001' |
| 77 |
return super()._get_merchant_category_code() |
| 78 |
|
| 79 |
def _get_error_messages_for_qr(self, qr_method, debtor_partner, currency): |
| 80 |
if qr_method == 'emv_qr' and self.country_code == 'KH': |
| 81 |
if currency.name not in ['KHR', 'USD']: |
| 82 |
return _("Can't generate a KHQR code with a currency other than KHR or USD.") |
| 83 |
return None |
| 84 |
|
| 85 |
return super()._get_error_messages_for_qr(qr_method, debtor_partner, currency) |
| 86 |
|
| 87 |
def _check_for_qr_code_errors(self, qr_method, amount, currency, debtor_partner, free_communication, structured_communication): |
| 88 |
if qr_method == 'emv_qr' and self.country_code == 'KH' and self.proxy_type not in ['bakong_id_solo', 'bakong_id_merchant']: |
| 89 |
return _("The proxy type of KHQR must be a Bakong Account ID") |
| 90 |
|
| 91 |
return super()._check_for_qr_code_errors(qr_method, amount, currency, debtor_partner, free_communication, structured_communication) |