[FIX] crm: keep team when assigning and deduplicating leads

New leads are automatically assigned to sales team and salesman during assign
process. Some of those leads are duplicates of existing opportunities. If a
new lead is merged into an existing opportunity, the opportunity is the master
data and the lead is duplicated. However the opportunity should keep its sales
team and not be assigned to the team being under automatic assign.

Before this commit, the merge was forcing the crm team to the 'current' team
during auto assignment and thus it was changing the team of the opportunity.
In this commit we now assign team to lead to merge before the merge and do
not force the crm team during merge process.

LINKS

Task ID-2444908 (assign fixes)
Task ID-2489951 (assign process improvements)
COM PR odoo/odoo#70172

X-original-commit: 356a55053eb1cc49ae6eed0e58338cff08eddc42
This commit is contained in:
Thibault François
2021-05-12 14:02:19 +00:00
committed by Thibault Delavallée
parent d92ca08fd7
commit f2ff222fba
3 changed files with 8 additions and 3 deletions
+3 -2
View File
@@ -497,11 +497,12 @@ class Team(models.Model):
leads_assigned += lead
leads_done_ids.add(lead.id)
leads_assigned._handle_salesmen_assignment(user_ids=None, team_id=self.id)
duplicates_to_assign = self.env['crm.lead'].union(*leads_dups_dict.keys())
(leads_assigned | duplicates_to_assign)._handle_salesmen_assignment(user_ids=None, team_id=self.id)
for lead in leads.filtered(lambda lead: lead in leads_dups_dict):
lead_duplicates = leads_dups_dict[lead]
merged = lead_duplicates._merge_opportunity(user_id=False, team_id=self.id, max_length=0)
merged = lead_duplicates._merge_opportunity(user_id=False, team_id=False, max_length=0)
leads_dup_ids.update((lead_duplicates - merged).ids)
leads_merged_ids.add(merged.id)
+3 -1
View File
@@ -196,6 +196,8 @@ class TestLeadAssign(TestLeadAssignCommon):
self.env['crm.team'].browse(self.sales_teams.ids)._action_assign_leads(work_days=2)
# teams assign
# due to duplicate management keeping master team, we may not ensure leads to be
# fulfilling their original team volume
leads = self.env['crm.lead'].search([('id', 'in', leads.ids)]) # ensure order
leads_st1 = leads.filtered_domain([('team_id', '=', self.sales_team_1.id)])
leads_stc = leads.filtered_domain([('team_id', '=', self.sales_team_convert.id)])
@@ -411,5 +413,5 @@ class TestLeadAssign(TestLeadAssignCommon):
sales_team_dupe._action_assign_leads(work_days=2)
self.assertFalse(dupe_lead.exists())
self.assertEqual(master_opp.team_id, sales_team_dupe, 'Opportunity: current team wins lead even if was on duplicated lead')
self.assertEqual(master_opp.team_id, self.sales_team_1, 'Opportunity: should keep its sales team')
self.assertEqual(master_opp.user_id, self.user_sales_manager, 'Opportunity: should keep its salesman')
+2
View File
@@ -54,6 +54,8 @@ class TestLeadAssignPerf(TestLeadAssignCommon):
self.env['crm.team'].browse(self.sales_teams.ids)._action_assign_leads(work_days=2)
# teams assign
# due to duplicate management keeping master team, we may not ensure leads to be
# fulfilling their original team volume
leads = self.env['crm.lead'].search([('id', 'in', leads.ids)]) # ensure order
leads_st1 = leads.filtered_domain([('team_id', '=', self.sales_team_1.id)])
leads_stc = leads.filtered_domain([('team_id', '=', self.sales_team_convert.id)])