Skip to content

Commit ff2a694

Browse files
[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#72088 Signed-off-by: Thibault Delavallee (tde) <[email protected]> Co-authored-by: Thibault Delavallée <[email protected]> Co-authored-by: Thibault François <[email protected]>
1 parent dc8d82b commit ff2a694

File tree

2 files changed

+28
-23
lines changed

2 files changed

+28
-23
lines changed

addons/crm/models/crm_lead.py

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -298,20 +298,28 @@ def _compute_company_id(self):
298298
for lead in self:
299299
proposal = lead.company_id
300300

301-
# invalidate wrong configuration: company not in responsible companies or in team company if set
302-
if proposal and lead.user_id and proposal not in lead.user_id.company_ids:
303-
proposal = False
304-
if proposal and lead.team_id.company_id and proposal != lead.team_id.company_id:
305-
proposal = False
301+
# invalidate wrong configuration
302+
if proposal:
303+
# company not in responsible companies
304+
if lead.user_id and proposal not in lead.user_id.company_ids:
305+
proposal = False
306+
# inconsistent
307+
if lead.team_id.company_id and proposal != lead.team_id.company_id:
308+
proposal = False
309+
# void company on team and no assignee
310+
if lead.team_id and not lead.team_id.company_id and not lead.user_id:
311+
proposal = False
312+
# no user and no team -> void company and let assignment do its job
313+
if not lead.team_id and not lead.user_id:
314+
proposal = False
306315

307316
# propose a new company based on responsible, limited by team
308317
if not proposal:
309-
if not lead.user_id or lead.user_id == self.env.user:
310-
proposal = self.env.company
311-
elif lead.user_id:
312-
proposal = lead.user_id.company_id
313-
314-
if lead.team_id.company_id and proposal != lead.team_id.company_id:
318+
if lead.user_id:
319+
proposal = lead.team_id.company_id or lead.user_id.company_id
320+
elif lead.team_id:
321+
proposal = lead.team_id.company_id
322+
else:
315323
proposal = False
316324

317325
# set a new company

addons/crm/tests/test_crm_lead_multicompany.py

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,7 @@ def test_lead_mc_company_computation(self):
3232
'team_id': False,
3333
'user_id': False,
3434
})
35-
# self.assertFalse(lead_no_team.company_id) FIXME: should not limit company
36-
self.assertEqual(lead_no_team.company_id, self.user_sales_manager_mc.company_id)
35+
self.assertFalse(lead_no_team.company_id)
3736
self.assertFalse(lead_no_team.team_id)
3837
self.assertFalse(lead_no_team.user_id)
3938

@@ -48,17 +47,15 @@ def test_lead_mc_company_computation(self):
4847

4948
# Update team wo company: reset lead company also
5049
lead_team_c2.team_id = self.sales_team_1
51-
# self.assertFalse(lead_team_c2.company_id) FIXME: currently kept
52-
self.assertEqual(lead_team_c2.company_id, self.company_2)
50+
self.assertFalse(lead_team_c2.company_id)
5351

5452
# Lead with global team has no company
5553
lead_team_no_company = self.env['crm.lead'].create({
5654
'name': 'No company',
5755
'team_id': self.sales_team_1.id,
5856
'user_id': False,
5957
})
60-
# self.assertFalse(lead_no_team.company_id) FIXME: should not limit company
61-
self.assertEqual(lead_no_team.company_id, self.user_sales_manager_mc.company_id)
58+
self.assertFalse(lead_no_team.company_id)
6259

6360
# Update team w company updates company
6461
lead_team_no_company.team_id = self.team_company2
@@ -89,12 +86,14 @@ def test_lead_mc_company_form(self):
8986
self.assertEqual(crm_lead_form.user_id, self.user_sales_manager_mc)
9087
self.assertEqual(crm_lead_form.team_id, self.env['crm.team'])
9188

92-
# remove both
89+
# remove both: void company to ease assignment
9390
crm_lead_form.user_id = self.env['res.users']
94-
self.assertEqual(crm_lead_form.company_id, self.company_2)
91+
self.assertEqual(crm_lead_form.company_id, self.env['res.company'])
9592
self.assertEqual(crm_lead_form.user_id, self.env['res.users'])
9693
self.assertEqual(crm_lead_form.team_id, self.env['crm.team'])
9794

95+
# force company manually
96+
crm_lead_form.company_id = self.company_2
9897
lead = crm_lead_form.save()
9998

10099
# user_sales_manager cannot read it due to MC rules
@@ -126,12 +125,10 @@ def test_lead_mc_company_form_progressives_setup(self):
126125
'team_id': False,
127126
})
128127
crm_lead_form = Form(lead)
129-
# self.assertEqual(crm_lead_form.company_id, self.env['res.company']) FIXME
130-
self.assertEqual(crm_lead_form.company_id, self.company_2)
128+
self.assertEqual(crm_lead_form.company_id, self.env['res.company'])
131129

132130
crm_lead_form.team_id = self.sales_team_1
133-
# self.assertEqual(crm_lead_form.company_id, self.env['res.company']) # FIXME
134-
self.assertEqual(crm_lead_form.company_id, self.company_2)
131+
self.assertEqual(crm_lead_form.company_id, self.env['res.company'])
135132

136133
crm_lead_form.user_id = self.env.user
137134
# self.assertEqual(crm_lead_form.company_id, self.env['res.company']) # FIXME

0 commit comments

Comments
 (0)