[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 """
# This part is from button_cancel_reconciliation()
aml_to_unbind = self.env["account.move.line"]
aml_to_cancel = self.env["account.move.line"]
payment_to_unreconcile = self.env["account.payment"]
payment_to_cancel = self.env["account.payment"]
payments_to_revert = self.env["account.payment"]
for st_line in self:
aml_to_unbind |= st_line.journal_entry_ids
for line in st_line.journal_entry_ids:
payment_to_unreconcile |= line.payment_id
if (
st_line.move_name
and line.payment_id.payment_reference == st_line.move_name
):
# there can be several moves linked to a statement line
# but maximum one created by the line itself
aml_to_cancel |= line
payment_to_cancel |= line.payment_id
aml_to_unbind = aml_to_unbind - aml_to_cancel
payments_to_revert |= line.payment_id
aml_to_unbind = aml_to_unbind
if aml_to_unbind:
aml_to_unbind.write({"statement_line_id": False})
payment_to_unreconcile = payment_to_unreconcile - payment_to_cancel
if payment_to_unreconcile:
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"})
for payment in payments_to_revert:
payment.unreconcile()
payment.action_document_reversal(date=date, journal_id=journal_id)
return True

View File

@@ -26,12 +26,11 @@ class AccountPayment(models.Model):
def action_document_reversal(self, date=None, journal_id=None):
""" Reverse all moves related to this payment + set state to cancel """
# Check document readiness
valid_state = (
len(self.mapped("state")) == 1
and list(set(self.mapped("state")))[0] == "posted"
for payment in self:
if payment.state not in ["sent", "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
move_lines = self.mapped("move_line_ids").filtered(
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(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):
""" 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(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):
""" Tests on exception case, if statement is already validated, but