[FIX] base: keep company currency active

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 <qdp@odoo.com>
Signed-off-by: de Wouters de Bouchout Jean-Benoît (jbw) <jbw@odoo.com>
This commit is contained in:
jbw
2023-03-16 07:29:49 +01:00
committed by jbw-odoo
parent 09c65eacd3
commit 17dda1ca7d
3 changed files with 19 additions and 1 deletions
@@ -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()
+6
View File
@@ -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 ""
+12
View File
@@ -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 {}