[IMP] crm: remove default PLS fields

The purpose is to ease the onboarding for the default CRM settings.
Now, we do not include any field by default and tags become an option
in the choice of fields apply to the computation.

task-2196200

closes odoo/odoo#48124

Signed-off-by: Thibault Delavallee (tde) <tde@openerp.com>
This commit is contained in:
Jérémy Hennecart
2020-11-05 11:07:11 +00:00
parent 3ec29ccd94
commit 054fb95d73
3 changed files with 37 additions and 12 deletions
+4 -1
View File
@@ -20,9 +20,12 @@
<record id="frequency_field_lang_id" model="crm.lead.scoring.frequency.field">
<field name="field_id" ref="crm.field_crm_lead__lang_id"/>
</record>
<record id="frequency_field_tag_ids" model="crm.lead.scoring.frequency.field">
<field name="field_id" ref="crm.field_crm_lead__tag_ids"/>
</record>
<record id="crm_pls_fields_param" model="ir.config_parameter">
<field name="key">crm.pls_fields</field>
<field name="value" eval="'state_id,country_id,phone_state,email_state,source_id'"/>
<field name="value" eval="'phone_state,email_state'"/>
</record>
<record id="crm_pls_start_date_param" model="ir.config_parameter">
<field name="key">crm.pls_start_date</field>
+19 -10
View File
@@ -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
+14 -1
View File
@@ -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)