diff --git a/addons/crm/crm_lead.py b/addons/crm/crm_lead.py index 9347955d809..fd0b502a72c 100644 --- a/addons/crm/crm_lead.py +++ b/addons/crm/crm_lead.py @@ -501,7 +501,7 @@ class crm_lead(crm_case, osv.osv): oldest = self._merge_find_oldest(cr, uid, ids, context=context) if ctx_opportunities : first_opportunity = ctx_opportunities[0] - tail_opportunities = opportunities_list + tail_opportunities = opportunities_list + ctx_opportunities[1:] else: first_opportunity = opportunities_list[0] tail_opportunities = opportunities_list[1:] @@ -570,8 +570,6 @@ class crm_lead(crm_case, osv.osv): for lead in self.browse(cr, uid, ids, context=context): if lead.state in ('done', 'cancel'): continue - if user_ids or section_id: - self.allocate_salesman(cr, uid, [lead.id], user_ids, section_id, context=context) vals = self._convert_opportunity_data(cr, uid, lead, customer, section_id, context=context) self.write(cr, uid, [lead.id], vals, context=context) @@ -583,6 +581,10 @@ class crm_lead(crm_case, osv.osv): mail_message.write(cr, uid, msg_ids, { 'partner_id': lead.partner_id.id }, context=context) + + if user_ids or section_id: + self.allocate_salesman(cr, uid, ids, user_ids, section_id, context=context) + return True def _lead_create_partner(self, cr, uid, lead, context=None): @@ -634,10 +636,10 @@ class crm_lead(crm_case, osv.osv): if context is None: context = {} partner_ids = {} + force_partner_id = partner_id for lead in self.browse(cr, uid, ids, context=context): if action == 'create': - if not partner_id: - partner_id = self._lead_create_partner(cr, uid, lead, context=context) + partner_id = force_partner_id or self._lead_create_partner(cr, uid, lead, context=context) self._lead_create_partner_address(cr, uid, lead, partner_id, context=context) self._lead_set_partner(cr, uid, lead, partner_id, context=context) partner_ids[lead.id] = partner_id diff --git a/addons/crm/crm_phonecall.py b/addons/crm/crm_phonecall.py index 8d9a47b15eb..acd8aabf79e 100644 --- a/addons/crm/crm_phonecall.py +++ b/addons/crm/crm_phonecall.py @@ -196,11 +196,11 @@ class crm_phonecall(crm_base, osv.osv): if context is None: context = {} partner_ids = {} + force_partner_id = partner_id for call in self.browse(cr, uid, ids, context=context): if action == 'create': - if not partner_id: - partner_id = self._call_create_partner(cr, uid, call, context=context) - self._call_create_partner_address(cr, uid, call, partner_id, context=context) + partner_id = force_partner_id or self._call_create_partner(cr, uid, call, context=context) + self._call_create_partner_address(cr, uid, call, partner_id, context=context) self._call_set_partner(cr, uid, [call.id], partner_id, context=context) partner_ids[call.id] = partner_id return partner_ids diff --git a/addons/crm/wizard/crm_lead_to_opportunity.py b/addons/crm/wizard/crm_lead_to_opportunity.py index 52a79e4514c..bad406da950 100644 --- a/addons/crm/wizard/crm_lead_to_opportunity.py +++ b/addons/crm/wizard/crm_lead_to_opportunity.py @@ -35,8 +35,8 @@ class crm_lead2opportunity_partner(osv.osv_memory): 'action': fields.selection([('exist', 'Link to an existing partner'), \ ('create', 'Create a new partner'), \ ('nothing', 'Do not link to a partner')], \ - 'Action', required=True), - 'name': fields.selection([('convert', 'Convert to Opportunity'), ('merge', 'Merge with existing Opportunity')],'Select Action', required=True), + 'Related Partner', required=True), + 'name': fields.selection([('convert', 'Convert to Opportunities'), ('merge', 'Merge with existing Opportunities')], 'Conversion Action', required=True), 'opportunity_ids': fields.many2many('crm.lead', string='Opportunities', domain=[('type', '=', 'opportunity')]), } @@ -70,8 +70,6 @@ class crm_lead2opportunity_partner(osv.osv_memory): if not partner_id: - label = False - opp_ids = [] if email: # Find email of existing opportunity matches the email_from of the lead cr.execute("""select id from crm_lead where type='opportunity' and @@ -108,23 +106,36 @@ class crm_lead2opportunity_partner(osv.osv_memory): if context is None: context = {} lead = self.pool.get('crm.lead') - partner_id = self._create_partner(cr, uid, ids, context=context) + res = False + partner_ids_map = self._create_partner(cr, uid, ids, context=context) lead_ids = vals.get('lead_ids', []) - user_ids = vals.get('user_ids', False) team_id = vals.get('section_id', False) - return lead.convert_opportunity(cr, uid, lead_ids, partner_id, user_ids, team_id, context=context) + for lead_id in lead_ids: + partner_id = partner_ids_map.get(lead_id, False) + # FIXME: cannot pass user_ids as the salesman allocation only works in batch + res = lead.convert_opportunity(cr, uid, [lead_id], partner_id, [], team_id, context=context) + # FIXME: must perform salesman allocation in batch separately here + user_ids = vals.get('user_ids', False) + if user_ids: + lead.allocate_salesman(cr, uid, lead_ids, user_ids, team_id=team_id, context=context) + return res def _merge_opportunity(self, cr, uid, ids, opportunity_ids, action='merge', context=None): - #TOFIX: is it usefully ? if context is None: context = {} - merge_opportunity = self.pool.get('crm.merge.opportunity') res = False - #If we convert in mass, don't merge if there is no other opportunity but no warning - if action == 'merge' and (len(opportunity_ids) > 1 or not context.get('mass_convert') ): - self.write(cr, uid, ids, {'opportunity_ids' : [(6,0, [opportunity_ids[0].id])]}, context=context) - context.update({'lead_ids' : record_id, "convert" : True}) - res = merge_opportunity.merge(cr, uid, data.opportunity_ids, context=context) + # Expected: all newly-converted leads (active_ids) will be merged with the opportunity(ies) + # that have been selected in the 'opportunity_ids' m2m, with all these records + # merged into the first opportunity (and the rest deleted) + opportunity_ids = [o.id for o in opportunity_ids] + lead_ids = context.get('active_ids', []) + if action == 'merge' and lead_ids and opportunity_ids: + # Add the leads in the to-merge list, next to other opps + # (the fact that they're passed in context['lead_ids'] means that + # they cannot be selected to contain the result of the merge. + opportunity_ids.extend(lead_ids) + context.update({'lead_ids': lead_ids, "convert" : True}) + res = self.pool.get('crm.lead').merge_opportunity(cr, uid, opportunity_ids, context=context) return res def action_apply(self, cr, uid, ids, context=None): @@ -133,12 +144,11 @@ class crm_lead2opportunity_partner(osv.osv_memory): """ if not context: context = {} - lead = self.pool.get('crm.lead') lead_ids = context.get('active_ids', []) data = self.browse(cr, uid, ids, context=context)[0] self._convert_opportunity(cr, uid, ids, {'lead_ids': lead_ids}, context=context) - self._merge_opportunity(cr, uid, ids, data.opportunity_ids, data.action, context=context) + self._merge_opportunity(cr, uid, ids, data.opportunity_ids, data.name, context=context) return lead.redirect_opportunity_view(cr, uid, lead_ids[0], context=context) crm_lead2opportunity_partner() @@ -150,9 +160,8 @@ class crm_lead2opportunity_mass_convert(osv.osv_memory): _columns = { - 'user_ids': fields.many2many('res.users', string='Salesmans'), + 'user_ids': fields.many2many('res.users', string='Salesmen'), 'section_id': fields.many2one('crm.case.section', 'Sales Team'), - } def _convert_opportunity(self, cr, uid, ids, vals, context=None): data = self.browse(cr, uid, ids, context=context)[0] @@ -164,9 +173,7 @@ class crm_lead2opportunity_mass_convert(osv.osv_memory): return super(crm_lead2opportunity_mass_convert, self)._convert_opportunity(cr, uid, ids, vals, context=context) def mass_convert(self, cr, uid, ids, context=None): - 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) return self.action_apply(cr, uid, ids, context=context) + 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 5f8453768c0..315f6de1d64 100644 --- a/addons/crm/wizard/crm_lead_to_opportunity_view.xml +++ b/addons/crm/wizard/crm_lead_to_opportunity_view.xml @@ -36,35 +36,45 @@ - - crm.lead2opportunity.partner.mass.form - crm.lead2opportunity.partner.mass - form - -
- - - - - - - - - - - - - - - - -