From 30f1d7ddbc69f30a452ba12dd34ddeb68177052e Mon Sep 17 00:00:00 2001 From: Florian da Costa Date: Mon, 30 Sep 2024 16:07:29 +0200 Subject: [PATCH 1/3] [FIX] account_reconcile_oca : Fix manual currency amount --- account_reconcile_oca/models/account_bank_statement_line.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/account_reconcile_oca/models/account_bank_statement_line.py b/account_reconcile_oca/models/account_bank_statement_line.py index a807a183b4..0b084ae25b 100644 --- a/account_reconcile_oca/models/account_bank_statement_line.py +++ b/account_reconcile_oca/models/account_bank_statement_line.py @@ -425,6 +425,10 @@ def _onchange_manual_reconcile_vals(self): for line in data: if line["reference"] == self.manual_reference: if self._check_line_changed(line): + if line["line_currency_id"] == self.company_id.currency_id.id: + currency_amount = self.manual_amount + else: + currency_amount = self.manual_amount_in_currency line.update( { "name": self.manual_name, @@ -442,7 +446,7 @@ def _onchange_manual_reconcile_vals(self): if self.manual_amount > 0 else 0.0, "analytic_distribution": self.analytic_distribution, - "currency_amount": self.manual_amount_in_currency, + "currency_amount": currency_amount, "kind": line["kind"] if line["kind"] != "suspense" else "other", From e0f27862ec907ec32192a99c425582a5283cf637 Mon Sep 17 00:00:00 2001 From: Florian da Costa Date: Mon, 30 Sep 2024 17:07:33 +0200 Subject: [PATCH 2/3] [FIX] account_reconcile_oca : multi currency management --- .../models/account_bank_statement_line.py | 29 ++++++++++++------- .../models/account_reconcile_abstract.py | 24 ++++++++++----- 2 files changed, 34 insertions(+), 19 deletions(-) diff --git a/account_reconcile_oca/models/account_bank_statement_line.py b/account_reconcile_oca/models/account_bank_statement_line.py index 0b084ae25b..4a7b3aba67 100644 --- a/account_reconcile_oca/models/account_bank_statement_line.py +++ b/account_reconcile_oca/models/account_bank_statement_line.py @@ -185,6 +185,18 @@ def _onchange_manual_model_id(self): )._default_reconcile_data() self.can_reconcile = self.reconcile_data_info.get("can_reconcile", False) + def _get_amount_currency(self, line, dest_curr): + if line["line_currency_id"] == dest_curr.id: + amount = line["currency_amount"] + else: + amount = self.company_id.currency_id._convert( + line["amount"], + dest_curr, + self.company_id, + self.date, + ) + return amount + @api.onchange("add_account_move_line_id") def _onchange_add_account_move_line_id(self): if self.add_account_move_line_id: @@ -192,16 +204,10 @@ 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 += currency._convert( - line["currency_amount"], - self.env["res.currency"].browse( - line.get("line_currency_id", currency.id) - ), - self.company_id, - self.date, + pending_amount += self._get_amount_currency( + line, self._get_reconcile_currency() ) if self.add_account_move_line_id.id in line.get( "counterpart_line_ids", [] @@ -582,6 +588,7 @@ def _default_reconcile_data(self, from_unreconcile=False): self.manual_reference, ) elif res and res.get("amls"): + # TODO should be signed in currency get_reconcile_currency amount = self.amount_total_signed for line in res.get("amls", []): reconcile_auxiliary_id, line_data = self._get_reconcile_line( @@ -885,7 +892,7 @@ def create(self, mvals): self.manual_reference, ) elif res.get("amls"): - amount = self.amount + amount = self.amount_currency or self.amount for line in res.get("amls", []): reconcile_auxiliary_id, line_datas = record._get_reconcile_line( line, "other", is_counterpart=True, max_amount=amount, move=True @@ -1110,7 +1117,7 @@ def add_statement(self): def _get_reconcile_currency(self): return ( - self.currency_id + self.foreign_currency_id or self.journal_id.currency_id - or self.company_id._currency_id + or self.company_id.currency_id ) diff --git a/account_reconcile_oca/models/account_reconcile_abstract.py b/account_reconcile_oca/models/account_reconcile_abstract.py index bd8c5025c7..3a77ff284b 100644 --- a/account_reconcile_oca/models/account_reconcile_abstract.py +++ b/account_reconcile_oca/models/account_reconcile_abstract.py @@ -47,18 +47,25 @@ def _get_reconcile_line( ): date = self.date if "date" in self._fields else line.date original_amount = amount = net_amount = line.debit - line.credit + line_currency = line.currency_id if is_counterpart: currency_amount = -line.amount_residual_currency or line.amount_residual amount = -line.amount_residual currency = line.currency_id or line.company_id.currency_id original_amount = net_amount = -line.amount_residual if max_amount: - real_currency_amount = currency._convert( - currency_amount, - self._get_reconcile_currency(), - self.company_id, - date, - ) + dest_currency = self._get_reconcile_currency() + if currency == dest_currency: + real_currency_amount = currency_amount + elif self.company_id.currency_id == dest_currency: + real_currency_amount = amount + else: + real_currency_amount = self.company_id.currency_id._convert( + amount, + dest_currency, + self.company_id, + date, + ) if ( -real_currency_amount > max_amount > 0 or -real_currency_amount < max_amount < 0 @@ -76,7 +83,8 @@ def _get_reconcile_line( date, ) else: - currency_amount = line.amount_currency + currency_amount = self.amount_currency or self.amount + line_currency = self._get_reconcile_currency() vals = { "move_id": move and line.move_id.id, "move": move and line.move_id.name, @@ -91,7 +99,7 @@ def _get_reconcile_line( "amount": amount, "net_amount": amount - net_amount, "currency_id": self.company_id.currency_id.id, - "line_currency_id": line.currency_id.id, + "line_currency_id": line_currency.id, "currency_amount": currency_amount, "analytic_distribution": line.analytic_distribution, "kind": kind, From 5695c2fd48b046ad584d611533c2aaa7c94178e5 Mon Sep 17 00:00:00 2001 From: Florian da Costa Date: Tue, 1 Oct 2024 12:11:57 +0200 Subject: [PATCH 3/3] [IMP] account_reconcile_oca : improve tests around multi-currency --- .../models/account_bank_statement_line.py | 2 +- .../tests/test_bank_account_reconcile.py | 122 +++++++++++------- 2 files changed, 76 insertions(+), 48 deletions(-) diff --git a/account_reconcile_oca/models/account_bank_statement_line.py b/account_reconcile_oca/models/account_bank_statement_line.py index 4a7b3aba67..97a3583929 100644 --- a/account_reconcile_oca/models/account_bank_statement_line.py +++ b/account_reconcile_oca/models/account_bank_statement_line.py @@ -1048,7 +1048,7 @@ def _get_exchange_rate_amount(self, amount, currency_amount, currency, line): self.company_id, self.date, ) - return to_amount - amount + return self.company_id.currency_id.round(to_amount - amount) def _compute_exchange_rate( self, diff --git a/account_reconcile_oca/tests/test_bank_account_reconcile.py b/account_reconcile_oca/tests/test_bank_account_reconcile.py index d2b3290ce7..76fd638d18 100644 --- a/account_reconcile_oca/tests/test_bank_account_reconcile.py +++ b/account_reconcile_oca/tests/test_bank_account_reconcile.py @@ -74,7 +74,6 @@ def test_reconcile_invoice_currency(self): inv1 = self.create_invoice(currency_id=self.currency_usd_id, invoice_amount=100) bank_stmt = self.acc_bank_stmt_model.create( { - "company_id": self.env.ref("base.main_company").id, "journal_id": self.bank_journal_euro.id, "date": time.strftime("%Y-07-15"), "name": "test", @@ -102,6 +101,42 @@ def test_reconcile_invoice_currency(self): self.assertFalse(f.add_account_move_line_id) self.assertTrue(f.can_reconcile) + def test_manual_line_with_currency(self): + bank_stmt = self.acc_bank_stmt_model.create( + { + "journal_id": self.bank_journal_euro.id, + "date": time.strftime("%Y-07-15"), + "name": "test", + } + ) + bank_stmt_line = self.acc_bank_stmt_line_model.create( + { + "name": "testLine", + "journal_id": self.bank_journal_euro.id, + "statement_id": bank_stmt.id, + "amount": 50, + "amount_currency": 100, + "foreign_currency_id": self.currency_usd_id, + "date": time.strftime("%Y-07-15"), + } + ) + receivable_acc = self.company_data["default_account_receivable"] + with Form( + bank_stmt_line, + view="account_reconcile_oca.bank_statement_line_form_reconcile_view", + ) as f: + self.assertFalse(f.can_reconcile) + f.manual_reference = "reconcile_auxiliary;1" + f.manual_account_id = receivable_acc + self.assertTrue(f.can_reconcile) + bank_stmt_line.reconcile_bank_line() + receivable_line = bank_stmt_line.line_ids.filtered( + lambda line: line.account_id == receivable_acc + ) + self.assertEqual(receivable_line.currency_id.id, self.currency_usd_id) + self.assertEqual(receivable_line.amount_currency, -100) + self.assertEqual(receivable_line.balance, -50) + def test_reconcile_invoice_reconcile_full(self): """ We want to test the reconcile widget for bank statements on invoices. @@ -113,7 +148,6 @@ def test_reconcile_invoice_reconcile_full(self): ) bank_stmt = self.acc_bank_stmt_model.create( { - "company_id": self.env.ref("base.main_company").id, "journal_id": self.bank_journal_euro.id, "date": time.strftime("%Y-07-15"), "name": "test", @@ -162,7 +196,6 @@ def test_reconcile_invoice_unreconcile(self): ) bank_stmt = self.acc_bank_stmt_model.create( { - "company_id": self.env.ref("base.main_company").id, "journal_id": self.bank_journal_euro.id, "date": time.strftime("%Y-07-15"), "name": "test", @@ -225,7 +258,6 @@ def test_reconcile_invoice_partial(self): ) bank_stmt = self.acc_bank_stmt_model.create( { - "company_id": self.env.ref("base.main_company").id, "journal_id": self.bank_journal_euro.id, "date": time.strftime("%Y-07-15"), "name": "test", @@ -289,7 +321,6 @@ def test_reconcile_invoice_partial_supplier(self): ) bank_stmt = self.acc_bank_stmt_model.create( { - "company_id": self.env.ref("base.main_company").id, "journal_id": self.bank_journal_euro.id, "date": time.strftime("%Y-07-15"), "name": "test", @@ -343,7 +374,6 @@ def test_reconcile_model(self): """ bank_stmt = self.acc_bank_stmt_model.create( { - "company_id": self.env.ref("base.main_company").id, "journal_id": self.bank_journal_euro.id, "date": time.strftime("%Y-07-15"), "name": "test", @@ -387,7 +417,6 @@ def test_reconcile_model_tax_included(self): ) bank_stmt = self.acc_bank_stmt_model.create( { - "company_id": self.env.ref("base.main_company").id, "journal_id": self.bank_journal_euro.id, "date": time.strftime("%Y-07-15"), "name": "test", @@ -442,7 +471,6 @@ def test_reconcile_invoice_model(self): ) bank_stmt = self.acc_bank_stmt_model.create( { - "company_id": self.env.ref("base.main_company").id, "journal_id": self.bank_journal_euro.id, "date": time.strftime("%Y-07-15"), "name": "test", @@ -502,7 +530,6 @@ def test_reconcile_rule_on_create(self): bank_stmt = self.acc_bank_stmt_model.create( { - "company_id": self.env.ref("base.main_company").id, "journal_id": self.bank_journal_euro.id, "date": time.strftime("%Y-07-15"), "name": "test", @@ -532,7 +559,6 @@ def test_reconcile_invoice_keep(self): ) bank_stmt = self.acc_bank_stmt_model.create( { - "company_id": self.env.ref("base.main_company").id, "journal_id": self.bank_journal_euro.id, "date": time.strftime("%Y-07-15"), "name": "test", @@ -590,7 +616,6 @@ def test_reconcile_invoice_to_check_reconciled(self): ) bank_stmt = self.acc_bank_stmt_model.create( { - "company_id": self.env.ref("base.main_company").id, "journal_id": self.bank_journal_euro.id, "date": time.strftime("%Y-07-15"), "name": "test", @@ -632,7 +657,6 @@ def test_reconcile_invoice_to_check_not_reconciled(self): """ bank_stmt = self.acc_bank_stmt_model.create( { - "company_id": self.env.ref("base.main_company").id, "journal_id": self.bank_journal_euro.id, "date": time.strftime("%Y-07-15"), "name": "test", @@ -667,7 +691,6 @@ def test_widget_invoice_clean(self): ) bank_stmt = self.acc_bank_stmt_model.create( { - "company_id": self.env.ref("base.main_company").id, "journal_id": self.bank_journal_euro.id, "date": time.strftime("%Y-07-15"), "name": "test", @@ -705,7 +728,6 @@ def test_widget_invoice_delete(self): ) bank_stmt = self.acc_bank_stmt_model.create( { - "company_id": self.env.ref("base.main_company").id, "journal_id": self.bank_journal_euro.id, "date": time.strftime("%Y-07-15"), "name": "test", @@ -746,7 +768,6 @@ def test_widget_invoice_unselect(self): ) bank_stmt = self.acc_bank_stmt_model.create( { - "company_id": self.env.ref("base.main_company").id, "journal_id": self.bank_journal_euro.id, "date": time.strftime("%Y-07-15"), "name": "test", @@ -787,7 +808,6 @@ def test_widget_invoice_change_partner(self): ) bank_stmt = self.acc_bank_stmt_model.create( { - "company_id": self.env.ref("base.main_company").id, "journal_id": self.bank_journal_euro.id, "date": time.strftime("%Y-07-15"), "name": "test", @@ -823,7 +843,6 @@ def test_widget_model_clean(self): """ bank_stmt = self.acc_bank_stmt_model.create( { - "company_id": self.env.ref("base.main_company").id, "journal_id": self.bank_journal_euro.id, "date": time.strftime("%Y-07-15"), "name": "test", @@ -874,7 +893,6 @@ def test_bank_statement_line_actions(self): """ bank_stmt = self.acc_bank_stmt_model.create( { - "company_id": self.env.ref("base.main_company").id, "journal_id": self.bank_journal_euro.id, "date": time.strftime("%Y-07-15"), "name": "test", @@ -917,7 +935,6 @@ def test_filter_partner(self): bank_stmt = self.acc_bank_stmt_model.create( { - "company_id": self.env.ref("base.main_company").id, "journal_id": self.bank_journal_euro.id, "date": time.strftime("%Y-07-15"), "name": "test", @@ -981,7 +998,6 @@ def test_partner_name_with_parent(self): bank_stmt = self.acc_bank_stmt_model.create( { - "company_id": self.env.ref("base.main_company").id, "journal_id": self.bank_journal_euro.id, "date": time.strftime("%Y-07-15"), "name": "test", @@ -1013,7 +1029,6 @@ def test_journal_foreign_currency(self): inv1 = self.create_invoice(currency_id=self.currency_usd_id, invoice_amount=100) bank_stmt = self.acc_bank_stmt_model.create( { - "company_id": self.env.ref("base.main_company").id, "journal_id": self.bank_journal_usd.id, "date": time.strftime("%Y-07-15"), "name": "test", @@ -1052,45 +1067,50 @@ def test_journal_foreign_currency(self): ) def test_journal_foreign_currency_change(self): + cny = self.env.ref("base.CNY") + cny.write({"active": True}) + cny_journal = self.env["account.journal"].create( + { + "name": "Bank CNY", + "type": "bank", + "currency_id": cny.id, + } + ) self.env["res.currency.rate"].create( { - "currency_id": self.env.ref("base.EUR").id, - "name": time.strftime("%Y-07-14"), - "rate": 1.15, + "name": time.strftime("%Y-09-10"), + "currency_id": cny.id, + "inverse_company_rate": 0.125989013758, + } + ) + self.env["res.currency.rate"].create( + { + "name": time.strftime("%Y-09-09"), + "currency_id": cny.id, + "inverse_company_rate": 0.126225969731, } ) bank_stmt = self.acc_bank_stmt_model.create( { - "company_id": self.env.ref("base.main_company").id, - "journal_id": self.bank_journal_usd.id, - "date": time.strftime("%Y-07-15"), + "journal_id": cny_journal.id, + "date": time.strftime("%Y-09-10"), "name": "test", } ) bank_stmt_line = self.acc_bank_stmt_line_model.create( { "name": "testLine", - "journal_id": self.bank_journal_usd.id, + "journal_id": cny_journal.id, "statement_id": bank_stmt.id, - "amount": 100, - "date": time.strftime("%Y-07-15"), + "amount": 259200, + "date": time.strftime("%Y-09-10"), } ) - with Form( - bank_stmt_line, - view="account_reconcile_oca.bank_statement_line_form_reconcile_view", - ) as f: - line = f.reconcile_data_info["data"][0] - self.assertEqual( - line["currency_amount"], - 100, - ) - self.env["res.currency.rate"].create( - { - "currency_id": self.env.ref("base.EUR").id, - "name": time.strftime("%Y-07-15"), - "rate": 1.2, - } + inv1 = self._create_invoice( + currency_id=cny.id, + invoice_amount=259200, + date_invoice=time.strftime("%Y-09-09"), + auto_validate=True, ) with Form( bank_stmt_line, @@ -1099,8 +1119,17 @@ def test_journal_foreign_currency_change(self): line = f.reconcile_data_info["data"][0] self.assertEqual( line["currency_amount"], - 100, + 259200, + ) + f.add_account_move_line_id = inv1.line_ids.filtered( + lambda l: l.account_id.account_type == "asset_receivable" ) + self.assertTrue(f.can_reconcile) + self.assertEqual(len(bank_stmt_line.reconcile_data_info["data"]), 3) + exchange_line = bank_stmt_line.reconcile_data_info["data"][-1] + self.assertEqual(exchange_line["amount"], 61.42) + bank_stmt_line.reconcile_bank_line() + self.assertEqual(inv1.payment_state, "paid") def test_invoice_foreign_currency_change(self): self.env["res.currency.rate"].create( @@ -1125,7 +1154,6 @@ def test_invoice_foreign_currency_change(self): ) bank_stmt = self.acc_bank_stmt_model.create( { - "company_id": self.env.ref("base.main_company").id, "journal_id": self.bank_journal_usd.id, "date": time.strftime("%Y-07-15"), "name": "test",