From 14008d055bc3fce37355e079245d8ce5b95088ce Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Thibault=20Delavall=C3=A9e?= <tde@openerp.com>
Date: Mon, 14 Dec 2015 15:37:51 +0100
Subject: [PATCH] [FIX] crm: default sales team computation

Delegate the whole default sales team computation to the _get_default_team_id
of sales team to ease the overriding and understanding. Otherwise part of the
behavior is implemented in crm, part in sales team, leading to a difficult
overriding.

Order

 - team the user is member of
 - team in context
 - Direct Sales
---
 addons/crm/crm_lead.py          | 6 +-----
 addons/sales_team/sales_team.py | 8 ++++----
 2 files changed, 5 insertions(+), 9 deletions(-)

diff --git a/addons/crm/crm_lead.py b/addons/crm/crm_lead.py
index 21187fc70dc0..84ca8d9a3935 100644
--- a/addons/crm/crm_lead.py
+++ b/addons/crm/crm_lead.py
@@ -294,11 +294,7 @@ class crm_lead(format_address, osv.osv):
             team = self.pool['crm.team'].browse(cr, uid, context['team_id'], context=context)
             if user_id in team.member_ids.ids:
                 return {}
-        team_ids = self.pool.get('crm.team').search(
-            cr, uid, ['|', ('user_id', '=', user_id), ('member_ids', '=', user_id)], limit=1, context=context)
-        team_id = team_ids and team_ids[0] or False
-        if not team_id:
-            team_id = self.pool['crm.team']._get_default_team_id(cr, uid, context=context, user_id=user_id)
+        team_id = self.pool['crm.team']._get_default_team_id(cr, uid, context=context, user_id=user_id)
         return {'value': {'team_id': team_id}}
 
     def stage_find(self, cr, uid, cases, team_id, domain=None, order='sequence', context=None):
diff --git a/addons/sales_team/sales_team.py b/addons/sales_team/sales_team.py
index 4ab2bc1fcee7..93cdee60ed51 100644
--- a/addons/sales_team/sales_team.py
+++ b/addons/sales_team/sales_team.py
@@ -16,10 +16,10 @@ class crm_team(osv.Model):
             context = {}
         if user_id is None:
             user_id = uid
-        team_id = context.get('default_team_id')
-        if not team_id:
-            team_ids = self.search(cr, uid, ['|', ('user_id', '=', user_id), ('member_ids', 'in', user_id)], limit=1, context=context)
-            team_id = team_ids[0] if team_ids else False
+        team_ids = self.search(cr, uid, ['|', ('user_id', '=', user_id), ('member_ids', 'in', user_id)], limit=1, context=context)
+        team_id = team_ids[0] if team_ids else False
+        if not team_id and context.get('default_team_id'):
+            team_id = context['default_team_id']
         if not team_id:
             team_id = self.pool['ir.model.data'].xmlid_to_res_id(cr, uid, 'sales_team.team_sales_department')
         return team_id
-- 
GitLab