12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091 |
- # -*- encoding: utf-8 -*-
- # Part of Odoo. See LICENSE file for full copyright and licensing details.
- from odoo import api, fields, models, tools, _
- import odoo.addons
- import logging
- import sys
- from odoo import api, SUPERUSER_ID
- _logger = logging.getLogger(__name__)
- def get_precision():
- def change_digit(cr, model_name, name):
- env = api.Environment(cr, SUPERUSER_ID, {})
- bill = model_name
- field = name
- xml_type = 'list'
- if 'form_edit' in env['ir.module.module']._installed():
- sql = """ select b.define_float from form_edit_modify_field_name a
- left join form_edit_modify_field_name_detail b
- on a.id = b.modify_field_name_id
- where a.table='{}' and a.user_id=2 and b.field_name='{}' and a.xml_type='{}'
- """.format(bill, field, xml_type)
- env.cr.execute(sql)
- res = env.cr.fetchone()
- else:
- res = False
- return res[0] if res else 2
- return change_digit
- class DecimalPrecision(models.Model):
- _name = 'decimal.precision'
- _description = 'Decimal Precision'
- name = fields.Char('Usage', required=True)
- digits = fields.Integer('Digits', required=True, default=2)
- _sql_constraints = [
- ('name_uniq', 'unique (name)', """Only one value can be defined for each given usage!"""),
- ]
- @api.model
- @tools.ormcache('application')
- def precision_get(self, application):
- self.flush_model(['name', 'digits'])
- self.env.cr.execute('select digits from decimal_precision where name=%s', (application,))
- res = self.env.cr.fetchone()
- return res[0] if res else 2
- @api.model_create_multi
- def create(self, vals_list):
- res = super(DecimalPrecision, self).create(vals_list)
- self.clear_caches()
- return res
- def write(self, data):
- res = super(DecimalPrecision, self).write(data)
- self.clear_caches()
- return res
- def unlink(self):
- res = super(DecimalPrecision, self).unlink()
- self.clear_caches()
- return res
- @api.onchange('digits')
- def _onchange_digits_warning(self):
- if self.digits < self._origin.digits:
- return {
- 'warning': {
- 'title': _("Warning for %s", self.name),
- 'message': _(
- "The precision has been reduced for %s.\n"
- "Note that existing data WON'T be updated by this change.\n\n"
- "As decimal precisions impact the whole system, this may cause critical issues.\n"
- "E.g. reducing the precision could disturb your financial balance.\n\n"
- "Therefore, changing decimal precisions in a running database is not recommended.",
- self.name,
- )
- }
- }
- # compatibility for decimal_precision.get_precision(): expose the module in addons namespace
- dp = sys.modules['odoo.addons.base.models.decimal_precision']
- odoo.addons.decimal_precision = dp
- sys.modules['odoo.addons.decimal_precision'] = dp
- sys.modules['openerp.addons.decimal_precision'] = dp
|