[IMP] crm: allow searching leads by phone or mobile numbers

PURPOSE

Purpose of this commit is to allow users to look for leads and opportunities
by using the phone/mobile number fields. For example this allows salesmen
working in call centers to quickly retrieve all open opportunities of a
specific caller.

SPECIFICATIONS

Add a phone_search field allowing to implement a search on both phone and
mobile fields.

Allow searching independently from local format used to store the phone number.
Imagine we have leads with phone or mobile number that looks like ``+32 485 11
22 33``. We want to find this lead even when typing ``485112233``.

A custom search method is therefore implemented. It uses only the digits in the
phone or mobile number to perform the search.

A limitation is added on search having to contain at last 3 digits. Indeed
as it returns a domain whose leaf is ``[('id', 'in', ids)]`` it may be huge
for production databases.

LINKS

Task 2122873
PR #40131

Signed-off-by: Thibault Delavallee (tde) <tde@openerp.com>
This commit is contained in:
mcm-odoo
2019-12-12 10:28:47 +00:00
committed by Thibault Delavallée
parent 90bf317c01
commit 32d8caf088
3 changed files with 29 additions and 1 deletions
+26
View File
@@ -136,6 +136,7 @@ class Lead(models.Model):
lang_id = fields.Many2one('res.lang', string='Language', help="Language of the lead.")
phone = fields.Char('Phone', tracking=50)
mobile = fields.Char('Mobile')
phone_mobile_search = fields.Char('Phone/Mobile', store=False, search='_search_phone_mobile_search')
function = fields.Char('Job Position')
title = fields.Many2one('res.partner.title')
company_id = fields.Many2one('res.company', string='Company', index=True, default=lambda self: self.env.company.id)
@@ -246,6 +247,31 @@ class Lead(models.Model):
for lead in self:
lead.meeting_count = mapped_data.get(lead.id, 0)
def _search_phone_mobile_search(self, operator, value):
if len(value) <= 2:
raise UserError(_('Please enter at least 3 digits when searching on phone / mobile.'))
query = f"""
SELECT model.id
FROM {self._table} model
WHERE REGEXP_REPLACE(model.phone, '[^\d+]+', '', 'g') SIMILAR TO CONCAT(%s, REGEXP_REPLACE(%s, '\D+', '', 'g'), '%%')
OR REGEXP_REPLACE(model.mobile, '[^\d+]+', '', 'g') SIMILAR TO CONCAT(%s, REGEXP_REPLACE(%s, '\D+', '', 'g'), '%%')
"""
# searching on +32485112233 should also finds 00485112233 (00 / + prefix are both valid)
# we therefore remove it from input value and search for both of them in db
if value.startswith('+') or value.startswith('00'):
value = value.replace('+', '').replace('00', '', 1)
starts_with = '00|\+'
else:
starts_with = '%'
self._cr.execute(query, (starts_with, value, starts_with, value))
res = self._cr.fetchall()
if not res:
return [(0, '=', 1)]
return [('id', 'in', [r[0] for r in res])]
def _onchange_partner_id_values(self, partner_id):
""" returns the new values when partner_id has changed """
if partner_id:
+2
View File
@@ -531,6 +531,7 @@
<field name="team_id"/>
<field name="country_id"/>
<field name="city"/>
<field name="phone_mobile_search"/>
<field name="lang_id"/>
<field name="create_date"/>
<field name="source_id"/>
@@ -682,6 +683,7 @@
<field name="stage_id" domain="[]"/>
<field name="country_id"/>
<field name="city"/>
<field name="phone_mobile_search"/>
<separator/>
<filter string="My Pipeline" name="assigned_to_me"
domain="[('user_id', '=', uid)]"
@@ -55,7 +55,7 @@
<filter name="unassigned" position="after">
<filter string="My Assigned Partners" name="my_assigned_partners" domain="[('partner_assigned_id.user_id', '=', uid)]"/>
</filter>
<field name="city" position="after">
<field name="phone_mobile_search" position="after">
<field name="partner_assigned_id"/>
</field>
</field>