[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
This commit is contained in:
@@ -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):
|
||||
|
||||
@@ -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,
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user