From c56c137f533a782c8a99e2c30c5b116af9ce81d2 Mon Sep 17 00:00:00 2001 From: "Ricardo Gomes Rodrigues (rigr)" Date: Tue, 31 May 2022 15:10:08 +0000 Subject: [PATCH] [IMP] account: improve tax name search MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This commit improves tax name search by ignoring punctuation and spaces. This allows faster searching for taxes (while editing an invoice line or while configuring them) Example: Searching for `21M` should match `21% M.` , `21% EU M.` and `21% M.Cocont`. Searching for `0EUT` should match `0% EU T`. Task id: 2851341 closes odoo/odoo#92683 Signed-off-by: William André (wan) --- addons/account/models/account_tax.py | 26 ++++++++++++++++++++++ addons/account/views/account_tax_views.xml | 2 +- 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/addons/account/models/account_tax.py b/addons/account/models/account_tax.py index 501382bc237..b6f4f540699 100644 --- a/addons/account/models/account_tax.py +++ b/addons/account/models/account_tax.py @@ -8,6 +8,7 @@ from odoo.tools import frozendict from collections import defaultdict import math +import re TYPE_TAX_USE = [ @@ -85,6 +86,10 @@ class AccountTax(models.Model): return self.env.ref('account.tax_group_taxes') name = fields.Char(string='Tax Name', required=True) + name_searchable = fields.Char(store=False, search='_search_name', + help="This dummy field lets us use another search method on the field 'name'." + "This allows more freedom on how to search the 'name' compared to 'filter_domain'." + "See '_search_name' and '_parse_name_search' for why this is not possible with 'filter_domain'.") type_tax_use = fields.Selection(TYPE_TAX_USE, string='Tax Type', required=True, default="sale", help="Determines where the tax is selectable. Note : 'None' means a tax can't be used by itself, however it can still be used in a group. 'adjustment' is used to perform tax adjustment.") tax_scope = fields.Selection([('service', 'Services'), ('consu', 'Goods')], string="Tax Scope", help="Restrict the use of taxes to a type of product.") @@ -175,6 +180,27 @@ class AccountTax(models.Model): return rslt + @staticmethod + def _parse_name_search(name): + """ + Parse the name to search the taxes faster. + Technical: 0EUM => 0%E%U%M + 21M => 2%1%M% where the % represents 0, 1 or multiple characters in a SQL 'LIKE' search. + Examples: 0EUM => VAT 0% EU M. + 21M => 21% M , 21% EU M and 21% M.Cocont. + """ + name = re.sub(r"\W+", "", name) # Remove non-alphanumeric characters. + return '%'.join(list(name)) + + @api.model + def name_search(self, name='', args=None, operator='ilike', limit=100): + return super().name_search(name=AccountTax._parse_name_search(name), args=args, operator=operator, limit=limit) + + def _search_name(self, operator, value): + if operator not in ("ilike", "like") or not isinstance(value, str): + return super()._search_name(operator, value) + return [('name', operator, AccountTax._parse_name_search(value))] + def _check_repartition_lines(self, lines): self.ensure_one() diff --git a/addons/account/views/account_tax_views.xml b/addons/account/views/account_tax_views.xml index 0c1f4fdce47..232196e7ac1 100644 --- a/addons/account/views/account_tax_views.xml +++ b/addons/account/views/account_tax_views.xml @@ -92,7 +92,7 @@ account.tax - +