From 498d438f3da7c3ef37ca548efcf7442755c10f6b Mon Sep 17 00:00:00 2001 From: "Levi Siuzdak (sile)" Date: Wed, 23 Aug 2023 11:27:01 +0000 Subject: [PATCH] [FIX] account: keep invoice currency on journal change MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Versions: --------- - 16.0 - saas-16.1 - saas-16.2 - saas-16.3 - saas-16.4 Steps to reproduce: ------------------- 1. install the purchase and account_accountant modules 2. create a purchase order using a non-default currency; 3. create bill; 4. switch to a journal without set currency. Issue: ------ Currency of bill automatically changes from the one used by the order to the company default. Cause: ------ The `account.move` model only looks at bank statements, journal, and the company default when computing currency, ignoring any value that might already be present in the invoice: ``` def _compute_currency_id(self): for invoice in self: currency = ( invoice.statement_line_id.foreign_currency_id or invoice.journal_id.currency_id or invoice.journal_id.company_id.currency_id ) invoice.currency_id = currency ``` Solution: --------- Try to retrieve currency from `invoice.currency_id` before falling back on the company default. opw-3434518 closes odoo/odoo#133867 X-original-commit: 114f328575000fd31f117c46381441d7ec0bca05 Signed-off-by: Levi Siuzdak (sile) Signed-off-by: Levi Siuzdak Signed-off-by: William André (wan) --- addons/account/models/account_move.py | 1 + .../tests/test_account_move_in_invoice.py | 38 +++++++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/addons/account/models/account_move.py b/addons/account/models/account_move.py index 73e5728b710..1c9bf7bc945 100644 --- a/addons/account/models/account_move.py +++ b/addons/account/models/account_move.py @@ -892,6 +892,7 @@ class AccountMove(models.Model): currency = ( invoice.statement_line_id.foreign_currency_id or invoice.journal_id.currency_id + or invoice.currency_id or invoice.journal_id.company_id.currency_id ) invoice.currency_id = currency diff --git a/addons/account/tests/test_account_move_in_invoice.py b/addons/account/tests/test_account_move_in_invoice.py index e437e328960..7cc96e4f648 100644 --- a/addons/account/tests/test_account_move_in_invoice.py +++ b/addons/account/tests/test_account_move_in_invoice.py @@ -2330,3 +2330,41 @@ class TestAccountMoveInInvoiceOnchanges(AccountTestInvoicingCommon): payment_state=payment_state, ): self._assert_payment_move_state(move_type, amount, counterpart_values_list, payment_state) + + def test_onchange_journal_currency(self): + """ + Ensure invoice currency changes on journal change, iff the journal + has a currency_id set. + """ + + chf = self.env.ref('base.CHF') + eur = self.env.ref('base.EUR') + + journal_gen_exp = self.env['account.journal'].create({ + 'name': 'Expenses (generic)', + 'type': 'purchase', + 'code': 'EXPGEN', + }) + journal_swiss_exp = self.env['account.journal'].create({ + 'name': 'Expenses (CHF)', + 'type': 'purchase', + 'code': 'EXPCHF', + 'currency_id': chf.id, + }) + + self.invoice.currency_id = eur + move_form = Form(self.invoice) + invoice = move_form.save() + self.assertEqual(invoice.currency_id, eur) + + # No change expected with generic journal + move_form.journal_id = journal_gen_exp + move_form.save() + self.assertEqual(invoice.currency_id, eur, + "Changing to a journal without set currency shouldn't affect invoice currency") + + # Currency should change + move_form.journal_id = journal_swiss_exp + move_form.save() + self.assertEqual(invoice.currency_id, chf, + "Changing to a journal with a set currency should change invoice currency")