2014-10-24 10:44:38 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
2014-12-02 12:55:17 +01: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.
|
2014-10-24 10:44:38 +02:00
|
|
|
import datetime
|
|
|
|
|
from decimal import Decimal
|
|
|
|
|
|
|
|
|
|
from proteus import Model
|
|
|
|
|
|
|
|
|
|
__all__ = ['get_currency']
|
|
|
|
|
|
|
|
|
|
_names = {
|
|
|
|
|
'USD': 'U.S. Dollar',
|
|
|
|
|
'EUR': 'Euro',
|
|
|
|
|
}
|
|
|
|
|
_symbols = {
|
|
|
|
|
'USD': '$',
|
2018-06-26 19:11:47 +02:00
|
|
|
'EUR': '€',
|
2014-10-24 10:44:38 +02:00
|
|
|
}
|
|
|
|
|
_rates = {
|
|
|
|
|
'USD': Decimal('1.0'),
|
|
|
|
|
'EUR': Decimal('2.0'),
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_currency(code='USD', config=None):
|
|
|
|
|
"Get currency with code"
|
|
|
|
|
Currency = Model.get('currency.currency', config=config)
|
|
|
|
|
CurrencyRate = Model.get('currency.currency.rate', config=config)
|
|
|
|
|
|
|
|
|
|
currencies = Currency.find([('code', '=', code)])
|
|
|
|
|
if not currencies:
|
|
|
|
|
currency = Currency(name=_names.get(code, code),
|
2023-02-23 13:39:32 +01:00
|
|
|
symbol=_symbols.get(code), code=code,
|
2017-11-25 22:17:02 +01:00
|
|
|
rounding=Decimal('0.01'))
|
2014-10-24 10:44:38 +02:00
|
|
|
currency.save()
|
|
|
|
|
rate = _rates.get(code)
|
|
|
|
|
if rate is not None:
|
|
|
|
|
CurrencyRate(date=datetime.date.min, rate=rate,
|
|
|
|
|
currency=currency).save()
|
|
|
|
|
else:
|
|
|
|
|
currency, = currencies
|
|
|
|
|
return currency
|