From 9f6540c41e05a1dae3738d8bf92b000334281300 Mon Sep 17 00:00:00 2001 From: David Beguin Date: Wed, 25 Sep 2019 12:15:03 +0000 Subject: [PATCH] [FIX] crm : update both auto and manual probability in PLS cron when runnning the PLS cron, only the automated probability was updated with the new values. But dissociating probability and automated probability (for the leads that were in auto mode), breaks the autom mode and set implicitelly the mode to manual. (as probability and automated_probability are not equal anymore). This commit update also the probability only in the case both fields were equal before the cron process. Task ID: 2044539 PR #37413 --- addons/crm/models/crm_lead.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/addons/crm/models/crm_lead.py b/addons/crm/models/crm_lead.py index 568aff28e73..c2a2bb40759 100644 --- a/addons/crm/models/crm_lead.py +++ b/addons/crm/models/crm_lead.py @@ -1465,8 +1465,15 @@ class Lead(models.Model): lead_probabilities = leads_to_update._pls_get_naive_bayes_probabilities(batch_mode=True) # Update in execute batch to avoid server roundtrips + page_size to 10000 to avoid memory errors - sql = "update crm_lead set automated_probability = %s where id = %s" - batch_params = [(lead_probabilities[lead.id], lead.id) for lead in leads_to_update if lead.id in lead_probabilities] + # Update both probability and automated_probability if they were equal, else, update only automated_probability + sql = """UPDATE crm_lead + SET automated_probability = %s, + probability = CASE WHEN (ROUND(probability::numeric, 2) = ROUND(automated_probability::numeric, 2) or probability is null) + THEN (%s) + ELSE (probability) + END + WHERE id = %s""" + batch_params = [(lead_probabilities[lead.id], lead_probabilities[lead.id], lead.id) for lead in leads_to_update if lead.id in lead_probabilities] extras.execute_batch(self._cr, sql, batch_params, page_size=10000) _logger.info("Predictive Lead Scoring : all automated probability updated (count: %d)" % (len(leads_to_update)))