[IMP]: crm: Lead to opportunity searches for following steps for partner/existing opportunity:

1. Find partner address matches the email_from of the lead
2. Find partner name that matches the name of the lead
3. Find email of existing opportunity matches the email_from of the lead
4. Find name of existing opportunity matches the name of the lead

bzr revid: rpa@tinyerp.com-20101008114345-mxrpwhm1ccto9mhu
This commit is contained in:
rpa (Open ERP)
2010-10-08 17:13:45 +05:30
parent 0aa7ad9d0b
commit 608d6ab32b
2 changed files with 35 additions and 4 deletions
@@ -46,6 +46,9 @@
<form string="Create a Partner">
<label string="Are you sure you want to create a partner based on this lead ?" colspan="4"/>
<label string="You may have to verify that this partner does not exist already." colspan="4"/>
<group colspan="4" col="1" attrs="{'invisible':[('msg','=',False)]}">
<field name="msg" colspan="4" nolabel="1" height="50"/>
</group>
<newline />
<field name="action"/>
<group attrs="{'invisible':[('action','!=','exist')]}">
+32 -4
View File
@@ -33,7 +33,8 @@ class crm_lead2partner(osv.osv_memory):
'action': fields.selection([('exist', 'Link to an existing partner'), \
('create', 'Create a new partner')], \
'Action', required=True),
'partner_id': fields.many2one('res.partner', 'Partner')
'partner_id': fields.many2one('res.partner', 'Partner'),
'msg': fields.text('Message', readonly=True)
}
def view_init(self, cr, uid, fields, context=None):
@@ -76,12 +77,19 @@ class crm_lead2partner(osv.osv_memory):
for lead in lead_obj.browse(cr, uid, data, context=context):
partner_ids = []
if lead.email_from:
email = re.findall(r'([^ ,<@]+@[^> ,]+)', lead.email_from)
address_ids = contact_obj.search(cr, uid, [('email', 'in', email)])
# Find partner address matches the email_from of the lead
email = re.findall(r'([^ ,<@]+@[^> ,]+)', lead.email_from or '')
email = map(lambda x: "'" + x + "'", email)
if email:
cr.execute("""select id from res_partner_address
where
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
# 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)
if not partner_ids:
@@ -90,6 +98,26 @@ class crm_lead2partner(osv.osv_memory):
partner_ids = map(lambda x: x[0], cr.fetchall())
partner_id = partner_ids and partner_ids[0] or False
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)))
opp_ids = map(lambda x:x[0], cr.fetchall())
label = opp_ids and 'email' or False
if not opp_ids:
# Find name of existing opportunity matches the name of the lead
cr.execute("""SELECT l.id from crm_lead l
where type = 'opportunity' and
regexp_replace(lower(l.name), '[^a-z]*', '', 'g') = regexp_replace(%s, '[^a-z]*', '', 'g')""", (lead.name.lower(), ))
opp_ids = map(lambda x: x[0], cr.fetchall())
label = opp_ids and 'name' or False
if label:
res.update({'msg': "An existing opportunity seems to match the %s of this lead, you should double-check before converting it." % (label)})
if 'partner_id' in fields:
res.update({'partner_id': partner_id})
if 'action' in fields: