2010-03-06 19:55:00 +00:00
|
|
|
# -*- encoding: utf-8 -*-
|
|
|
|
##############################################################################
|
|
|
|
#
|
|
|
|
# OpenERP, Open Source Management Solution
|
|
|
|
# Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>).
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU Affero General Public License as
|
|
|
|
# published by the Free Software Foundation, either version 3 of the
|
|
|
|
# License, or (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU Affero General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU Affero General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
#
|
|
|
|
##############################################################################
|
|
|
|
|
2013-03-27 11:46:58 +00:00
|
|
|
import openerp
|
2012-08-31 13:51:36 +00:00
|
|
|
from openerp import SUPERUSER_ID
|
2013-03-27 11:46:58 +00:00
|
|
|
from openerp import tools
|
2013-10-21 09:45:33 +00:00
|
|
|
from openerp.osv import orm, fields
|
2014-01-10 12:54:33 +00:00
|
|
|
from openerp.modules.registry import RegistryManager
|
2010-09-21 12:44:35 +00:00
|
|
|
|
2013-10-21 09:45:33 +00:00
|
|
|
class decimal_precision(orm.Model):
|
2010-03-06 19:55:00 +00:00
|
|
|
_name = 'decimal.precision'
|
|
|
|
_columns = {
|
2010-09-28 11:19:47 +00:00
|
|
|
'name': fields.char('Usage', size=50, select=True, required=True),
|
2010-03-06 19:55:00 +00:00
|
|
|
'digits': fields.integer('Digits', required=True),
|
|
|
|
}
|
|
|
|
_defaults = {
|
2010-10-29 11:13:28 +00:00
|
|
|
'digits': 2,
|
2010-03-06 19:55:00 +00:00
|
|
|
}
|
2010-09-21 12:44:35 +00:00
|
|
|
|
|
|
|
_sql_constraints = [
|
2010-09-22 16:52:47 +00:00
|
|
|
('name_uniq', 'unique (name)', """Only one value can be defined for each given usage!"""),
|
2010-09-21 12:44:35 +00:00
|
|
|
]
|
|
|
|
|
2011-08-31 18:16:38 +00:00
|
|
|
@tools.ormcache(skiparg=3)
|
2010-03-06 20:52:19 +00:00
|
|
|
def precision_get(self, cr, uid, application):
|
|
|
|
cr.execute('select digits from decimal_precision where name=%s', (application,))
|
|
|
|
res = cr.fetchone()
|
2011-04-27 10:07:03 +00:00
|
|
|
return res[0] if res else 2
|
2010-03-06 20:52:19 +00:00
|
|
|
|
2014-01-10 12:54:33 +00:00
|
|
|
def clear_cache(self, cr):
|
|
|
|
"""clear cache and update models. Notify other workers to restart their registry."""
|
|
|
|
self.precision_get.clear_cache(self)
|
|
|
|
for obj in self.pool.obj_list():
|
|
|
|
for colname, col in self.pool.get(obj)._columns.items():
|
2014-01-16 15:46:45 +00:00
|
|
|
if hasattr(col, 'digits_change'):
|
2014-01-10 12:54:33 +00:00
|
|
|
col.digits_change(cr)
|
2014-01-16 15:46:45 +00:00
|
|
|
RegistryManager.signal_caches_change(cr.dbname)
|
2014-01-10 12:54:33 +00:00
|
|
|
|
2012-05-23 08:52:34 +00:00
|
|
|
def create(self, cr, uid, data, context=None):
|
|
|
|
res = super(decimal_precision, self).create(cr, uid, data, context=context)
|
2014-01-10 12:54:33 +00:00
|
|
|
self.clear_cache(cr)
|
2012-05-23 08:52:34 +00:00
|
|
|
return res
|
|
|
|
|
|
|
|
def unlink(self, cr, uid, ids, context=None):
|
|
|
|
res = super(decimal_precision, self).unlink(cr, uid, ids, context=context)
|
2014-01-10 12:54:33 +00:00
|
|
|
self.clear_cache(cr)
|
2012-05-23 08:52:34 +00:00
|
|
|
return res
|
|
|
|
|
2010-03-06 19:55:00 +00:00
|
|
|
def write(self, cr, uid, ids, data, *args, **argv):
|
|
|
|
res = super(decimal_precision, self).write(cr, uid, ids, data, *args, **argv)
|
2014-01-10 12:54:33 +00:00
|
|
|
self.clear_cache(cr)
|
2010-03-06 19:55:00 +00:00
|
|
|
return res
|
2010-09-21 12:44:35 +00:00
|
|
|
|
2010-03-06 19:55:00 +00:00
|
|
|
|
|
|
|
def get_precision(application):
|
|
|
|
def change_digit(cr):
|
2013-03-27 11:46:58 +00:00
|
|
|
decimal_precision = openerp.registry(cr.dbname)['decimal.precision']
|
|
|
|
res = decimal_precision.precision_get(cr, SUPERUSER_ID, application)
|
2010-09-21 13:26:39 +00:00
|
|
|
return (16, res)
|
2010-03-06 19:55:00 +00:00
|
|
|
return change_digit
|
2010-09-21 12:44:35 +00:00
|
|
|
|
2013-10-21 09:45:33 +00:00
|
|
|
class DecimalPrecisionFloat(orm.AbstractModel):
|
|
|
|
""" Override qweb.field.float to add a `decimal_precision` domain option
|
|
|
|
and use that instead of the column's own value if it is specified
|
|
|
|
"""
|
|
|
|
_inherit = 'ir.qweb.field.float'
|
|
|
|
|
|
|
|
|
|
|
|
def precision(self, cr, uid, column, options=None, context=None):
|
|
|
|
dp = options and options.get('decimal_precision')
|
|
|
|
if dp:
|
|
|
|
return self.pool['decimal.precision'].precision_get(
|
|
|
|
cr, uid, dp)
|
|
|
|
|
|
|
|
return super(DecimalPrecisionFloat, self).precision(
|
|
|
|
cr, uid, column, options=options, context=context)
|
|
|
|
|
|
|
|
class DecimalPrecisionTestModel(orm.Model):
|
|
|
|
_name = 'decimal.precision.test'
|
|
|
|
|
|
|
|
_columns = {
|
|
|
|
'float': fields.float(),
|
|
|
|
'float_2': fields.float(digits=(16, 2)),
|
|
|
|
'float_4': fields.float(digits=(16, 4)),
|
|
|
|
}
|
|
|
|
|
2011-01-13 09:38:02 +00:00
|
|
|
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
|