asset: feld 'Datum' + 'Name' neu, rec_name optimiert, Test korrigiert
This commit is contained in:
parent
38b9847ccc
commit
1daa743631
6 changed files with 133 additions and 34 deletions
76
asset.py
76
asset.py
|
@ -7,6 +7,7 @@ from trytond.model import ModelView, ModelSQL, fields
|
|||
from trytond.transaction import Transaction
|
||||
from trytond.pool import Pool
|
||||
from trytond.pyson import Eval, Bool, And
|
||||
from trytond.report import Report
|
||||
from decimal import Decimal
|
||||
from datetime import time
|
||||
from sql.functions import CurrentTime
|
||||
|
@ -17,6 +18,8 @@ class Asset(ModelSQL, ModelView):
|
|||
'Asset'
|
||||
__name__ = 'investment.asset'
|
||||
|
||||
name = fields.Function(fields.Char(string='Name', readonly=True),
|
||||
'on_change_with_name')
|
||||
company = fields.Many2One(string='Company', model_name='company.company',
|
||||
required=True, ondelete="RESTRICT")
|
||||
product = fields.Many2One(string='Product', required=True,
|
||||
|
@ -38,7 +41,9 @@ class Asset(ModelSQL, ModelView):
|
|||
model_name='investment.rate')
|
||||
rate = fields.Function(fields.Numeric(string='Current Rate',
|
||||
readonly=True, digits=(16, Eval('currency_digits', 4)),
|
||||
depends=['currency_digits']), 'on_change_with_rate')
|
||||
depends=['currency_digits']), 'get_rate_data')
|
||||
date = fields.Function(fields.Date(string='Date', readonly=True,
|
||||
help='Date of current rate'), 'get_rate_data')
|
||||
|
||||
company_currency = fields.Function(fields.Many2One(readonly=True,
|
||||
string='Company Currency', states={'invisible': True},
|
||||
|
@ -96,6 +101,45 @@ class Asset(ModelSQL, ModelView):
|
|||
"""
|
||||
return 4
|
||||
|
||||
@classmethod
|
||||
def get_rate_data(cls, assets, names):
|
||||
""" get date and rate of asset
|
||||
"""
|
||||
pool = Pool()
|
||||
Asset = pool.get('investment.asset')
|
||||
Rate = pool.get('investment.rate')
|
||||
tab_asset = Asset.__table__()
|
||||
tab_rate = Rate.__table__()
|
||||
cursor = Transaction().connection.cursor()
|
||||
|
||||
query = tab_asset.join(tab_rate,
|
||||
condition=tab_asset.id==tab_rate.asset
|
||||
).select(
|
||||
tab_asset.id,
|
||||
tab_rate.rate,
|
||||
tab_rate.date,
|
||||
distinct_on=[tab_asset.id],
|
||||
order_by=[tab_asset.id, tab_rate.date.desc],
|
||||
where=tab_asset.id.in_([x.id for x in assets]),
|
||||
)
|
||||
cursor.execute(*query)
|
||||
records = cursor.fetchall()
|
||||
|
||||
result = {x:{y.id: None for y in assets} for x in names}
|
||||
|
||||
for record in records:
|
||||
(id1, rate1, date1) = record
|
||||
|
||||
asset = Asset(id1)
|
||||
exp = Decimal(Decimal(1) / 10 ** (asset.currency_digits or 4))
|
||||
|
||||
values = {'rate': record[1].quantize(exp), 'date': record[2]}
|
||||
|
||||
for name in names:
|
||||
result[name][record[0]] = values[name]
|
||||
|
||||
return result
|
||||
|
||||
@fields.depends('updtsource', 'updttime')
|
||||
def on_change_updtsource(self):
|
||||
""" clear time-fields
|
||||
|
@ -105,23 +149,6 @@ class Asset(ModelSQL, ModelView):
|
|||
else :
|
||||
self.updttime = time(11, 30)
|
||||
|
||||
@fields.depends('id', 'currency_digits')
|
||||
def on_change_with_rate(self, name=None):
|
||||
""" get current rate
|
||||
"""
|
||||
pool = Pool()
|
||||
Rate = pool.get('investment.rate')
|
||||
IrDate = pool.get('ir.date')
|
||||
|
||||
if self.id:
|
||||
rates = Rate.search([
|
||||
('date', '<=', IrDate.today()),
|
||||
('asset.id', '=', self.id),
|
||||
], order=[('date', 'DESC')], limit=1)
|
||||
if len(rates) > 0:
|
||||
exp = Decimal(Decimal(1) / 10 ** (self.currency_digits or 4))
|
||||
return rates[0].rate.quantize(exp)
|
||||
|
||||
@fields.depends('product', 'uom')
|
||||
def on_change_product(self):
|
||||
""" update unit by product
|
||||
|
@ -138,6 +165,13 @@ class Asset(ModelSQL, ModelView):
|
|||
if self.currency:
|
||||
self.currency_digits = self.currency.digits
|
||||
|
||||
@fields.depends('product')
|
||||
def on_change_with_name(self, name=None):
|
||||
""" get name of product
|
||||
"""
|
||||
if self.product:
|
||||
return self.product.name
|
||||
|
||||
@fields.depends('product')
|
||||
def on_change_with_product_uom(self, name=None):
|
||||
""" get category of product-uom
|
||||
|
@ -294,10 +328,14 @@ class Asset(ModelSQL, ModelView):
|
|||
def get_rec_name(self, name):
|
||||
""" record name
|
||||
"""
|
||||
return '%(prod)s [%(curr)s/%(unit)s]' % {
|
||||
return '%(prod)s - %(rate)s %(curr)s/%(unit)s [%(date)s]' % {
|
||||
'prod': getattr(self.product, 'rec_name', '-'),
|
||||
'curr': getattr(self.currency, 'rec_name', '-'),
|
||||
'unit': getattr(self.uom, 'rec_name', '-'),
|
||||
'rate': Report.format_number(self.rate, lang=None,
|
||||
digits=self.currency_digits or 4) \
|
||||
if self.rate is not None else '-',
|
||||
'date': Report.format_date(self.date) if self.date is not None else '-',
|
||||
}
|
||||
|
||||
@classmethod
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue