83 lines
2.6 KiB
Python
83 lines
2.6 KiB
Python
# -*- coding: utf-8 -*-
|
|
# This file is part of the investment-module from m-ds for Tryton.
|
|
# 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
|
|
from trytond.transaction import Transaction
|
|
from trytond.pool import Pool
|
|
from trytond.pyson import Eval, Bool
|
|
|
|
|
|
class Rate(SymbolMixin, ModelSQL, ModelView):
|
|
'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',
|
|
readonly=True), 'on_change_with_asset_digits')
|
|
currency = fields.Function(fields.Many2One(string='Currency',
|
|
readonly=True, model_name='currency.currency'),
|
|
'on_change_with_currency')
|
|
uom = fields.Function(fields.Many2One(string='Uom',
|
|
readonly=True, model_name='product.uom'),
|
|
'on_change_with_uom')
|
|
symbol = fields.Function(fields.Char(string='Symbol',
|
|
readonly=True), 'on_change_with_symbol')
|
|
|
|
@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()
|
|
|
|
def on_change_with_symbol(self, name=None):
|
|
""" symbol:%
|
|
"""
|
|
return '%'
|
|
|
|
@fields.depends('asset', '_parent_asset.uom')
|
|
def on_change_with_uom(self, name=None):
|
|
""" get unit of asset
|
|
"""
|
|
if self.asset:
|
|
return self.asset.uom.id
|
|
|
|
@fields.depends('asset', '_parent_asset.currency')
|
|
def on_change_with_currency(self, name=None):
|
|
""" get currency
|
|
"""
|
|
if self.asset:
|
|
return self.asset.currency.id
|
|
|
|
@fields.depends('asset', '_parent_asset.currency_digits')
|
|
def on_change_with_asset_digits(self, name=None):
|
|
""" get digits for asset
|
|
"""
|
|
if self.asset:
|
|
return self.asset.currency_digits
|
|
return 4
|
|
|
|
# Rate
|