[FW][FIX] crm: correctly reset date_closed when going out of won stage

Probability is not always given to write, hence date_closed is currently not
always reset. Indeed as it is a computed field it can be updated after stage
update, without being explicitly in values.

In this fix we consider that going out of won stage without any other hints
on probability is like being not won anymore, therefore resetting date closed.

Task-2917116
Closes odoo/odoo#93704
Closes odoo/odoo#93704

X-original-commit: 1cf1b572b10bfbd02581c12f89824a028c4f23e6
Part-of: odoo/odoo#96190
This commit is contained in:
Thibault Delavallée
2022-07-18 19:55:38 +02:00
parent 2f7fe3b016
commit 9cd840f25b
2 changed files with 21 additions and 5 deletions
+10 -5
View File
@@ -725,26 +725,31 @@ class Lead(models.Model):
def write(self, vals):
if vals.get('website'):
vals['website'] = self.env['res.partner']._clean_website(vals['website'])
stage_is_won = False
stage_updated, stage_is_won = vals.get('stage_id'), False
# stage change: update date_last_stage_update
if 'stage_id' in vals:
stage_id = self.env['crm.stage'].browse(vals['stage_id'])
if stage_id.is_won:
if stage_updated:
stage = self.env['crm.stage'].browse(vals['stage_id'])
if stage.is_won:
vals.update({'probability': 100, 'automated_probability': 100})
stage_is_won = True
# stage change with new stage: update probability and date_closed
if vals.get('probability', 0) >= 100 or not vals.get('active', True):
vals['date_closed'] = fields.Datetime.now()
elif vals.get('probability', 0) > 0:
vals['date_closed'] = False
elif stage_updated and not stage_is_won and not 'probability' in vals:
vals['date_closed'] = False
if any(field in ['active', 'stage_id'] for field in vals):
self._handle_won_lost(vals)
if not stage_is_won:
return super(Lead, self).write(vals)
# stage change between two won stages: does not change the date_closed
leads_already_won = self.filtered(lambda r: r.stage_id.is_won)
leads_already_won = self.filtered(lambda lead: lead.stage_id.is_won)
remaining = self - leads_already_won
if remaining:
result = super(Lead, remaining).write(vals)
+11
View File
@@ -279,6 +279,17 @@ class TestCRMLead(TestCrmCommon):
self.assertEqual(won_lead.date_closed, old_date_closed, 'Should not change date')
self.assertEqual(other_lead.date_closed, datetime(2020, 2, 2, 18, 0, 0))
# back to open stage
leads.write({'stage_id': self.stage_team1_2.id})
self.assertFalse(won_lead.date_closed)
self.assertFalse(other_lead.date_closed)
# close with lost
with freeze_time('2020-02-02 18:00'):
leads.action_set_lost()
self.assertEqual(won_lead.date_closed, datetime(2020, 2, 2, 18, 0, 0))
self.assertEqual(other_lead.date_closed, datetime(2020, 2, 2, 18, 0, 0))
@users('user_sales_leads')
@freeze_time("2012-01-14")
def test_crm_lead_lost_date_closed(self):