investment/rate.py

109 lines
3.4 KiB
Python
Raw Permalink 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.
2023-06-07 16:44:53 +00:00
from trytond.model import (
2023-06-07 19:52:09 +00:00
ModelView, ModelSQL, fields, Unique, Check, SymbolMixin, Index)
2022-11-13 21:57:40 +00:00
from trytond.transaction import Transaction
from trytond.pool import Pool
2023-06-07 16:44:53 +00:00
from trytond.pyson import Eval
2022-11-13 21:57:40 +00:00
class Rate(SymbolMixin, ModelSQL, ModelView):
2022-11-13 21:57:40 +00:00
'Rate'
__name__ = 'investment.rate'
2023-06-07 16:44:53 +00:00
asset = fields.Many2One(
2023-06-07 19:52:09 +00:00
string='Asset', required=True, ondelete='CASCADE',
2022-11-13 21:57:40 +00:00
model_name='investment.asset')
2023-06-07 19:52:09 +00:00
date = fields.Date(string='Date', required=True)
2023-06-07 16:44:53 +00:00
rate = fields.Numeric(
2023-06-07 19:52:09 +00:00
string='Rate', required=True,
2023-06-07 16:44:53 +00:00
digits=(16, Eval('asset_digits', 4)), depends=['asset_digits'])
2022-11-13 21:57:40 +00:00
2023-06-07 16:44:53 +00:00
asset_digits = fields.Function(fields.Integer(
string='Digits', readonly=True), 'get_rate_data')
currency = fields.Function(fields.Many2One(
string='Currency', readonly=True, model_name='currency.currency'),
2023-01-05 21:25:16 +00:00
'get_rate_data')
2023-06-07 16:44:53 +00:00
uom = fields.Function(fields.Many2One(
string='Uom', readonly=True, model_name='product.uom'),
'get_rate_data')
symbol = fields.Function(fields.Char(
string='Symbol', 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'))
2023-06-07 19:52:09 +00:00
cls._sql_indexes.update({
Index(
t,
(t.date, Index.Range(order='DESC'))),
Index(
t,
(t.rate, Index.Range())),
2023-06-23 14:02:31 +00:00
Index(
t,
(t.asset, Index.Equality())),
Index(
t,
(t.asset, Index.Equality()),
(t.date, Index.Range(order='DESC'))),
2023-06-07 19:52:09 +00:00
})
2022-11-13 21:57:40 +00:00
@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-06-07 16:44:53 +00:00
query = tab_asset.join(
tab_rate,
condition=tab_asset.id == tab_rate.asset,
2023-01-05 21:25:16 +00:00
).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-06-07 16:44:53 +00:00
result = {x: {y.id: None for y in rates} for x in names}
2023-01-05 21:25:16 +00:00
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