[IMP] crm : set automated_probability = 100 when stage won

If the stage is set to won, automated probability should = 100%
Without forcing it, it should be the case as all lead at stage won
have been won. (thanks Captain Obvious!)

But if a user mark as lost a lead that is at won stage, it will reduce the
probability of the won stage.

Also, if the cron didn't run recently, it could be that the autmomated probability
will give the same result than in previous stage (if no lead have been won at all).

Task ID: 2044539
PR #37413
This commit is contained in:
David Beguin
2019-09-25 14:42:31 +00:00
parent c9317a3f47
commit b8dda50aaa
+9 -1
View File
@@ -1318,6 +1318,7 @@ class Lead(models.Model):
# get stages
first_stage_id = self.env['crm.stage'].search([], order='sequence', limit=1)
won_stage_ids = self.env['crm.stage'].search([('is_won', '=', True)]).ids
# Get all leads values, no matter the team_id
leads_values_dict = self._pls_get_lead_pls_values(batch_mode=batch_mode)
@@ -1326,8 +1327,11 @@ class Lead(models.Model):
# Get unique couples to search in frequency table
leads_values = set()
for values in leads_values_dict.values():
won_leads = set()
for lead_id, values in leads_values_dict.items():
for couple in values['values']:
if couple[0] == 'stage_id' and couple[1] in won_stage_ids:
won_leads.add(lead_id)
leads_values.add(couple)
# get all variable related records from frequency table, no matter the team_id
@@ -1377,6 +1381,10 @@ class Lead(models.Model):
if not 'stage_id' in lead_fields:
lead_probabilities[lead_id] = 0
continue
# if lead stage is won, return 100
elif lead_id in won_leads:
lead_probabilities[lead_id] = 100
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