asset: aktueller kurs-anzeige, online-update begonnen

This commit is contained in:
Frederik Jaeckel 2022-11-14 23:03:54 +01:00
parent 42ede3decb
commit a936d85043
13 changed files with 317 additions and 5 deletions

View file

@ -7,10 +7,15 @@ from trytond.pool import Pool
from .asset import Asset
from .rate import Rate
from .identifier import Identifier
from .cron import Cron
from .onlinesource import OnlineSource
def register():
Pool.register(
OnlineSource,
Asset,
Rate,
Identifier,
Cron,
module='investment', type_='model')

114
asset.py
View file

@ -6,7 +6,9 @@
from trytond.model import ModelView, ModelSQL, fields
from trytond.transaction import Transaction
from trytond.pool import Pool
from trytond.pyson import Eval, Bool
from trytond.pyson import Eval, Bool, And
from decimal import Decimal
from datetime import time
class Asset(ModelSQL, ModelView):
@ -32,6 +34,9 @@ class Asset(ModelSQL, ModelView):
], depends=['product_uom', 'product'])
rates = fields.One2Many(string='Rates', field='asset',
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')
company_currency = fields.Function(fields.Many2One(readonly=True,
string='Company Currency', states={'invisible': True},
@ -56,6 +61,29 @@ class Asset(ModelSQL, ModelView):
help='Stock market symbol'),
'get_identifiers', searcher='search_identifier')
updtsource = fields.Many2One(string='Update Source',
help='Select a source for the course update.',
ondelete='SET NULL', model_name='investment.source')
updttime1 = fields.Time(string='Time 1',
states={
'readonly': ~Bool(Eval('updtsource')),
}, depends=['updtsource'])
updttime2 = fields.Time(string='Time 2',
states={
'readonly': ~And(
Bool(Eval('updtsource')),
Bool(Eval('updttime1')),
),
}, depends=['updtsource', 'updttime1'])
updttime3 = fields.Time(string='Time 3',
states={
'readonly': ~And(
Bool(Eval('updtsource')),
Bool(Eval('updttime1')),
Bool(Eval('updttime2')),
),
}, depends=['updtsource', 'updttime1', 'updttime2'])
@classmethod
def default_currency(cls):
""" currency of company
@ -78,6 +106,49 @@ class Asset(ModelSQL, ModelView):
"""
return 4
@fields.depends('updtsource', 'updttime1', 'updttime2', 'updttime3')
def on_change_updtsource(self):
""" clear time-fields
"""
if self.updtsource is None:
self.updttime1 = None
self.updttime2 = None
self.updttime3 = None
else :
self.updttime1 = time(11, 30)
@fields.depends('updttime1', 'updttime2', 'updttime3')
def on_change_updttime1(self):
""" clear fiels
"""
if self.updttime1 is None:
self.updttime2 = None
self.updttime3 = None
@fields.depends('updttime2', 'updttime3')
def on_change_updttime2(self):
""" clear fiels
"""
if self.updttime2 is None:
self.updttime3 = None
@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
@ -213,4 +284,45 @@ class Asset(ModelSQL, ModelView):
"""
return [('product.rec_name',) + tuple(clause[1:])]
@classmethod
def cron_update(cls):
""" update asset-rates
"""
pool = Pool()
Asset2 = pool.get('investment.asset')
IrDate = pool.get('ir.date')
assets = Asset2.search([
('updtsource', '!=', None),
(),
])
print('\n## assets:', assets)
@classmethod
def create(cls, vlist):
""" add debit/credit
"""
vlist = [x.copy() for x in vlist]
for values in vlist:
if 'updtsource' in values.keys():
if values['updtsource'] is None:
values['updttime1'] = None
values['updttime2'] = None
values['updttime3'] = None
return super(Asset, cls).create(vlist)
@classmethod
def write(cls, *args):
""" deny update if cashbook.line!='open',
add or update debit/credit
"""
actions = iter(args)
for lines, values in zip(actions, actions):
if 'updtsource' in values.keys():
if values['updtsource'] is None:
values['updttime1'] = None
values['updttime2'] = None
values['updttime3'] = None
super(Asset, cls).write(*args)
# end Asset

18
cron.py Normal file
View file

@ -0,0 +1,18 @@
# -*- 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.pool import PoolMeta
class Cron(metaclass=PoolMeta):
__name__ = 'ir.cron'
@classmethod
def __setup__(cls):
super(Cron, cls).__setup__()
cls.method.selection.append(
('investment.asset|cron_update', "Update Asset Rates"))
# end Cron

15
cron.xml Normal file
View file

@ -0,0 +1,15 @@
<?xml version="1.0"?>
<!-- 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. -->
<tryton>
<data>
<record model="ir.cron" id="asset_cron">
<field name="method">investment.asset|cron_update</field>
<field name="interval_number" eval="10"/>
<field name="interval_type">minutes</field>
</record>
</data>
</tryton>

View file

@ -15,6 +15,14 @@ msgid "Investment Administrator"
msgstr "Investition Administrator"
###########
# ir.cron #
###########
msgctxt "selection:ir.cron,method:"
msgid "Update Asset Rates"
msgstr "Vermögenswertkurse Aktualisieren"
##############
# ir.message #
##############
@ -66,6 +74,14 @@ msgctxt "view:investment.asset:"
msgid "Rates"
msgstr "Kurse"
msgctxt "view:investment.asset:"
msgid "Course Update"
msgstr "Kursaktualisierung"
msgctxt "view:investment.asset:"
msgid "You can have the course updated up to three times a day. The results are averaged."
msgstr "Sie können den Kurs bis zu dreimal täglich aktualisieren lassen. Die Ergebnisse werden gemittelt."
msgctxt "field:investment.asset,company:"
msgid "Company"
msgstr "Unternehmen"
@ -130,6 +146,30 @@ msgctxt "field:investment.asset,rates:"
msgid "Rates"
msgstr "Kurse"
msgctxt "field:investment.asset,rate:"
msgid "Current Rate"
msgstr "aktueller Kurs"
msgctxt "field:investment.asset,updtsource:"
msgid "Update Source"
msgstr "Kursquelle"
msgctxt "help:investment.asset,updtsource:"
msgid "Select a source for the course update."
msgstr "Wählen Sie eine Quelle für die Kursaktualisierung aus."
msgctxt "field:investment.asset,updttime1:"
msgid "Time 1"
msgstr "Zeitpunkt 1"
msgctxt "field:investment.asset,updttime2:"
msgid "Time 2"
msgstr "Zeitpunkt 2"
msgctxt "field:investment.asset,updttime3:"
msgid "Time 3"
msgstr "Zeitpunkt 3"
###################
# investment.rate #

View file

@ -10,6 +10,10 @@ msgctxt "model:res.group,name:group_investment_admin"
msgid "Investment Administrator"
msgstr "Investment Administrator"
msgctxt "selection:ir.cron,method:"
msgid "Update Asset Rates"
msgstr "Update Asset Rates"
msgctxt "model:ir.message,text:msg_currency_unique_rate_date"
msgid "A asset can only have one rate by date."
msgstr "A asset can only have one rate by date."
@ -42,6 +46,18 @@ msgctxt "view:investment.asset:"
msgid "Identifiers"
msgstr "Identifiers"
msgctxt "view:investment.asset:"
msgid "Rates"
msgstr "Rates"
msgctxt "view:investment.asset:"
msgid "Course Update"
msgstr "Course Update"
msgctxt "view:investment.asset:"
msgid "You can have the course updated up to three times a day. The results are averaged."
msgstr "You can have the course updated up to three times a day. The results are averaged."
msgctxt "field:investment.asset,company:"
msgid "Company"
msgstr "Company"
@ -102,10 +118,42 @@ msgctxt "help:investment.asset,secsymb:"
msgid "Stock market symbol"
msgstr "Stock market symbol"
msgctxt "field:investment.asset,rates:"
msgid "Rates"
msgstr "Rates"
msgctxt "field:investment.asset,rate:"
msgid "Current Rate"
msgstr "Current Rate"
msgctxt "field:investment.asset,updtsource:"
msgid "Update Source"
msgstr "Update Source"
msgctxt "help:investment.asset,updtsource:"
msgid "Select a source for the course update."
msgstr "Select a source for the course update."
msgctxt "field:investment.asset,updttime1:"
msgid "Time 1"
msgstr "Time 1"
msgctxt "field:investment.asset,updttime2:"
msgid "Time 2"
msgstr "Time 2"
msgctxt "field:investment.asset,updttime3:"
msgid "Time 3"
msgstr "Time 3"
msgctxt "model:investment.rate,name:"
msgid "Rate"
msgstr "Rate"
msgctxt "view:investment.rate:"
msgid "per"
msgstr "per"
msgctxt "field:investment.rate,asset:"
msgid "Asset"
msgstr "Asset"

View file

@ -1,5 +1,5 @@
<?xml version="1.0"?>
<!-- This file is part of the cashbook-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
full copyright notices and license terms. -->
<tryton>

17
onlinesource.py Normal file
View file

@ -0,0 +1,17 @@
# -*- 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
from trytond.transaction import Transaction
from trytond.pool import Pool
class OnlineSource(ModelSQL, ModelView):
'Online Source'
__name__ = 'investment.source'
name = fields.Char(string='Name')
# end OnlineSource

View file

@ -1,5 +1,5 @@
# -*- coding: utf-8 -*-
# This file is part of the cashbook-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
# full copyright notices and license terms.
@ -7,6 +7,7 @@ from trytond.tests.test_tryton import ModuleTestCase, with_transaction
from trytond.pool import Pool
from trytond.modules.company.tests import create_company
from decimal import Decimal
from datetime import time
class AssetTestCase(ModuleTestCase):
@ -79,6 +80,43 @@ class AssetTestCase(ModuleTestCase):
company=company,
product = product)
@with_transaction()
def test_asset_check_onlinesource_onoff(self):
""" create asset, switch online-source on/off
"""
pool = Pool()
OnlineSource = pool.get('investment.source')
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)
o_source, = OnlineSource.create([{
'name': 'Source 1',
}])
self.assertEqual(asset.updtsource, None)
self.assertEqual(asset.updttime1, None)
self.assertEqual(asset.updttime2, None)
self.assertEqual(asset.updttime3, None)
asset.updtsource = o_source
asset.updttime1 = time(10, 45)
asset.save()
self.assertEqual(asset.updtsource.rec_name, 'Source 1')
self.assertEqual(asset.updttime1, time(10, 45))
asset.updtsource = None
asset.on_change_updtsource()
self.assertEqual(asset.updtsource, None)
self.assertEqual(asset.updttime1, None)
@with_transaction()
def test_asset_indentifiers(self):
""" create asset, add identifiers

View file

@ -1,5 +1,5 @@
# -*- coding: utf-8 -*-
# This file is part of the cashbook-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
# full copyright notices and license terms.

View file

@ -1,6 +1,7 @@
[tryton]
version=6.0.0
depends:
ir
res
company
currency
@ -12,3 +13,4 @@ xml:
asset.xml
rate.xml
menu.xml
cron.xml

View file

@ -5,7 +5,8 @@ full copyright notices and license terms. -->
<form col="4">
<label name="product" />
<field name="product" />
<newline/>
<label name="rate" />
<field name="rate" symbol="currency"/>
<separator id="sepunits" colspan="4" string="Currency and Units"/>
<label name="currency" />
@ -31,6 +32,21 @@ full copyright notices and license terms. -->
<page name="rates" col="1" string="Rates">
<field name="rates" mode="tree,form,graph"/>
</page>
<page id="pgupdate" col="6" string="Course Update">
<label name="updtsource"/>
<field name="updtsource" colspan="3"/>
<newline/>
<label name="updttime1"/>
<field name="updttime1"/>
<label name="updttime2"/>
<field name="updttime2"/>
<label name="updttime3"/>
<field name="updttime3"/>
<label id="lab1" colspan="6" xalign="0.0"
string="You can have the course updated up to three times a day. The results are averaged."/>
</page>
</notebook>
</form>

View file

@ -7,6 +7,7 @@ full copyright notices and license terms. -->
<field name="isin"/>
<field name="secsymb"/>
<field name="wkn"/>
<field name="rate"/>
<field name="currency"/>
<field name="uom"/>
</tree>