From baaf16d34857ab2925294c98c6d04bd755539028 Mon Sep 17 00:00:00 2001 From: clementthomas Date: Thu, 17 Oct 2024 10:22:53 +0200 Subject: [PATCH] [16.0][l10n_fr_department] improve search res_partner by department --- .../model/res_country_department.py | 29 +++++++++++++++++++ l10n_fr_department/view/res_partner.xml | 1 + 2 files changed, 30 insertions(+) diff --git a/l10n_fr_department/model/res_country_department.py b/l10n_fr_department/model/res_country_department.py index b124b8502..43c7f0db6 100644 --- a/l10n_fr_department/model/res_country_department.py +++ b/l10n_fr_department/model/res_country_department.py @@ -4,6 +4,8 @@ # @author Alexis de Lattre (alexis.delattre@akretion.com) # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). +import re + from odoo import api, fields, models @@ -48,3 +50,30 @@ def name_get(self): dname = "{} ({})".format(rec.name, rec.code) res.append((rec.id, dname)) return res + + @api.model + def _name_search( + self, name, args=None, operator="ilike", limit=100, name_get_uid=None + ): + args = args or [] + + if name: + # Be sure name_search is symetric to name_get + match = re.match(r"^(.*)\s\((.*)\)$", name) + if match: + dpt_name = match.group(1) + dpt_code = match.group(2) + args += [("code", operator, dpt_code), ("name", operator, dpt_name)] + else: + # Search on code and name + if operator in ("not ilike", "!="): + bool_operator = "&" # for negative comparators, use AND + else: + bool_operator = "|" # for positive comparators, use OR + args += [ + bool_operator, + ("code", operator, name), + ("name", operator, name), + ] + + return self._search(args, limit=limit, access_rights_uid=name_get_uid) diff --git a/l10n_fr_department/view/res_partner.xml b/l10n_fr_department/view/res_partner.xml index dcce6569b..92728511d 100644 --- a/l10n_fr_department/view/res_partner.xml +++ b/l10n_fr_department/view/res_partner.xml @@ -11,6 +11,7 @@ +