diff --git a/addons/crm/data/crm_lead_prediction_data.xml b/addons/crm/data/crm_lead_prediction_data.xml
index 83e62782b3c..472850dcf3d 100644
--- a/addons/crm/data/crm_lead_prediction_data.xml
+++ b/addons/crm/data/crm_lead_prediction_data.xml
@@ -20,9 +20,12 @@
+
+
+
crm.pls_fields
-
+
crm.pls_start_date
diff --git a/addons/crm/models/crm_lead.py b/addons/crm/models/crm_lead.py
index 8a8b618edf6..8257b958420 100644
--- a/addons/crm/models/crm_lead.py
+++ b/addons/crm/models/crm_lead.py
@@ -359,7 +359,7 @@ class Lead(models.Model):
for lead in self:
lead.is_automated_probability = tools.float_compare(lead.probability, lead.automated_probability, 2) == 0
- @api.depends(lambda self: ['tag_ids', 'stage_id', 'team_id'] + self._pls_get_safe_fields())
+ @api.depends(lambda self: ['stage_id', 'team_id'] + self._pls_get_safe_fields())
def _compute_probabilities(self):
lead_probabilities = self._pls_get_naive_bayes_probabilities()
for lead in self:
@@ -1915,8 +1915,7 @@ class Lead(models.Model):
def _pls_prepare_frequencies(self, lead_values, leads_pls_fields, target_state=None):
"""new state is used when getting frequencies for leads that are changing to lost or won.
Stays none if we are checking frequencies for leads already won or lost."""
- # Frequencies must include tag_id
- pls_fields = set(leads_pls_fields + ['tag_id'])
+ pls_fields = leads_pls_fields.copy()
frequencies = dict((field, {}) for field in pls_fields)
stage_ids = self.env['crm.stage'].search_read([], ['sequence', 'name', 'id'], order='sequence')
@@ -1985,6 +1984,11 @@ class Lead(models.Model):
leads_values_dict = OrderedDict()
pls_fields = ["stage_id", "team_id"] + self._pls_get_safe_fields()
+ # Check if tag_ids is in the pls_fields and removed it from the list. The tags will be managed separately.
+ use_tags = 'tag_ids' in pls_fields
+ if use_tags:
+ pls_fields.remove('tag_ids')
+
if domain:
# active_test = False as domain should take active into 'active' field it self
from_clause, where_clause, where_params = self.env['crm.lead'].with_context(active_test=False)._where_calc(domain).get_sql()
@@ -2000,15 +2004,19 @@ class Lead(models.Model):
self._cr.execute(query, where_params)
lead_results = self._cr.dictfetchall()
- # Get tags values
- query = """SELECT crm_lead.id as lead_id, t.id as tag_id
+ if use_tags:
+ # Get tags values
+ query = """SELECT crm_lead.id as lead_id, t.id as tag_id
FROM %s
LEFT JOIN crm_tag_rel rel ON crm_lead.id = rel.lead_id
LEFT JOIN crm_tag t ON rel.tag_id = t.id
WHERE %s order by crm_lead.team_id asc"""
- query = sql.SQL(query % (from_clause, where_clause)).format(*args)
- self._cr.execute(query, where_params)
- tag_results = self._cr.dictfetchall()
+ args.append(sql.Identifier('tag_id'))
+ query = sql.SQL(query % (from_clause, where_clause)).format(*args)
+ self._cr.execute(query, where_params)
+ tag_results = self._cr.dictfetchall()
+ else:
+ tag_results = []
# get all (variable, value) couple for all in self
for lead in lead_results:
@@ -2036,7 +2044,8 @@ class Lead(models.Model):
value = lead[field].id if isinstance(lead[field], models.BaseModel) else lead[field]
if value or field in ('email_state', 'phone_state'):
lead_values.append((field, value))
- for tag in lead.tag_ids:
- lead_values.append(('tag_id', tag.id))
+ if use_tags:
+ for tag in lead.tag_ids:
+ lead_values.append(('tag_id', tag.id))
leads_values_dict[lead.id] = {'values': lead_values, 'team_id': lead['team_id'].id}
return leads_values_dict
diff --git a/addons/crm/tests/test_crm_pls.py b/addons/crm/tests/test_crm_pls.py
index 24c50111832..bb070187c8c 100644
--- a/addons/crm/tests/test_crm_pls.py
+++ b/addons/crm/tests/test_crm_pls.py
@@ -98,7 +98,7 @@ class TestCRMPLS(TransactionCase):
# Set the PLS config
self.env['ir.config_parameter'].sudo().set_param("crm.pls_start_date", "2000-01-01")
- self.env['ir.config_parameter'].sudo().set_param("crm.pls_fields", "country_id,state_id,email_state,phone_state,source_id")
+ self.env['ir.config_parameter'].sudo().set_param("crm.pls_fields", "country_id,state_id,email_state,phone_state,source_id,tag_ids")
# set leads as won and lost
# for Team 1
@@ -376,3 +376,16 @@ class TestCRMPLS(TransactionCase):
self.assertEqual(tools.float_compare(leads[3].automated_probability, 4.21, 2), 0)
self.assertEqual(tools.float_compare(leads[8].automated_probability, 0.23, 2), 0)
+
+ # remove tag_ids from the calculation
+ self.assertEqual(tools.float_compare(lead_tag_1.automated_probability, 28.6, 2), 0)
+ self.assertEqual(tools.float_compare(lead_tag_2.automated_probability, 28.6, 2), 0)
+ self.assertEqual(tools.float_compare(lead_tag_1_2.automated_probability, 28.6, 2), 0)
+
+ lead_tag_1.tag_ids = [(5, 0, 0)] # remove all tags
+ lead_tag_2.tag_ids = [(4, tag_ids[0])] # add tag 1
+ lead_tag_1_2.tag_ids = [(3, tag_ids[1], 0)] # remove tag 2
+
+ self.assertEqual(tools.float_compare(lead_tag_1.automated_probability, 28.6, 2), 0)
+ self.assertEqual(tools.float_compare(lead_tag_2.automated_probability, 28.6, 2), 0)
+ self.assertEqual(tools.float_compare(lead_tag_1_2.automated_probability, 28.6, 2), 0)