summaryrefslogtreecommitdiff
path: root/addons/account/models/res_currency.py
diff options
context:
space:
mode:
authoroco-odoo <oco@odoo.com>2019-12-17 11:55:41 +0000
committerLaurent Smet <las@odoo.com>2020-02-05 11:53:45 +0000
commit31e1322b40ab33bf81d4c000f2b2a3f3eb8d0958 (patch)
tree1766dffcffe5e05d0f52a070bb21c28a3aaab120 /addons/account/models/res_currency.py
parent4da64b3f3c8dff88d8e0e01c65374590cc0bf4f7 (diff)
[IMP] account: don't always allow modifying the decimal places of a currency
If accounting entries have already been generated by this currency, we now forbid setting it a lesser number of decimal places (so, higher rounding factor). We also display a warning when increasing the number of decimal places, to make sure the user is aware of the impact it'll have. closes odoo/odoo#42017 Signed-off-by: Laurent Smet <smetl@users.noreply.github.com>
Diffstat (limited to 'addons/account/models/res_currency.py')
-rw-r--r--addons/account/models/res_currency.py37
1 files changed, 37 insertions, 0 deletions
diff --git a/addons/account/models/res_currency.py b/addons/account/models/res_currency.py
new file mode 100644
index 00000000000..12f401d82a0
--- /dev/null
+++ b/addons/account/models/res_currency.py
@@ -0,0 +1,37 @@
+# -*- coding: utf-8 -*-
+# Part of Odoo. See LICENSE file for full copyright and licensing details.
+
+from odoo import api, models, fields, _
+
+from odoo.exceptions import UserError
+
+
+class ResCurrency(models.Model):
+ _inherit = 'res.currency'
+
+ display_rounding_warning = fields.Boolean(string="Display Rounding Warning", compute='_compute_display_rounding_warning', help="Technical field used to tell whether or not to display the warning informing a rounding factor change might be dangerous on res.currency's form view.")
+
+
+ @api.depends('rounding')
+ def _compute_display_rounding_warning(self):
+ for record in self:
+ record.display_rounding_warning = record._origin.rounding != record.rounding \
+ and record._origin._has_accounting_entries()
+
+ def write(self, vals):
+ if 'rounding' in vals:
+ rounding_val = vals['rounding']
+ for record in self:
+ if (rounding_val > record.rounding or rounding_val == 0) and record._has_accounting_entries():
+ raise UserError(_("You cannot reduce the number of decimal places of a currency which has already been used to make accounting entries. If you really need to do that, please contact tech support."))
+
+ return super(ResCurrency, self).write(vals)
+
+ def _has_accounting_entries(self):
+ """ Returns True iff this currency has been used to generate (hence, round)
+ some move lines (either as their foreign currency, or as the main currency
+ of their company).
+ """
+ self.ensure_one()
+ return bool(self.env['account.move.line'].search_count(['|', ('currency_id', '=', self.id), ('company_currency_id', '=', self.id)]))
+