Files
tryton/currency.py
T

252 lines
8.2 KiB
Python
Raw Normal View History

2008-10-08 12:18:56 +02:00
#This file is part of Tryton. The COPYRIGHT file at the top level
#of this repository contains the full copyright notices and license terms.
2010-08-10 11:58:58 -04:00
import datetime
from decimal import Decimal, ROUND_HALF_EVEN
2009-02-28 16:59:47 +01:00
from trytond.model import ModelView, ModelSQL, fields
2009-12-17 11:42:42 +01:00
from trytond.tools import safe_eval, datetime_strftime
2010-08-10 11:58:58 -04:00
from trytond.transaction import Transaction
2011-07-03 13:18:55 +02:00
from trytond.pool import Pool
2008-05-09 17:26:14 +02:00
2009-02-28 16:59:47 +01:00
class Currency(ModelSQL, ModelView):
2008-05-09 17:26:14 +02:00
'Currency'
_name = 'currency.currency'
_description = __doc__
2008-06-18 17:15:27 +02:00
name = fields.Char('Name', required=True, translate=True)
symbol = fields.Char('Symbol', size=10, required=True)
2008-05-09 17:26:14 +02:00
code = fields.Char('Code', size=3, required=True)
numeric_code = fields.Char('Numeric Code', size=3)
2010-03-11 16:47:49 +01:00
rate = fields.Function(fields.Numeric('Current rate', digits=(12, 6),
on_change_with=['rates']), 'get_rate')
2008-05-09 17:26:14 +02:00
rates = fields.One2Many('currency.currency.rate', 'currency', 'Rates')
rounding = fields.Numeric('Rounding factor', digits=(12, 6), required=True)
2009-06-26 17:40:21 +02:00
digits = fields.Integer('Display Digits')
2008-05-09 17:26:14 +02:00
active = fields.Boolean('Active')
2008-08-27 17:35:00 +02:00
# monetary formatting
2008-09-22 20:42:00 +02:00
mon_grouping = fields.Char('Grouping', required=True)
mon_decimal_point = fields.Char('Decimal Separator', required=True)
mon_thousands_sep = fields.Char('Thousands Separator')
p_sign_posn = fields.Integer('Positive Sign Position')
n_sign_posn = fields.Integer('Negative Sign Position')
positive_sign = fields.Char('Positive Sign')
negative_sign = fields.Char('Negative Sign')
2009-01-22 12:16:15 +01:00
p_cs_precedes = fields.Boolean('Positive Currency Symbol Precedes')
n_cs_precedes = fields.Boolean('Negative Currency Symbol Precedes')
p_sep_by_space = fields.Boolean('Positive Separate by Space')
n_sep_by_space = fields.Boolean('Negative Separate by Space')
2008-08-27 17:35:00 +02:00
2008-05-09 17:26:14 +02:00
def __init__(self):
super(Currency, self).__init__()
self._order.insert(0, ('code', 'ASC'))
2008-08-27 17:35:00 +02:00
self._constraints += [
('check_mon_grouping', 'invalid_mon_grouping'),
]
2008-07-17 19:36:42 +02:00
self._error_messages.update({
'no_rate': 'No rate found for the currency: %s at the date: %s',
2008-08-27 17:35:00 +02:00
'invalid_grouping': 'Invalid Grouping!',
2008-07-17 19:36:42 +02:00
})
2009-09-02 18:11:34 +02:00
self._rpc.update({
2009-09-03 12:19:11 +02:00
'compute': False,
2009-09-02 18:11:34 +02:00
})
2008-05-09 17:26:14 +02:00
2010-08-10 11:58:58 -04:00
def default_active(self):
2008-05-09 17:26:14 +02:00
return True
2010-08-10 11:58:58 -04:00
def default_rounding(self):
return Decimal('0.01')
2008-05-09 17:26:14 +02:00
2010-08-10 11:58:58 -04:00
def default_digits(self):
2008-05-09 17:26:14 +02:00
return 2
2010-08-10 11:58:58 -04:00
def default_mon_grouping(self):
2008-08-27 17:35:00 +02:00
return '[]'
2010-08-10 11:58:58 -04:00
def default_mon_thousands_sep(self):
2008-08-27 17:35:00 +02:00
return ','
2010-08-10 11:58:58 -04:00
def default_mon_decimal_point(self):
2008-08-27 17:35:00 +02:00
return '.'
2010-08-10 11:58:58 -04:00
def default_p_sign_posn(self):
2008-08-27 17:35:00 +02:00
return 1
2010-08-10 11:58:58 -04:00
def default_n_sign_posn(self):
2008-08-27 17:35:00 +02:00
return 1
2010-08-10 11:58:58 -04:00
def default_negative_sign(self):
2008-08-27 17:35:00 +02:00
return '-'
2010-08-10 11:58:58 -04:00
def default_positive_sign(self):
2008-08-27 17:35:00 +02:00
return ''
2010-08-10 11:58:58 -04:00
def default_p_cs_precedes(self):
2008-08-27 17:35:00 +02:00
return True
2010-08-10 11:58:58 -04:00
def default_n_cs_precedes(self):
2008-08-27 17:35:00 +02:00
return True
2010-08-10 11:58:58 -04:00
def default_p_sep_by_space(self):
2008-08-27 17:35:00 +02:00
return False
2010-08-10 11:58:58 -04:00
def default_n_sep_by_space(self):
2008-08-27 17:35:00 +02:00
return False
2010-08-10 11:58:58 -04:00
def check_mon_grouping(self, ids):
2008-08-27 17:35:00 +02:00
'''
Check if mon_grouping is list of numbers
'''
2010-08-10 11:58:58 -04:00
for currency in self.browse(ids):
2008-08-27 17:35:00 +02:00
try:
2009-09-28 11:23:07 +02:00
grouping = safe_eval(currency.mon_grouping)
2008-08-27 17:35:00 +02:00
for i in grouping:
if not isinstance(i, int):
return False
2010-07-08 19:56:00 +02:00
except Exception:
2008-08-27 17:35:00 +02:00
return False
return True
2010-08-10 11:58:58 -04:00
def check_xml_record(self, ids, values):
2009-04-02 20:00:11 +02:00
return True
2010-08-10 11:58:58 -04:00
def search_rec_name(self, name, clause):
ids = []
field = None
for field in ('code', 'numeric_code'):
2010-08-10 11:58:58 -04:00
ids = self.search([(field,) + clause[1:]], limit=1)
if ids:
break
if ids:
return [(field,) + clause[1:]]
return [(self._rec_name,) + clause[1:]]
2008-05-09 17:26:14 +02:00
2010-08-10 11:58:58 -04:00
def on_change_with_rate(self, vals):
now = datetime.date.today()
closer = datetime.date.min
res = Decimal('0.0')
for rate in vals.get('rates', []):
if 'date' not in rate or 'rate' not in rate:
continue
if rate['date'] <= now and rate['date'] > closer:
res = rate['rate']
closer = rate['date']
return res
2010-08-10 11:58:58 -04:00
def get_rate(self, ids, name):
2008-05-09 17:26:14 +02:00
'''
Return the rate at the date from the context or the current date
'''
2011-07-03 13:18:55 +02:00
rate_obj = Pool().get('currency.currency.rate')
date_obj = Pool().get('ir.date')
res = {}
2010-08-10 11:58:58 -04:00
date = Transaction().context.get('date', date_obj.today())
2008-05-09 17:26:14 +02:00
for currency_id in ids:
2010-08-10 11:58:58 -04:00
rate_ids = rate_obj.search([
2008-05-09 17:26:14 +02:00
('currency', '=', currency_id),
('date', '<=', date),
], limit=1, order=[('date', 'DESC')])
if rate_ids:
res[currency_id] = rate_ids[0]
else:
res[currency_id] = 0.0
rate_ids = [x for x in res.values() if x]
2010-08-10 11:58:58 -04:00
rates = rate_obj.browse(rate_ids)
2008-05-09 17:26:14 +02:00
id2rate = {}
for rate in rates:
id2rate[rate.id] = rate
for currency_id in res.keys():
if res[currency_id]:
res[currency_id] = id2rate[res[currency_id]].rate
return res
2010-08-10 11:58:58 -04:00
def round(self, currency, amount, rounding=ROUND_HALF_EVEN):
'''
Round the amount depending of the currency
:param cursor: the database cursor
:param user: the user id
:param currency: a BrowseRecord of currency.currency
:param amout: a Decimal
:param rounding: the rounding option
:return: a Decimal
'''
return (amount / currency.rounding).quantize(Decimal('1.'),
rounding=rounding) * currency.rounding
2008-05-09 17:26:14 +02:00
2010-08-10 11:58:58 -04:00
def is_zero(self, currency, amount):
2008-05-09 17:26:14 +02:00
'Return True if the amount can be considered as zero for the currency'
2010-08-10 11:58:58 -04:00
return abs(self.round(currency, amount)) < currency.rounding
2008-05-09 17:26:14 +02:00
2010-08-10 11:58:58 -04:00
def compute(self, from_currency, amount, to_currency, round=True):
2008-05-09 17:26:14 +02:00
'''
Take a currency and an amount
Return the amount to the new currency
Use the rate of the date of the context or the current date if ids are
given
2008-05-09 17:26:14 +02:00
'''
2011-07-03 13:18:55 +02:00
date_obj = Pool().get('ir.date')
lang_obj = Pool().get('ir.lang')
2008-05-09 17:26:14 +02:00
if isinstance(from_currency, (int, long)):
2010-08-10 11:58:58 -04:00
from_currency = self.browse(from_currency)
2008-05-09 17:26:14 +02:00
if isinstance(to_currency, (int, long)):
2010-08-10 11:58:58 -04:00
to_currency = self.browse(to_currency)
if to_currency == from_currency:
if round:
2010-08-10 11:58:58 -04:00
return self.round(to_currency, amount)
else:
return amount
2008-05-09 17:26:14 +02:00
if (not from_currency.rate) or (not to_currency.rate):
2010-08-10 11:58:58 -04:00
date = Transaction().context.get('date', date_obj.today())
2008-05-09 17:26:14 +02:00
if not from_currency.rate:
name = from_currency.name
else:
name = to_currency.name
2010-08-10 11:58:58 -04:00
for code in [Transaction().language, 'en_US']:
lang_ids = lang_obj.search([
('code', '=', code),
2010-08-10 11:58:58 -04:00
])
if lang_ids:
break
2010-08-10 11:58:58 -04:00
lang = lang_obj.browse(lang_ids[0])
2010-08-10 11:58:58 -04:00
self.raise_user_error('no_rate', (name,
datetime_strftime(date, str(lang.date))))
if round:
2010-08-10 11:58:58 -04:00
return self.round(to_currency,
amount * to_currency.rate / from_currency.rate)
2008-05-09 17:26:14 +02:00
else:
return amount * to_currency.rate / from_currency.rate
2008-05-09 17:26:14 +02:00
Currency()
2009-02-28 16:59:47 +01:00
class Rate(ModelSQL, ModelView):
2008-05-09 17:26:14 +02:00
'Rate'
_name = 'currency.currency.rate'
_description = __doc__
_rec_name = 'date'
date = fields.Date('Date', required=True, select=1)
rate = fields.Numeric('Rate', digits=(12, 6), required=1)
currency = fields.Many2One('currency.currency', 'Currency',
ondelete='CASCADE',)
2008-05-09 17:26:14 +02:00
def __init__(self):
super(Rate, self).__init__()
self._sql_constraints = [
('date_currency_uniq', 'UNIQUE(date, currency)',
'A currency can only have one rate by date!'),
]
self._order.insert(0, ('date', 'DESC'))
2010-08-10 11:58:58 -04:00
def default_date(self):
2011-07-03 13:18:55 +02:00
date_obj = Pool().get('ir.date')
2010-08-10 11:58:58 -04:00
return date_obj.today()
2008-05-09 17:26:14 +02:00
2010-08-10 11:58:58 -04:00
def check_xml_record(self, ids, values):
2008-08-25 11:52:20 +02:00
return True
2008-05-09 17:26:14 +02:00
Rate()