From 15e2a1ebe2e64b752fb1c1d6bc422beb7d548b78 Mon Sep 17 00:00:00 2001 From: adda-odoo <adda@odoo.com> Date: Thu, 9 Mar 2023 18:48:33 +0000 Subject: [PATCH] [FIX] crm: add minimum of return value to _get_assignment_quota The method _assign_and_convert_leads() gets called when the CRM: Assign Leads cron is called. The values in the list `weights` gets caluclated in a way that memebers with a lower `lead_month`count` value gets a higher weight when randomizing assignement of a new lead. Assuming that the max assignment per member is consistent (or default = 30). Assume that each sale member belonging to any team has around 500 leads assigned to them the previous month(`lead_month_count`) and `work_days` is set to `0.2`. The return value of the method `_get_assignement_quota()` in this case would be 0 for every member, which causes the list `weights` to get populated with just zeros. This raises a `ValueError: Total of weights must be greater than zero`. Fix: Make sure the minimum weight for each member is at least 1 and not 0. opw-3171085 closes odoo/odoo#118156 Signed-off-by: Thibault Delavallee (tde) <tde@openerp.com> --- addons/crm/models/crm_team_member.py | 2 +- addons/crm/tests/test_crm_lead_assignment.py | 29 ++++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/addons/crm/models/crm_team_member.py b/addons/crm/models/crm_team_member.py index 3e4057725654..e6333e01fbea 100644 --- a/addons/crm/models/crm_team_member.py +++ b/addons/crm/models/crm_team_member.py @@ -156,7 +156,7 @@ class Team(models.Model): # auto-commit except in testing mode auto_commit = not getattr(threading.current_thread(), 'testing', False) commit_bundle_size = int(self.env['ir.config_parameter'].sudo().get_param('crm.assignment.commit.bundle', 100)) - while population: + while population and any(weights): counter += 1 member_id = random.choices(population, weights=weights, k=1)[0] member_index = population.index(member_id) diff --git a/addons/crm/tests/test_crm_lead_assignment.py b/addons/crm/tests/test_crm_lead_assignment.py index e528b5a87cb8..4379290cbb70 100644 --- a/addons/crm/tests/test_crm_lead_assignment.py +++ b/addons/crm/tests/test_crm_lead_assignment.py @@ -533,3 +533,32 @@ class TestLeadAssign(TestLeadAssignCommon): self.assertFalse(dupe_lead.exists()) 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') + + def test_no_assign_if_exceed_max_assign(self): + """ Test no leads being assigned to any team member if weights list sums to 0""" + + leads = self._create_leads_batch( + lead_type='lead', + user_ids=[False], + count=1 + ) + + sales_team_4 = self.env['crm.team'].create({ + 'name': 'Sales Team 4', + 'sequence': 15, + 'use_leads': True, + }) + sales_team_4_m1 = self.env['crm.team.member'].create({ + 'user_id': self.user_sales_salesman.id, + 'crm_team_id': sales_team_4.id, + 'assignment_max': 30, + }) + + sales_team_4_m1.lead_month_count = 50 + leads.team_id = sales_team_4.id + + members_data = sales_team_4_m1._assign_and_convert_leads(work_days=0.2) + self.assertEqual( + len(members_data[sales_team_4_m1]['assigned']), + 0, + "If team member has lead count greater than max assign,then do not assign any more") -- GitLab