Add price accuracy

bzr revid: ced-5e19af1aec575f157b5636902b54eb7efcb1e67d
This commit is contained in:
ced 2006-12-28 09:44:56 +00:00
parent e5206f9329
commit 58ee4af23c
8 changed files with 108 additions and 6 deletions

View File

@ -1026,7 +1026,7 @@
<record model="res.currency" id="EUR">
<field name="name">EUR</field>
<field name="code">EUR</field>
<field name="rounding">2</field>
<field name="rounding">0.01</field>
<field name="accuracy">4</field>
</record>
<record model="res.currency.rate" id="rateEUR">

View File

@ -13,7 +13,7 @@
<record model="res.currency" id="USD">
<field name="name">USD</field>
<field name="rounding">2</field>
<field name="rounding">0.01</field>
<field name="accuracy">4</field>
</record>

View File

@ -56,7 +56,7 @@ class res_currency(osv.osv):
'rate': fields.function(_current_rate, method=True, string='Current rate',digits=(12,6)),
'rate_ids': fields.one2many('res.currency.rate', 'currency_id', 'Rates'),
'accuracy': fields.integer('Computational Accuracy'),
'rounding': fields.float('Rounding factor'),
'rounding': fields.float('Rounding factor', digits=(12,6)),
'active': fields.boolean('Active'),
}
_defaults = {
@ -65,7 +65,7 @@ class res_currency(osv.osv):
_order = "code"
def round(self, cr, uid, currency, amount):
return round(amount, currency.rouding)
return round(amount / currency.rounding) * currency.rounding
def compute(self, cr, uid, from_currency_id, to_currency_id, from_amount):
if to_currency_id==from_currency_id:
@ -74,7 +74,7 @@ class res_currency(osv.osv):
[to_currency] = self.read(cr, uid, [to_currency_id])
if from_currency['rate'] == 0 or to_currency['rate'] == 0:
raise osv.except_osv('Error', 'No rate found for the currency')
return currency(from_amount * from_currency['rate']/to_currency['rate'], to_currency['accuracy'], to_currency['rounding'])
return self.round(cr, uid, to_currency, from_amount * from_currency['rate']/to_currency['rate'])
res_currency()
class res_currency_rate(osv.osv):

View File

@ -58,6 +58,7 @@ class configmanager(object):
'secure': False,
'smtp_server': 'localhost',
'stop_after_init': False, # this will stop the server after initialization
'price_accuracy': 2,
}
parser = optparse.OptionParser(version=tinyerp_version_string)
@ -79,6 +80,7 @@ class configmanager(object):
parser.add_option('--debug', dest='debug_mode', action='store_true', default=False, help='enable debug mode')
parser.add_option("-S", "--secure", dest="secure", action="store_true", help="launch server over https instead of http", default=False)
parser.add_option('--smtp', dest='smtp_server', default='', help='specify the SMTP server for sending mail')
parser.add_option('--price_accuracy', dest='price_accuracy', default='2', help='specify the price accuracy')
group = optparse.OptionGroup(parser, "Modules related options")
group.add_option("-g", "--upgrade", action="store_true", dest="upgrade", default=False, help="Upgrade/install/uninstall modules")
@ -129,7 +131,7 @@ class configmanager(object):
self.options['pidfile'] = False
for arg in ('interface', 'port', 'db_name', 'db_user', 'db_password', 'db_host',
'db_port', 'logfile', 'pidfile', 'secure', 'smtp_server'):
'db_port', 'logfile', 'pidfile', 'secure', 'smtp_server', 'price_accuracy'):
if getattr(opt, arg):
self.options[arg] = getattr(opt, arg)

View File

@ -205,6 +205,7 @@ class UpdateableStr(local):
def __nonzero__(self):
return bool(self.string)
# Don't use ! Use res.currency.round()
class currency(float):
def __init__(self, value, accuracy=2, rounding=None):

View File

@ -0,0 +1,91 @@
##############################################################################
#
# Copyright (c) 2006 TINY SPRL. (http://tiny.be) All Rights Reserved.
#
# WARNING: This program as such is intended to be used by professional
# programmers who take the whole responsability of assessing all potential
# consequences resulting from its eventual inadequacies and bugs
# End users who are looking for a ready-to-use solution with commercial
# garantees and support are strongly adviced to contract a Free Software
# Service Company
#
# This program is Free Software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
##############################################################################
__author__ = 'Gaetan de Menten, <ged@tiny.be>'
__version__ = '0.1.0'
import psycopg
import optparse
import ConfigParser
# -----
parser = optparse.OptionParser(version="Tiny ERP server migration script " + __version__)
parser.add_option("-c", "--config", dest="config", help="specify path to Tiny ERP config file")
group = optparse.OptionGroup(parser, "Database related options")
group.add_option("--db_host", dest="db_host", help="specify the database host")
group.add_option("--db_port", dest="db_port", help="specify the database port")
group.add_option("-d", "--database", dest="db_name", help="specify the database name")
group.add_option("-r", "--db_user", dest="db_user", help="specify the database user name")
group.add_option("-w", "--db_password", dest="db_password", help="specify the database password")
parser.add_option_group(group)
options = optparse.Values()
options.db_name = 'terp' # default value
parser.parse_args(values=options)
if hasattr(options, 'config'):
configparser = ConfigParser.ConfigParser()
configparser.read([options.config])
for name, value in configparser.items('options'):
if not (hasattr(options, name) and getattr(options, name)):
if value in ('true', 'True'):
value = True
if value in ('false', 'False'):
value = False
setattr(options, name, value)
# -----
host = hasattr(options, 'db_host') and "host=%s" % options.db_host or ''
port = hasattr(options, 'db_port') and "port=%s" % options.db_port or ''
name = "dbname=%s" % options.db_name
user = hasattr(options, 'db_user') and "user=%s" % options.db_user or ''
password = hasattr(options, 'db_password') and "password=%s" % options.db_password or ''
db = psycopg.connect('%s %s %s %s %s' % (host, port, name, user, password), serialize=0)
cr = db.cursor()
# ------------------------ #
# change currency rounding #
# ------------------------ #
cr.execute("""SELECT c.relname,a.attname,a.attlen,a.atttypmod,a.attnotnull,a.atthasdef,t.typname,CASE WHEN a.attlen=-1 THEN a.atttypmod-4 ELSE a.attlen END as size FROM pg_class c,pg_attribute a,pg_type t WHERE c.relname='res_currency' AND a.attname='rounding' AND c.oid=a.attrelid AND a.atttypid=t.oid""")
res = cr.dictfetchall()
if res[0]['typname'] != 'float':
for line in (
"ALTER TABLE res_currency RENAME rounding TO rounding_bak",
"ALTER TABLE res_currency ADD rounding NUMERIC(12,6)",
"UPDATE res_currency SET rounding = power(10, - rounding_bak)",
"ALTER TABLE res_currency DROP rounding_bak",
):
cr.execute(line)
cr.commit()
cr.close

View File

@ -88,6 +88,10 @@ Specify the database host.
Specify the database port.
.br
(default None)
.TP
.IR price_accuracy
Specify the price accuracy.
.br
.SH AUTHORS

View File

@ -56,6 +56,10 @@ Initialize a module (use "all" for all modules).
.B \-u \fIMODULE\fB, \-\-update=\fIMODULE\fB
Update a module (use "all" for all modules).
.TP
.B \-\-price_accuracy=\fIPRICE_ACCURACY\fB
Specify the price accuracy.
.TP
.B \-v, \-\-verbose
Enable verbose mode.