From 83a43743630b0a47f2866fcfe1270749ce620656 Mon Sep 17 00:00:00 2001 From: Denis Ledoux Date: Wed, 5 Mar 2014 17:40:16 +0100 Subject: [PATCH] [FIX] crm: lead_to_opp, do not force assignation in mass convert with deduplication (to prevent change the salesman of an existing opp), + fix get duplicated lead, now based on probability of stage instead of probability of the opp itself bzr revid: dle@openerp.com-20140305164016-432vz5axo5l4wqko --- addons/crm/wizard/crm_lead_to_opportunity.py | 35 +++++++++++++------ .../wizard/crm_lead_to_opportunity_view.xml | 2 ++ .../wizard/crm_merge_opportunities_view.xml | 2 +- 3 files changed, 27 insertions(+), 12 deletions(-) diff --git a/addons/crm/wizard/crm_lead_to_opportunity.py b/addons/crm/wizard/crm_lead_to_opportunity.py index 09e917479dc..ea95a3c5425 100644 --- a/addons/crm/wizard/crm_lead_to_opportunity.py +++ b/addons/crm/wizard/crm_lead_to_opportunity.py @@ -46,12 +46,12 @@ class crm_lead2opportunity_partner(osv.osv_memory): results = [] if partner_id: # Search for opportunities that have the same partner and that arent done or cancelled - ids = lead_obj.search(cr, uid, [('partner_id', '=', partner_id), '|', ('probability', '=', False), ('probability', '<', '100')]) + ids = lead_obj.search(cr, uid, [('partner_id', '=', partner_id), '|', ('stage_id.probability', '=', False), ('stage_id.probability', '<', '100')]) for id in ids: results.append(id) email = re.findall(r'([^ ,<@]+@[^> ,]+)', email or '') if email: - ids = lead_obj.search(cr, uid, [('email_from', '=ilike', email[0]), '|', ('probability', '=', False), ('probability', '<', '100')]) + ids = lead_obj.search(cr, uid, [('email_from', '=ilike', email[0]), '|', ('stage_id.probability', '=', False), ('stage_id.probability', '<', '100')]) for id in ids: results.append(id) return list(set(results)) @@ -124,14 +124,17 @@ class crm_lead2opportunity_partner(osv.osv_memory): lead_ids = vals.get('lead_ids', []) team_id = vals.get('section_id', False) data = self.browse(cr, uid, ids, context=context)[0] - for lead_id in lead_ids: - partner_id = self._create_partner(cr, uid, lead_id, data.action, data.partner_id.id, context=context) - # 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 + leads = lead.browse(cr, uid, lead_ids, context=context) + for lead_id in leads: + partner_id = self._create_partner(cr, uid, lead_id.id, data.action, lead_id.partner_id.id, context=context) + res = lead.convert_opportunity(cr, uid, [lead_id.id], partner_id, [], team_id, context=context) user_ids = vals.get('user_ids', False) + if context.get('no_force_assignation'): + leads_to_allocate = [lead_id.id for lead_id in leads if not lead_id.user_id] + else: + leads_to_allocate = lead_ids if user_ids: - lead.allocate_salesman(cr, uid, lead_ids, user_ids, team_id=team_id, context=context) + lead.allocate_salesman(cr, uid, leads_to_allocate, user_ids, team_id=team_id, context=context) return res def action_apply(self, cr, uid, ids, context=None): @@ -142,15 +145,19 @@ class crm_lead2opportunity_partner(osv.osv_memory): if context is None: context = {} + lead_obj = self.pool['crm.lead'] + w = self.browse(cr, uid, ids, context=context)[0] opp_ids = [o.id for o in w.opportunity_ids] if w.name == 'merge': - lead_id = self.pool.get('crm.lead').merge_opportunity(cr, uid, opp_ids, w.user_id.id, w.section_id.id, context=context) + lead_id = lead_obj.merge_opportunity(cr, uid, opp_ids, context=context) lead_ids = [lead_id] - lead = self.pool.get('crm.lead').read(cr, uid, lead_id, ['type'], context=context) + lead = lead_obj.read(cr, uid, lead_id, ['type', 'user_id'], context=context) if lead['type'] == "lead": context.update({'active_ids': lead_ids}) self._convert_opportunity(cr, uid, ids, {'lead_ids': lead_ids, 'user_ids': [w.user_id.id], 'section_id': w.section_id.id}, context=context) + elif not context.get('no_force_assignation') or not lead['user_id']: + lead_obj.write(cr, uid, lead_id, {'user_id': w.user_id.id, 'section_id': w.section_id.id}, context=context) else: lead_ids = context.get('active_ids', []) self._convert_opportunity(cr, uid, ids, {'lead_ids': lead_ids, 'user_ids': [w.user_id.id], 'section_id': w.section_id.id}, context=context) @@ -184,11 +191,13 @@ class crm_lead2opportunity_mass_convert(osv.osv_memory): _columns = { 'user_ids': fields.many2many('res.users', string='Salesmen'), 'section_id': fields.many2one('crm.case.section', 'Sales Team'), - 'deduplicate': fields.boolean('Apply deduplication', help='Merge with existing leads/opportunities of each partner'), + 'deduplicate': fields.boolean('Apply deduplication', help='Merge with existing leads/opportunities of each partner'), 'action': fields.selection([ ('each_exist_or_create', 'Use existing partner or create'), ('nothing', 'Do not link to a customer') ], 'Related Customer', required=True), + # Uncomment me in trunk + # 'force_assignation': fields.boolean('Force assignation', help='If unchecked, this will leave the salesman of duplicated opportunities'), } _defaults = { @@ -264,6 +273,10 @@ class crm_lead2opportunity_mass_convert(osv.osv_memory): active_ids = active_ids.difference(merged_lead_ids) active_ids = active_ids.union(remaining_lead_ids) ctx['active_ids'] = list(active_ids) + # Remove me in trunk + ctx['no_force_assignation'] = True + # Uncomment me in trunk + # ctx['no_force_assignation'] = not data.force_assignation return self.action_apply(cr, uid, ids, context=ctx) # 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 58fbe244ba4..0f8f676d92d 100644 --- a/addons/crm/wizard/crm_lead_to_opportunity_view.xml +++ b/addons/crm/wizard/crm_lead_to_opportunity_view.xml @@ -58,6 +58,8 @@ + +