From 0841412f9e2d70edd6653ec4dfad8d161d635242 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thibault=20Delavall=C3=A9e?= Date: Thu, 24 May 2012 16:26:16 +0200 Subject: [PATCH] [IMP] crm_case, crm_lead: (default) stage, stage_find: updated code to be more easily usable by external classes. bzr revid: tde@openerp.com-20120524142616-9gn6o8yj1pbzxnx0 --- addons/crm/crm.py | 49 +++++++------------ addons/crm/crm_lead.py | 59 ++++++++++++++++++----- addons/crm/crm_lead_demo.xml | 2 +- addons/crm/crm_lead_view.xml | 2 +- addons/crm_claim/crm_claim.py | 2 +- addons/crm_fundraising/crm_fundraising.py | 2 +- addons/crm_helpdesk/crm_helpdesk.py | 2 +- addons/project_issue/project_issue.py | 2 +- 8 files changed, 70 insertions(+), 50 deletions(-) diff --git a/addons/crm/crm.py b/addons/crm/crm.py index 86d5d30382b..a0a390c2f12 100644 --- a/addons/crm/crm.py +++ b/addons/crm/crm.py @@ -352,7 +352,7 @@ class crm_case(object): """ Gives default email address for current user :param context: if portal in context is false return false anyway """ - if not context.get('portal', False): + if context and context.get('portal'): return False user = self.pool.get('res.users').browse(cr, uid, uid, context=context) return user.user_email @@ -361,7 +361,7 @@ class crm_case(object): """ Gives current user id :param context: if portal in context is false return false anyway """ - if context and context.get('portal', False): + if context and context.get('portal'): return False return uid @@ -390,43 +390,31 @@ class crm_case(object): data.update(self.onchange_partner_address_id(cr, uid, ids, addr['contact'])['value']) return {'value': data} - def _get_default_section(self, cr, uid, context=None): - """ Gives default section by checking if present in the context """ - if context is None: - context = {} - if context.get('portal', False): - return False - if type(context.get('default_section_id')) in (int, long): - return context.get('default_section_id') - if isinstance(context.get('default_section_id'), basestring): - section_name = context['default_section_id'] - section_ids = self.pool.get('crm.case.section').name_search(cr, uid, name=section_name, context=context) - if len(section_ids) == 1: - return section_ids[0][0] + def _get_default_section_id(self, cr, uid, context=None): + """ Gives default section """ return False def _get_default_stage_id(self, cr, uid, context=None): """ Gives default stage_id """ - section_id = self._get_default_section(cr, uid, context=context) - return self.stage_find(cr, uid, section_id, [('state', '=', 'draft')], context=context) + return self.stage_find(cr, uid, [], None, [('state', '=', 'draft')], context=context) - def stage_find(self, cr, uid, section_id, domain=[], order='sequence', context=None): - """ Find stage, within a sales team, with a domain on the search, + def stage_find(self, cr, uid, cases, section_id, domain=[], order='sequence', context=None): + """ Find stage, with a given (optional) domain on the search, ordered by the order parameter. If several stages match the search criterions, the first one will be returned, according to the requested search order. - :param section_id: if set, the search is limited to stages that - belongs to the given sales team, or that are - global (case_default flag set to True) + This method is meant to be overriden by subclasses. That way + specific behaviors can be achieved for every class inheriting + from crm_case. + + :param cases: browse_record of cases + :param section_id: section limitating the search, given for + a generic search (for example default search). + A section models concepts such as Sales team + (for CRM), ou departments (for HR). :param domain: a domain on the search of stages :param order: order of the search """ - domain = list(domain) - if section_id: - domain += ['|', ('section_ids', '=', section_id), ('case_default', '=', True)] - stage_ids = self.pool.get('crm.case.stage').search(cr, uid, domain, order=order, context=context) - if stage_ids: - return stage_ids[0] return False def stage_set_with_state_name(self, cr, uid, cases, state_name, context=None): @@ -436,8 +424,7 @@ class crm_case(object): if isinstance(cases, (int, long)): cases = self.browse(cr, uid, cases, context=context) for case in cases: - section_id = case.section_id.id if case.section_id else None - stage_id = self.stage_find(cr, uid, section_id, [('state', '=', state_name)], context=context) + stage_id = self.stage_find(cr, uid, [case], None, [('state', '=', state_name)], context=context) if stage_id: self.stage_set(cr, uid, [case.id], stage_id, context=context) return True @@ -457,7 +444,7 @@ class crm_case(object): section_id = None if case.section_id: section_id = case.section_id.id - next_stage_id = self.stage_find(cr, uid, section_id, [('sequence',op,seq)],order) + next_stage_id = self.stage_find(cr, uid, [case], None, [('sequence',op,seq)],order) if next_stage_id: return self.stage_set(cr, uid, [case.id], next_stage_id, context=context) return False diff --git a/addons/crm/crm_lead.py b/addons/crm/crm_lead.py index 32c69527dd6..cbe47aa1fe5 100644 --- a/addons/crm/crm_lead.py +++ b/addons/crm/crm_lead.py @@ -42,20 +42,25 @@ class crm_lead(crm_case, osv.osv): _order = "priority,date_action,id desc" _inherit = ['ir.needaction_mixin', 'mail.thread','res.partner'] + def _get_default_section_id(self, cr, uid, context=None): + """ Gives default section by checking if present in the context """ + return self._resolve_section_id_from_context(cr, uid, context=context) + def _resolve_section_id_from_context(self, cr, uid, context=None): """ Returns ID of section based on the value of 'section_id' - context key, or None if it cannot be resolved to a single project + context key, or None if it cannot be resolved to a single + Sales Team. """ if context is None: context = {} - if type(context.get('section_id')) in (int, long): - return context.get('section_id') - if isinstance(context.get('section_id'), basestring): - section_name = context['section_id'] + if type(context.get('default_section_id')) in (int, long): + return context.get('default_section_id') + if isinstance(context.get('default_section_id'), basestring): + section_name = context['default_section_id'] section_ids = self.pool.get('crm.case.section').name_search(cr, uid, name=section_name, context=context) if len(section_ids) == 1: - return section_ids[0][0] - return None + return int(section_ids[0][0]) + return False def _read_group_stage_ids(self, cr, uid, ids, domain, read_group_order=None, access_rights_uid=None, context=None): access_rights_uid = access_rights_uid or uid @@ -175,7 +180,6 @@ class crm_lead(crm_case, osv.osv): 'email_cc': fields.text('Global CC', size=252 , help="These email addresses will be added to the CC field of all inbound and outbound emails for this record before being sent. Separate multiple email addresses with a comma"), 'description': fields.text('Notes'), 'write_date': fields.datetime('Update Date' , readonly=True), - 'categ_id': fields.many2one('crm.case.categ', 'Category', \ domain="['|',('section_id','=',section_id),('section_id','=',False), ('object_id.model', '=', 'crm.lead')]"), 'type_id': fields.many2one('crm.case.resource.type', 'Campaign', \ @@ -188,7 +192,7 @@ class crm_lead(crm_case, osv.osv): 'type':fields.selection([ ('lead','Lead'), ('opportunity','Opportunity'), ],'Type', help="Type is used to separate Leads and Opportunities"), 'priority': fields.selection(crm.AVAILABLE_PRIORITIES, 'Priority', select=True), 'date_closed': fields.datetime('Closed', readonly=True), - 'stage_id': fields.many2one('crm.case.stage', 'Stage', domain="[('section_ids', '=', section_id)]"), + 'stage_id': fields.many2one('crm.case.stage', 'Stage', domain="['&', ('section_ids', '=', section_id), ('type', '=', type)]"), 'user_id': fields.many2one('res.users', 'Salesperson'), 'referred': fields.char('Referred By', size=64), 'date_open': fields.datetime('Opened', readonly=True), @@ -227,11 +231,11 @@ class crm_lead(crm_case, osv.osv): _defaults = { 'active': 1, - 'user_id': crm_case._get_default_user, - 'email_from': crm_case._get_default_email, + 'user_id': lambda s, cr, uid, c: s._get_default_user(cr, uid, c), + 'email_from': lambda s, cr, uid, c: s._get_default_email(cr, uid, c), 'type': 'lead', - 'stage_id': crm_case._get_default_stage_id, - 'section_id': crm_case._get_default_section, + 'stage_id': lambda s, cr, uid, c: s._get_default_stage_id(cr, uid, c), + 'section_id': lambda s, cr, uid, c: s._get_default_section_id(cr, uid, 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[2][0], 'color': 0, @@ -264,6 +268,35 @@ class crm_lead(crm_case, osv.osv): return {'value':{}} return {'value':{'probability': stage.probability}} + def _get_default_stage_id(self, cr, uid, context=None): + """ Gives default stage_id """ + section_id = self._get_default_section_id(cr, uid, context=context) + return self.stage_find(cr, uid, [], section_id, [('state', '=', 'draft'), ('type', '=', 'both')], context=context) + + def stage_find(self, cr, uid, cases, section_id, domain=[], order='sequence', context=None): + """ Override of the base.stage method + Parameter of the stage search taken from the lead: + - type: stage type must be the same or 'both' + - section_id: if set, stages must belong to this section or + be a default case + """ + if isinstance(cases, (int, long)): + cases = self.browse(cr, uid, cases, context=context) + domain = list(domain) + if section_id: + domain += ['|', ('section_ids', '=', section_id), ('case_default', '=', True)] + for lead in cases: + lead_section_id = lead.section_id.id if lead.section_id else None + domain += ['|', ('type', '=', lead.type), ('type', '=', 'both')] + if lead_section_id: + domain += ['|', ('section_ids', '=', lead_section_id), ('case_default', '=', True)] + stage_ids = self.pool.get('crm.case.stage').search(cr, uid, domain, order=order, context=context) + print domain + print stage_ids + if stage_ids: + return stage_ids[0] + return False + def case_cancel(self, cr, uid, ids, context=None): """Overrides cancel for crm_case for setting probability """ diff --git a/addons/crm/crm_lead_demo.xml b/addons/crm/crm_lead_demo.xml index 02e6c4310bb..30a000a5814 100644 --- a/addons/crm/crm_lead_demo.xml +++ b/addons/crm/crm_lead_demo.xml @@ -208,7 +208,7 @@ - diff --git a/addons/crm/crm_lead_view.xml b/addons/crm/crm_lead_view.xml index 00bf0c33267..a74d4ef0c3e 100644 --- a/addons/crm/crm_lead_view.xml +++ b/addons/crm/crm_lead_view.xml @@ -580,7 +580,7 @@ help="Unassigned Opportunities" /> + context="{'invisible_section': False, 'default_section_id': self}">