[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
This commit is contained in:
Thibault Delavallée 2012-05-24 16:26:16 +02:00
parent 24cc7c2cfc
commit 0841412f9e
8 changed files with 70 additions and 50 deletions

View File

@ -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

View File

@ -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
"""

View File

@ -208,7 +208,7 @@
</record>
<!-- Call Function to Cancel the leads (set as Dead) -->
<function model="crm.lead" name="case_close"
<function model="crm.lead" name="case_cancel"
eval="[ref('crm_case_electonicgoodsdealer0'), ref('crm_case_company_partnership0'), ref('crm_case_vpoperations0'), ref('crm_case_developingwebapplications0'), ref('crm_case_webvisitor0')], {'install_mode': True}"
/>

View File

@ -580,7 +580,7 @@
help="Unassigned Opportunities" />
</field>
<field name="section_id"
context="{'invisible_section': False, 'section_id': self}">
context="{'invisible_section': False, 'default_section_id': self}">
<filter icon="terp-personal+"
domain="['|', ('section_id.user_id','=',uid), ('section_id.member_ids', 'in', [uid])]"
context="{'invisible_section': False}"

View File

@ -88,7 +88,7 @@ class crm_claim(crm.crm_case, osv.osv):
'user_id': crm.crm_case._get_default_user,
'partner_id': crm.crm_case._get_default_partner,
'email_from':crm.crm_case._get_default_email,
'section_id':crm.crm_case._get_default_section,
'section_id':crm.crm_case._get_default_section_id,
'date': lambda *a: time.strftime('%Y-%m-%d %H:%M:%S'),
'company_id': lambda s, cr, uid, c: s.pool.get('res.company')._company_default_get(cr, uid, 'crm.case', context=c),
'priority': lambda *a: crm.AVAILABLE_PRIORITIES[2][0],

View File

@ -101,7 +101,7 @@ class crm_fundraising(crm.crm_case, osv.osv):
'user_id': crm.crm_case._get_default_user,
'partner_id': crm.crm_case._get_default_partner,
'email_from': crm.crm_case._get_default_email,
'section_id': crm.crm_case._get_default_section,
'section_id': crm.crm_case._get_default_section_id,
'company_id': lambda s, cr, uid, c: s.pool.get('res.company')._company_default_get(cr, uid, 'crm.case', context=c),
'priority': crm.AVAILABLE_PRIORITIES[2][0],
'probability': 0.0,

View File

@ -87,7 +87,7 @@ class crm_helpdesk(crm.crm_case, osv.osv):
'email_from': crm.crm_case._get_default_email,
'state': lambda *a: 'draft',
'date': lambda *a: time.strftime('%Y-%m-%d %H:%M:%S'),
'section_id': crm.crm_case._get_default_section,
'section_id': crm.crm_case._get_default_section_id,
'company_id': lambda s, cr, uid, c: s.pool.get('res.company')._company_default_get(cr, uid, 'crm.helpdesk', context=c),
'priority': lambda *a: crm.AVAILABLE_PRIORITIES[2][0],
}

View File

@ -228,7 +228,7 @@ class project_issue(crm.crm_case, osv.osv):
'partner_id': crm.crm_case._get_default_partner,
'email_from': crm.crm_case._get_default_email,
'state': 'draft',
'section_id': crm.crm_case._get_default_section,
'section_id': crm.crm_case._get_default_section_id,
'company_id': lambda s, cr, uid, c: s.pool.get('res.company')._company_default_get(cr, uid, 'crm.helpdesk', context=c),
'priority': crm.AVAILABLE_PRIORITIES[2][0],
'categ_id' : lambda *a: False,