investment/rate.py

91 lines
2.9 KiB
Python
Raw Normal View History

2022-11-13 21:57:40 +00:00
# -*- coding: utf-8 -*-
# This file is part of the investment-module from m-ds.de for Tryton.
2022-11-13 21:57:40 +00:00
# The COPYRIGHT file at the top level of this repository contains the
# full copyright notices and license terms.
from trytond.model import ModelView, ModelSQL, fields, Unique, Check, SymbolMixin
2022-11-13 21:57:40 +00:00
from trytond.transaction import Transaction
from trytond.pool import Pool
from trytond.pyson import Eval, Bool
class Rate(SymbolMixin, ModelSQL, ModelView):
2022-11-13 21:57:40 +00:00
'Rate'
__name__ = 'investment.rate'
asset = fields.Many2One(string='Asset', required=True,
select=True, ondelete='CASCADE',
model_name='investment.asset')
date = fields.Date(string='Date', required=True, select=True)
rate = fields.Numeric(string='Rate', required=True,
digits=(16, Eval('asset_digits', 4)),
depends=['asset_digits'])
asset_digits = fields.Function(fields.Integer(string='Digits',
2023-01-05 21:25:16 +00:00
readonly=True), 'get_rate_data')
2022-11-13 21:57:40 +00:00
currency = fields.Function(fields.Many2One(string='Currency',
readonly=True, model_name='currency.currency'),
2023-01-05 21:25:16 +00:00
'get_rate_data')
2022-11-13 21:57:40 +00:00
uom = fields.Function(fields.Many2One(string='Uom',
2023-01-05 21:25:16 +00:00
readonly=True, model_name='product.uom'), 'get_rate_data')
symbol = fields.Function(fields.Char(string='Symbol',
2023-01-05 21:25:16 +00:00
readonly=True), 'get_rate_data')
2022-11-13 21:57:40 +00:00
@classmethod
def __setup__(cls):
super(Rate, cls).__setup__()
t = cls.__table__()
cls._sql_constraints = [
('date_asset_uniq',
Unique(t, t.date, t.asset),
'investment.msg_unique_rate_date'),
('check_rate',
Check(t, t.rate >= 0),
'currency.msg_rate_positive'),
]
cls._order.insert(0, ('date', 'DESC'))
@classmethod
def default_date(cls):
""" today
"""
IrDate = Pool().get('ir.date')
return IrDate.today()
2023-01-05 21:25:16 +00:00
@classmethod
def get_rate_data(cls, rates, names):
""" speed up: get values for rate
"""
2023-01-05 21:25:16 +00:00
pool = Pool()
Asset = pool.get('investment.asset')
tab_asset = Asset.__table__()
tab_rate = cls.__table__()
cursor = Transaction().connection.cursor()
2023-01-05 21:25:16 +00:00
query = tab_asset.join(tab_rate,
condition=tab_asset.id==tab_rate.asset,
).select(
tab_rate.id,
tab_asset.uom,
tab_asset.currency,
tab_asset.currency_digits,
where=tab_rate.id.in_([x.id for x in rates]),
)
cursor.execute(*query)
records = cursor.fetchall()
2022-11-13 21:57:40 +00:00
2023-01-05 21:25:16 +00:00
result = {x:{y.id: None for y in rates} for x in names}
for record in records:
r1 = {
'symbol': '%',
'uom': record[1],
'currency': record[2],
'asset_digits': record[3],
}
2022-11-13 21:57:40 +00:00
2023-01-05 21:25:16 +00:00
for n in names:
result[n][record[0]] = r1[n]
return result
2022-11-13 21:57:40 +00:00
# Rate