From f2b8144bccf4d7b61bccfc36a892ae38f3b7b2d9 Mon Sep 17 00:00:00 2001 From: Nicolas Lempereur Date: Wed, 22 Apr 2015 10:57:49 +0200 Subject: [PATCH] [IMP] crm: set user partner team as default team This fix sets the first sales team of the current user as the default sales team if no other one is given. For models: crm.lead, crm.phonecall, crm.claim, crm.helpdesk, project.issue, sale.order, account.invoice Existing methods are removed and new methods in sales team model (crm.team) have to be called when needed. closes #6438 opw-633562 --- addons/crm/crm_lead.py | 28 +++------------- addons/crm/crm_phonecall.py | 1 + addons/crm_claim/crm_claim.py | 8 ++--- addons/crm_helpdesk/crm_helpdesk.py | 1 + addons/project_issue/project_issue.py | 1 + addons/sale/sale.py | 46 ++------------------------- addons/sales_team/sales_team.py | 22 +++++++++++++ 7 files changed, 34 insertions(+), 73 deletions(-) diff --git a/addons/crm/crm_lead.py b/addons/crm/crm_lead.py index 42eb41043b8..7f998c7cb95 100644 --- a/addons/crm/crm_lead.py +++ b/addons/crm/crm_lead.py @@ -92,31 +92,11 @@ class crm_lead(format_address, osv.osv): context['empty_list_help_document_name'] = _("leads") return super(crm_lead, self).get_empty_list_help(cr, uid, help, context=context) - def _get_default_team_id(self, cr, uid, user_id=False, context=None): - """ Gives default team by checking if present in the context """ - team_id = self._resolve_team_id_from_context(cr, uid, context=context) or False - return team_id - def _get_default_stage_id(self, cr, uid, context=None): """ Gives default stage_id """ - team_id = self._get_default_team_id(cr, uid, context=context) + team_id = self.pool['crm.team']._get_default_team_id(cr, uid, context=context) return self.stage_find(cr, uid, [], team_id, [('fold', '=', False)], context=context) - def _resolve_team_id_from_context(self, cr, uid, context=None): - """ Returns ID of team based on the value of 'team_id' - context key, or None if it cannot be resolved to a single - Sales Team. - """ - if context is None: - context = {} - if type(context.get('default_team_id')) in (int, long): - return context.get('default_team_id') - if isinstance(context.get('default_team_id'), basestring): - team_ids = self.pool.get('crm.team').name_search(cr, uid, name=context['default_team_id'], context=context) - if len(team_ids) == 1: - return int(team_ids[0][0]) - return None - def _resolve_type_from_context(self, cr, uid, context=None): """ Returns the type (lead or opportunity) from the type context key. Returns None if it cannot be resolved. @@ -137,7 +117,7 @@ class crm_lead(format_address, osv.osv): # - OR ('case_default', '=', True), ('fold', '=', False): add default columns that are not folded # - OR ('team_ids', '=', team_id), ('fold', '=', False) if team_id: add team columns that are not folded search_domain = [] - team_id = self._resolve_team_id_from_context(cr, uid, context=context) + team_id = self.pool['crm.team']._resolve_team_id_from_context(cr, uid, context=context) if team_id: search_domain += ['|', ('team_ids', '=', team_id)] search_domain += [('id', 'in', ids)] @@ -292,7 +272,7 @@ class crm_lead(format_address, osv.osv): 'type': 'lead', 'user_id': lambda s, cr, uid, c: uid, 'stage_id': lambda s, cr, uid, c: s._get_default_stage_id(cr, uid, c), - 'team_id': lambda s, cr, uid, c: s._get_default_team_id(cr, uid, context=c), + 'team_id': lambda s, cr, uid, c: s.pool['crm.team']._get_default_team_id(cr, uid, context=c), 'company_id': lambda s, cr, uid, c: s.pool.get('res.company')._company_default_get(cr, uid, 'crm.lead', context=c), 'priority': lambda *a: crm.AVAILABLE_PRIORITIES[0][0], 'color': 0, @@ -339,7 +319,7 @@ class crm_lead(format_address, osv.osv): def on_change_user(self, cr, uid, ids, user_id, context=None): """ When changing the user, also set a team_id or restrict team id to the ones user_id is member of. """ - team_id = self._get_default_team_id(cr, uid, context=context) + team_id = self.pool['crm.team']._get_default_team_id(cr, uid, user_id=user_id, context=context) if user_id and not team_id and self.pool['res.users'].has_group(cr, uid, 'base.group_multi_salesteams'): team_ids = self.pool.get('crm.team').search(cr, uid, ['|', ('user_id', '=', user_id), ('member_ids', '=', user_id)], context=context) if team_ids: diff --git a/addons/crm/crm_phonecall.py b/addons/crm/crm_phonecall.py index f7e964aee12..b7885bf190b 100644 --- a/addons/crm/crm_phonecall.py +++ b/addons/crm/crm_phonecall.py @@ -77,6 +77,7 @@ class crm_phonecall(osv.osv): 'priority': '1', 'state': _get_default_state, 'user_id': lambda self, cr, uid, ctx: uid, + 'team_id': lambda s, cr, uid, c: s.pool['crm.team']._get_default_team_id(cr, uid, context=c), 'active': 1 } diff --git a/addons/crm_claim/crm_claim.py b/addons/crm_claim/crm_claim.py index 286d97d996f..d417d8a1cac 100644 --- a/addons/crm_claim/crm_claim.py +++ b/addons/crm_claim/crm_claim.py @@ -59,13 +59,9 @@ class crm_claim(osv.osv): _order = "priority,date desc" _inherit = ['mail.thread'] - def _get_default_team_id(self, cr, uid, context=None): - """ Gives default team by checking if present in the context """ - return self.pool.get('crm.lead')._resolve_team_id_from_context(cr, uid, context=context) or False - def _get_default_stage_id(self, cr, uid, context=None): """ Gives default stage_id """ - team_id = self._get_default_team_id(cr, uid, context=context) + team_id = self.pool['crm.team']._get_default_team_id(cr, uid, context=context) return self.stage_find(cr, uid, [], team_id, [('sequence', '=', '1')], context=context) _columns = { @@ -103,7 +99,7 @@ class crm_claim(osv.osv): _defaults = { 'user_id': lambda s, cr, uid, c: uid, - 'team_id': lambda s, cr, uid, c: s._get_default_team_id(cr, uid, c), + 'team_id': lambda s, cr, uid, c: s.pool['crm.team']._get_default_team_id(cr, uid, context=c), 'date': fields.datetime.now, 'company_id': lambda s, cr, uid, c: s.pool.get('res.company')._company_default_get(cr, uid, 'crm.case', context=c), 'priority': '1', diff --git a/addons/crm_helpdesk/crm_helpdesk.py b/addons/crm_helpdesk/crm_helpdesk.py index c622d45da3a..8d7c85bf7a7 100644 --- a/addons/crm_helpdesk/crm_helpdesk.py +++ b/addons/crm_helpdesk/crm_helpdesk.py @@ -81,6 +81,7 @@ class crm_helpdesk(osv.osv): 'user_id': lambda s, cr, uid, c: uid, 'state': lambda *a: 'draft', 'date': fields.datetime.now, + 'team_id': lambda s, cr, uid, c: s.pool['crm.team']._get_default_team_id(cr, uid, context=c), 'company_id': lambda s, cr, uid, c: s.pool.get('res.company')._company_default_get(cr, uid, 'crm.helpdesk', context=c), 'priority': '1', } diff --git a/addons/project_issue/project_issue.py b/addons/project_issue/project_issue.py index 7c836e7bb7d..52e97c42317 100644 --- a/addons/project_issue/project_issue.py +++ b/addons/project_issue/project_issue.py @@ -294,6 +294,7 @@ class project_issue(osv.Model): _defaults = { 'active': 1, + 'team_id': lambda s, cr, uid, c: s.pool['crm.team']._get_default_team_id(cr, uid, context=c), 'stage_id': lambda s, cr, uid, c: s._get_default_stage_id(cr, uid, c), 'company_id': lambda s, cr, uid, c: s.pool.get('res.company')._company_default_get(cr, uid, 'crm.helpdesk', context=c), 'priority': '0', diff --git a/addons/sale/sale.py b/addons/sale/sale.py index 5c0bb97f744..b7ca0e79a6b 100644 --- a/addons/sale/sale.py +++ b/addons/sale/sale.py @@ -155,26 +155,6 @@ class sale_order(osv.osv): raise UserError(_('There is no default company for the current user!')) return company_id - def _get_default_team_id(self, cr, uid, context=None): - """ Gives default team by checking if present in the context """ - team_id = self._resolve_team_id_from_context(cr, uid, context=context) or False - return team_id - - def _resolve_team_id_from_context(self, cr, uid, context=None): - """ Returns ID of team based on the value of 'team_id' - context key, or None if it cannot be resolved to a single - Sales Team. - """ - if context is None: - context = {} - if type(context.get('default_team_id')) in (int, long): - return context.get('default_team_id') - if isinstance(context.get('default_team_id'), basestring): - team_ids = self.pool.get('crm.team').name_search(cr, uid, name=context['default_team_id'], context=context) - if len(team_ids) == 1: - return int(team_ids[0][0]) - return None - _columns = { 'name': fields.char('Order Reference', required=True, copy=False, readonly=True, states={'draft': [('readonly', False)], 'sent': [('readonly', False)]}, select=True), @@ -256,7 +236,7 @@ class sale_order(osv.osv): 'partner_invoice_id': lambda self, cr, uid, context: context.get('partner_id', False) and self.pool.get('res.partner').address_get(cr, uid, [context['partner_id']], ['invoice'])['invoice'], 'partner_shipping_id': lambda self, cr, uid, context: context.get('partner_id', False) and self.pool.get('res.partner').address_get(cr, uid, [context['partner_id']], ['delivery'])['delivery'], 'note': lambda self, cr, uid, context: self.pool.get('res.users').browse(cr, uid, uid, context=context).company_id.sale_note, - 'team_id': lambda s, cr, uid, c: s._get_default_team_id(cr, uid, c), + 'team_id': lambda s, cr, uid, c: s.pool['crm.team']._get_default_team_id(cr, uid, context=c), } _sql_constraints = [ ('name_uniq', 'unique(name, company_id)', 'Order Reference must be unique per Company!'), @@ -343,7 +323,7 @@ class sale_order(osv.osv): val.update(delivery_onchange['value']) if pricelist: val['pricelist_id'] = pricelist - if not self._get_default_team_id(cr, uid, context=context) and part.team_id: + if not self.pool['crm.team']._get_default_team_id(cr, uid, context=context) and part.team_id: val['team_id'] = part.team_id.id sale_note = self.get_salenote(cr, uid, ids, part.id, context=context) if sale_note: val.update({'note': sale_note}) @@ -1185,32 +1165,12 @@ class mail_compose_message(osv.Model): class account_invoice(osv.Model): _inherit = 'account.invoice' - def _get_default_team_id(self, cr, uid, context=None): - """ Gives default team by checking if present in the context """ - team_id = self._resolve_team_id_from_context(cr, uid, context=context) or False - return team_id - - def _resolve_team_id_from_context(self, cr, uid, context=None): - """ Returns ID of team based on the value of 'team_id' - context key, or None if it cannot be resolved to a single - Sales Team. - """ - if context is None: - context = {} - if type(context.get('default_team_id')) in (int, long): - return context.get('default_team_id') - if isinstance(context.get('default_team_id'), basestring): - team_ids = self.pool.get('crm.team').name_search(cr, uid, name=context['default_team_id'], context=context) - if len(team_ids) == 1: - return int(team_ids[0][0]) - return None - _columns = { 'team_id': fields.many2one('crm.team', 'Sales Team', oldname='section_id'), } _defaults = { - 'team_id': lambda self, cr, uid, c=None: self._get_default_team_id(cr, uid, context=c) + 'team_id': lambda s, cr, uid, c: s.pool['crm.team']._get_default_team_id(cr, uid, context=c), } def confirm_paid(self, cr, uid, ids, context=None): diff --git a/addons/sales_team/sales_team.py b/addons/sales_team/sales_team.py index 8b78b85573e..c00b33607ed 100644 --- a/addons/sales_team/sales_team.py +++ b/addons/sales_team/sales_team.py @@ -63,6 +63,28 @@ class crm_team(osv.Model): team_result[self._period_number - (month_delta.months + 1)] = {'value': group.get(value_field, 0), 'tooltip': group.get(groupby_field, 0)} return team_result + def _get_default_team_id(self, cr, uid, user_id=None, context=None): + team_id = self._resolve_team_id_from_context(cr, uid, context=context) or False + if not team_id: + team_ids = self.search(cr, uid, [('member_ids', '=', user_id or uid)], limit=1, context=context) + team_id = team_ids[0] if team_ids else False + return team_id + + def _resolve_team_id_from_context(self, cr, uid, context=None): + """ Returns ID of team based on the value of 'default_team_id' + context key, or None if it cannot be resolved to a single + Sales Team. + """ + if context is None: + context = {} + if type(context.get('default_team_id')) in (int, long): + return context.get('default_team_id') + if isinstance(context.get('default_team_id'), basestring): + team_ids = self.name_search(cr, uid, name=context['default_team_id'], context=context) + if len(team_ids) == 1: + return int(team_ids[0][0]) + return None + _columns = { 'name': fields.char('Sales Team', size=64, required=True, translate=True), 'complete_name': fields.function(get_full_name, type='char', size=256, readonly=True, store=True, string="Full Name"),