[FIX] account_reconcile_model_oca: Add textual tokens to improve matching rules

Making `SUBSTRING(REGEXP_REPLACE(LOWER(...), '[^0-9a-z\s]', '', 'g'), '\S(?:.*\S)*')` is costly in sql.
To avoid that, the tokens are computed python-side to avoid this extra processing.
Also, we want to avoid extra conditions on the tokens before executing the queries (and avoid it if necessary).
For example, we want to execute the query matching the sale orders only if at least one token is prefixed correctly by 'SO'.

Related to 76da4b055f
pull/819/head
Víctor Martínez 2025-03-31 08:45:06 +02:00
parent bdfc430bf6
commit bd711b03a6
1 changed files with 12 additions and 5 deletions

View File

@ -345,8 +345,12 @@ class AccountReconcileModel(models.Model):
significant_token_size = 4 significant_token_size = 4
numerical_tokens = [] numerical_tokens = []
exact_tokens = [] exact_tokens = []
text_tokens = []
for text_value in st_line_text_values: for text_value in st_line_text_values:
tokens = (text_value or "").split() tokens = [
"".join(x for x in token if re.match(r"[0-9a-zA-Z\s]", x))
for token in (text_value or "").split()
]
# Numerical tokens # Numerical tokens
for token in tokens: for token in tokens:
@ -354,6 +358,7 @@ class AccountReconcileModel(models.Model):
if len(token) < significant_token_size: if len(token) < significant_token_size:
continue continue
text_tokens.append(token)
formatted_token = "".join(x for x in token if x.isdecimal()) formatted_token = "".join(x for x in token if x.isdecimal())
# The token is too short after formatting to be significant. # The token is too short after formatting to be significant.
@ -365,7 +370,7 @@ class AccountReconcileModel(models.Model):
# Exact tokens. # Exact tokens.
if len(tokens) == 1: if len(tokens) == 1:
exact_tokens.append(tokens[0]) exact_tokens.append(tokens[0])
return numerical_tokens, exact_tokens return numerical_tokens, exact_tokens, text_tokens
def _get_invoice_matching_amls_candidates(self, st_line, partner): def _get_invoice_matching_amls_candidates(self, st_line, partner):
"""Returns the match candidates for the 'invoice_matching' rule, with respect to """Returns the match candidates for the 'invoice_matching' rule, with respect to
@ -391,9 +396,11 @@ class AccountReconcileModel(models.Model):
sub_queries = [] sub_queries = []
all_params = [] all_params = []
numerical_tokens, exact_tokens = self._get_invoice_matching_st_line_tokens( (
st_line numerical_tokens,
) exact_tokens,
_text_tokens,
) = self._get_invoice_matching_st_line_tokens(st_line)
if numerical_tokens: if numerical_tokens:
for table_alias, field in ( for table_alias, field in (
("account_move_line", "name"), ("account_move_line", "name"),