From dae20acffb0ed4d98581ffd53d58e784ae06d16b Mon Sep 17 00:00:00 2001 From: dbkosky Date: Fri, 25 Feb 2022 15:55:53 +0000 Subject: [PATCH] [FIX] l10n_it_edi_sdicoop: adapt user registration on res_config_settings To properly register account_edi_proxy_client.user when saving the res config settings page, the radio buttons needed to be modified, and the 'register' button was adapted to be a checkbox instead. Registering multiple demo proxy clients lead to an error, as it is asserted that id_client should be unique, and the demo client has 'id_client': 'demo'. To fix this, the id of a demo client is 'demo' + the company id. Searching for demo clients is adapted to search on the first four characters. closes odoo/odoo#85794 X-original-commit: 54e54466f0858d4b19fa8f61ff2b927e00f8f5a8 Signed-off-by: Josse Colpaert Signed-off-by: Daniel Kosky (dako) --- .../account_edi_proxy_client/__manifest__.py | 2 - .../models/account_edi_proxy_user.py | 2 +- .../models/res_config_settings.py | 66 +++++++++++++++++-- .../views/res_config_settings_views.xml | 47 ++++++------- 4 files changed, 83 insertions(+), 34 deletions(-) diff --git a/addons/account_edi_proxy_client/__manifest__.py b/addons/account_edi_proxy_client/__manifest__.py index 66e2c6f6a7b..5d20df4a13f 100644 --- a/addons/account_edi_proxy_client/__manifest__.py +++ b/addons/account_edi_proxy_client/__manifest__.py @@ -17,8 +17,6 @@ Odoo database. }, 'data': [ 'security/ir.model.access.csv', - ], - 'demo': [ 'data/config_demo.xml', ], 'installable': True, diff --git a/addons/account_edi_proxy_client/models/account_edi_proxy_user.py b/addons/account_edi_proxy_client/models/account_edi_proxy_user.py index de2ffb0fc91..3a5ea322d25 100644 --- a/addons/account_edi_proxy_client/models/account_edi_proxy_user.py +++ b/addons/account_edi_proxy_client/models/account_edi_proxy_user.py @@ -137,7 +137,7 @@ class AccountEdiProxyClientUser(models.Model): ) if self._get_demo_state() == 'demo': # simulate registration - response = {'id_client': 'demo', 'refresh_token': 'demo'} + response = {'id_client': f'demo{company.id}', 'refresh_token': 'demo'} else: try: # b64encode returns a bytestring, we need it as a string diff --git a/addons/l10n_it_edi_sdicoop/models/res_config_settings.py b/addons/l10n_it_edi_sdicoop/models/res_config_settings.py index 7e76b7740d6..299ead5b441 100644 --- a/addons/l10n_it_edi_sdicoop/models/res_config_settings.py +++ b/addons/l10n_it_edi_sdicoop/models/res_config_settings.py @@ -1,21 +1,28 @@ # -*- coding: utf-8 -*- -from odoo import api, models, fields - +from odoo import api, models, fields, _ +from odoo.exceptions import UserError class ResConfigSettings(models.TransientModel): _inherit = 'res.config.settings' is_edi_proxy_active = fields.Boolean(compute='_compute_is_edi_proxy_active') + l10n_it_edi_proxy_current_state = fields.Char(compute='_compute_l10n_it_edi_proxy_current_state') + l10n_it_edi_sdicoop_register = fields.Boolean(compute='_compute_l10n_it_edi_sdicoop_register', inverse='_set_l10n_it_edi_sdicoop_register_demo_mode') l10n_it_edi_sdicoop_demo_mode = fields.Selection( [('demo', 'Demo'), ('test', 'Test (experimental)'), ('prod', 'Official')], compute='_compute_l10n_it_edi_sdicoop_demo_mode', - inverse='_set_l10n_it_edi_sdicoop_demo_mode', + inverse='_set_l10n_it_edi_sdicoop_register_demo_mode', readonly=False) - @api.depends("is_edi_proxy_active") + def _create_proxy_user(self, company_id): + fattura_pa = self.env.ref('l10n_it_edi.edi_fatturaPA') + edi_identification = fattura_pa._get_proxy_identification(company_id) + self.env['account_edi_proxy_client.user']._register_proxy_user(company_id, fattura_pa, edi_identification) + + @api.depends('company_id.account_edi_proxy_client_ids', 'company_id.account_edi_proxy_client_ids.active') def _compute_l10n_it_edi_sdicoop_demo_mode(self): for config in self: config.l10n_it_edi_sdicoop_demo_mode = self.env['account_edi_proxy_client.user']._get_demo_state() @@ -29,6 +36,22 @@ class ResConfigSettings(models.TransientModel): for config in self: config.is_edi_proxy_active = config.company_id.account_edi_proxy_client_ids + @api.depends('company_id.account_edi_proxy_client_ids', 'company_id.account_edi_proxy_client_ids.active') + def _compute_l10n_it_edi_proxy_current_state(self): + fattura_pa = self.env.ref('l10n_it_edi.edi_fatturaPA') + for config in self: + proxy_user = config.company_id.account_edi_proxy_client_ids.search([ + ('company_id', '=', config.company_id.id), + ('edi_format_id', '=', fattura_pa.id), + ], limit=1) + + config.l10n_it_edi_proxy_current_state = 'inactive' if not proxy_user else 'demo' if proxy_user.id_client[:4] == 'demo' else 'active' + + @api.depends('company_id') + def _compute_l10n_it_edi_sdicoop_register(self): + """Needed because it expects a compute""" + self.l10n_it_edi_sdicoop_register = False + def button_create_proxy_user(self): # For now, only fattura_pa uses the proxy. # To use it for more, we have to either make the activation of the proxy on a format basis @@ -39,3 +62,38 @@ class ResConfigSettings(models.TransientModel): return self.env['account_edi_proxy_client.user']._register_proxy_user(self.company_id, fattura_pa, edi_identification) + + def _set_l10n_it_edi_sdicoop_register_demo_mode(self): + + fattura_pa = self.env.ref('l10n_it_edi.edi_fatturaPA') + for config in self: + + proxy_user = self.env['account_edi_proxy_client.user'].search([ + ('company_id', '=', config.company_id.id), + ('edi_format_id', '=', fattura_pa.id) + ], limit=1) + + real_proxy_users = self.env['account_edi_proxy_client.user'].sudo().search([ + ('id_client', 'not like', 'demo'), + ]) + + # Update the config as per the selected radio button + previous_demo_state = proxy_user._get_demo_state() + self.env['ir.config_parameter'].set_param('account_edi_proxy_client.demo', config.l10n_it_edi_sdicoop_demo_mode) + + # If the user is trying to change from a state in which they have a registered official or testing proxy client + # to another state, we should stop them + if real_proxy_users and previous_demo_state != config.l10n_it_edi_sdicoop_demo_mode: + raise UserError(_("The company has already registered with the service as 'Test' or 'Official', it cannot change.")) + + if config.l10n_it_edi_sdicoop_register: + # There should only be one user at a time, if there are no users, register one + if not proxy_user: + self._create_proxy_user(config.company_id) + return + + # If there is a demo user, and we are transitioning from demo to test or production, we should + # delete all demo users and then create the new user. + elif proxy_user.id_client[:4] == 'demo' and config.l10n_it_edi_sdicoop_demo_mode != 'demo': + self.env['account_edi_proxy_client.user'].search([('id_client', '=like', 'demo%')]).sudo().unlink() + self._create_proxy_user(config.company_id) diff --git a/addons/l10n_it_edi_sdicoop/views/res_config_settings_views.xml b/addons/l10n_it_edi_sdicoop/views/res_config_settings_views.xml index 777e2f7923e..fe37d7d1260 100644 --- a/addons/l10n_it_edi_sdicoop/views/res_config_settings_views.xml +++ b/addons/l10n_it_edi_sdicoop/views/res_config_settings_views.xml @@ -12,44 +12,37 @@
- Allow Odoo to process invoices -
- By clicking this button, I accept that Odoo may process my invoices. -
-
- -
-
-
-
-
-
-
-
-
-
+
+ Fattura Elettronica mode
In demo mode Odoo will just simulate the sending of invoices to the government.
In test mode (experimental) Odoo will send the invoices to a non-production service. + Saving this change will direct all companies on this database to this use this configuration. + Once registered for testing or official, the mode cannot be changed.
+
+ Allow Odoo to process invoices +
+ By checking this box, I accept that Odoo may process my invoices. +
+
+ +
+ +
+
+ An Official or Test service has been registered. +
+
+ A Demo service is in use. +