From 59d154dcc1524c8f08c6a1e7aee18c722ab2e0ad Mon Sep 17 00:00:00 2001
From: jvm-odoo <jvm@odoo.com>
Date: Wed, 6 Nov 2019 10:53:43 +0000
Subject: [PATCH] [FIX] crm: fix lead/opportunity meeting scheduling

Reproduce the issue:
 - In the CRM app, create a lead/opportunity.
 - Schedule a meeting for this opportunity via
   the chatter and click on create.
 - Go back, the "Meeting" stat button is now at 1
 - Schedule a meeting for this opportunity via
   the chatter and click on edit then create.
 - Go back, the "Meeting" stat button is still at 1

Cause:
When we click directly on create, the opportunity ID is in the defaults
option but when we click on edit, there is no opportunity ID.

So, this commit use the context to retrieve the concerned opportunity
and set it if it is not already set.

I splitted the solution into a private method for a better readability.

OPW-2092920

closes odoo/odoo#39853

Signed-off-by: Simon Goffin (sig) <sig@openerp.com>
---
 addons/crm/models/calendar.py | 18 +++++++++++++++---
 1 file changed, 15 insertions(+), 3 deletions(-)

diff --git a/addons/crm/models/calendar.py b/addons/crm/models/calendar.py
index 8a13f3d3a17b..1d1c07154a85 100644
--- a/addons/crm/models/calendar.py
+++ b/addons/crm/models/calendar.py
@@ -7,6 +7,17 @@ from odoo import api, fields, models
 class CalendarEvent(models.Model):
     _inherit = 'calendar.event'
 
+    def _is_crm_lead(self, defaults, ctx=None):
+        """
+            This method checks if the concerned model is a CRM lead.
+            The information is not always in the defaults values,
+            this is why it is necessary to check the context too.
+        """
+        res_model = defaults.get('res_model', False) or ctx and ctx.get('default_res_model')
+        res_model_id = defaults.get('res_model_id', False) or ctx and ctx.get('default_res_model_id')
+
+        return res_model and res_model == 'crm.lead' or res_model_id and self.env['ir.model'].sudo().browse(res_model_id).model == 'crm.lead'
+
     @api.model
     def default_get(self, fields):
         if self.env.context.get('default_opportunity_id'):
@@ -17,9 +28,10 @@ class CalendarEvent(models.Model):
         defaults = super(CalendarEvent, self).default_get(fields)
 
         # sync res_model / res_id to opportunity id (aka creating meeting from lead chatter)
-        if 'opportunity_id' not in defaults and defaults.get('res_id') and (defaults.get('res_model') or defaults.get('res_model_id')):
-            if (defaults.get('res_model') and defaults['res_model'] == 'crm.lead') or (defaults.get('res_model_id') and self.env['ir.model'].sudo().browse(defaults['res_model_id']).model == 'crm.lead'):
-                defaults['opportunity_id'] = defaults['res_id']
+        ctx = self.env.context
+        if 'opportunity_id' not in defaults:
+            if self._is_crm_lead(defaults, ctx):
+                defaults['opportunity_id'] = defaults.get('res_id', False) or ctx.get('default_res_id', False)
 
         return defaults
 
-- 
GitLab