diff --git a/addons/crm/wizard/crm_lead_to_opportunity.py b/addons/crm/wizard/crm_lead_to_opportunity.py index ead591d39c6..51d3c80a92e 100644 --- a/addons/crm/wizard/crm_lead_to_opportunity.py +++ b/addons/crm/wizard/crm_lead_to_opportunity.py @@ -130,6 +130,9 @@ Leads Could not convert into Opportunity")) @return : View dictionary opening the Opportunity form view """ + if not context: + context = {} + record_id = context and context.get('active_ids') or False if not record_id: return {'type': 'ir.actions.act_window_close'} @@ -154,7 +157,7 @@ Leads Could not convert into Opportunity")) cr, uid, opportunity_view_tree, context=context).res_id for lead in leads.browse(cr, uid, record_id, context=context): - if(lead.section_id): + if lead.section_id: stage_ids = self.pool.get('crm.case.stage').search(cr, uid, [('type','=','opportunity'),('sequence','>=',1), ('section_ids','=', lead.section_id.id)]) else: stage_ids = self.pool.get('crm.case.stage').search(cr, uid, [('type','=','opportunity'),('sequence','>=',1)]) @@ -166,7 +169,8 @@ Leads Could not convert into Opportunity")) partner_id = partner_ids and partner_ids[0] or data.partner_id.id self._convert(cr, uid, ids, lead, partner_id, stage_ids, context=context) - if data.name == 'merge': + #If we convert in mass, don't merge if there is no other opportunity but no warning + if data.name == 'merge' and (len(data.opportunity_ids) > 1 or not context.get('mass-convert') ): merge_obj = self.pool.get('crm.merge.opportunity') self.write(cr, uid, ids, {'opportunity_ids' : [(6,0, [data.opportunity_ids[0].id])]}, context=context) context.update({'lead_ids' : record_id}) @@ -189,4 +193,66 @@ Leads Could not convert into Opportunity")) crm_lead2opportunity_partner() +class crm_lead2opportunity_mass_convert(osv.osv_memory): + _name = 'crm.lead2opportunity.partner.mass' + _description = 'Mass Lead To Opportunity Partner' + _inherit = 'crm.lead2opportunity.partner' + + + _columns = { + 'user_ids': fields.many2many('res.users', 'mass_convert_rel', 'user_id', 'wizard_id', 'Salesmans'), + 'section_id': fields.many2one('crm.case.section', 'Sales Team'), + + } + + def mass_convert(self, cr, uid, ids, context=None): + lead_obj = self.pool.get('crm.lead') + if not context: + context = {} + + active_ids = context.get('active_ids') + data = self.browse(cr, uid, ids, context=context)[0] + + salesteam = data.section_id and data.section_id.id + if data.user_ids: + salesmans = map(lambda x : x.id, data.user_ids) + index = 0 + else: + salesmans = False + + for lead_id in active_ids: + value = {} + if salesteam: + value['section_id'] = salesteam + if salesmans: + value['user_id'] = salesmans[index] + index += 1 + index = index < len(salesmans) and index or 0 + if value: + lead_obj.write(cr, uid, [lead_id], value, context=context) + + context['active_ids'] = [lead_id] + value = self.default_get(cr, uid, ['partner_id', 'opportunity_ids'], context=context) + value['opportunity_ids'] = [(6, 0, value['opportunity_ids'])] + self.write(cr, uid, ids, value, context=context) + + self.action_apply(cr, uid, ids, context=context) + + + + models_data = self.pool.get('ir.model.data') + result = models_data._get_id(cr, uid, 'crm', 'view_crm_case_opportunities_filter') + opportunity_view_search = models_data.browse(cr, uid, result, context=context).res_id + + return { + 'name': _('Opportunity'), + 'view_type': 'form', + 'view_mode': 'tree,form', + 'res_model': 'crm.lead', + 'domain': [('type', '=', 'opportunity'), ('id', 'in', active_ids)], + 'type': 'ir.actions.act_window', + 'search_view_id': opportunity_view_search, + } + +crm_lead2opportunity_mass_convert() # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: diff --git a/addons/crm/wizard/crm_lead_to_opportunity_view.xml b/addons/crm/wizard/crm_lead_to_opportunity_view.xml index e86e14af218..833c04c3575 100644 --- a/addons/crm/wizard/crm_lead_to_opportunity_view.xml +++ b/addons/crm/wizard/crm_lead_to_opportunity_view.xml @@ -36,5 +36,42 @@ + + crm.lead2opportunity.partner.mass.form + crm.lead2opportunity.partner.mass + form + +
+ + + + + + + + + + + + + + + + +