[FIX] account_reconcile_model_oca: only pre-mount true matches with reconcile models

Before, when using reconcile models that wanted to match invoice/bills
to statement based on the Label and/or Note and/or Reference, if there
is no real match with the statement label, the bank reco model would
mount/suggest another entry from the same partner.

This is misleading as we expect to be suggested with lines that actually
match with the statement, as stated in the reconciliation model.

Now, if any of the Invoices/bills matching rule is toggled, it will
suggest *only* matching amls. If no rule is toggled, it will behave like
before (mounting a match if any, or an aml from the same partner).

Related to 8948b31407
pull/819/head
Víctor Martínez 2025-03-31 09:34:13 +02:00
parent 13f438d320
commit 6d53fcd7f8
2 changed files with 50 additions and 10 deletions

View File

@ -481,6 +481,15 @@ class AccountReconcileModel(models.Model):
"allow_auto_reconcile": True, "allow_auto_reconcile": True,
"amls": self.env["account.move.line"].browse(candidate_ids), "amls": self.env["account.move.line"].browse(candidate_ids),
} }
elif (
self.match_text_location_label
or self.match_text_location_note
or self.match_text_location_reference
):
# In the case any of the Label, Note or Reference matching rule has been
# toggled, and the query didn't return
# any candidates, the model should not try to mount another aml instead.
return
if not partner: if not partner:
st_line_currency = ( st_line_currency = (

View File

@ -284,6 +284,7 @@ class TestReconciliationMatchingRules(AccountTestInvoicingCommon):
def test_matching_fields(self): def test_matching_fields(self):
# Check without restriction. # Check without restriction.
self.rule_1.match_text_location_label = False
self._check_statement_matching( self._check_statement_matching(
self.rule_1, self.rule_1,
{ {
@ -299,6 +300,7 @@ class TestReconciliationMatchingRules(AccountTestInvoicingCommon):
) )
def test_matching_fields_match_journal_ids(self): def test_matching_fields_match_journal_ids(self):
self.rule_1.match_text_location_label = False
self.rule_1.match_journal_ids |= self.cash_line_1.journal_id self.rule_1.match_journal_ids |= self.cash_line_1.journal_id
self._check_statement_matching( self._check_statement_matching(
self.rule_1, self.rule_1,
@ -310,6 +312,7 @@ class TestReconciliationMatchingRules(AccountTestInvoicingCommon):
) )
def test_matching_fields_match_nature(self): def test_matching_fields_match_nature(self):
self.rule_1.match_text_location_label = False
self.rule_1.match_nature = "amount_received" self.rule_1.match_nature = "amount_received"
self._check_statement_matching( self._check_statement_matching(
self.rule_1, self.rule_1,
@ -335,6 +338,7 @@ class TestReconciliationMatchingRules(AccountTestInvoicingCommon):
) )
def test_matching_fields_match_amount(self): def test_matching_fields_match_amount(self):
self.rule_1.match_text_location_label = False
self.rule_1.match_amount = "lower" self.rule_1.match_amount = "lower"
self.rule_1.match_amount_max = 150 self.rule_1.match_amount_max = 150
self._check_statement_matching( self._check_statement_matching(
@ -378,6 +382,7 @@ class TestReconciliationMatchingRules(AccountTestInvoicingCommon):
) )
def test_matching_fields_match_label(self): def test_matching_fields_match_label(self):
self.rule_1.match_text_location_label = False
self.rule_1.match_label = "contains" self.rule_1.match_label = "contains"
self.rule_1.match_label_param = "yyyyy" self.rule_1.match_label_param = "yyyyy"
self._check_statement_matching( self._check_statement_matching(
@ -424,21 +429,27 @@ class TestReconciliationMatchingRules(AccountTestInvoicingCommon):
) )
# Exact matching. # Exact matching.
st_line = self._create_st_line(amount=bsl_sign * 1000.0) st_line = self._create_st_line(
amount=bsl_sign * 1000.0, payment_ref=invl.name
)
self._check_statement_matching( self._check_statement_matching(
rule, rule,
{st_line: {"amls": invl, "model": rule}}, {st_line: {"amls": invl, "model": rule}},
) )
# No matching because there is no tolerance. # No matching because there is no tolerance.
st_line = self._create_st_line(amount=bsl_sign * 990.0) st_line = self._create_st_line(
amount=bsl_sign * 990.0, payment_ref=invl.name
)
self._check_statement_matching( self._check_statement_matching(
rule, rule,
{st_line: {}}, {st_line: {}},
) )
# The payment amount is higher than the invoice one. # The payment amount is higher than the invoice one.
st_line = self._create_st_line(amount=bsl_sign * 1010.0) st_line = self._create_st_line(
amount=bsl_sign * 1010.0, payment_ref=invl.name
)
self._check_statement_matching( self._check_statement_matching(
rule, rule,
{st_line: {"amls": invl, "model": rule}}, {st_line: {"amls": invl, "model": rule}},
@ -461,7 +472,9 @@ class TestReconciliationMatchingRules(AccountTestInvoicingCommon):
) )
# No matching because there is no tolerance. # No matching because there is no tolerance.
st_line = self._create_st_line(amount=bsl_sign * 990.0) st_line = self._create_st_line(
amount=bsl_sign * 990.0, payment_ref="123456"
)
self._check_statement_matching( self._check_statement_matching(
rule, rule,
{st_line: {}}, {st_line: {}},
@ -490,7 +503,9 @@ class TestReconciliationMatchingRules(AccountTestInvoicingCommon):
) )
# No matching because there is no enough tolerance. # No matching because there is no enough tolerance.
st_line = self._create_st_line(amount=bsl_sign * 990.0) st_line = self._create_st_line(
amount=bsl_sign * 990.0, payment_ref=invl.name
)
self._check_statement_matching( self._check_statement_matching(
rule, rule,
{st_line: {}}, {st_line: {}},
@ -499,7 +514,9 @@ class TestReconciliationMatchingRules(AccountTestInvoicingCommon):
# The payment amount is higher than the invoice one. # The payment amount is higher than the invoice one.
# However, since the invoice amount is lower than the payment amount, # However, since the invoice amount is lower than the payment amount,
# the tolerance is not checked and the invoice line is matched. # the tolerance is not checked and the invoice line is matched.
st_line = self._create_st_line(amount=bsl_sign * 1010.0) st_line = self._create_st_line(
amount=bsl_sign * 1010.0, payment_ref=invl.name
)
self._check_statement_matching( self._check_statement_matching(
rule, rule,
{st_line: {"amls": invl, "model": rule}}, {st_line: {"amls": invl, "model": rule}},
@ -518,7 +535,9 @@ class TestReconciliationMatchingRules(AccountTestInvoicingCommon):
) )
# Enough tolerance to match the invoice line. # Enough tolerance to match the invoice line.
st_line = self._create_st_line(amount=bsl_sign * 1185.80) st_line = self._create_st_line(
amount=bsl_sign * 1185.80, payment_ref=invl.name
)
self._check_statement_matching( self._check_statement_matching(
rule, rule,
{st_line: {"amls": invl, "model": rule, "status": "write_off"}}, {st_line: {"amls": invl, "model": rule, "status": "write_off"}},
@ -527,7 +546,9 @@ class TestReconciliationMatchingRules(AccountTestInvoicingCommon):
# The payment amount is higher than the invoice one. # The payment amount is higher than the invoice one.
# However, since the invoice amount is lower than the payment amount, # However, since the invoice amount is lower than the payment amount,
# the tolerance is not checked and the invoice line is matched. # the tolerance is not checked and the invoice line is matched.
st_line = self._create_st_line(amount=bsl_sign * 1234.20) st_line = self._create_st_line(
amount=bsl_sign * 1234.20, payment_ref=invl.name
)
self._check_statement_matching( self._check_statement_matching(
rule, rule,
{st_line: {"amls": invl, "model": rule}}, {st_line: {"amls": invl, "model": rule}},
@ -576,7 +597,9 @@ class TestReconciliationMatchingRules(AccountTestInvoicingCommon):
invl = self._create_invoice_line( invl = self._create_invoice_line(
990.0, self.partner_a, inv_type, inv_date="2019-01-01" 990.0, self.partner_a, inv_type, inv_date="2019-01-01"
) )
st_line = self._create_st_line(amount=bsl_sign * 1000) st_line = self._create_st_line(
amount=bsl_sign * 1000, payment_ref=invl.name
)
# Partial reconciliation. # Partial reconciliation.
self._check_statement_matching( self._check_statement_matching(
@ -656,6 +679,7 @@ class TestReconciliationMatchingRules(AccountTestInvoicingCommon):
) )
def test_matching_fields_match_partner_category_ids(self): def test_matching_fields_match_partner_category_ids(self):
self.rule_1.match_text_location_label = False
test_category = self.env["res.partner.category"].create( test_category = self.env["res.partner.category"].create(
{"name": "Consulting Services"} {"name": "Consulting Services"}
) )
@ -676,6 +700,7 @@ class TestReconciliationMatchingRules(AccountTestInvoicingCommon):
def test_mixin_rules(self): def test_mixin_rules(self):
"""Test usage of rules together.""" """Test usage of rules together."""
self.rule_1.match_text_location_label = False
# rule_1 is used before rule_2. # rule_1 is used before rule_2.
self.rule_1.sequence = 1 self.rule_1.sequence = 1
self.rule_2.sequence = 2 self.rule_2.sequence = 2
@ -749,6 +774,7 @@ class TestReconciliationMatchingRules(AccountTestInvoicingCommon):
self.rule_1.sequence = 2 self.rule_1.sequence = 2
self.rule_1.auto_reconcile = True self.rule_1.auto_reconcile = True
self.rule_1.payment_tolerance_param = 10.0 self.rule_1.payment_tolerance_param = 10.0
self.rule_1.match_text_location_label = False
self.rule_2.sequence = 1 self.rule_2.sequence = 1
self.rule_2.match_partner_ids |= self.partner_2 self.rule_2.match_partner_ids |= self.partner_2
self.rule_2.auto_reconcile = True self.rule_2.auto_reconcile = True
@ -785,6 +811,7 @@ class TestReconciliationMatchingRules(AccountTestInvoicingCommon):
self.rule_1.allow_payment_tolerance = False self.rule_1.allow_payment_tolerance = False
self.rule_1.auto_reconcile = True self.rule_1.auto_reconcile = True
self.rule_1.line_ids = [(5, 0, 0)] self.rule_1.line_ids = [(5, 0, 0)]
self.rule_1.match_text_location_label = False
self._check_statement_matching( self._check_statement_matching(
self.rule_1, self.rule_1,
@ -952,6 +979,7 @@ class TestReconciliationMatchingRules(AccountTestInvoicingCommon):
move_reversed = move._reverse_moves() move_reversed = move._reverse_moves()
self.assertTrue(move_reversed.exists()) self.assertTrue(move_reversed.exists())
self.rule_1.match_text_location_label = False
self.bank_line_1.write( self.bank_line_1.write(
{ {
"payment_ref": "8", "payment_ref": "8",
@ -993,7 +1021,7 @@ class TestReconciliationMatchingRules(AccountTestInvoicingCommon):
"partner_id": partner.id, "partner_id": partner.id,
"foreign_currency_id": currency_statement.id, "foreign_currency_id": currency_statement.id,
"amount_currency": 100, "amount_currency": 100,
"payment_ref": "test", "payment_ref": invoice_line.name,
} }
) )
self._check_statement_matching( self._check_statement_matching(
@ -1127,6 +1155,7 @@ class TestReconciliationMatchingRules(AccountTestInvoicingCommon):
"match_same_currency": False, "match_same_currency": False,
"company_id": self.company_data["company"].id, "company_id": self.company_data["company"].id,
"past_months_limit": False, "past_months_limit": False,
"match_text_location_label": False,
} }
) )
@ -1303,6 +1332,7 @@ class TestReconciliationMatchingRules(AccountTestInvoicingCommon):
200 should be proposed. 200 should be proposed.
""" """
self.rule_1.allow_payment_tolerance = False self.rule_1.allow_payment_tolerance = False
self.rule_1.match_text_location_label = False
self.bank_line_2.amount = 250 self.bank_line_2.amount = 250
self.bank_line_1.partner_id = None self.bank_line_1.partner_id = None
@ -1325,6 +1355,7 @@ class TestReconciliationMatchingRules(AccountTestInvoicingCommon):
other ones are disregarded. other ones are disregarded.
""" """
self.rule_1.allow_payment_tolerance = False self.rule_1.allow_payment_tolerance = False
self.rule_1.match_text_location_label = False
self.bank_line_2.amount = 300 self.bank_line_2.amount = 300
self.bank_line_1.partner_id = None self.bank_line_1.partner_id = None