| 1 |
# Part of Odoo. See LICENSE file for full copyright and licensing details. |
| 2 |
from odoo import api, fields, models |
| 3 |
from odoo.exceptions import UserError |
| 4 |
|
| 5 |
|
| 6 |
class ResPartner(models.Model): |
| 7 |
_inherit = 'res.partner' |
| 8 |
|
| 9 |
# ------------------ |
| 10 |
# Fields declaration |
| 11 |
# ------------------ |
| 12 |
|
| 13 |
l10n_my_tin_validation_state = fields.Selection( |
| 14 |
selection=[ |
| 15 |
('valid', 'Valid'), |
| 16 |
('invalid', 'Invalid'), |
| 17 |
], |
| 18 |
string='Tin Validation State', |
| 19 |
help="Technical field, hold the result of TIN validation using MyInvois API.\n" |
| 20 |
"It is non blocking, and will simply help ensure that the customer of an invoice is valid to avoid submission errors.", |
| 21 |
compute='_compute_l10n_my_tin_validation_state', |
| 22 |
readonly=False, |
| 23 |
store=True, |
| 24 |
export_string_translation=False, |
| 25 |
) |
| 26 |
l10n_my_edi_display_tin_warning = fields.Boolean( |
| 27 |
compute='_compute_l10n_my_edi_display_tin_warning', |
| 28 |
) |
| 29 |
|
| 30 |
l10n_my_identification_type = fields.Selection( |
| 31 |
string="ID Type", |
| 32 |
selection=[ |
| 33 |
('NRIC', 'MyKad/MyTentera/MyPR/MyKAS'), |
| 34 |
('BRN', 'Business Registration Number'), |
| 35 |
('PASSPORT', 'Passport'), |
| 36 |
('ARMY', 'Army'), |
| 37 |
], |
| 38 |
default="BRN", |
| 39 |
help="The identification type and number used by the MyTax/MyInvois system to identify the user.\nNote: For MyPR and MyKAS to use NRIC scheme", |
| 40 |
) |
| 41 |
l10n_my_identification_number = fields.Char(string="ID Number") |
| 42 |
l10n_my_identification_number_placeholder = fields.Char(compute="_compute_l10n_my_identification_number_placeholder") |
| 43 |
l10n_my_edi_industrial_classification = fields.Many2one( |
| 44 |
comodel_name='l10n_my_edi.industry_classification', |
| 45 |
string="Ind. Classification", |
| 46 |
compute='_compute_l10n_my_edi_industrial_classification', |
| 47 |
store=True, |
| 48 |
readonly=False, |
| 49 |
) |
| 50 |
l10n_my_edi_malaysian_tin = fields.Char( |
| 51 |
string="Malaysian TIN", |
| 52 |
help="The value set in this field will be used as TIN for the customer/supplier.\n" |
| 53 |
"If left empty, the Tax ID field will be used.", |
| 54 |
) |
| 55 |
|
| 56 |
# -------------------------------- |
| 57 |
# Compute, inverse, search methods |
| 58 |
# -------------------------------- |
| 59 |
|
| 60 |
@api.depends('l10n_my_identification_type', 'l10n_my_identification_number', 'vat', 'l10n_my_edi_malaysian_tin') |
| 61 |
def _compute_l10n_my_tin_validation_state(self): |
| 62 |
""" The three @depends are used for the validation. If they change, we will invalidate it and expect the user to revalidate. """ |
| 63 |
self.l10n_my_tin_validation_state = False |
| 64 |
|
| 65 |
@api.depends_context('company', 'l10n_my_identification_number') |
| 66 |
def _compute_l10n_my_edi_display_tin_warning(self): |
| 67 |
""" We want to display the tin warning for companies registered to use MyInvois. """ |
| 68 |
# We need to sudo here, as all users having access to partners may not have the rights to access the proxy users. |
| 69 |
proxy_user = self.env.company.sudo().l10n_my_edi_proxy_user_id |
| 70 |
is_edi_used = proxy_user and proxy_user.proxy_type == 'l10n_my_edi' |
| 71 |
for partner in self: |
| 72 |
# Users with no business number can't be validated using the api |
| 73 |
partner.l10n_my_edi_display_tin_warning = is_edi_used and partner.l10n_my_identification_number |
| 74 |
|
| 75 |
@api.depends('l10n_my_identification_type') |
| 76 |
def _compute_l10n_my_identification_number_placeholder(self): |
| 77 |
""" Computes a dynamic placeholder that depends on the selected type to help the user inputs their data. |
| 78 |
The placeholders have been taken from the MyInvois doc. |
| 79 |
""" |
| 80 |
for partner in self: |
| 81 |
placeholder = 'N/A' |
| 82 |
if partner.l10n_my_identification_type == 'NRIC': |
| 83 |
placeholder = '830503114923' |
| 84 |
elif partner.l10n_my_identification_type == 'BRN': |
| 85 |
placeholder = '202201234565' |
| 86 |
elif partner.l10n_my_identification_type == 'PASSPORT': |
| 87 |
placeholder = 'A00000000' |
| 88 |
elif partner.l10n_my_identification_type == 'ARMY': |
| 89 |
placeholder = '830805134983' |
| 90 |
partner.l10n_my_identification_number_placeholder = placeholder |
| 91 |
|
| 92 |
def _compute_l10n_my_edi_industrial_classification(self): |
| 93 |
default_classification = self.env.ref('l10n_my_edi.class_00000', raise_if_not_found=False) |
| 94 |
self.filtered(lambda p: not p.l10n_my_edi_industrial_classification).l10n_my_edi_industrial_classification = default_classification |
| 95 |
|
| 96 |
# -------------- |
| 97 |
# Action methods |
| 98 |
# -------------- |
| 99 |
|
| 100 |
def action_validate_tin(self): |
| 101 |
""" Calling this action will reach our EDI proxy in order to validate the TIN against the provided identification information. """ |
| 102 |
self.ensure_one() |
| 103 |
if not self._l10n_my_edi_get_tin_for_myinvois() or not self.l10n_my_identification_type or not self.l10n_my_identification_number: |
| 104 |
raise UserError(self.env._('In order to validate the TIN, you must provide the Identification type and number.')) |
| 105 |
|
| 106 |
# Sudo to allow a user without access to the proxy user to validate the ID if needed. |
| 107 |
proxy_user = self.env.company.sudo().l10n_my_edi_proxy_user_id |
| 108 |
if not proxy_user: |
| 109 |
raise UserError(self.env._("Please register for the E-Invoicing service in the settings first.")) |
| 110 |
|
| 111 |
response = proxy_user._l10n_my_edi_contact_proxy('api/l10n_my_edi/1/validate_tin', params={ |
| 112 |
'identification_values': { |
| 113 |
'tin': self._l10n_my_edi_get_tin_for_myinvois(), |
| 114 |
'id_type': self.l10n_my_identification_type, |
| 115 |
'id_val': self.l10n_my_identification_number, |
| 116 |
}, |
| 117 |
}) |
| 118 |
|
| 119 |
if 'error' in response: |
| 120 |
ref = response['error']['reference'] |
| 121 |
# No need to rollback, we don't want to be blocking on that. |
| 122 |
if ref == 'document_tin_not_found': |
| 123 |
self._message_log(body=self.env._('MyInvois was not able to match the TIN with the provided identification number.\nThis may happen when using generic TIN and will not prevent you from invoicing.')) |
| 124 |
self.l10n_my_tin_validation_state = 'invalid' |
| 125 |
else: |
| 126 |
self._message_log(body=self.env._('An unexpected error occurred while validating the TIN. Please try again later.')) |
| 127 |
else: |
| 128 |
self.l10n_my_tin_validation_state = 'valid' if response.get('success') else 'invalid' |
| 129 |
|
| 130 |
def _l10n_my_edi_get_tin_for_myinvois(self): |
| 131 |
""" Helper to return the VAT number relevant to the situation. """ |
| 132 |
self.ensure_one() |
| 133 |
# When l10n_my_edi_malaysian_tin is set, it will be used instead of the VAT. |
| 134 |
# A user may want to keep the correct VAT on a foreign contact while also use myinvois with a malaysia TIN/Generic TIN |
| 135 |
# Using the Tax ID field also causes issue when base_vat is enabled, which block setting foreign VAT numbers. |
| 136 |
return self.l10n_my_edi_malaysian_tin or self.vat |
| 137 |
|
| 138 |
@api.model |
| 139 |
def _commercial_fields(self): |
| 140 |
return super()._commercial_fields() + ['l10n_my_identification_type', 'l10n_my_identification_number', 'l10n_my_edi_industrial_classification', 'l10n_my_edi_malaysian_tin'] |
| 141 |
|
| 142 |
def _get_frontend_writable_fields(self): |
| 143 |
frontend_writable_fields = super()._get_frontend_writable_fields() |
| 144 |
frontend_writable_fields.update({'l10n_my_identification_type', 'l10n_my_identification_number', 'l10n_my_edi_industrial_classification'}) |
| 145 |
|
| 146 |
return frontend_writable_fields |