diff --git a/account_document_reversal/models/__init__.py b/account_document_reversal/models/__init__.py index 8cc5934d9..d118ca14e 100644 --- a/account_document_reversal/models/__init__.py +++ b/account_document_reversal/models/__init__.py @@ -1,5 +1,3 @@ -# Copyright 2019 Ecosoft Co., Ltd (http://ecosoft.co.th/) -# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html) from . import account from . import account_document_reversal from . import account_payment @@ -7,3 +5,4 @@ from . import account_bank_statement from . import account_move from . import sale from . import purchase +from . import reconciliation_widget diff --git a/account_document_reversal/models/account_bank_statement.py b/account_document_reversal/models/account_bank_statement.py index 7a21d2016..8464f2552 100644 --- a/account_document_reversal/models/account_bank_statement.py +++ b/account_document_reversal/models/account_bank_statement.py @@ -39,4 +39,5 @@ class AccountPayment(models.Model): for payment in payments_to_revert: payment.unreconcile() payment.action_document_reversal(date=date, journal_id=journal_id) + self.write({"move_name": False}) return True diff --git a/account_document_reversal/models/reconciliation_widget.py b/account_document_reversal/models/reconciliation_widget.py new file mode 100644 index 000000000..55e8b3d8d --- /dev/null +++ b/account_document_reversal/models/reconciliation_widget.py @@ -0,0 +1,61 @@ +# Copyright 2023 ForgeFlow, S.L. (https://www.forgeflow.com) +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html) +from odoo import _, api, models +from odoo.exceptions import ValidationError + + +class AccountReconciliation(models.AbstractModel): + _inherit = "account.reconciliation.widget" + + def _search_domain_payment_reconciliation_to_remove(self, domain): + start_index = domain.index(("statement_line_id", "=", False)) + end_index = domain.index(("balance", "!=", 0.0)) + 1 + return start_index, end_index + + @api.model + def _domain_move_lines_for_reconciliation( + self, + st_line, + aml_accounts, + partner_id, + excluded_ids=None, + search_str=False, + mode="rp", + ): + domain = super()._domain_move_lines_for_reconciliation( + st_line, + aml_accounts, + partner_id, + excluded_ids=excluded_ids, + search_str=search_str, + mode=mode, + ) + aml_accounts = [ + st_line.journal_id.default_credit_account_id.id, + st_line.journal_id.default_debit_account_id.id, + ] + try: + ( + start_index, + end_index, + ) = self._search_domain_payment_reconciliation_to_remove(domain) + except ValueError as e: + raise ValidationError( + _( + "Could not implement the restriction to remove " + "reversed payments: %s" + ) + % str(e) + ) + + domain_reconciliation_to_add = [ + "&", + ("statement_line_id", "=", False), + ("account_id", "in", aml_accounts), + ("payment_id", "<>", False), + ("balance", "!=", 0.0), + ("payment_id.state", "!=", "cancelled"), + ] + if start_index and end_index: + domain[start_index:end_index] = domain_reconciliation_to_add + return domain diff --git a/account_document_reversal/tests/test_payment_reversal.py b/account_document_reversal/tests/test_payment_reversal.py index c1328247d..d36dac9cd 100644 --- a/account_document_reversal/tests/test_payment_reversal.py +++ b/account_document_reversal/tests/test_payment_reversal.py @@ -20,6 +20,7 @@ class TestPaymentReversal(SavepointCase): cls.account_move_line_model = cls.env["account.move.line"] cls.invoice_line_model = cls.env["account.move.line"] cls.payment_model = cls.env["account.payment"] + cls.reconciliation_widget = cls.env["account.reconciliation.widget"] # Records cls.account_type_bank = cls.account_account_type_model.create( {"name": "Test Bank", "type": "liquidity", "internal_group": "asset"} @@ -306,6 +307,7 @@ class TestPaymentReversal(SavepointCase): statement line. I expect: - Reversal journal entry is created, and reconciled with original entry - The line in the statement is ready to reconcile again + - If I try to reconcile again this line, the original payment is not listed again """ # Test journal self.bank_journal.write({"cancel_method": "reversal"}) @@ -360,6 +362,25 @@ class TestPaymentReversal(SavepointCase): self.assertTrue(reversed_move_reconcile) self.assertEqual(move_reconcile, reversed_move_reconcile) self.assertFalse(bank_stmt_line.journal_entry_ids) + mv_lines_rec = self.env[ + "account.reconciliation.widget" + ].get_move_lines_for_bank_statement_line( + bank_stmt_line.id, + partner_id=False, + excluded_ids=[], + search_str=False, + mode="rp", + ) + mv_lines_ids = [l["id"] for l in mv_lines_rec] + bank_accounts = ( + self.bank_journal.default_credit_account_id + | self.bank_journal.default_debit_account_id + ) + bank_moves = original_move_lines.filtered( + lambda l: l.account_id in bank_accounts + ) + self.assertTrue(bank_moves) + self.assertNotIn(bank_moves[0].id, mv_lines_ids) def test_bank_statement_cancel_exception(self): """ Tests on exception case, if statement is already validated, but