[IMP] crm: better use toggle_active and add business behavior in it

Purpose of this commit is to make use of toggle_active to implement business
behavior linked to active field being changed. In CRM notably

  * when archiving: it is considered as lost, and therefore probability is
    set to 0;
  * when reactivating: void the lost reason and update probabilities for PLS;

In this commit we also update tracking subtype to more clearly track
lost / restored subtypes

  * lost: writing a lost reason (resetting it is not lost), archiving
    it if no other subtype;
  * restored: activating it again;

Task ID 2170708
Community PR odoo/odoo#46563
This commit is contained in:
Thibault Delavallée
2020-04-03 12:59:28 +00:00
parent 318f02b8e8
commit 9b76c6b7f1
2 changed files with 19 additions and 7 deletions
+18 -5
View File
@@ -486,8 +486,17 @@ class Lead(models.Model):
# ------------------------------------------------------------
def toggle_active(self):
""" When archiving: mark probability as 0. When re-activating
update probability again, for leads and opportunities. """
res = super(Lead, self).toggle_active()
self.filtered(lambda lead: lead.active)._update_probability()
activated = self.filtered(lambda lead: lead.active)
archived = self.filtered(lambda lead: not lead.active)
if activated:
activated.write({'lost_reason': False})
activated._update_probability()
if archived:
archived.write({'probability': 0, 'automated_probability': 0})
archived._rebuild_pls_frequency_table_threshold()
return res
def _rebuild_pls_frequency_table_threshold(self):
@@ -506,12 +515,14 @@ class Lead(models.Model):
def action_set_lost(self, **additional_values):
""" Lost semantic: probability = 0 or active = False """
result = self.write({'active': False, 'probability': 0, 'automated_probability': 0, **additional_values})
self._rebuild_pls_frequency_table_threshold()
return result
res = self.action_archive()
if additional_values:
self.write(dict(additional_values))
return res
def action_set_won(self):
""" Won semantic: probability = 100 (active untouched) """
self.action_unarchive()
for lead in self:
stage_id = lead._stage_find(domain=[('is_won', '=', True)])
lead.write({'stage_id': stage_id.id, 'probability': 100})
@@ -1039,12 +1050,14 @@ class Lead(models.Model):
self.ensure_one()
if 'stage_id' in init_values and self.probability == 100 and self.stage_id:
return self.env.ref('crm.mt_lead_won')
elif 'lost_reason' in init_values:
elif 'lost_reason' in init_values and self.lost_reason:
return self.env.ref('crm.mt_lead_lost')
elif 'stage_id' in init_values:
return self.env.ref('crm.mt_lead_stage')
elif 'active' in init_values and self.active:
return self.env.ref('crm.mt_lead_restored')
elif 'active' in init_values and not self.active:
return self.env.ref('crm.mt_lead_lost')
return super(Lead, self)._track_subtype(init_values)
def _notify_get_groups(self):
+1 -2
View File
@@ -125,8 +125,7 @@ class Lead2OpportunityPartner(models.TransientModel):
def _action_merge(self):
to_merge = self.duplicated_lead_ids
result_opportunity = to_merge.merge_opportunity(auto_unlink=False)
if not result_opportunity.active:
result_opportunity.write({'active': True, 'lost_reason': False})
result_opportunity.action_unarchive()
if result_opportunity.type == "lead":
self._convert_and_allocate(result_opportunity, [self.user_id.id], team_id=self.team_id.id)