decimal_precision.py 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. # -*- encoding: utf-8 -*-
  2. # Part of Odoo. See LICENSE file for full copyright and licensing details.
  3. from odoo import api, fields, models, tools, _
  4. import odoo.addons
  5. import logging
  6. import sys
  7. from odoo import api, SUPERUSER_ID
  8. _logger = logging.getLogger(__name__)
  9. def get_precision():
  10. def change_digit(cr, model_name, name):
  11. env = api.Environment(cr, SUPERUSER_ID, {})
  12. bill = model_name
  13. field = name
  14. xml_type = 'list'
  15. if 'form_edit' in env['ir.module.module']._installed():
  16. sql = """ select b.define_float from form_edit_modify_field_name a
  17. left join form_edit_modify_field_name_detail b
  18. on a.id = b.modify_field_name_id
  19. where a.table='{}' and a.user_id=2 and b.field_name='{}' and a.xml_type='{}'
  20. """.format(bill, field, xml_type)
  21. env.cr.execute(sql)
  22. res = env.cr.fetchone()
  23. else:
  24. res = False
  25. return res[0] if res else 2
  26. return change_digit
  27. class DecimalPrecision(models.Model):
  28. _name = 'decimal.precision'
  29. _description = 'Decimal Precision'
  30. name = fields.Char('Usage', required=True)
  31. digits = fields.Integer('Digits', required=True, default=2)
  32. _sql_constraints = [
  33. ('name_uniq', 'unique (name)', """Only one value can be defined for each given usage!"""),
  34. ]
  35. @api.model
  36. @tools.ormcache('application')
  37. def precision_get(self, application):
  38. self.flush_model(['name', 'digits'])
  39. self.env.cr.execute('select digits from decimal_precision where name=%s', (application,))
  40. res = self.env.cr.fetchone()
  41. return res[0] if res else 2
  42. @api.model_create_multi
  43. def create(self, vals_list):
  44. res = super(DecimalPrecision, self).create(vals_list)
  45. self.clear_caches()
  46. return res
  47. def write(self, data):
  48. res = super(DecimalPrecision, self).write(data)
  49. self.clear_caches()
  50. return res
  51. def unlink(self):
  52. res = super(DecimalPrecision, self).unlink()
  53. self.clear_caches()
  54. return res
  55. @api.onchange('digits')
  56. def _onchange_digits_warning(self):
  57. if self.digits < self._origin.digits:
  58. return {
  59. 'warning': {
  60. 'title': _("Warning for %s", self.name),
  61. 'message': _(
  62. "The precision has been reduced for %s.\n"
  63. "Note that existing data WON'T be updated by this change.\n\n"
  64. "As decimal precisions impact the whole system, this may cause critical issues.\n"
  65. "E.g. reducing the precision could disturb your financial balance.\n\n"
  66. "Therefore, changing decimal precisions in a running database is not recommended.",
  67. self.name,
  68. )
  69. }
  70. }
  71. # compatibility for decimal_precision.get_precision(): expose the module in addons namespace
  72. dp = sys.modules['odoo.addons.base.models.decimal_precision']
  73. odoo.addons.decimal_precision = dp
  74. sys.modules['odoo.addons.decimal_precision'] = dp
  75. sys.modules['openerp.addons.decimal_precision'] = dp