| 1 |
# Part of Odoo. See LICENSE file for full copyright and licensing details. |
| 2 |
from odoo import fields, models |
| 3 |
from odoo.exceptions import UserError |
| 4 |
from odoo.tools.urls import urljoin as url_join |
| 5 |
|
| 6 |
from odoo.addons.account_edi_proxy_client.models.account_edi_proxy_user import ( |
| 7 |
AccountEdiProxyError, |
| 8 |
) |
| 9 |
|
| 10 |
|
| 11 |
class AccountEdiProxyClientUser(models.Model): |
| 12 |
_inherit = 'account_edi_proxy_client.user' |
| 13 |
|
| 14 |
# ------------------ |
| 15 |
# Fields declaration |
| 16 |
# ------------------ |
| 17 |
|
| 18 |
proxy_type = fields.Selection(selection_add=[('l10n_my_edi', 'Malaysian EDI')], ondelete={'l10n_my_edi': 'cascade'}) |
| 19 |
|
| 20 |
_unique_identification_l10n_my_edi = models.UniqueIndex( |
| 21 |
"(edi_identification, proxy_type, edi_mode) WHERE (active AND proxy_type = 'l10n_my_edi')", |
| 22 |
"This edi identification is already assigned to an active user", |
| 23 |
) |
| 24 |
|
| 25 |
# ----------------------- |
| 26 |
# CRUD, inherited methods |
| 27 |
# ----------------------- |
| 28 |
|
| 29 |
def _get_proxy_urls(self): |
| 30 |
# EXTENDS 'account_edi_proxy_client' |
| 31 |
urls = super()._get_proxy_urls() |
| 32 |
# We do not use demo with MyInvois as during a demo, showing the invoice on the pre-prod platform will be better. |
| 33 |
urls['l10n_my_edi'] = { |
| 34 |
'demo': False, |
| 35 |
'prod': 'https://l10n-my-edi.api.odoo.com', |
| 36 |
'test': self.env['ir.config_parameter'].sudo().get_param('l10n_my_edi_test_server_url', 'https://l10n-my-edi.test.odoo.com'), |
| 37 |
} |
| 38 |
return urls |
| 39 |
|
| 40 |
def _get_proxy_identification(self, company, proxy_type): |
| 41 |
# EXTENDS 'account_edi_proxy_client' |
| 42 |
if proxy_type == 'l10n_my_edi': |
| 43 |
if not company.vat: |
| 44 |
raise UserError( |
| 45 |
company.env._( |
| 46 |
'Please fill the TIN of company "%(company_name)s" before enabling the integration with MyInvois.', |
| 47 |
company_name=company.display_name, |
| 48 |
) |
| 49 |
) |
| 50 |
return company.vat |
| 51 |
return super()._get_proxy_identification(company, proxy_type) |
| 52 |
|
| 53 |
# ---------------- |
| 54 |
# Business methods |
| 55 |
# ---------------- |
| 56 |
|
| 57 |
def _l10n_my_edi_contact_proxy(self, endpoint, params): |
| 58 |
self.ensure_one() |
| 59 |
try: |
| 60 |
response = self._make_request( |
| 61 |
url=url_join(self._get_server_url(), endpoint), |
| 62 |
params=params, |
| 63 |
) |
| 64 |
except AccountEdiProxyError as _error: |
| 65 |
# Request error while contacting the IAP server. We assume it is a temporary error. |
| 66 |
raise UserError(self.env._("Failed to contact the E-Invoicing service. Please try again later.")) |
| 67 |
|
| 68 |
return response |