[FIX] base: fix partner merge wizard

Steps to reproduce:

  - Install `Contacts` module
  - Create 3 contacts with the same name
  - Archive one of the contacts
  - Go to list view
  - Edit search filter to display archived and non archived contacts
  - Select the 3 created contacts
  - Open `Action` menu and click on `Merge`

Issues:

  - Archived partner is set by default as destination partner.
  - Archived partner is not displayed in the list of partners to merge.

Cause:

  - The default destination partner set is the last record of the record
    set returned by `_get_ordered_partner` (where `Archived` partners
    are the last ones in the record set).
  - The field `partner_ids` does not allow archived records.

Solution:

  - Sort the partners to have the archived ones on top (since we took
    the last one).
  - Set `active_test` to False in the context of the partner_ids field.

opw-3205577

closes odoo/odoo#114508

X-original-commit: 8d0cec6844b248d3787bd7d6b8868d930d25e38c
Signed-off-by: Xavier Morel (xmo) <xmo@odoo.com>
Signed-off-by: Nasreddin Boulif (bon) <bon@odoo.com>
This commit is contained in:
Nasreddin Boulif (bon)
2023-03-07 02:57:02 +01:00
parent e55edef6ae
commit 8e299fb2ee
2 changed files with 21 additions and 2 deletions
@@ -66,7 +66,7 @@ class MergePartnerAutomatic(models.TransientModel):
number_group = fields.Integer('Group of Contacts', readonly=True)
current_line_id = fields.Many2one('base.partner.merge.line', string='Current Line')
line_ids = fields.One2many('base.partner.merge.line', 'wizard_id', string='Lines')
partner_ids = fields.Many2many('res.partner', string='Contacts')
partner_ids = fields.Many2many('res.partner', string='Contacts', context={'active_test': False})
dst_partner_id = fields.Many2one('res.partner', string='Destination Contact')
exclude_contact = fields.Boolean('A user associated to the contact')
@@ -429,7 +429,7 @@ class MergePartnerAutomatic(models.TransientModel):
:param partner_ids : list of partner ids to sort
"""
return self.env['res.partner'].browse(partner_ids).sorted(
key=lambda p: (p.active, (p.create_date or datetime.datetime(1970, 1, 1))),
key=lambda p: (not p.active, (p.create_date or datetime.datetime(1970, 1, 1))),
reverse=True,
)
@@ -169,6 +169,25 @@ class One2manyCase(TransactionCase):
self.assertTrue(model2.exists())
self.assertEqual(model2.pArTneR_321_id, p1)
def test_merge_partner_archived(self):
partner = self.env['res.partner']
p1 = partner.create({'name': 'test1'})
p2 = partner.create({'name': 'test2'})
p3 = partner.create({'name': 'test3', 'active': False})
partners_ids = (p1 + p2 + p3)
wizard = self.env['base.partner.merge.automatic.wizard'].with_context(active_ids=partners_ids.ids, active_model='res.partner').create({})
self.assertEqual(wizard.partner_ids, partners_ids)
self.assertEqual(wizard.dst_partner_id, p2)
wizard.action_merge()
self.assertFalse(p1.exists())
self.assertTrue(p2.exists())
self.assertFalse(p3.exists())
def test_cache_invalidation(self):
""" Cache invalidation for one2many with integer inverse. """
record0 = self.env['test_new_api.attachment.host'].create({})