Skip to content

Commit

Permalink
[IMP] account_reconcile_oca: Improve multicurrency management.
Browse files Browse the repository at this point in the history
We will use currency of the line in order to get the suspense and max line value
  • Loading branch information
etobella committed Sep 13, 2024
1 parent 6adc750 commit 7018d67
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 12 deletions.
45 changes: 38 additions & 7 deletions account_reconcile_oca/models/account_bank_statement_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,9 +192,17 @@ def _onchange_add_account_move_line_id(self):
new_data = []
is_new_line = True
pending_amount = 0.0
currency = self._get_reconcile_currency()
for line in data:
if line["kind"] != "suspense":
pending_amount += line["amount"]
pending_amount += currency._convert(
line["currency_amount"],
self.env["res.currency"].browse(
line.get("line_currency_id", currency.id)
),
self.company_id,
self.date,
)
if self.add_account_move_line_id.id in line.get(
"counterpart_line_ids", []
):
Expand All @@ -203,7 +211,10 @@ def _onchange_add_account_move_line_id(self):
new_data.append(line)
if is_new_line:
reconcile_auxiliary_id, lines = self._get_reconcile_line(
self.add_account_move_line_id, "other", True, pending_amount
self.add_account_move_line_id,
"other",
True,
max_amount=pending_amount,
)
new_data += lines
self.reconcile_data_info = self._recompute_suspense_line(
Expand Down Expand Up @@ -234,9 +245,12 @@ def _recompute_suspense_line(self, data, reconcile_auxiliary_id, manual_referenc
else:
suspense_line = line
if not float_is_zero(
total_amount, precision_digits=self.currency_id.decimal_places
total_amount, precision_digits=self.company_id.currency_id.decimal_places
):
can_reconcile = False
currency_amount = self.company_id.currency_id._convert(
total_amount, self._get_reconcile_currency(), self.company_id, self.date
)
if suspense_line:
suspense_line.update(
{
Expand All @@ -246,6 +260,7 @@ def _recompute_suspense_line(self, data, reconcile_auxiliary_id, manual_referenc
}
)
else:

suspense_line = {
"reference": "reconcile_auxiliary;%s" % reconcile_auxiliary_id,
"id": False,
Expand All @@ -260,8 +275,8 @@ def _recompute_suspense_line(self, data, reconcile_auxiliary_id, manual_referenc
"debit": -total_amount if total_amount < 0 else 0.0,
"kind": "suspense",
"currency_id": self.company_id.currency_id.id,
"line_currency_id": self.company_id.currency_id.id,
"currency_amount": -total_amount,
"line_currency_id": self.currency_id.id,
"currency_amount": -currency_amount,
}
reconcile_auxiliary_id += 1
new_data.append(suspense_line)
Expand Down Expand Up @@ -368,7 +383,7 @@ def _onchange_manual_amount_in_currency(self):
if self.manual_line_id.exists() and self.manual_line_id:
self.manual_amount = self.manual_in_currency_id._convert(
self.manual_amount_in_currency,
self.company_id.currency_id,
self._get_reconcile_currency(),
self.company_id,
self.manual_line_id.date,
)
Expand Down Expand Up @@ -405,6 +420,13 @@ def _onchange_manual_reconcile_vals(self):
if self.manual_amount > 0
else 0.0,
"analytic_distribution": self.analytic_distribution,
"currency_amount": self._get_reconcile_currency()._convert(
self.manual_amount,
self.manual_in_currency_id
or self._get_reconcile_currency(),
self.company_id,
line["date"],
),
"kind": line["kind"]
if line["kind"] != "suspense"
else "other",
Expand Down Expand Up @@ -717,12 +739,13 @@ def _unreconcile_bank_line_keep(self):
to_reverse._reverse_moves(default_values_list, cancel=True)

def _reconcile_move_line_vals(self, line, move_id=False):
return {
vals = {
"move_id": move_id or self.move_id.id,
"account_id": line["account_id"][0],
"partner_id": line.get("partner_id") and line["partner_id"][0],
"credit": line["credit"],
"debit": line["debit"],
"currency_id": line.get("line_currency_id", self.company_id.currency_id.id),
"tax_ids": line.get("tax_ids", []),
"tax_tag_ids": line.get("tax_tag_ids", []),
"group_tax_id": line.get("group_tax_id"),
Expand All @@ -731,6 +754,7 @@ def _reconcile_move_line_vals(self, line, move_id=False):
"name": line.get("name"),
"reconcile_model_id": line.get("reconcile_model_id"),
}
return vals

@api.model_create_multi
def create(self, mvals):
Expand Down Expand Up @@ -968,3 +992,10 @@ def add_statement(self):
"split_line_id": self.id,
}
return action

def _get_reconcile_currency(self):
return (
self.currency_id
or self.journal_id.currency_id
or self.company_id._currency_id
)
19 changes: 14 additions & 5 deletions account_reconcile_oca/models/account_reconcile_abstract.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ class AccountReconcileAbstract(models.AbstractModel):
related="company_id.currency_id", string="Company Currency"
)

def _get_reconcile_currency(self):
return self.currency_id or self.company_id._currency_id

def _get_reconcile_line(
self, line, kind, is_counterpart=False, max_amount=False, from_unreconcile=False
):
Expand All @@ -41,16 +44,22 @@ def _get_reconcile_line(
if is_counterpart:
currency_amount = -line.amount_residual_currency or line.amount_residual
amount = -line.amount_residual
currency = line.currency_id or self.company_id.currency_id
currency = line.currency_id or line.company_id.currency_id
original_amount = net_amount = -line.amount_residual
if max_amount:
currency_max_amount = self.company_id.currency_id._convert(
max_amount, currency, self.company_id, date
real_currency_amount = currency._convert(
currency_amount,
self._get_reconcile_currency(),
self.company_id,
date,
)
if (
-currency_amount > currency_max_amount > 0
or -currency_amount < currency_max_amount < 0
-real_currency_amount > max_amount > 0
or -real_currency_amount < max_amount < 0
):
currency_max_amount = self._get_reconcile_currency()._convert(
max_amount, currency, self.company_id, date
)
amount = currency_max_amount
net_amount = -max_amount
currency_amount = -amount
Expand Down

0 comments on commit 7018d67

Please sign in to comment.