From ae4c2498824b9d9dc8a9e7eec9f839e4cbe351b5 Mon Sep 17 00:00:00 2001 From: Rodrigo Date: Wed, 12 Apr 2023 16:00:51 +0000 Subject: [PATCH] [IMP] account: improve tax name search This commit allows the exact search of a text that is searched between dashes. Examples: 0EUM => VAT 0% EU M. 21M => 21% M , 21% EU M, 21% M.Cocont and 21% EX M. 21 M" => 21% M and 21% M.Cocont. 21" M"co => 21% M.Cocont. X-original-commit: ad2682c513c82052539cf99a54994b035b2afe6d Part-of: odoo/odoo#119756 --- addons/account/models/account_tax.py | 24 ++++++++++++++++++------ addons/account/tests/test_tax.py | 24 ++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 6 deletions(-) diff --git a/addons/account/models/account_tax.py b/addons/account/models/account_tax.py index 81b159854d6..2d6f81a70d3 100644 --- a/addons/account/models/account_tax.py +++ b/addons/account/models/account_tax.py @@ -231,13 +231,25 @@ class AccountTax(models.Model): 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. + Technical: 0EUM => 0%E%U%M + 21M => 2%1%M% where the % represents 0, 1 or multiple characters in a SQL 'LIKE' search. + 21" M" => 2%1% M% + 21" M"co => 2%1% M%c%o% + Examples: 0EUM => VAT 0% EU M. + 21M => 21% M , 21% EU M, 21% M.Cocont and 21% EX M. + 21" M" => 21% M and 21% M.Cocont. + 21" M"co => 21% M.Cocont. """ - name = re.sub(r"\W+", "", name) # Remove non-alphanumeric characters. - return '%'.join(list(name)) + regex = r"(\"[^\"]*\")" + list_name = re.split(regex, name) + for i, name in enumerate(list_name.copy()): + if not name: + continue + if re.search(regex, name): + list_name[i] = "%" + name.replace("%", "_").replace("\"", "") + "%" + else: + list_name[i] = '%'.join(re.sub(r"\W+", "", name)) + return ''.join(list_name) @api.model def _name_search(self, name, domain=None, operator='ilike', limit=None, order=None, name_get_uid=None): diff --git a/addons/account/tests/test_tax.py b/addons/account/tests/test_tax.py index 969b76e7898..e3ef9370009 100644 --- a/addons/account/tests/test_tax.py +++ b/addons/account/tests/test_tax.py @@ -1144,3 +1144,27 @@ class TestTax(TestTaxCommon): ], compute_all_res ) + + def test_parse_name_search(self): + list_ten_fixed_tax = self.env["account.tax"] + ten_fixed_tax = self.env["account.tax"].create( + {"name": "Ten Fixed tax", "amount_type": "fixed", "amount": 10} + ) + list_ten_fixed_tax |= ten_fixed_tax + ten_fixed_tax_tix = self.env["account.tax"].create( + {"name": "Ten Fixed tax tix", "amount_type": "fixed", "amount": 10} + ) + list_ten_fixed_tax |= ten_fixed_tax_tix + + self.assertListEqual( + [x[0] for x in self.env["account.tax"].name_search("tix")], + list_ten_fixed_tax.ids, + ) + self.assertListEqual( + [x[0] for x in self.env["account.tax"].name_search("\"tix\"")], + ten_fixed_tax_tix.ids, + ) + self.assertListEqual( + [x[0] for x in self.env["account.tax"].name_search("Ten \"tix\"")], + ten_fixed_tax_tix.ids, + )