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 @@ + - +