diff --git a/account_reconcile_oca/models/account_bank_statement_line.py b/account_reconcile_oca/models/account_bank_statement_line.py index be9ce1ca00..a807a183b4 100644 --- a/account_reconcile_oca/models/account_bank_statement_line.py +++ b/account_reconcile_oca/models/account_bank_statement_line.py @@ -233,6 +233,7 @@ def _recompute_suspense_line(self, data, reconcile_auxiliary_id, manual_referenc new_data = [] suspense_line = False counterparts = [] + suspense_currency = self.foreign_currency_id or self.currency_id for line in data: if line.get("counterpart_line_ids"): counterparts += line["counterpart_line_ids"] @@ -244,24 +245,25 @@ def _recompute_suspense_line(self, data, reconcile_auxiliary_id, manual_referenc if line["kind"] != "suspense": new_data.append(line) total_amount += line["amount"] - if line.get("currency_amount"): - currency_amount += ( - self.env["res.currency"] - .browse(line["line_currency_id"]) - ._convert( - line["currency_amount"], - self._get_reconcile_currency(), + if not line.get("is_exchange_counterpart"): + # case of statement line with foreign_currency + if ( + line["kind"] == "liquidity" + and line["line_currency_id"] != suspense_currency.id + ): + currency_amount += self.amount_currency + elif ( + line.get("currency_amount") + and line.get("line_currency_id") == suspense_currency.id + ): + currency_amount += line.get("currency_amount") + else: + currency_amount += self.company_id.currency_id._convert( + line["amount"], + suspense_currency, self.company_id, self.date, ) - ) - else: - currency_amount += self.company_id.currency_id._convert( - line["amount"], - self._get_reconcile_currency(), - self.company_id, - self.date, - ) else: suspense_line = line if not float_is_zero( @@ -274,6 +276,7 @@ def _recompute_suspense_line(self, data, reconcile_auxiliary_id, manual_referenc "amount": -total_amount, "credit": total_amount if total_amount > 0 else 0.0, "debit": -total_amount if total_amount < 0 else 0.0, + "currency_amount": -currency_amount, } ) else: @@ -292,7 +295,7 @@ 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.currency_id.id, + "line_currency_id": suspense_currency.id, "currency_amount": -currency_amount, } reconcile_auxiliary_id += 1 @@ -314,7 +317,9 @@ def _check_line_changed(self, line): or self.manual_account_id.id != line["account_id"][0] or self.manual_name != line["name"] or ( - self.manual_partner_id and self.manual_partner_id.name_get()[0] or False + self.manual_partner_id + and self.manual_partner_id.name_get()[0] + or [False, False] ) != line.get("partner_id") ) @@ -400,7 +405,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._get_reconcile_currency(), + self.company_id.currency_id, self.company_id, self.manual_line_id.date, ) @@ -437,13 +442,7 @@ 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"], - ), + "currency_amount": self.manual_amount_in_currency, "kind": line["kind"] if line["kind"] != "suspense" else "other", @@ -1007,26 +1006,38 @@ def _get_reconcile_line( ) rates = [] for vals in new_vals: + rate = False if vals["partner_id"] is False: vals["partner_id"] = (False, self.partner_name) - reconcile_auxiliary_id, rate = self._compute_exchange_rate( - vals, line, reconcile_auxiliary_id - ) + if vals.get("kind") not in ("suspense", "liquidity"): + reconcile_auxiliary_id, rate = self._compute_exchange_rate( + vals, line, reconcile_auxiliary_id + ) if rate: rates.append(rate) new_vals += rates return reconcile_auxiliary_id, new_vals def _get_exchange_rate_amount(self, amount, currency_amount, currency, line): - return ( - currency._convert( + if self.foreign_currency_id: + # take real rate of statement line to compute the exchange rate gain/loss + real_rate = self.amount / self.amount_currency + to_amount_journal_currency = currency_amount * real_rate + to_amount_company_currency = self.currency_id._convert( + to_amount_journal_currency, + self.company_id.currency_id, + self.company_id, + self.date, + ) + to_amount = self.company_id.currency_id.round(to_amount_company_currency) + else: + to_amount = currency._convert( currency_amount, self.company_id.currency_id, self.company_id, self.date, ) - - amount - ) + return to_amount - amount def _compute_exchange_rate( self,