[IMP] crm: fix companies and bypass checks in partners merge
Facing the following cases, only the database admin can merge res_partners: 1. The email addresses are different. 2. The origin contact(s) is (are) linked to some account move lines. This commit adds a kwarg in the merge function. This argument allows to skip the two tests. So they can be bypassed when overloading the function (depending of the current user access rights for instance) in order to merge partners even if they have different email addresses or with account move lines on the origin one. It can also be called programatically and bypass the checks. This commit also makes the merge wizard correct any inconsistence in the allowed companies/currency company of the merged contacts and the related user(s). Closes #24376
This commit is contained in:
@@ -249,11 +249,16 @@ class MergePartnerAutomatic(models.TransientModel):
|
||||
except ValidationError:
|
||||
_logger.info('Skip recursive partner hierarchies for parent_id %s of partner: %s', parent_id, dst_partner.id)
|
||||
|
||||
def _merge(self, partner_ids, dst_partner=None):
|
||||
def _merge(self, partner_ids, dst_partner=None, extra_checks=True):
|
||||
""" private implementation of merge partner
|
||||
:param partner_ids : ids of partner to merge
|
||||
:param dst_partner : record of destination res.partner
|
||||
:param extra_checks: pass False to bypass extra sanity check (e.g. email address)
|
||||
"""
|
||||
# super-admin can be used to bypass extra checks
|
||||
if self.env.uid == SUPERUSER_ID:
|
||||
extra_checks = False
|
||||
|
||||
Partner = self.env['res.partner']
|
||||
partner_ids = Partner.browse(partner_ids).exists()
|
||||
if len(partner_ids) < 2:
|
||||
@@ -269,8 +274,7 @@ class MergePartnerAutomatic(models.TransientModel):
|
||||
if partner_ids & child_ids:
|
||||
raise UserError(_("You cannot merge a contact with one of his parent."))
|
||||
|
||||
# check only admin can merge partners with different emails
|
||||
if SUPERUSER_ID != self.env.uid and len(set(partner.email for partner in partner_ids)) > 1:
|
||||
if extra_checks and len(set(partner.email for partner in partner_ids)) > 1:
|
||||
raise UserError(_("All contacts must have the same email. Only the Administrator can merge contacts with different emails."))
|
||||
|
||||
# remove dst_partner from partners to merge
|
||||
@@ -283,9 +287,14 @@ class MergePartnerAutomatic(models.TransientModel):
|
||||
_logger.info("dst_partner: %s", dst_partner.id)
|
||||
|
||||
# FIXME: is it still required to make and exception for account.move.line since accounting v9.0 ?
|
||||
if SUPERUSER_ID != self.env.uid and 'account.move.line' in self.env and self.env['account.move.line'].sudo().search([('partner_id', 'in', [partner.id for partner in src_partners])]):
|
||||
if extra_checks and 'account.move.line' in self.env and self.env['account.move.line'].sudo().search([('partner_id', 'in', [partner.id for partner in src_partners])]):
|
||||
raise UserError(_("Only the destination contact may be linked to existing Journal Items. Please ask the Administrator if you need to merge several contacts linked to existing Journal Items."))
|
||||
|
||||
# Make the company of all related users consistent
|
||||
for user in partner_ids.mapped('user_ids'):
|
||||
user.write({'company_ids': [(6, 0, [dst_partner.company_id.id])],
|
||||
'company_id': dst_partner.company_id.id})
|
||||
|
||||
# call sub methods to do the merge
|
||||
self._update_foreign_keys(src_partners, dst_partner)
|
||||
self._update_reference_fields(src_partners, dst_partner)
|
||||
|
||||
Reference in New Issue
Block a user