From e5f35a60743185c7aee2043ce3c0db93157125e7 Mon Sep 17 00:00:00 2001 From: Hubert Van de Walle Date: Tue, 1 Feb 2022 10:42:32 +0000 Subject: [PATCH] [FIX] crm, sales_team: compute company within allowed_company_ids Steps to follow - Select only one company in the company selector but not the default one from the current user; - Go the the CRM app; - Create a lead; -> A multi company error appears ! Solution - The default team is now restricted to the companies in the context - When computing a lead's company_id when the team has no company, keep only the ones within the allowed_company_ids; opw-2713757 closes odoo/odoo#83764 X-original-commit: 58b9b3e839c7725fdcd66376564ce6554a4b82ac Signed-off-by: Thibault Delavallee (tde) --- addons/crm/models/crm_lead.py | 6 +++-- .../crm/tests/test_crm_lead_multicompany.py | 24 +++++++++---------- addons/sales_team/models/crm_team.py | 2 +- 3 files changed, 17 insertions(+), 15 deletions(-) diff --git a/addons/crm/models/crm_lead.py b/addons/crm/models/crm_lead.py index d7b909347fb..e1e17a045c6 100644 --- a/addons/crm/models/crm_lead.py +++ b/addons/crm/models/crm_lead.py @@ -295,8 +295,10 @@ class Lead(models.Model): # propose a new company based on responsible, limited by team if not proposal: - if lead.user_id: - proposal = lead.team_id.company_id or lead.user_id.company_id + if lead.user_id and 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 else: diff --git a/addons/crm/tests/test_crm_lead_multicompany.py b/addons/crm/tests/test_crm_lead_multicompany.py index 6848cba3acf..3c2ecf15789 100644 --- a/addons/crm/tests/test_crm_lead_multicompany.py +++ b/addons/crm/tests/test_crm_lead_multicompany.py @@ -101,20 +101,20 @@ class TestCRMLeadMultiCompany(TestCrmCommon): lead = LeadUnsyncCids.sudo().create({ 'name': 'My Lead MC', }) - self.assertEqual(lead.company_id, self.company_2) - self.assertEqual(lead.team_id, self.team_company2) - # self.assertEqual(lead.team_id, self.sales_team_1, - # 'Lead: due to MC rule, took first availability in other company') + 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) - # multicompany raises if trying to create manually - with self.assertRaises(AccessError): - lead = LeadUnsyncCids.create({ - 'name': 'My Lead MC', - }) - # self.assertEqual(lead.company_id.id, False) - # self.assertEqual(lead.team_id, self.sales_team_1) - # self.assertEqual(lead.user_id, self.user_sales_manager_mc) + # manual creation + lead = 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) @users('user_sales_manager_mc') def test_lead_mc_company_form(self): diff --git a/addons/sales_team/models/crm_team.py b/addons/sales_team/models/crm_team.py index 034efd50ce0..0cc1c28b78b 100644 --- a/addons/sales_team/models/crm_team.py +++ b/addons/sales_team/models/crm_team.py @@ -45,7 +45,7 @@ class CrmTeam(models.Model): user = self.env.user else: user = self.env['res.users'].sudo().browse(user_id) - valid_cids = [False] + user.company_ids.ids + valid_cids = [False] + [c for c in user.company_ids.ids if c in self.env.companies.ids] # 1- find in user memberships - note that if current user in C1 searches # for team belonging to a user in C1/C2 -> only results for C1 will be returned