[FIX/IMP] crm: allow more specific lead default company

# Purpose

Take into account the current company when creating a new lead if not defined
earlier.

# Specifications

If there is no company provided by the sales team and the user belongs
to several companies and several companies are allowed by context,
we preferentially take the current company over the user's company if allowed.

# Additional considerations

This commit
* builds on the multi-company fix brought by e5f35a60 avoiding the restricted
access error when setting to the user's base company if not allowed by context,
but will also allow to first preferentially use the current company instead of None.
* Complete tests

Task-2824913

X-original-commit: ce62780153183545ba423a7a8d945d9665cdc50e
Part-of: odoo/odoo#97136
Co-authored-by: Florian Charlier <flch@odoo.com>
Co-authored-by: Fabio Barbero <faba@odoo.com>
This commit is contained in:
Florian Charlier
2022-08-16 09:18:56 +02:00
co-authored by Fabio Barbero
parent 61c9f6aec0
commit 6f81b5e0a8
4 changed files with 93 additions and 30 deletions
+6 -5
View File
@@ -304,14 +304,15 @@ class Lead(models.Model):
(not lead.partner_id or lead.partner_id.company_id != proposal):
proposal = False
# propose a new company based on responsible, limited by team
# propose a new company based on team > user (respecting context) > partner
if not proposal:
if lead.user_id and lead.team_id.company_id:
if lead.team_id.company_id:
proposal = lead.team_id.company_id
elif lead.user_id:
proposal = lead.user_id.company_id & self.env.companies
elif lead.team_id:
proposal = lead.team_id.company_id
if self.env.company in lead.user_id.company_ids:
proposal = self.env.company
else:
proposal = lead.user_id.company_id & self.env.companies
elif lead.partner_id:
proposal = lead.partner_id.company_id
else:
+77 -15
View File
@@ -90,32 +90,94 @@ class TestCRMLeadMultiCompany(TestCrmCommon):
@users('user_sales_manager_mc')
def test_lead_mc_company_computation_env_user_restrict(self):
""" Check that the computed company is allowed (aka in self.env.companies).
User is logged in company_main even their default default company is
company_2. """
If the assigned team has a set company, the lead has the same one. Otherwise, use one
allowed by user, preferentially choose env current company, user's company otherwise."""
# User is logged in company_main even their default company is company_2
LeadUnsyncCids = self.env['crm.lead'].with_context(allowed_company_ids=[self.company_main.id])
self.assertEqual(LeadUnsyncCids.env.company, self.company_main)
self.assertEqual(LeadUnsyncCids.env.companies, self.company_main)
self.assertEqual(LeadUnsyncCids.env.user.company_id, self.company_2)
# simulate auto-creation through sudo (assignment-like)
lead = LeadUnsyncCids.sudo().create({
'name': 'My Lead MC',
lead_1_auto = LeadUnsyncCids.sudo().create({
'name': 'My Lead MC 1 Auto',
})
self.assertFalse(lead.company_id,
'Lead: due to MC rule, avoid setting a company when it would cause crashes')
self.assertEqual(lead.team_id, self.sales_team_1,
'Lead: due to MC rule, took first availability in other company')
self.assertEqual(lead.user_id, self.user_sales_manager_mc)
self.assertEqual(lead_1_auto.team_id, self.sales_team_1,
'[Auto/1] First available team in current company should have been assigned (fallback as user in no team in Main Company).')
self.assertEqual(lead_1_auto.company_id, self.company_main,
'[Auto/1] Current company should be set on the lead as no company was assigned given by team and company is allowed for user.')
self.assertEqual(lead_1_auto.user_id, self.user_sales_manager_mc, '[Auto/1] Current user should have been assigned.')
# manual creation
lead = LeadUnsyncCids.create({
lead_1_manual = LeadUnsyncCids.create({
'name': 'My Lead MC',
})
self.assertFalse(lead.company_id,
'Lead: due to MC rule, avoid setting a company when it would cause crashes')
self.assertEqual(lead.team_id, self.sales_team_1)
self.assertEqual(lead.user_id, self.user_sales_manager_mc)
self.assertEqual(lead_1_manual.team_id, self.sales_team_1,
'[Auto/1] First available team in current company should have been assigned (fallback as user in no team in Main Company).')
self.assertEqual(lead_1_manual.company_id, self.company_main,
'[Auto/1] Current company should be set on the lead as no company was given by team and company is allowed for user.')
self.assertEqual(lead_1_manual.user_id, self.user_sales_manager_mc, '[Manual/1] Current user should have been assigned.')
# Logged on other company will use that one for the lead company with sales_team_2 as is assigned to company_2
LeadUnsyncCids = self.env['crm.lead'].with_context(allowed_company_ids=[self.company_main.id, self.company_2.id])
LeadUnsyncCids = LeadUnsyncCids.with_company(self.company_2)
self.assertEqual(LeadUnsyncCids.env.company, self.company_2)
lead_2_auto = LeadUnsyncCids.sudo().create({
'name': 'My Lead MC 2 Auto',
})
self.assertEqual(lead_2_auto.team_id, self.team_company2,
'[Auto/2] First available team user is a member of, in current company, should have been assigned.')
self.assertEqual(lead_2_auto.company_id, self.company_2,
'[Auto/2] Current company should be set on the lead as company was assigned on team.')
self.assertEqual(lead_2_auto.user_id, self.user_sales_manager_mc, '[Auto/2] Current user should have been assigned.')
lead_2_manual = LeadUnsyncCids.create({
'name': 'My Lead MC 2 Manual',
})
self.assertEqual(lead_2_manual.team_id, self.team_company2,
'[Manual/2] First available team user is a member of, in current company, should have been assigned.')
self.assertEqual(lead_2_manual.company_id, self.company_2,
'[Manual/2] Current company should be set on the lead as company was assigned on team.')
self.assertEqual(lead_2_manual.user_id, self.user_sales_manager_mc, '[Manual/2] Current user should have been assigned.')
# If assigned team has no company, use company
self.team_company2.write({'company_id': False})
lead_3_auto = LeadUnsyncCids.sudo().create({
'name': 'My Lead MC 3 Auto',
})
self.assertEqual(lead_3_auto.team_id, self.team_company2,
'[Auto/3] First available team user is a member of should have been assigned (fallback as no team with same company defined).')
self.assertEqual(lead_3_auto.company_id, self.company_2,
'[Auto/3] Current company should be set on the lead as no company was given by team and company is allowed for user.')
self.assertEqual(lead_3_auto.user_id, self.user_sales_manager_mc, '[Auto/3] Current user should have been assigned.')
lead_3_manual = LeadUnsyncCids.create({
'name': 'My Lead MC 3 Manual',
})
self.assertEqual(lead_3_manual.company_id, self.company_2,
'[Auto/3] First available team user is a member of should have been assigned (fallback as no team with same company defined).')
self.assertEqual(lead_3_manual.team_id, self.team_company2,
'[Auto/3] Current company should be set on the lead as no company was given by team and company is allowed for user.')
self.assertEqual(lead_3_manual.user_id, self.user_sales_manager_mc, '[Manual/3] Current user should have been assigned.')
# If all teams have no company and don't have user as member, the first sales team is used.
self.team_company2.write({'member_ids': [(3, self.user_sales_manager_mc.id)]})
lead_4_auto = LeadUnsyncCids.sudo().create({
'name': 'My Lead MC 4 Auto',
})
self.assertEqual(lead_4_auto.team_id, self.sales_team_1,
'[Auto/4] As no team has current user as member nor current company as company_id, first available team should have been assigned.')
self.assertEqual(lead_4_auto.company_id, self.company_2,
'[Auto/4] Current company should be set on the lead as no company was given by team and company is allowed for user.')
self.assertEqual(lead_4_auto.user_id, self.user_sales_manager_mc, '[Auto/4] Current user should have been assigned.')
lead_4_manual = LeadUnsyncCids.create({
'name': 'My Lead MC 4 Manual',
})
self.assertEqual(lead_4_manual.company_id, self.company_2,
'[Manual/4] As no team has current user as member nor current company as company_id, first available team should have been assigned.')
self.assertEqual(lead_4_manual.team_id, self.sales_team_1,
'[Manual/4] Current company should be set on the lead as no company was given by team and company is allowed for user.')
self.assertEqual(lead_4_manual.user_id, self.user_sales_manager_mc,
'[Manual/4] Current user should have been assigned.')
@users('user_sales_manager_mc')
def test_lead_mc_company_computation_partner_restrict(self):
+1 -1
View File
@@ -175,7 +175,7 @@ class TestLeadAssignPerf(TestLeadAssignCommon):
leads.flush_recordset()
with self.with_user('user_sales_manager'):
with self.assertQueryCount(user_sales_manager=6279):
with self.assertQueryCount(user_sales_manager=6280):
self.env['crm.team'].browse(sales_teams.ids)._action_assign_leads(work_days=30)
# teams assign
@@ -234,7 +234,7 @@ class TestRegistrationPerformance(EventPerformanceCase):
"""
event = self.env['event.event'].browse(self.test_event.ids)
with freeze_time(self.reference_now), self.assertQueryCount(event_user=686): # tef only: 639 - com runbot 681
with freeze_time(self.reference_now), self.assertQueryCount(event_user=687): # tef only: 640 - com runbot 682
self.env.cr._now = self.reference_now # force create_date to check schedulers
registration_values = [
dict(reg_data,
@@ -280,7 +280,7 @@ class TestRegistrationPerformance(EventPerformanceCase):
form like) """
event = self.env['event.event'].browse(self.test_event.ids)
with freeze_time(self.reference_now), self.assertQueryCount(event_user=697): # tef only: 650 - com runbot 692
with freeze_time(self.reference_now), self.assertQueryCount(event_user=698): # tef only: 651 - com runbot 693
self.env.cr._now = self.reference_now # force create_date to check schedulers
registration_values = [
dict(reg_data,
@@ -301,7 +301,7 @@ class TestRegistrationPerformance(EventPerformanceCase):
""" Test a single registration creation using Form """
event = self.env['event.event'].browse(self.test_event.ids)
with freeze_time(self.reference_now), self.assertQueryCount(event_user=202): # tef only: 184 - com runbot: 189
with freeze_time(self.reference_now), self.assertQueryCount(event_user=203): # tef only: 185 - com runbot: 190
self.env.cr._now = self.reference_now # force create_date to check schedulers
with Form(self.env['event.registration']) as reg_form:
reg_form.event_id = event
@@ -317,7 +317,7 @@ class TestRegistrationPerformance(EventPerformanceCase):
""" Test a single registration creation using Form """
event = self.env['event.event'].browse(self.test_event.ids)
with freeze_time(self.reference_now), self.assertQueryCount(event_user=209): # tef only: 188 - com runbot: 193
with freeze_time(self.reference_now), self.assertQueryCount(event_user=210): # tef only: 189 - com runbot: 194
self.env.cr._now = self.reference_now # force create_date to check schedulers
with Form(self.env['event.registration']) as reg_form:
reg_form.event_id = event
@@ -330,7 +330,7 @@ class TestRegistrationPerformance(EventPerformanceCase):
""" Test a single registration creation using Form """
event = self.env['event.event'].browse(self.test_event.ids)
with freeze_time(self.reference_now), self.assertQueryCount(event_user=124): # tef only: 120? - com runbot 108 - ent runbot 122
with freeze_time(self.reference_now), self.assertQueryCount(event_user=125): # tef only: 121? - com runbot 109
self.env.cr._now = self.reference_now # force create_date to check schedulers
with Form(self.env['event.registration'].with_context(event_lead_rule_skip=True)) as reg_form:
reg_form.event_id = event
@@ -344,7 +344,7 @@ class TestRegistrationPerformance(EventPerformanceCase):
event = self.env['event.event'].browse(self.test_event.ids)
# simple customer data
with freeze_time(self.reference_now), self.assertQueryCount(event_user=125): # tef only: 106 - com runbot: 122
with freeze_time(self.reference_now), self.assertQueryCount(event_user=126): # tef only: 107 - com runbot: 123
self.env.cr._now = self.reference_now # force create_date to check schedulers
registration_values = dict(
self.customer_data[0],
@@ -358,7 +358,7 @@ class TestRegistrationPerformance(EventPerformanceCase):
event = self.env['event.event'].browse(self.test_event.ids)
# partner-based customer
with freeze_time(self.reference_now), self.assertQueryCount(event_user=129): # tef only: 118 - com runbot: 125
with freeze_time(self.reference_now), self.assertQueryCount(event_user=130): # tef only: 119 - com runbot: 126
self.env.cr._now = self.reference_now # force create_date to check schedulers
registration_values = {
'event_id': event.id,
@@ -373,7 +373,7 @@ class TestRegistrationPerformance(EventPerformanceCase):
event = self.env['event.event'].browse(self.test_event.ids)
# partner-based customer
with freeze_time(self.reference_now), self.assertQueryCount(event_user=46): # tef only: 40 - com runbot: 42
with freeze_time(self.reference_now), self.assertQueryCount(event_user=47): # tef only: 40 - com runbot: 42
self.env.cr._now = self.reference_now # force create_date to check schedulers
registration_values = {
'event_id': event.id,
@@ -388,7 +388,7 @@ class TestRegistrationPerformance(EventPerformanceCase):
event = self.env['event.event'].browse(self.test_event.ids)
# website customer data
with freeze_time(self.reference_now), self.assertQueryCount(event_user=135): # tef only: 124 - com runbot: 128
with freeze_time(self.reference_now), self.assertQueryCount(event_user=136): # tef only: 125 - com runbot: 129
self.env.cr._now = self.reference_now # force create_date to check schedulers
registration_values = dict(
self.website_customer_data[0],