From 32d8caf08812bb027e228ef2e866ae268d7053b8 Mon Sep 17 00:00:00 2001 From: mcm-odoo Date: Tue, 12 Nov 2019 14:03:21 +0000 Subject: [PATCH] [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) --- addons/crm/models/crm_lead.py | 26 +++++++++++++++++++ addons/crm/views/crm_lead_views.xml | 2 ++ .../views/crm_lead_views.xml | 2 +- 3 files changed, 29 insertions(+), 1 deletion(-) diff --git a/addons/crm/models/crm_lead.py b/addons/crm/models/crm_lead.py index 6a24b684349..bade6ebd584 100644 --- a/addons/crm/models/crm_lead.py +++ b/addons/crm/models/crm_lead.py @@ -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: diff --git a/addons/crm/views/crm_lead_views.xml b/addons/crm/views/crm_lead_views.xml index 481b4643304..c0b8f90dcae 100644 --- a/addons/crm/views/crm_lead_views.xml +++ b/addons/crm/views/crm_lead_views.xml @@ -531,6 +531,7 @@ + @@ -682,6 +683,7 @@ + - +