From 962d8a443ba556e16c1794d6dec827ca2a94748d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thibault=20Delavall=C3=A9e?= Date: Fri, 28 May 2021 10:43:14 +0000 Subject: [PATCH] [FIX] crm: set lost leads as last when sorting by confidence level When sorting by confidence level, put lost (inactive) leads last. This should not happen when using the merge tool that filter out inactive leads but may happen in case of manual merge. Task Id-2457941 COM PR odoo/odoo#68884 --- addons/crm/models/crm_lead.py | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/addons/crm/models/crm_lead.py b/addons/crm/models/crm_lead.py index a8d5202399e..42a51605f3b 100644 --- a/addons/crm/models/crm_lead.py +++ b/addons/crm/models/crm_lead.py @@ -1549,12 +1549,24 @@ class Lead(models.Model): return self.with_context(active_test=False).search(domain) def _sort_by_confidence_level(self, reverse=False): - """ Sorting the leads/opps according to the confidence level of its stage, which relates to the probability of winning it - The confidence level increases with the stage sequence - An Opportunity always has higher confidence level than a lead + """ Sorting the leads/opps according to the confidence level to it + being won. It is sorted following this incremental heuristics : + + * "not lost" first (inactive leads are lost); normally all leads + should be active but in case lost one, they are always last. + Inactive opportunities are considered as valid; + * opportunity is more reliable than a lead which is a pre-stage + used mainly for first classification; + * stage sequence: the higher the better as it indicates we are moving + towards won stage; + * ID: the higher the better when all other parameters are equal. We + consider newer leads to be more reliable; """ def opps_key(opportunity): - return opportunity.type == 'opportunity', opportunity.stage_id.sequence, -opportunity._origin.id + return opportunity.type == 'opportunity' or opportunity.active, \ + opportunity.type == 'opportunity', \ + opportunity.stage_id.sequence, \ + -opportunity._origin.id return self.sorted(key=opps_key, reverse=reverse)