From 9b76c6b7f1eec4057289de90d62b8a7a266b0aac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thibault=20Delavall=C3=A9e?= Date: Fri, 28 Feb 2020 13:08:36 +0000 Subject: [PATCH] [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 --- addons/crm/models/crm_lead.py | 23 +++++++++++++++----- addons/crm/wizard/crm_lead_to_opportunity.py | 3 +-- 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/addons/crm/models/crm_lead.py b/addons/crm/models/crm_lead.py index 59540383fc7..f791d4be431 100644 --- a/addons/crm/models/crm_lead.py +++ b/addons/crm/models/crm_lead.py @@ -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): diff --git a/addons/crm/wizard/crm_lead_to_opportunity.py b/addons/crm/wizard/crm_lead_to_opportunity.py index e56dd0583d0..a00f76f12da 100644 --- a/addons/crm/wizard/crm_lead_to_opportunity.py +++ b/addons/crm/wizard/crm_lead_to_opportunity.py @@ -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)