[FIX] account_document_reversal: reversal from bank statement lines was not working correctly

It was probably incorrectly migrated
This commit is contained in:
Jordi Ballester Alomar
2023-07-10 17:01:10 +02:00
parent 3c023cefec
commit e41e858ede
3 changed files with 13 additions and 28 deletions

View File

@@ -24,35 +24,19 @@ class AccountPayment(models.Model):
""" Reverse all moves related to this statement + delete payment """ """ Reverse all moves related to this statement + delete payment """
# This part is from button_cancel_reconciliation() # This part is from button_cancel_reconciliation()
aml_to_unbind = self.env["account.move.line"] aml_to_unbind = self.env["account.move.line"]
aml_to_cancel = self.env["account.move.line"] payments_to_revert = self.env["account.payment"]
payment_to_unreconcile = self.env["account.payment"]
payment_to_cancel = self.env["account.payment"]
for st_line in self: for st_line in self:
aml_to_unbind |= st_line.journal_entry_ids aml_to_unbind |= st_line.journal_entry_ids
for line in st_line.journal_entry_ids: for line in st_line.journal_entry_ids:
payment_to_unreconcile |= line.payment_id
if ( if (
st_line.move_name st_line.move_name
and line.payment_id.payment_reference == st_line.move_name and line.payment_id.payment_reference == st_line.move_name
): ):
# there can be several moves linked to a statement line payments_to_revert |= line.payment_id
# but maximum one created by the line itself aml_to_unbind = aml_to_unbind
aml_to_cancel |= line
payment_to_cancel |= line.payment_id
aml_to_unbind = aml_to_unbind - aml_to_cancel
if aml_to_unbind: if aml_to_unbind:
aml_to_unbind.write({"statement_line_id": False}) aml_to_unbind.write({"statement_line_id": False})
for payment in payments_to_revert:
payment_to_unreconcile = payment_to_unreconcile - payment_to_cancel payment.unreconcile()
if payment_to_unreconcile: payment.action_document_reversal(date=date, journal_id=journal_id)
payment_to_unreconcile.unreconcile()
# --
# Find account moves to cancel reversal
moves = aml_to_cancel.mapped("move_id")
# Create reverse entries
moves._cancel_reversal(journal_id)
# Set cancel related payments
payment_to_cancel.write({"state": "cancelled"})
return True return True

View File

@@ -26,12 +26,11 @@ class AccountPayment(models.Model):
def action_document_reversal(self, date=None, journal_id=None): def action_document_reversal(self, date=None, journal_id=None):
""" Reverse all moves related to this payment + set state to cancel """ """ Reverse all moves related to this payment + set state to cancel """
# Check document readiness # Check document readiness
valid_state = ( for payment in self:
len(self.mapped("state")) == 1 if payment.state not in ["sent", "posted"]:
and list(set(self.mapped("state")))[0] == "posted" raise UserError(
) _("Only validated document can be cancelled (reversal)")
if not valid_state: )
raise UserError(_("Only validated document can be cancelled (reversal)"))
# Find moves to get reversed # Find moves to get reversed
move_lines = self.mapped("move_line_ids").filtered( move_lines = self.mapped("move_line_ids").filtered(
lambda x: x.journal_id == self.mapped("journal_id")[0] lambda x: x.journal_id == self.mapped("journal_id")[0]

View File

@@ -298,6 +298,7 @@ class TestPaymentReversal(SavepointCase):
self.assertTrue(move_reconcile) self.assertTrue(move_reconcile)
self.assertTrue(reversed_move_reconcile) self.assertTrue(reversed_move_reconcile)
self.assertEqual(move_reconcile, reversed_move_reconcile) self.assertEqual(move_reconcile, reversed_move_reconcile)
self.assertFalse(bank_stmt_line.journal_entry_ids)
def test_bank_statement_cancel_reversal_02(self): def test_bank_statement_cancel_reversal_02(self):
""" Tests that I can create a bank statement line and reconcile it """ Tests that I can create a bank statement line and reconcile it
@@ -358,6 +359,7 @@ class TestPaymentReversal(SavepointCase):
self.assertTrue(move_reconcile) self.assertTrue(move_reconcile)
self.assertTrue(reversed_move_reconcile) self.assertTrue(reversed_move_reconcile)
self.assertEqual(move_reconcile, reversed_move_reconcile) self.assertEqual(move_reconcile, reversed_move_reconcile)
self.assertFalse(bank_stmt_line.journal_entry_ids)
def test_bank_statement_cancel_exception(self): def test_bank_statement_cancel_exception(self):
""" Tests on exception case, if statement is already validated, but """ Tests on exception case, if statement is already validated, but