From c9317a3f47ada89e34101b4045d84544a4fc4c0b Mon Sep 17 00:00:00 2001 From: David Beguin Date: Mon, 23 Sep 2019 14:10:38 +0000 Subject: [PATCH] [FIX] crm : compute PLS probability only if stage_id is set As PLS is based on stage flow, probability will return 0 if stage_id is not set. Task ID: 2044539 PR #37413 --- addons/crm/models/crm_lead.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/addons/crm/models/crm_lead.py b/addons/crm/models/crm_lead.py index bdb39032b89..ab72b177edc 100644 --- a/addons/crm/models/crm_lead.py +++ b/addons/crm/models/crm_lead.py @@ -1372,6 +1372,12 @@ class Lead(models.Model): save_team_id = None p_won, p_lost = 1, 1 for lead_id, lead_values in leads_values_dict.items(): + # if stage_id is null, return 0 and bypass computation + lead_fields = [value[0] for value in lead_values.get('values', [])] + if not 'stage_id' in lead_fields: + lead_probabilities[lead_id] = 0 + continue + lead_team_id = lead_values['team_id'] if lead_values['team_id'] else 0 # team_id = None -> Convert to 0 lead_team_id = lead_team_id if lead_team_id in result else -1 # team_id not in frequency Table -> convert to -1 if lead_team_id != save_team_id: @@ -1429,7 +1435,7 @@ class Lead(models.Model): # Get also all the lead without probability --> These are the new leads. Activate auto probability on them. pls_start_date = self._pls_get_safe_start_date() if pls_start_date: - pending_lead_domain = ['&', ('create_date', '>', pls_start_date), + pending_lead_domain = ['&', '&', ('stage_id', '!=', False), ('create_date', '>', pls_start_date), '|', ('probability', '=', False), '&', ('probability', '<', 100), ('probability', '>', 0)] leads_to_update = self.env['crm.lead'].search(pending_lead_domain) lead_probabilities = leads_to_update._pls_get_naive_bayes_probabilities(batch_mode=True) @@ -1652,7 +1658,7 @@ class Lead(models.Model): :param first_stage_id: stage with smallest sequence :return: won count, lost count and total count for all records in frequencies """ - if str(first_stage_id.id) not in team_results['stage_id']: + if str(first_stage_id.id) not in team_results.get('stage_id', []): return 0, 0, 0 stage_result = team_results['stage_id'][str(first_stage_id.id)] return stage_result['won'], stage_result['lost'], stage_result['won'] + stage_result['lost']