asset: feld 'Datum' + 'Name' neu, rec_name optimiert, Test korrigiert

This commit is contained in:
Frederik Jaeckel 2022-11-23 10:44:09 +01:00
parent aeb949cc20
commit 4947b495c2
6 changed files with 133 additions and 34 deletions

View file

@ -7,6 +7,7 @@ from trytond.model import ModelView, ModelSQL, fields
from trytond.transaction import Transaction from trytond.transaction import Transaction
from trytond.pool import Pool from trytond.pool import Pool
from trytond.pyson import Eval, Bool, And from trytond.pyson import Eval, Bool, And
from trytond.report import Report
from decimal import Decimal from decimal import Decimal
from datetime import time from datetime import time
from sql.functions import CurrentTime from sql.functions import CurrentTime
@ -17,6 +18,8 @@ class Asset(ModelSQL, ModelView):
'Asset' 'Asset'
__name__ = 'investment.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', company = fields.Many2One(string='Company', model_name='company.company',
required=True, ondelete="RESTRICT") required=True, ondelete="RESTRICT")
product = fields.Many2One(string='Product', required=True, product = fields.Many2One(string='Product', required=True,
@ -38,7 +41,9 @@ class Asset(ModelSQL, ModelView):
model_name='investment.rate') model_name='investment.rate')
rate = fields.Function(fields.Numeric(string='Current Rate', rate = fields.Function(fields.Numeric(string='Current Rate',
readonly=True, digits=(16, Eval('currency_digits', 4)), 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, company_currency = fields.Function(fields.Many2One(readonly=True,
string='Company Currency', states={'invisible': True}, string='Company Currency', states={'invisible': True},
@ -96,6 +101,45 @@ class Asset(ModelSQL, ModelView):
""" """
return 4 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') @fields.depends('updtsource', 'updttime')
def on_change_updtsource(self): def on_change_updtsource(self):
""" clear time-fields """ clear time-fields
@ -105,23 +149,6 @@ class Asset(ModelSQL, ModelView):
else : else :
self.updttime = time(11, 30) 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') @fields.depends('product', 'uom')
def on_change_product(self): def on_change_product(self):
""" update unit by product """ update unit by product
@ -138,6 +165,13 @@ class Asset(ModelSQL, ModelView):
if self.currency: if self.currency:
self.currency_digits = self.currency.digits 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') @fields.depends('product')
def on_change_with_product_uom(self, name=None): def on_change_with_product_uom(self, name=None):
""" get category of product-uom """ get category of product-uom
@ -294,10 +328,14 @@ class Asset(ModelSQL, ModelView):
def get_rec_name(self, name): def get_rec_name(self, name):
""" record 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', '-'), 'prod': getattr(self.product, 'rec_name', '-'),
'curr': getattr(self.currency, 'rec_name', '-'), 'curr': getattr(self.currency, 'rec_name', '-'),
'unit': getattr(self.uom, '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 @classmethod

View file

@ -162,10 +162,22 @@ msgctxt "field:investment.asset,rates:"
msgid "Rates" msgid "Rates"
msgstr "Kurse" msgstr "Kurse"
msgctxt "field:investment.asset,name:"
msgid "Name"
msgstr "Name"
msgctxt "field:investment.asset,rate:" msgctxt "field:investment.asset,rate:"
msgid "Current Rate" msgid "Current Rate"
msgstr "aktueller Kurs" msgstr "aktueller Kurs"
msgctxt "field:investment.asset,date:"
msgid "Date"
msgstr "Datum"
msgctxt "help:investment.asset,date:"
msgid "Date of current rate"
msgstr "Datum des aktuellen Kurses"
msgctxt "field:investment.asset,updtsource:" msgctxt "field:investment.asset,updtsource:"
msgid "Update Source" msgid "Update Source"
msgstr "Kursquelle" msgstr "Kursquelle"

View file

@ -134,10 +134,22 @@ msgctxt "field:investment.asset,rates:"
msgid "Rates" msgid "Rates"
msgstr "Rates" msgstr "Rates"
msgctxt "field:investment.asset,name:"
msgid "Name"
msgstr "Name"
msgctxt "field:investment.asset,rate:" msgctxt "field:investment.asset,rate:"
msgid "Current Rate" msgid "Current Rate"
msgstr "Current Rate" msgstr "Current Rate"
msgctxt "field:investment.asset,date:"
msgid "Date"
msgstr "Date"
msgctxt "help:investment.asset,date:"
msgid "Date of current rate"
msgstr "Date of current rate"
msgctxt "field:investment.asset,updtsource:" msgctxt "field:investment.asset,updtsource:"
msgid "Update Source" msgid "Update Source"
msgstr "Update Source" msgstr "Update Source"

View file

@ -61,7 +61,7 @@ class AssetTestCase(ModuleTestCase):
'currency_digits': 4, 'currency_digits': 4,
'uom': product.default_uom.id, 'uom': product.default_uom.id,
}]) }])
self.assertEqual(asset.rec_name, '%s [usd/%s]' % ( self.assertEqual(asset.rec_name, '%s - - usd/%s [-]' % (
product.rec_name, product.rec_name,
asset.uom.rec_name, asset.uom.rec_name,
)) ))
@ -84,6 +84,33 @@ class AssetTestCase(ModuleTestCase):
company=company, company=company,
product = product) product = product)
@with_transaction()
def test_asset_rec_name(self):
""" create asset
"""
Asset = Pool().get('investment.asset')
company = self.prep_asset_company()
product = self.prep_asset_product(
name='Product 1',
description='some asset')
asset = self.prep_asset_item(
company=company,
product = product)
self.assertEqual(asset.rec_name, 'Product 1 - - usd/Unit [-]')
Asset.write(*[
[asset],
{
'rates': [('create', [{
'date': date(2022, 5, 15),
'rate': Decimal('2.45'),
}])],
}])
self.assertEqual(asset.rec_name, 'Product 1 - 2.4500 usd/Unit [05/15/2022]')
@with_transaction() @with_transaction()
def test_asset_check_onlinesource_onoff(self): def test_asset_check_onlinesource_onoff(self):
""" create asset, switch online-source on/off """ create asset, switch online-source on/off
@ -154,8 +181,10 @@ class AssetTestCase(ModuleTestCase):
'qdate': date(2022, 10, 15), 'qdate': date(2022, 10, 15),
'qtime': time(10, 30), 'qtime': time(10, 30),
}): }):
# re-read to make context work
asset2, = Asset.browse([asset.id])
# no rates exists - wait for 10:45 # no rates exists - wait for 10:45
self.assertEqual(asset.updtneeded, True) self.assertEqual(asset2.updtneeded, False)
self.assertEqual( self.assertEqual(
Asset.search_count([('updtneeded', '=', True)]), Asset.search_count([('updtneeded', '=', True)]),
0) 0)
@ -165,7 +194,8 @@ class AssetTestCase(ModuleTestCase):
'qtime': time(10, 46), 'qtime': time(10, 46),
}): }):
# no rates exists - run at 10:46 # no rates exists - run at 10:46
self.assertEqual(asset.updtneeded, True) asset2, = Asset.browse([asset.id])
self.assertEqual(asset2.updtneeded, True)
self.assertEqual( self.assertEqual(
Asset.search_count([('updtneeded', '=', True)]), Asset.search_count([('updtneeded', '=', True)]),
1) 1)
@ -186,7 +216,8 @@ class AssetTestCase(ModuleTestCase):
'qtime': time(10, 30), 'qtime': time(10, 30),
}): }):
# 1x rate exists - run at 10:30 # 1x rate exists - run at 10:30
self.assertEqual(asset.updtneeded, True) asset2, = Asset.browse([asset.id])
self.assertEqual(asset2.updtneeded, False)
self.assertEqual( self.assertEqual(
Asset.search_count([('updtneeded', '=', True)]), Asset.search_count([('updtneeded', '=', True)]),
0) 0)
@ -196,7 +227,8 @@ class AssetTestCase(ModuleTestCase):
'qtime': time(10, 46), 'qtime': time(10, 46),
}): }):
# 1x rate exists yesterday - run at 10:46 # 1x rate exists yesterday - run at 10:46
self.assertEqual(asset.updtneeded, True) asset2, = Asset.browse([asset.id])
self.assertEqual(asset2.updtneeded, True)
self.assertEqual( self.assertEqual(
Asset.search_count([('updtneeded', '=', True)]), Asset.search_count([('updtneeded', '=', True)]),
1) 1)
@ -217,7 +249,8 @@ class AssetTestCase(ModuleTestCase):
'qtime': time(10, 47), 'qtime': time(10, 47),
}): }):
# 1x rate exists today - run at 10:47 # 1x rate exists today - run at 10:47
self.assertEqual(asset.updtneeded, True) asset2, = Asset.browse([asset.id])
self.assertEqual(asset2.updtneeded, False)
self.assertEqual( self.assertEqual(
Asset.search_count([('updtneeded', '=', True)]), Asset.search_count([('updtneeded', '=', True)]),
0) 0)

View file

@ -2,24 +2,28 @@
<!-- This file is part of the investment-module from m-ds for Tryton. <!-- 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 The COPYRIGHT file at the top level of this repository contains the
full copyright notices and license terms. --> full copyright notices and license terms. -->
<form col="4"> <form col="6">
<label name="product" /> <label name="product" />
<field name="product" /> <field name="product" />
<label name="rate" /> <label name="rate" />
<field name="rate" symbol="currency"/> <field name="rate" symbol="currency"/>
<label name="date" />
<field name="date"/>
<separator id="sepunits" colspan="4" string="Currency and Units"/> <separator id="sepunits" colspan="6" string="Currency and Units"/>
<label name="currency" /> <label name="currency" />
<field name="currency" /> <field name="currency" />
<label name="currency_digits" /> <label name="currency_digits" />
<field name="currency_digits" /> <field name="currency_digits" />
<newline/>
<label name="uom" /> <label name="uom" />
<field name="uom" /> <field name="uom" />
<label name="product_uom" /> <label name="product_uom" />
<field name="product_uom" /> <field name="product_uom" />
<newline/>
<notebook> <notebook colspan="6">
<page id="pgids" col="4" string="Identifiers"> <page id="pgids" col="4" string="Identifiers">
<label name="wkn" /> <label name="wkn" />
<field name="wkn" /> <field name="wkn" />

View file

@ -3,11 +3,11 @@
The COPYRIGHT file at the top level of this repository contains the The COPYRIGHT file at the top level of this repository contains the
full copyright notices and license terms. --> full copyright notices and license terms. -->
<tree> <tree>
<field name="rec_name"/> <field name="name" expand="2"/>
<field name="isin"/> <field name="isin" expand="1"/>
<field name="secsymb"/> <field name="wkn" expand="1"/>
<field name="wkn"/> <field name="date" expand="1"/>
<field name="rate"/> <field name="rate" expand="1"/>
<field name="currency"/> <field name="currency"/>
<field name="uom"/> <field name="uom" />
</tree> </tree>