From 17dda1ca7dec9848c4609899cf33c3e64e372ef4 Mon Sep 17 00:00:00 2001 From: jbw Date: Mon, 16 May 2022 11:32:53 +0000 Subject: [PATCH] [FIX] base: keep company currency active MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This commit aims at preventing the deactivation of a company currency. Was the issue on 2852452 support ticket (v14). But it seems appropriate to merge it in 13.0 as it is probably a good idea that a company currency always stays active. How to reproduce bug: In 13.0: Install accounting with demo data > activate multi currency in settings > deactivate usd > create new invoice > select eur currency > cannot set usd currency back on invoice Reconcile JS traceback in 14.0: Install accounting with demo data > activate multi currency in settings > deactivate usd > go to accounting dashboard > click on reconcile 7 items on Bank journal > click on any “customer/vendor matching” line. closes odoo/odoo#115444 Task: 2852452 X-original-commit: 09cb57446d99a48651cbfeeb5405029bc2d77226 Related: odoo/enterprise#38255 Signed-off-by: Quentin De Paoli Signed-off-by: de Wouters de Bouchout Jean-Benoît (jbw) --- .../tests/test_sale_product_matrix.py | 2 +- odoo/addons/base/i18n/base.pot | 6 ++++++ odoo/addons/base/models/res_currency.py | 12 ++++++++++++ 3 files changed, 19 insertions(+), 1 deletion(-) diff --git a/addons/test_sale_product_configurators/tests/test_sale_product_matrix.py b/addons/test_sale_product_configurators/tests/test_sale_product_matrix.py index 4ef1815b3cf..ad6777376e1 100644 --- a/addons/test_sale_product_configurators/tests/test_sale_product_matrix.py +++ b/addons/test_sale_product_configurators/tests/test_sale_product_matrix.py @@ -26,7 +26,7 @@ class TestSaleMatrixUi(TestMatrixCommon): cls.env['res.partner'].create({'name': 'Agrolait'}) # Setup currency - cls.env['res.currency'].search([('name', '!=', 'USD')]).action_archive() + cls.env['res.currency'].search([('name', '!=', 'USD')]).with_context(force_deactivate=True).action_archive() cls.currency = cls.env['res.currency'].search([('name', '=', 'USD')]) cls.currency.action_unarchive() diff --git a/odoo/addons/base/i18n/base.pot b/odoo/addons/base/i18n/base.pot index 3eb54b095bc..678eb7c2631 100644 --- a/odoo/addons/base/i18n/base.pot +++ b/odoo/addons/base/i18n/base.pot @@ -24385,3 +24385,9 @@ msgstr "" #: model:res.country,name:base.ax msgid "Åland Islands" msgstr "" + +#. module: base +#: code:addons/base/models/res_currency.py:0 +#, python-format +msgid "This currency is set on a company and therefore cannot be deactivated." +msgstr "" diff --git a/odoo/addons/base/models/res_currency.py b/odoo/addons/base/models/res_currency.py index d00fbe3abdf..82918af6e60 100644 --- a/odoo/addons/base/models/res_currency.py +++ b/odoo/addons/base/models/res_currency.py @@ -95,6 +95,18 @@ class Currency(models.Model): if group_user and group_mc: group_user.sudo()._remove_group(group_mc.sudo()) + @api.constrains('active') + def _check_company_currency_stays_active(self): + if self._context.get('install_mode') or self._context.get('force_deactivate'): + # install_mode : At install, when this check is run, the "active" field of a currency added to a company will + # still be evaluated as False, despite it's automatically set at True when added to the company. + # force_deactivate : Allows deactivation of a currency in tests to enable non multi_currency behaviors + return + + currencies = self.filtered(lambda c: not c.active) + if self.env['res.company'].search([('currency_id', 'in', currencies.ids)]): + raise UserError(_("This currency is set on a company and therefore cannot be deactivated.")) + def _get_rates(self, company, date): if not self.ids: return {}