diff --git a/addons/crm/crm.py b/addons/crm/crm.py
index 4a8b9c089fc..b7ff5a39246 100644
--- a/addons/crm/crm.py
+++ b/addons/crm/crm.py
@@ -47,7 +47,68 @@ AVAILABLE_PRIORITIES = [
class crm_case(object):
"""A simple python class to be used for common functions """
+
+ def _find_lost_stage(self, cr, uid, type, section_id):
+ return self._find_percent_stage(cr, uid, 0.0, type, section_id)
+
+ def _find_won_stage(self, cr, uid, type, section_id):
+ return self._find_percent_stage(cr, uid, 100.0, type, section_id)
+
+ def _find_percent_stage(self, cr, uid, percent, type, section_id):
+ """
+ Return the first stage with a probability == percent
+ """
+ stage_pool = self.pool.get('crm.case.stage')
+ if section_id :
+ ids = stage_pool.search(cr, uid, [("probability", '=', percent), ("type", 'like', type), ("section_ids", 'in', [section_id])])
+ else :
+ ids = stage_pool.search(cr, uid, [("probability", '=', percent), ("type", 'like', type)])
+
+ if ids:
+ return ids[0]
+ return False
+
+
+ def _find_first_stage(self, cr, uid, type, section_id):
+ """
+ return the first stage that has a sequence number equal or higher than sequence
+ """
+ stage_pool = self.pool.get('crm.case.stage')
+ if section_id :
+ ids = stage_pool.search(cr, uid, [("sequence", '>', 0), ("type", 'like', type), ("section_ids", 'in', [section_id])])
+ else :
+ ids = stage_pool.search(cr, uid, [("sequence", '>', 0), ("type", 'like', type)])
+
+ if ids:
+ stages = stage_pool.browse(cr, uid, ids)
+ stage_min = stages[0]
+ for stage in stages:
+ if stage_min.sequence > stage.sequence:
+ stage_min = stage
+ return stage_min.id
+ else :
+ return False
+
+ def onchange_stage_id(self, cr, uid, ids, stage_id, context={}):
+
+ """ @param self: The object pointer
+ @param cr: the current row, from the database cursor,
+ @param uid: the current user’s ID for security checks,
+ @param ids: List of stage’s IDs
+ @stage_id: change state id on run time """
+
+ if not stage_id:
+ return {'value':{}}
+
+ stage = self.pool.get('crm.case.stage').browse(cr, uid, stage_id, context)
+
+ if not stage.on_change:
+ return {'value':{}}
+ return {'value':{'probability': stage.probability}}
+
+
def _get_default_partner_address(self, cr, uid, context=None):
+
"""Gives id of default address for current user
@param self: The object pointer
@param cr: the current row, from the database cursor,
@@ -149,6 +210,9 @@ class crm_case(object):
return False
next_seq = next_stage.sequence
+ if not current_seq :
+ current_seq = 0
+
if (abs(next_seq - current_seq)) >= 1:
return next_stage
else :
@@ -157,17 +221,21 @@ class crm_case(object):
def stage_change(self, cr, uid, ids, context=None, order='sequence'):
if context is None:
context = {}
+
stage_pool = self.pool.get('crm.case.stage')
stage_type = context and context.get('stage_type','')
+
current_seq = False
next_stage_id = False
+
for case in self.browse(cr, uid, ids, context=context):
next_stage = False
- data = {}
+ value = {}
+ if case.section_id.id :
+ domain = [('type', '=', stage_type),('section_ids', '=', case.section_id.id)]
+ else :
+ domain = [('type', '=', stage_type)]
- domain = [('type', '=', stage_type),('section_ids', '=', case.section_id.id)]
- if case.section_id and case.section_id.stage_ids:
- domain.append(('id', 'in', map(lambda x: x.id, case.section_id.stage_ids)))
stages = stage_pool.search(cr, uid, domain, order=order)
current_seq = case.stage_id.sequence
@@ -176,14 +244,16 @@ class crm_case(object):
index = stages.index(case.stage_id.id)
next_stage = self._find_next_stage(cr, uid, stages, index, current_seq, stage_pool, context=context)
+
if next_stage:
next_stage_id = next_stage.id
- data = {'stage_id': next_stage.id}
+ value.update({'stage_id': next_stage.id})
if next_stage.on_change:
- data.update({'probability': next_stage.probability})
- self.write(cr, uid, [case.id], data, context=context)
+ value.update({'probability': next_stage.probability})
+ self.write(cr, uid, [case.id], value, context=context)
- return next_stage_id
+
+ return next_stage_id #FIXME should return a list of all id
def stage_next(self, cr, uid, ids, context=None):
@@ -194,7 +264,6 @@ class crm_case(object):
@param uid: the current user’s ID for security checks,
@param ids: List of case IDs
@param context: A standard dictionary for contextual values"""
-
return self.stage_change(cr, uid, ids, context=context, order='sequence')
@@ -257,6 +326,7 @@ class crm_case(object):
@param ids: List of case Ids
@param *args: Tuple Value for additional Params
"""
+
cases = self.browse(cr, uid, ids)
self._history(cr, uid, cases, _('Open'))
for case in cases:
@@ -264,7 +334,9 @@ class crm_case(object):
if not case.user_id:
data['user_id'] = uid
self.write(cr, uid, case.id, data)
- self._action(cr, uid, cases, 'open')
+
+
+ self._action(cr, uid, cases, 'open')
return True
def case_close(self, cr, uid, ids, *args):
@@ -483,6 +555,12 @@ class crm_case_stage(osv.osv):
_description = "Stage of case"
_rec_name = 'name'
_order = "sequence"
+
+
+
+ def _get_type_value(self, cr, user, context):
+ return [('lead','Lead'),('opportunity','Opportunity')]
+
_columns = {
'name': fields.char('Stage Name', size=64, required=True, translate=True),
@@ -491,9 +569,10 @@ class crm_case_stage(osv.osv):
'on_change': fields.boolean('Change Probability Automatically', \
help="Change Probability on next and previous stages."),
'requirements': fields.text('Requirements'),
- 'type': fields.selection([('lead','Lead'),('opportunity','Opportunity'),('claim','Claim'), ('fundraising','Fundraising')], 'Type'),
+ 'type': fields.selection(_get_type_value, 'Type'),
}
-
+
+
def _find_stage_type(self, cr, uid, context=None):
"""Finds type of stage according to object.
@param self: The object pointer
diff --git a/addons/crm/crm_lead.py b/addons/crm/crm_lead.py
index e319711ca26..a72eaf83c17 100644
--- a/addons/crm/crm_lead.py
+++ b/addons/crm/crm_lead.py
@@ -151,18 +151,6 @@ class crm_lead(crm_case, osv.osv):
'message_ids': fields.one2many('mailgate.message', 'res_id', 'Messages', domain=[('model','=',_name)]),
}
- def _get_stage_id(self, cr, uid, context=None):
- """Finds type of stage according to object.
- @param self: The object pointer
- @param cr: the current row, from the database cursor,
- @param uid: the current user’s ID for security checks,
- @param context: A standard dictionary for contextual values
- """
- if context is None:
- context = {}
- type = context and context.get('stage_type', '')
- stage_ids = self.pool.get('crm.case.stage').search(cr, uid, [('type','=',type),('sequence','>=',1)])
- return stage_ids and stage_ids[0] or False
_defaults = {
'active': lambda *a: 1,
@@ -173,8 +161,10 @@ class crm_lead(crm_case, osv.osv):
'section_id': crm_case._get_section,
'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],
- 'stage_id': _get_stage_id,
+ #'stage_id': _get_stage_id,
}
+
+
def onchange_partner_address_id(self, cr, uid, ids, add, email=False):
"""This function returns value of partner email based on Partner Address
@@ -198,27 +188,30 @@ class crm_lead(crm_case, osv.osv):
@param ids: List of case's Ids
@param *args: Give Tuple Value
"""
- old_state = self.read(cr, uid, ids, ['state'])[0]['state']
- old_stage_id = self.read(cr, uid, ids, ['stage_id'])[0]['stage_id']
+ leads = self.browse(cr, uid, ids)
+
+
+
+ for i in xrange(0, len(ids)):
+ if leads[i].state == 'draft':
+ value = {}
+ if not leads[i].stage_id :
+ stage_id = self._find_first_stage(cr, uid, leads[i].type, leads[i].section_id.id or False)
+ value.update({'stage_id' : stage_id})
+ value.update({'date_open': time.strftime('%Y-%m-%d %H:%M:%S')})
+ self.write(cr, uid, [ids[i]], value)
+ self.log_open( cr, uid, leads[i])
res = super(crm_lead, self).case_open(cr, uid, ids, *args)
- if old_state == 'draft':
- value = {}
- if not old_stage_id:
- stage_id = super(crm_lead, self).stage_next(cr, uid, ids, *args)
- if stage_id:
- value.update(self.onchange_stage_id(cr, uid, ids, stage_id, context={})['value'])
- value.update({'date_open': time.strftime('%Y-%m-%d %H:%M:%S')})
- self.write(cr, uid, ids, value)
-
- for case in self.browse(cr, uid, ids):
- if case.type == 'lead':
- message = _("The lead '%s' has been opened.") % case.name
- elif case.type == 'opportunity':
- message = _("The opportunity '%s' has been opened.") % case.name
- else:
- message = _("The case '%s' has been opened.") % case.name
- self.log(cr, uid, case.id, message)
return res
+
+ def log_open(self, cr, uid, case):
+ if case.type == 'lead':
+ message = _("The lead '%s' has been opened.") % case.name
+ elif case.type == 'opportunity':
+ message = _("The opportunity '%s' has been opened.") % case.name
+ else:
+ message = _("The case '%s' has been opened.") % case.name
+ self.log(cr, uid, case.id, message)
def case_close(self, cr, uid, ids, *args):
"""Overrides close for crm_case for setting close date
@@ -228,7 +221,7 @@ class crm_lead(crm_case, osv.osv):
@param ids: List of case Ids
@param *args: Tuple Value for additional Params
"""
- res = super(crm_lead, self).case_close(cr, uid, ids, args)
+ res = super(crm_lead, self).case_close(cr, uid, ids, *args)
self.write(cr, uid, ids, {'date_closed': time.strftime('%Y-%m-%d %H:%M:%S')})
for case in self.browse(cr, uid, ids):
if case.type == 'lead':
@@ -399,7 +392,6 @@ class crm_lead(crm_case, osv.osv):
if case.state in CRM_LEAD_PENDING_STATES:
values.update(state=crm.AVAILABLE_STATES[1][0]) #re-open
res = self.write(cr, uid, [case.id], values, context=context)
-
return res
def msg_send(self, cr, uid, id, *args, **argv):
diff --git a/addons/crm/crm_lead_view.xml b/addons/crm/crm_lead_view.xml
index 979ab26cfd1..63056b89539 100644
--- a/addons/crm/crm_lead_view.xml
+++ b/addons/crm/crm_lead_view.xml
@@ -13,6 +13,8 @@
{'type':'lead'}
Create specific stages that will help your sales better organise their sales pipeline by maintaining them to their leads and sales opportunities. It will allow them to easily track how is positioned a specific lead or opportunity in the sales cycle.
+
+