diff --git a/addons/crm/wizard/crm_lead_to_opportunity.py b/addons/crm/wizard/crm_lead_to_opportunity.py index 44c83f7c70c..ea6145f3719 100644 --- a/addons/crm/wizard/crm_lead_to_opportunity.py +++ b/addons/crm/wizard/crm_lead_to_opportunity.py @@ -22,6 +22,7 @@ from osv import osv, fields from tools.translate import _ import tools +import re import time @@ -54,6 +55,8 @@ class crm_lead2opportunity_partner(osv.osv_memory): partner_id = False for lead in lead_obj.browse(cr, uid, opportunities, context=context): partner_id = lead.partner_id and lead.partner_id.id or False + email = re.findall(r'([^ ,<@]+@[^> ,]+)', lead.email_from or '') + email = map(lambda x: "'" + x + "'", email) if not partner_id and res.get('partner_id'): partner_id = res.get('partner_id') @@ -63,6 +66,18 @@ class crm_lead2opportunity_partner(osv.osv_memory): ids = lead_obj.search(cr, uid, [('partner_id', '=', partner_id), ('type', '=', 'opportunity'), '!', ('state', 'in', ['done', 'cancel'])]) 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 + substring(email_from from '([^ ,<@]+@[^> ,]+)') in (%s)""" % (','.join(email))) + ids = map(lambda x:x[0], cr.fetchall()) + if ids: + opportunities.append(ids[0]) if 'action' in fields: res.update({'action' : partner_id and 'exist' or 'create'}) @@ -84,7 +99,6 @@ class crm_lead2opportunity_partner(osv.osv_memory): @param uid: the current user’s ID for security checks, @param fields: List of fields for default value @param context: A standard dictionary for contextual values - """ if context is None: context = {} @@ -92,8 +106,7 @@ class crm_lead2opportunity_partner(osv.osv_memory): for lead in lead_obj.browse(cr, uid, context.get('active_ids', []), context=context): if lead.state in ['done', 'cancel']: - raise osv.except_osv(_("Warning !"), _("Closed/Cancelled \ -Leads Could not convert into Opportunity")) + raise osv.except_osv(_("Warning !"), _("Closed/Cancelled Leads Could not convert into Opportunity")) return False def _convert(self, cr, uid, ids, lead, partner_id, stage_ids, context=None): diff --git a/addons/crm/wizard/crm_lead_to_partner.py b/addons/crm/wizard/crm_lead_to_partner.py index 1a9304adc85..a3db910a815 100644 --- a/addons/crm/wizard/crm_lead_to_partner.py +++ b/addons/crm/wizard/crm_lead_to_partner.py @@ -85,15 +85,15 @@ class crm_lead2partner(osv.osv_memory): substring(email from '([^ ,<@]+@[^> ,]+)') in (%s)""" % (','.join(email))) address_ids = map(lambda x: x[0], cr.fetchall()) if address_ids: - addresses = contact_obj.browse(cr, uid, address_ids) - partner_ids = addresses and [addresses[0].partner_id.id] or False - + partner_ids = partner_obj.search(cr, uid, [('address', 'in', address_ids)], context=context) + # Find partner name that matches the name of the lead if not partner_ids and lead.partner_name: partner_ids = partner_obj.search(cr, uid, [('name', '=', lead.partner_name)], context=context) - partner_id = partner_ids and partner_ids[0] or False + + if 'partner_id' in fields: res.update({'partner_id': partner_id})