[FIX] crm: Fix lead multicompany issue

Context
-------

Leads get assigned a different company than the one defined on the salesman
after being merged with another lead.

This happens because some leads assigned to a user_id have no company set. They
are merged with leads that receive self.env.company during assigment to the
to the team and then get merged with the lead without company. So the company
of the new lead is written on the old one.

Behavior before this PR
-----------------------

When the crm.team have the field company_id = False
With the following flow

 1) Create a lead without a user_id and a team_id
 2) Assign a team to the lead
 3) Assign a user_id

The status of the company field on the lead is the following

 1) set the company of the env.user
 2) Keep the company of the lead
 3) set the user company if the current company is not one of the allowed
    company of the user

Behavior after this PR
----------------------

 1) set the company of the env.user
 2) set the company of the team even if it's False
    (so erase the company if the team has no company set)
 3) set the user company if the current company is not one of the allowed
    company of the user

Other changes
-------------

When resetting team and user: void company to avoid having leads without
any information but a company set. It eases assignment.

Task-2520276

closes odoo/odoo#78860

X-original-commit: ff2a6949c3a59be98cd1849e7a2e38c6ccc3187e
Signed-off-by: Thibault Delavallee (tde) <tde@openerp.com>
Co-authored-by: Thibault Delavallée <tde@odoo.com>
Co-authored-by: Thibault François <tfr@odoo.com>
This commit is contained in:
Thibault François
2021-10-27 06:39:50 +00:00
committed by Thibault Delavallée
co-authored by Thibault Delavallée
parent d3f35714f6
commit de10f6093f
2 changed files with 28 additions and 23 deletions
+19 -11
View File
@@ -273,20 +273,28 @@ class Lead(models.Model):
for lead in self:
proposal = lead.company_id
# invalidate wrong configuration: company not in responsible companies or in team company if set
if proposal and lead.user_id and proposal not in lead.user_id.company_ids:
proposal = False
if proposal and lead.team_id.company_id and proposal != lead.team_id.company_id:
proposal = False
# invalidate wrong configuration
if proposal:
# company not in responsible companies
if lead.user_id and proposal not in lead.user_id.company_ids:
proposal = False
# inconsistent
if lead.team_id.company_id and proposal != lead.team_id.company_id:
proposal = False
# void company on team and no assignee
if lead.team_id and not lead.team_id.company_id and not lead.user_id:
proposal = False
# no user and no team -> void company and let assignment do its job
if not lead.team_id and not lead.user_id:
proposal = False
# propose a new company based on responsible, limited by team
if not proposal:
if not lead.user_id or lead.user_id == self.env.user:
proposal = self.env.company
elif lead.user_id:
proposal = lead.user_id.company_id
if lead.team_id.company_id and proposal != lead.team_id.company_id:
if lead.user_id:
proposal = lead.team_id.company_id or lead.user_id.company_id
elif lead.team_id:
proposal = lead.team_id.company_id
else:
proposal = False
# set a new company
+9 -12
View File
@@ -31,8 +31,7 @@ class TestCRMLeadMultiCompany(TestCrmCommon):
'team_id': False,
'user_id': False,
})
# self.assertFalse(lead_no_team.company_id) FIXME: should not limit company
self.assertEqual(lead_no_team.company_id, self.user_sales_manager_mc.company_id)
self.assertFalse(lead_no_team.company_id)
self.assertFalse(lead_no_team.team_id)
self.assertFalse(lead_no_team.user_id)
@@ -47,8 +46,7 @@ class TestCRMLeadMultiCompany(TestCrmCommon):
# Update team wo company: reset lead company also
lead_team_c2.team_id = self.sales_team_1
# self.assertFalse(lead_team_c2.company_id) FIXME: currently kept
self.assertEqual(lead_team_c2.company_id, self.company_2)
self.assertFalse(lead_team_c2.company_id)
# Lead with global team has no company
lead_team_no_company = self.env['crm.lead'].create({
@@ -56,8 +54,7 @@ class TestCRMLeadMultiCompany(TestCrmCommon):
'team_id': self.sales_team_1.id,
'user_id': False,
})
# self.assertFalse(lead_no_team.company_id) FIXME: should not limit company
self.assertEqual(lead_no_team.company_id, self.user_sales_manager_mc.company_id)
self.assertFalse(lead_no_team.company_id)
# Update team w company updates company
lead_team_no_company.team_id = self.team_company2
@@ -88,12 +85,14 @@ class TestCRMLeadMultiCompany(TestCrmCommon):
self.assertEqual(crm_lead_form.user_id, self.user_sales_manager_mc)
self.assertEqual(crm_lead_form.team_id, self.env['crm.team'])
# remove both
# remove both: void company to ease assignment
crm_lead_form.user_id = self.env['res.users']
self.assertEqual(crm_lead_form.company_id, self.company_2)
self.assertEqual(crm_lead_form.company_id, self.env['res.company'])
self.assertEqual(crm_lead_form.user_id, self.env['res.users'])
self.assertEqual(crm_lead_form.team_id, self.env['crm.team'])
# force company manually
crm_lead_form.company_id = self.company_2
lead = crm_lead_form.save()
# user_sales_manager cannot read it due to MC rules
@@ -125,12 +124,10 @@ class TestCRMLeadMultiCompany(TestCrmCommon):
'team_id': False,
})
crm_lead_form = Form(lead)
# self.assertEqual(crm_lead_form.company_id, self.env['res.company']) FIXME
self.assertEqual(crm_lead_form.company_id, self.company_2)
self.assertEqual(crm_lead_form.company_id, self.env['res.company'])
crm_lead_form.team_id = self.sales_team_1
# self.assertEqual(crm_lead_form.company_id, self.env['res.company']) # FIXME
self.assertEqual(crm_lead_form.company_id, self.company_2)
self.assertEqual(crm_lead_form.company_id, self.env['res.company'])
crm_lead_form.user_id = self.env.user
# self.assertEqual(crm_lead_form.company_id, self.env['res.company']) # FIXME