Debian Codegraph @

addons/l10n_tr_nilvera/models/res_config_settings.py

raw symbols SCIP
1 from odoo import fields, models, _
2 from odoo.addons.l10n_tr_nilvera.lib.nilvera_client import _get_nilvera_client
3
4
5 class ResConfigSettings(models.TransientModel):
6 _inherit = 'res.config.settings'
7
8 l10n_tr_nilvera_api_key = fields.Char(
9 related='company_id.l10n_tr_nilvera_api_key',
10 string="Nilvera API key",
11 readonly=False,
12 )
13 l10n_tr_nilvera_use_test_env = fields.Boolean(
14 related='company_id.l10n_tr_nilvera_use_test_env',
15 string="Use testing environment",
16 required=True,
17 readonly=False,
18 )
19 l10n_tr_nilvera_purchase_journal_id = fields.Many2one(
20 related='company_id.l10n_tr_nilvera_purchase_journal_id',
21 readonly=False,
22 )
23 l10n_tr_nilvera_vat = fields.Char(related="company_id.vat", string="Nilvera VAT")
24
25 def nilvera_ping(self):
26 """ Test the connection and the API key. """
27 self.check_access('read') # To make sure not everyone can call this method as it's public.
28 with _get_nilvera_client(self.env.company) as client:
29 # As there is no endpoint to ping Nilvera to make sure the connection works, try an endpoint to get the
30 # company's data and this way we can verify the connection and the tax ID in the same step.
31 response = client.request("GET", "/general/Company", handle_response=False)
32 if response.status_code == 200:
33 nilvera_registered_tax_number = response.json().get('TaxNumber')
34 if self.env.company.vat == nilvera_registered_tax_number:
35 self.env['bus.bus']._sendone(self.env.user.partner_id, 'simple_notification', {
36 'type': 'success',
37 'message': _("Nilvera connection successful!"),
38 })
39 else:
40 self.env['bus.bus']._sendone(self.env.user.partner_id, 'simple_notification', {
41 'type': 'success',
42 'message': _("Nilvera connection successful but the tax number on Nilvera and Odoo doesn't match. Check Nilvera."),
43 })
44 elif response.status_code == 401:
45 self.env['bus.bus']._sendone(self.env.user.partner_id, 'simple_notification', {
46 'type': 'danger',
47 'message': _("Nilvera connection was unsuccessful, check the API key."),
48 })
49 else:
50 self.env['bus.bus']._sendone(self.env.user.partner_id, 'simple_notification', {
51 'type': 'danger',
52 'message': _("An error occurred. Try again later."),
53 })