[FIX] account_peppol: fix migration key, sql constraints

Currently, the migration key is stored through a workaround as a config parameter,
because the behaviour needed to be changed in stable.
Now, several things can be fixed:
- `account_peppol_migration_key` is now a field on `res.company`, not `account_edi_proxy_client.user`
- `account_peppol_attachment_ids` can be removed as the specs changed and it's no longer necessary
- `peppol_proxy_state` can be removed as it's no longer useful after `enable_peppol` was added
- SQL constraints need to be changed to only include active users.
  This allows users to deregister/migrate away from Peppol and then re-register later, with their previous edi user being archived

closes odoo/odoo#129595

Related: odoo/upgrade#4980
Signed-off-by: Laurent Smet (las) <las@odoo.com>
This commit is contained in:
aliya
2023-08-03 08:26:28 +02:00
parent 0065a17c4c
commit 2d051168a4
6 changed files with 22 additions and 41 deletions
@@ -1,5 +1,6 @@
from odoo import api, models, fields, _
from odoo.exceptions import UserError
from odoo.tools import index_exists
from .account_edi_proxy_auth import OdooEdiProxyAuth
from cryptography.hazmat.backends import default_backend
@@ -64,10 +65,25 @@ class AccountEdiProxyClientUser(models.Model):
_sql_constraints = [
('unique_id_client', 'unique(id_client)', 'This id_client is already used on another user.'),
('unique_edi_identification', 'unique(edi_identification, proxy_type, edi_mode)', 'This edi identification is already assigned to a user'),
('unique_company_proxy', 'unique(company_id, proxy_type, edi_mode)', 'This company has a user already created for this EDI type'),
('unique_active_edi_identification', '', 'This edi identification is already assigned to an active user'),
('unique_active_company_proxy', '', 'This company has an active user already created for this EDI type'),
]
def _auto_init(self):
super()._auto_init()
if not index_exists(self.env.cr, 'account_edi_proxy_client_user_unique_active_edi_identification'):
self.env.cr.execute("""
CREATE UNIQUE INDEX account_edi_proxy_client_user_unique_active_edi_identification
ON account_edi_proxy_client_user(edi_identification, proxy_type, edi_mode)
WHERE (active = True)
""")
if not index_exists(self.env.cr, 'account_edi_proxy_client_user_unique_active_company_proxy'):
self.env.cr.execute("""
CREATE UNIQUE INDEX account_edi_proxy_client_user_unique_active_company_proxy
ON account_edi_proxy_client_user(company_id, proxy_type, edi_mode)
WHERE (active = True)
""")
@api.depends('company_id')
def _compute_proxy_type(self):
for user in self:
@@ -13,7 +13,6 @@ _logger = logging.getLogger(__name__)
class AccountEdiProxyClientUser(models.Model):
_inherit = 'account_edi_proxy_client.user'
peppol_migration_key = fields.Char(string="Migration Key")
peppol_verification_code = fields.Char(string='SMS verification code')
proxy_type = fields.Selection(
selection_add=[('peppol', 'PEPPOL')],
@@ -37,11 +36,8 @@ class AccountEdiProxyClientUser(models.Model):
self.company_id.write({
'account_peppol_proxy_state': 'not_registered',
'is_account_peppol_participant': False,
'account_peppol_migration_key': False,
})
self.env['ir.config_parameter'].set_param(
f'account_peppol.migration_key_{self.company_id.id}',
False
)
# commit the above changes before raising below
if not tools.config['test_enable'] and not modules.module.current_test:
self.env.cr.commit()
+1 -5
View File
@@ -37,16 +37,12 @@ PEPPOL_ENDPOINT_WARNING = {
class ResCompany(models.Model):
_inherit = 'res.company'
# to be removed once the module is available
account_peppol_attachment_ids = fields.Many2many(
comodel_name='ir.attachment',
string='Peppol Identification Documents',
)
account_peppol_contact_email = fields.Char(
string='Primary contact email',
compute='_compute_account_peppol_contact_email', store=True, readonly=False,
help='Primary contact email for Peppol-related communication',
)
account_peppol_migration_key = fields.Char(string="Migration Key")
account_peppol_phone_number = fields.Char(
string='Phone number (for validation)',
help='You will receive a verification code to this phone number',
@@ -27,21 +27,10 @@ class ResConfigSettings(models.TransientModel):
string="Warning",
compute="_compute_account_peppol_endpoint_warning",
)
# to be changed in master to be a related field on res_company
account_peppol_migration_key = fields.Char(
compute="_compute_account_peppol_migration_key",
inverse="_inverse_account_peppol_migration_key",
readonly=False,
)
account_peppol_migration_key = fields.Char(related='company_id.account_peppol_migration_key', readonly=False)
account_peppol_phone_number = fields.Char(related='company_id.account_peppol_phone_number', readonly=False)
account_peppol_proxy_state = fields.Selection(related='company_id.account_peppol_proxy_state', readonly=False)
account_peppol_purchase_journal_id = fields.Many2one(related='company_id.peppol_purchase_journal_id', readonly=False)
# to be removed once the module is available
account_peppol_attachment_ids = fields.Many2many(
comodel_name='ir.attachment',
string='Peppol Identification Documents',
related='company_id.account_peppol_attachment_ids', readonly=False,
)
account_peppol_verification_code = fields.Char(related='account_peppol_edi_user.peppol_verification_code', readonly=False)
is_account_peppol_eligible = fields.Boolean(
string='PEPPOL eligible',
@@ -108,20 +97,6 @@ class ResConfigSettings(models.TransientModel):
config.account_peppol_endpoint_warning = _("The endpoint number might not be correct. "
"Please check if you entered the right identification number.")
@api.depends('company_id')
def _compute_account_peppol_migration_key(self):
for config in self:
config.account_peppol_migration_key = self.env['ir.config_parameter'].get_param(
f'account_peppol.migration_key_{config.company_id.id}'
)
def _inverse_account_peppol_migration_key(self):
for config in self:
self.env['ir.config_parameter'].set_param(
f'account_peppol.migration_key_{config.company_id.id}',
config.account_peppol_migration_key
)
# -------------------------------------------------------------------------
# BUSINESS ACTIONS
# -------------------------------------------------------------------------
@@ -172,7 +172,7 @@ class TestPeppolParticipant(TransactionCase):
try:
settings.button_update_peppol_user_data()
except UserError:
settings.execute()
settings = self.env['res.config.settings'].create({})
self.assertRecordValues(
settings, [{
'account_peppol_migration_key': False,
@@ -16,8 +16,6 @@ class AccountMoveSend(models.Model):
help='Send the invoice via PEPPOL',
)
enable_peppol = fields.Boolean(compute='_compute_send_mail_extra_fields')
# to be removed once the module is fully available
peppol_proxy_state = fields.Selection(related='company_id.account_peppol_proxy_state')
# technical field needed for computing a warning text about the peppol configuration
peppol_warning = fields.Char(
string="Warning",