From 8278369af9aad211c79e08749698c0f53dde878a Mon Sep 17 00:00:00 2001 From: Anjeel Haria Date: Fri, 27 Sep 2024 18:03:31 +0530 Subject: [PATCH] [FIX] account_reconcile_oca: Allow counterpart entries for invoice matching reconciliation models Add test scripts --- .../models/account_bank_statement_line.py | 50 +++++++++++++++++-- .../tests/test_bank_account_reconcile.py | 43 ++++++++++++++++ 2 files changed, 89 insertions(+), 4 deletions(-) diff --git a/account_reconcile_oca/models/account_bank_statement_line.py b/account_reconcile_oca/models/account_bank_statement_line.py index 545bae82d8..87bda7b459 100644 --- a/account_reconcile_oca/models/account_bank_statement_line.py +++ b/account_reconcile_oca/models/account_bank_statement_line.py @@ -539,10 +539,30 @@ def _default_reconcile_data(self, from_unreconcile=False): ._apply_rules(self, self._retrieve_partner()) ) if res and res.get("status", "") == "write_off": + model = res["model"] + amount = self.amount_total_signed + if ( + model + and model.rule_type == "invoice_matching" + and model.allow_payment_tolerance + and not model.payment_tolerance_param == 0 + ): + for line in res.get("amls", []): + line_data = self._get_reconcile_line( + line, + "other", + is_counterpart=True, + ) + data.append(line_data) + else: + for line in res.get("amls", []): + line_data = self._get_reconcile_line( + line, "other", is_counterpart=True, max_amount=amount + ) + amount -= line_data.get("amount") + data.append(line_data) return self._recompute_suspense_line( - *self._reconcile_data_by_model( - data, res["model"], reconcile_auxiliary_id - ), + *self._reconcile_data_by_model(data, model, reconcile_auxiliary_id), self.manual_reference, ) elif res and res.get("amls"): @@ -763,9 +783,31 @@ def create(self, mvals): data += lines reconcile_auxiliary_id = 1 if res.get("status", "") == "write_off": + model = res["model"] + amount = self.amount + if ( + model + and model.rule_type == "invoice_matching" + and model.allow_payment_tolerance + and not model.payment_tolerance_param == 0 + ): + for line in res.get("amls", []): + line_data = record._get_reconcile_line( + line, + "other", + is_counterpart=True, + ) + data.append(line_data) + else: + for line in res.get("amls", []): + line_data = record._get_reconcile_line( + line, "other", is_counterpart=True, max_amount=amount + ) + amount -= line_data.get("amount") + data.append(line_data) data = record._recompute_suspense_line( *record._reconcile_data_by_model( - data, res["model"], reconcile_auxiliary_id + data, model, reconcile_auxiliary_id ), self.manual_reference, ) diff --git a/account_reconcile_oca/tests/test_bank_account_reconcile.py b/account_reconcile_oca/tests/test_bank_account_reconcile.py index d2b3290ce7..d151a6d734 100644 --- a/account_reconcile_oca/tests/test_bank_account_reconcile.py +++ b/account_reconcile_oca/tests/test_bank_account_reconcile.py @@ -1155,3 +1155,46 @@ def test_invoice_foreign_currency_change(self): self.assertFalse(f.add_account_move_line_id) self.assertTrue(f.can_reconcile) self.assertEqual(3, len(f.reconcile_data_info["data"])) + + def test_reconcile_model_payment_tolerance(self): + """ + We want to test what happens when we have a reconcile model with + payment tolerance enabled. + """ + inv_1 = self._create_invoice( + currency_id=self.currency_euro_id, invoice_amount=100, date_invoice=time.strftime("%Y-07-15"),auto_validate=True + ) + self.env["account.reconcile.model"].create( + { + "name": "payment_tolerance_allowed_reconcile_model", + "rule_type": "invoice_matching", + "allow_payment_tolerance": True, + "auto_reconcile": True, + "payment_tolerance_type": "percentage", + "payment_tolerance_param": 2.0, + "line_ids": [(0, 0, {"account_id": self.company.default_cash_difference_expense_account_id.id})], + } + ) + bank_stmt = self.acc_bank_stmt_model.create( + { + "company_id": self.env.ref("base.main_company").id, + "journal_id": self.bank_journal_euro.id, + "date": time.strftime("%Y-07-15"), + "name": "test", + } + ) + bank_stmt_line = self.acc_bank_stmt_line_model.create( + { + "name": "Demo payment tolerance", + "journal_id": self.bank_journal_euro.id, + "statement_id": bank_stmt.id, + "amount": 98, + "date": time.strftime("%Y-07-15"), + "partner_id" : inv_1.partner_id.id, + } + ) + self.assertIn( + self.company.default_cash_difference_expense_account_id, + bank_stmt_line.mapped("move_id.line_ids.account_id"), + ) + self.assertEqual(3, len(bank_stmt_line.reconcile_data_info["data"]))