[FIX] crm: prevent division by zero in lead probability computation

If for some reason we cannot compute lead probability, we should simply
continue to the next value without causing a division by zero.

For example, this can happen if all stages are team specific, as there is
a current limitation regarding the first stage (used to know how many lost
and won there is) that requires to have no team assigned to it. This is a
side effect of the commit https://github.com/odoo/odoo/commit/cd291b79eb2d2df80899867263ec71438ab8fe87 introduced in V14, and we should add
a test to ensure we do not crash in that case.

closes odoo/odoo#76379

X-original-commit: 9793bb29fe6c4d0d59905b2fcfe78ebe51e17579
Signed-off-by: Thibault Delavallee (tde) <tde@openerp.com>
Signed-off-by: Alex Tuyls <alt-odoo@users.noreply.github.com>
This commit is contained in:
alt-odoo
2021-09-14 10:51:14 +00:00
parent 4cc00d44d6
commit c80648f16c
2 changed files with 16 additions and 0 deletions
+3
View File
@@ -1956,6 +1956,9 @@ class Lead(models.Model):
total_won = team_won if field == 'stage_id' else field_result['won_total']
total_lost = team_lost if field == 'stage_id' else field_result['lost_total']
# if one count = 0, we cannot compute lead probability
if not total_won or not total_lost:
continue
s_lead_won *= value_result['won'] / total_won
s_lead_lost *= value_result['lost'] / total_lost
+13
View File
@@ -500,6 +500,19 @@ class TestCRMPLS(TransactionCase):
self.assertEqual(Date.to_string(res_config_new.predictive_lead_scoring_start_date),
str_date_8_days_ago, "If config param is not a valid date, date in settings should be set to 8 days before today")
def test_pls_no_share_stage(self):
""" We test here the situation where all stages are team specific, as there is
a current limitation (can be seen in _pls_get_won_lost_total_count) regarding
the first stage (used to know how many lost and won there is) that requires
to have no team assigned to it."""
Lead = self.env['crm.lead']
team_id = self.env['crm.team'].create([{'name': 'Team Test'}]).id
self.env['crm.stage'].search([('team_id', '=', False)]).write({'team_id': team_id})
lead = Lead.create({'name': 'team', 'team_id': team_id, 'probability': 41.23})
Lead._cron_update_automated_probabilities()
self.assertEqual(tools.float_compare(lead.probability, 41.23, 2), 0)
self.assertEqual(tools.float_compare(lead.automated_probability, 0, 2), 0)
@users('user_sales_manager')
def test_team_unlink(self):
""" Test that frequencies are sent to "no team" when unlinking a team