diff --git a/addons/crm/crm.py b/addons/crm/crm.py index c3a4ef44a9c..5336bdd23fb 100644 --- a/addons/crm/crm.py +++ b/addons/crm/crm.py @@ -22,6 +22,7 @@ import time import base64 import tools + from osv import fields from osv import osv from tools.translate import _ @@ -135,6 +136,52 @@ class crm_case(object): user = self.pool.get('res.users').browse(cr, uid, uid, context=context) return user.context_section_id.id or False + def _find_next_stage(self, cr, uid, stage_list, index, current_seq, stage_pool, context=None): + if index + 1 == len(stage_list): + return False + next_stage_id = stage_list[index + 1] + next_stage = stage_pool.browse(cr, uid, next_stage_id, context=context) + if not next_stage: + return False + + next_seq = next_stage.sequence + if (abs(next_seq - current_seq)) >= 1: + return next_stage + else : + return self._find_next_stage(cr, uid, stage_list, index + 1, current_seq, stage_pool) + + def stage_change(self, cr, uid, ids, context=None, order='sequence'): + if not context: + 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): + next_stage = False + data = {} + + 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 + index = -1 + if case.stage_id and case.stage_id.id in stages: + 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} + if next_stage.on_change: + data.update({'probability': next_stage.probability}) + self.write(cr, uid, [case.id], data, context=context) + + return next_stage_id + + def stage_next(self, cr, uid, ids, context=None): """This function computes next stage for case from its current stage using available stage for that case type @@ -143,36 +190,10 @@ 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""" - if not context: - context = {} - stage_pool = self.pool.get('crm.case.stage') - stage_type = context and context.get('stage_type','') - current_seq = False - for case in self.browse(cr, uid, ids, context): - next_stage = False - data = {} - 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='sequence') - index = -1 - if case.stage_id and case.stage_id.id in stages: - index = stages.index(case.stage_id.id) - if index + 1 == len(stages): - return False - else: - next_stage = stages[index + 1] - current_seq = case.stage_id.sequence - if next_stage: - stage = stage_pool.browse(cr, uid, next_stage, context=context) - next_seq = stage.sequence - if current_seq and (next_seq - current_seq) >= 1: - data = {'stage_id': next_stage} - if stage.on_change: - data.update({'probability': stage.probability}) - self.write(cr, uid, [case.id], data, context=context) - return next_stage - + + + return self.stage_change(cr, uid, ids, context=context, order='sequence') + def stage_previous(self, cr, uid, ids, context=None): """This function computes previous stage for case from its current stage using available stage for that case type @@ -181,34 +202,7 @@ 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""" - if not context: - context = {} - stage_pool = self.pool.get('crm.case.stage') - stage_type = context and context.get('stage_type','') - for case in self.browse(cr, uid, ids, context): - prev_stage = False - data = {} - 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='sequence') - index = 0 - if case.stage_id and case.stage_id.id in stages: - index = stages.index(case.stage_id.id) - if index == 0: - return False - else: - prev_stage = stages[index - 1] - current_seq = case.stage_id.sequence - if prev_stage: - stage = stage_pool.browse(cr, uid, prev_stage, context=context) - prev_seq = stage.sequence - if current_seq and (prev_seq - current_seq) <= 1: - data = {'stage_id': prev_stage} - if stage.on_change: - data.update({'probability': stage.probability}) - self.write(cr, uid, [case.id], data, context=context) - return prev_stage + return self.stage_change(cr, uid, ids, context=context, order='sequence desc') def onchange_partner_id(self, cr, uid, ids, part, email=False): """This function returns value of partner address based on partner @@ -636,6 +630,7 @@ class crm_case_stage(osv.osv): _columns = { 'section_ids':fields.many2many('crm.case.section', 'section_stage_rel', 'stage_id', 'section_id', 'Sections'), } + crm_case_stage() diff --git a/addons/crm/crm_lead_data.xml b/addons/crm/crm_lead_data.xml index f10ba72ef8e..3cb34c49299 100644 --- a/addons/crm/crm_lead_data.xml +++ b/addons/crm/crm_lead_data.xml @@ -2,7 +2,12 @@ - + + Lost + + + lead + New @@ -40,12 +45,7 @@ lead - - Lost - - - lead - + Sales Department diff --git a/addons/crm/crm_lead_view.xml b/addons/crm/crm_lead_view.xml index 87eb7ff3bd1..979ab26cfd1 100644 --- a/addons/crm/crm_lead_view.xml +++ b/addons/crm/crm_lead_view.xml @@ -57,14 +57,14 @@ - +