diff --git a/addons/crm/crm_lead.py b/addons/crm/crm_lead.py index b9741d17c3a..703a389ba93 100644 --- a/addons/crm/crm_lead.py +++ b/addons/crm/crm_lead.py @@ -531,7 +531,7 @@ class crm_lead(base_stage, 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:] @@ -591,8 +591,6 @@ class crm_lead(base_stage, 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) @@ -604,6 +602,10 @@ class crm_lead(base_stage, 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_contact(self, cr, uid, lead, name, is_company, parent_id=False, context=None): @@ -663,10 +665,12 @@ class crm_lead(base_stage, 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._create_lead_partner(cr, uid, lead, context) + partner_id = force_partner_id or self._create_lead_partner(cr, uid, lead, context=context) self._lead_set_partner(cr, uid, lead, partner_id, context=context) partner_ids[lead.id] = partner_id return partner_ids @@ -825,7 +829,7 @@ class crm_lead(base_stage, osv.osv): if update_vals is None: update_vals = {} if msg.get('priority') in dict(crm.AVAILABLE_PRIORITIES): - vals['priority'] = msg.get('priority') + update_vals['priority'] = msg.get('priority') maps = { 'cost':'planned_cost', 'revenue': 'planned_revenue', @@ -836,7 +840,7 @@ class crm_lead(base_stage, osv.osv): res = tools.misc.command_re.match(line) if res and maps.get(res.group(1).lower()): key = maps.get(res.group(1).lower()) - vals[key] = res.group(2).lower() + update_vals[key] = res.group(2).lower() return super(crm_lead, self).message_update(cr, uid, ids, msg, update_vals=update_vals, context=context) diff --git a/addons/crm/crm_phonecall.py b/addons/crm/crm_phonecall.py index e613733d9ab..44894fc881d 100644 --- a/addons/crm/crm_phonecall.py +++ b/addons/crm/crm_phonecall.py @@ -177,11 +177,11 @@ class crm_phonecall(base_state, 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 547d3ed299e..fc366768023 100644 --- a/addons/crm/wizard/crm_lead_to_opportunity.py +++ b/addons/crm/wizard/crm_lead_to_opportunity.py @@ -24,8 +24,6 @@ from tools.translate import _ import tools import re -import time - class crm_lead2opportunity_partner(osv.osv_memory): _name = 'crm.lead2opportunity.partner' _description = 'Lead To Opportunity Partner' @@ -35,8 +33,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')]), } @@ -68,8 +66,6 @@ class crm_lead2opportunity_partner(osv.osv_memory): if ids: opportunities.append(ids[0]) 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 @@ -106,23 +102,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): @@ -131,15 +140,13 @@ 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() class crm_lead2opportunity_mass_convert(osv.osv_memory): _name = 'crm.lead2opportunity.partner.mass' @@ -148,9 +155,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] @@ -162,9 +168,6 @@ 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 8829749efa3..6a826f7bc0e 100644 --- a/addons/crm/wizard/crm_lead_to_opportunity_view.xml +++ b/addons/crm/wizard/crm_lead_to_opportunity_view.xml @@ -37,24 +37,38 @@ crm.lead2opportunity.partner.mass
- - - - + + + + - - + + - + + + + + + + + + + + + + + +
-
+ -
+
diff --git a/addons/crm/wizard/crm_lead_to_partner.py b/addons/crm/wizard/crm_lead_to_partner.py index 60d358e034c..9b239bab7b9 100644 --- a/addons/crm/wizard/crm_lead_to_partner.py +++ b/addons/crm/wizard/crm_lead_to_partner.py @@ -55,6 +55,7 @@ class crm_lead2partner(osv.osv_memory): partner = self.pool.get('res.partner') lead = self.pool.get('crm.lead') this = lead.browse(cr, uid, context.get('active_id'), context=context) + partner_id = False if this.email_from: partner_ids = partner.search(cr, uid, [('email', '=', this.email_from)], context=context) if partner_ids: @@ -106,15 +107,16 @@ class crm_lead2partner(osv.osv_memory): lead_ids = context and context.get('active_ids') or [] data = self.browse(cr, uid, ids, context=context)[0] partner_id = data.partner_id and data.partner_id.id or False - partner_ids = lead.convert_partner(cr, uid, lead_ids, data.action, partner_id, context=context) - return partner_ids[lead_ids[0]] + return lead.convert_partner(cr, uid, lead_ids, data.action, partner_id, context=context) def make_partner(self, cr, uid, ids, context=None): """ This function Makes partner based on action. """ - partner_id = self._create_partner(cr, uid, ids, context=context) - return self.pool.get('res.partner').redirect_partner_form(cr, uid, partner_id, context=context) + # Only called from Form view, so only meant to convert one Lead. + lead_id = context and context.get('active_id') or False + partner_ids_map = self._create_partner(cr, uid, ids, context=context) + return self.pool.get('res.partner').redirect_partner_form(cr, uid, partner_ids_map.get(lead_id, False), context=context) crm_lead2partner() diff --git a/addons/crm/wizard/crm_phonecall_to_partner.py b/addons/crm/wizard/crm_phonecall_to_partner.py index 422af02f99d..715b7396c66 100644 --- a/addons/crm/wizard/crm_phonecall_to_partner.py +++ b/addons/crm/wizard/crm_phonecall_to_partner.py @@ -57,12 +57,10 @@ class crm_phonecall2partner(osv.osv_memory): if context is None: context = {} phonecall = self.pool.get('crm.phonecall') - data = self.browse(cr, uid, ids, context=context)[0] call_ids = context and context.get('active_ids') or [] partner_id = data.partner_id and data.partner_id.id or False - partner_ids = phonecall.convert_partner(cr, uid, call_ids, data.action, partner_id, context=context) - return partner_ids[call_ids[0]] + return phonecall.convert_partner(cr, uid, call_ids, data.action, partner_id, context=context) crm_phonecall2partner()