Debian Codegraph @

addons/l10n_my_edi/models/res_config_settings.py

raw
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 ResConfigSettings(models.TransientModel):
7 _inherit = 'res.config.settings'
8
9 # ------------------
10 # Fields declaration
11 # ------------------
12
13 l10n_my_edi_mode = fields.Selection(related="company_id.l10n_my_edi_mode", readonly=False)
14 l10n_my_edi_default_import_journal_id = fields.Many2one(related="company_id.l10n_my_edi_default_import_journal_id", readonly=False)
15 l10n_my_edi_proxy_user_id = fields.Many2one(related="company_id.l10n_my_edi_proxy_user_id")
16 l10n_my_edi_company_vat = fields.Char(related="company_id.vat")
17 l10n_my_accept_processing = fields.Boolean()
18
19 # ----------------
20 # Onchange methods
21 # ----------------
22
23 @api.onchange('l10n_my_edi_mode')
24 def _onchange_l10n_my_edi_mode(self):
25 """ This onchange is mostly here to improve usability by avoiding the need to save when changing the mode. """
26 self.l10n_my_edi_proxy_user_id = self.company_id.account_edi_proxy_client_ids.filtered(
27 lambda u: u.proxy_type == 'l10n_my_edi' and u.edi_mode == self.l10n_my_edi_mode
28 )
29
30 # --------------
31 # Action methods
32 # --------------
33
34 def action_l10n_my_edi_allow_processing(self):
35 """ We always expect the user to give his consent by pressing the button, in any mode, to enable the edi. """
36 self.company_id._l10n_my_edi_create_proxy_user()
37
38 def action_l10n_my_edi_unregister(self):
39 """ Send a notification to the proxy to free the ID (vat) of the user, and archive the local proxy user.
40 Useful if there has been a misconfiguration or the user wishes to use a new database/...
41 """
42 proxy_user = self.env.company.l10n_my_edi_proxy_user_id
43 if not proxy_user:
44 return
45
46 # Start by notifying the proxy that we wish to deregister.
47 result = proxy_user._l10n_my_edi_contact_proxy('api/l10n_my_edi/1/unregister', {})
48
49 if not result.get('success'):
50 # If we get a result, it should always be successful as we only archive. If for any reason it is not, we will raise an error.
51 raise UserError(proxy_user.env._("An unexpected error occurred while unregistering. Please try again later."))
52
53 # If all goes well we can deactivate the local user.
54 proxy_user.active = False
55
56 def action_open_company_form(self):
57 """ This will be used to ease the configuration by allowing to quickly access the company. """
58 self.ensure_one()
59 return {
60 'type': 'ir.actions.act_window',
61 'res_id': self.env.company.id,
62 'res_model': 'res.company',
63 'target': 'new',
64 'view_mode': 'form',
65 }