Skip to content

Commit

Permalink
[FIX] Clean statement line reconcile data in case of inconsistencies …
Browse files Browse the repository at this point in the history
…in amounts with the accounting entries

It may happen in case of changes on statement line that trigger a synchronization with the accounting entries. (see #779 as an example but could happen just changing the amount on the statement line for instance)
  • Loading branch information
florian-dacosta committed Jan 20, 2025
1 parent 909977c commit 137b4f3
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 1 deletion.
41 changes: 40 additions & 1 deletion account_reconcile_oca/models/account_bank_statement_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from odoo import Command, _, api, fields, models
from odoo.exceptions import UserError
from odoo.fields import first
from odoo.tools import float_is_zero
from odoo.tools import float_compare, float_is_zero


class AccountBankStatementLine(models.Model):
Expand Down Expand Up @@ -1159,3 +1159,42 @@ def _get_reconcile_currency(self):
or self.journal_id.currency_id
or self.company_id.currency_id
)

def _check_reconcile_data_consistency(self):
for line in self:
data = self.reconcile_data_info.get("data", [])
liquidity_lines, _suspense_lines, _other_lines = self._seek_for_lines()
move_amount_cur = sum(liquidity_lines.mapped("amount_currency"))
move_credit = sum(liquidity_lines.mapped("credit"))
move_debit = sum(liquidity_lines.mapped("debit"))
stmt_amount_curr = stmt_debit = stmt_credit = 0.0
for line_data in data:
if line_data["kind"] != "liquidity":
continue
stmt_amount_curr += line_data["currency_amount"]
stmt_debit += line_data["debit"]
stmt_credit += line_data["credit"]
prec = line.currency_id.rounding
if (
float_compare(move_amount_cur, move_amount_cur, precision_rounding=prec)
!= 0
or float_compare(move_credit, stmt_credit, precision_rounding=prec) != 0
or float_compare(move_debit, stmt_debit, precision_rounding=prec) != 0
):
line.reconcile_data_info = self._default_reconcile_data()

def write(self, vals):
res = super().write(vals)
if any(
field_name in set(vals.keys())
for field_name in (
"payment_ref",
"amount",
"amount_currency",
"foreign_currency_id",
"currency_id",
"partner_id",
)
):
self._check_reconcile_data_consistency()
return res
37 changes: 37 additions & 0 deletions account_reconcile_oca/tests/test_bank_account_reconcile.py
Original file line number Diff line number Diff line change
Expand Up @@ -1267,3 +1267,40 @@ def test_invoice_foreign_currency_late_change_of_rate(self):
self.assertEqual(3, len(f.reconcile_data_info["data"]))
self.assertTrue(f.can_reconcile)
self.assertEqual(f.reconcile_data_info["data"][-1]["amount"], 3.63)

def test_statement_line_reconcile_data_amount_consistency(self):
self.env["res.currency.rate"].create(
{
"currency_id": self.env.ref("base.USD").id,
"name": time.strftime("%Y-07-14"),
"rate": 2,
}
)
bank_stmt_line = self.acc_bank_stmt_line_model.create(
{
"name": "testLine",
"journal_id": self.bank_journal_usd.id,
"amount": 100,
"date": time.strftime("%Y-07-15"),
}
)
# force to store the reconcile_data
bank_stmt_line.write(
{"reconcile_data_info": bank_stmt_line.reconcile_data_info}
)
self.assertEqual(bank_stmt_line.reconcile_data_info["data"][0]["debit"], 50.0)

# create a rate for the statement line date and then update the partner.
self.env["res.currency.rate"].create(
{
"currency_id": self.env.ref("base.USD").id,
"name": time.strftime("%Y-07-15"),
"rate": 1.5,
}
)
partner = self.env.ref("base.res_partner_2")
bank_stmt_line.write({"partner_id": partner.id})
self.assertEqual(bank_stmt_line.line_ids[0].partner_id, partner)
self.assertEqual(bank_stmt_line.line_ids[0].debit, 66.67)
self.assertEqual(bank_stmt_line.reconcile_data_info["data"][0]["debit"], 66.67)
# check the reconcile_data amount is still consistent with accounting entries

0 comments on commit 137b4f3

Please sign in to comment.