prozentwerte begonnen
This commit is contained in:
parent
5587bfea3a
commit
57cb06d60e
5 changed files with 255 additions and 4 deletions
129
asset.py
129
asset.py
|
@ -10,8 +10,9 @@ 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
|
||||
from sql.functions import CurrentTime, CurrentDate
|
||||
from sql.conditionals import Case
|
||||
from sql import Literal
|
||||
|
||||
|
||||
class Asset(ModelSQL, ModelView):
|
||||
|
@ -79,6 +80,28 @@ class Asset(ModelSQL, ModelView):
|
|||
readonly=True),
|
||||
'on_change_with_updtneeded', searcher='search_updtneeded')
|
||||
|
||||
# percentage change
|
||||
change_today = fields.Function(fields.Numeric(string='Previous Day',
|
||||
help='percentage change in value compared to the previous day',
|
||||
readonly=True, digits=(16,1)),
|
||||
'get_percentage_change')
|
||||
change_month = fields.Function(fields.Numeric(string='1 Month',
|
||||
help='percentage change in value compared to last month',
|
||||
readonly=True, digits=(16,1)),
|
||||
'get_percentage_change')
|
||||
change_3month = fields.Function(fields.Numeric(string='3 Months',
|
||||
help='percentage change in value during 3 months',
|
||||
readonly=True, digits=(16,1)),
|
||||
'get_percentage_change')
|
||||
change_6month = fields.Function(fields.Numeric(string='6 Months',
|
||||
help='percentage change in value during 6 months',
|
||||
readonly=True, digits=(16,1)),
|
||||
'get_percentage_change')
|
||||
change_12month = fields.Function(fields.Numeric(string='1 Year',
|
||||
help='percentage change in value during 1 year',
|
||||
readonly=True, digits=(16,1)),
|
||||
'get_percentage_change')
|
||||
|
||||
@classmethod
|
||||
def default_currency(cls):
|
||||
""" currency of company
|
||||
|
@ -101,6 +124,105 @@ class Asset(ModelSQL, ModelView):
|
|||
"""
|
||||
return 4
|
||||
|
||||
@classmethod
|
||||
def get_percentage_sql(cls, table_asset):
|
||||
""" get table for percentages and dates
|
||||
"""
|
||||
pool = Pool()
|
||||
Rate = pool.get('investment.rate')
|
||||
tab_rate_today = Rate.__table__()
|
||||
tab_rate_1day = Rate.__table__()
|
||||
tab_rate_1month = Rate.__table__()
|
||||
tab_rate_3month = Rate.__table__()
|
||||
context = Transaction().context
|
||||
|
||||
query_date = context.get('qdate', CurrentDate())
|
||||
query_today = table_asset.join(tab_rate_today,
|
||||
condition=table_asset.id==tab_rate_today.asset,
|
||||
).select(
|
||||
table_asset.id,
|
||||
tab_rate_today.date,
|
||||
tab_rate_today.rate,
|
||||
distinct_on=[table_asset.id],
|
||||
order_by=[table_asset.id, tab_rate_today.date.desc],
|
||||
where=tab_rate_today.date <= query_date,
|
||||
)
|
||||
|
||||
query = query_today.join(tab_rate_1day,
|
||||
# select newest date from yesterday until 3 days old
|
||||
condition=(query_today.id==tab_rate_1day.asset) & \
|
||||
(query_today.date > tab_rate_1day.date) & \
|
||||
(query_today.date < tab_rate_1day.date + Literal(3)),
|
||||
type_ = 'LEFT OUTER',
|
||||
).join(tab_rate_1month,
|
||||
# select newest date from 1 month ago until +3 days old
|
||||
condition=(query_today.id==tab_rate_1month.asset) & \
|
||||
(query_today.date > tab_rate_1month.date + Literal(30)) & \
|
||||
(query_today.date < tab_rate_1month.date + Literal(33)),
|
||||
type_ = 'LEFT OUTER',
|
||||
).select(
|
||||
query_today.id,
|
||||
query_today.date,
|
||||
Case(
|
||||
((tab_rate_1day.rate != None) & (query_today.rate != None) & \
|
||||
(tab_rate_1day.rate != Literal(0.0)),
|
||||
query_today.rate * Literal(100.0) / tab_rate_1day.rate - Literal(100.0)),
|
||||
else_ = None,
|
||||
).as_('day1'),
|
||||
Case(
|
||||
((tab_rate_1month.rate != None) & (query_today.rate != None) & \
|
||||
(tab_rate_1month.rate != Literal(0.0)),
|
||||
query_today.rate * Literal(100.0) / tab_rate_1month.rate - Literal(100.0)),
|
||||
else_ = None,
|
||||
).as_('month1'),
|
||||
distinct_on=[query_today.id],
|
||||
order_by=[
|
||||
query_today.id,
|
||||
tab_rate_1day.date.desc,
|
||||
tab_rate_1month.date.desc,
|
||||
],
|
||||
)
|
||||
|
||||
return query
|
||||
|
||||
@classmethod
|
||||
def get_percentage_change(cls, assets, names):
|
||||
""" get percentage per period
|
||||
"""
|
||||
pool = Pool()
|
||||
Asset = pool.get('investment.asset')
|
||||
tab_asset = Asset.__table__()
|
||||
cursor = Transaction().connection.cursor()
|
||||
|
||||
tab_percent = cls.get_percentage_sql(tab_asset)
|
||||
query = tab_percent.select(
|
||||
tab_percent.id,
|
||||
tab_percent.day1,
|
||||
tab_percent.month1,
|
||||
where=tab_percent.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:
|
||||
values = {
|
||||
'change_today': record[1].quantize(Decimal('0.1')) \
|
||||
if record[1] is not None else None,
|
||||
'change_month': record[2].quantize(Decimal('0.1')) \
|
||||
if record[2] is not None else None,
|
||||
'change_3month': None,
|
||||
'change_6month': None,
|
||||
'change_12month': None,
|
||||
}
|
||||
|
||||
for name in names:
|
||||
result[name][record[0]] = values[name]
|
||||
|
||||
print('-- result:', result)
|
||||
return result
|
||||
|
||||
|
||||
@classmethod
|
||||
def get_rate_data(cls, assets, names):
|
||||
""" get date and rate of asset
|
||||
|
@ -219,13 +341,12 @@ class Asset(ModelSQL, ModelView):
|
|||
pool = Pool()
|
||||
Asset2 = pool.get('investment.asset')
|
||||
Rate = pool.get('investment.rate')
|
||||
IrDate = pool.get('ir.date')
|
||||
tab_asset = Asset2.__table__()
|
||||
tab_rate = Rate.__table__()
|
||||
Operator = fields.SQL_OPERATORS[clause[1]]
|
||||
context = Transaction().context
|
||||
|
||||
query_date = context.get('qdate', IrDate.today())
|
||||
query_date = context.get('qdate', CurrentDate())
|
||||
query_time = context.get('qtime', CurrentTime())
|
||||
|
||||
query = tab_asset.join(tab_rate,
|
||||
|
@ -330,7 +451,7 @@ class Asset(ModelSQL, ModelView):
|
|||
"""
|
||||
return '%(prod)s - %(rate)s %(curr)s/%(unit)s [%(date)s]' % {
|
||||
'prod': getattr(self.product, 'rec_name', '-'),
|
||||
'curr': getattr(self.currency, 'rec_name', '-'),
|
||||
'curr': getattr(self.currency, 'symbol', '-'),
|
||||
'unit': getattr(self.uom, 'rec_name', '-'),
|
||||
'rate': Report.format_number(self.rate, lang=None,
|
||||
digits=self.currency_digits or 4) \
|
||||
|
|
40
locale/de.po
40
locale/de.po
|
@ -194,6 +194,46 @@ msgctxt "field:investment.asset,updtneeded:"
|
|||
msgid "Course update needed"
|
||||
msgstr "Kursaktualisierung nötig"
|
||||
|
||||
msgctxt "field:investment.asset,change_today:"
|
||||
msgid "Previous Day"
|
||||
msgstr "Vortag"
|
||||
|
||||
msgctxt "help:investment.asset,change_today:"
|
||||
msgid "percentage change in value compared to the previous day"
|
||||
msgstr "prozentuale Wertänderung zum Vortag"
|
||||
|
||||
msgctxt "field:investment.asset,change_month:"
|
||||
msgid "1 Month"
|
||||
msgstr "1 Monat"
|
||||
|
||||
msgctxt "help:investment.asset,change_month:"
|
||||
msgid "percentage change in value compared to last month"
|
||||
msgstr "prozentuale Wertänderung zum letzten Monat"
|
||||
|
||||
msgctxt "field:investment.asset,change_3month:"
|
||||
msgid "3 Months"
|
||||
msgstr "3 Monate"
|
||||
|
||||
msgctxt "help:investment.asset,change_3month:"
|
||||
msgid "percentage change in value during 3 months"
|
||||
msgstr "Prozentuale Wertänderung während 3 Monate"
|
||||
|
||||
msgctxt "field:investment.asset,change_6month:"
|
||||
msgid "6 Months"
|
||||
msgstr "6 Monate"
|
||||
|
||||
msgctxt "help:investment.asset,change_6month:"
|
||||
msgid "percentage change in value during 6 months"
|
||||
msgstr "Prozentuale Wertänderung während 6 Monate"
|
||||
|
||||
msgctxt "field:investment.asset,change_12month:"
|
||||
msgid "1 Year"
|
||||
msgstr "1 Jahr"
|
||||
|
||||
msgctxt "help:investment.asset,change_12month:"
|
||||
msgid "percentage change in value during 1 year"
|
||||
msgstr "Prozentuale Wertänderung während 1 Jahr"
|
||||
|
||||
|
||||
#####################
|
||||
# investment.source #
|
||||
|
|
40
locale/en.po
40
locale/en.po
|
@ -166,6 +166,46 @@ msgctxt "field:investment.asset,updtneeded:"
|
|||
msgid "Course update needed"
|
||||
msgstr "Course update needed"
|
||||
|
||||
msgctxt "field:investment.asset,change_today:"
|
||||
msgid "Previous Day"
|
||||
msgstr "Previous Day"
|
||||
|
||||
msgctxt "help:investment.asset,change_today:"
|
||||
msgid "percentage change in value compared to the previous day"
|
||||
msgstr "percentage change in value compared to the previous day"
|
||||
|
||||
msgctxt "field:investment.asset,change_month:"
|
||||
msgid "1 Month"
|
||||
msgstr "1 Month"
|
||||
|
||||
msgctxt "help:investment.asset,change_month:"
|
||||
msgid "percentage change in value compared to last month"
|
||||
msgstr "percentage change in value compared to last month"
|
||||
|
||||
msgctxt "field:investment.asset,change_3month:"
|
||||
msgid "3 Months"
|
||||
msgstr "3 Months"
|
||||
|
||||
msgctxt "help:investment.asset,change_3month:"
|
||||
msgid "percentage change in value during 3 months"
|
||||
msgstr "percentage change in value during 3 months"
|
||||
|
||||
msgctxt "field:investment.asset,change_6month:"
|
||||
msgid "6 Months"
|
||||
msgstr "6 Months"
|
||||
|
||||
msgctxt "help:investment.asset,change_6month:"
|
||||
msgid "percentage change in value during 6 months"
|
||||
msgstr "percentage change in value during 6 months"
|
||||
|
||||
msgctxt "field:investment.asset,change_12month:"
|
||||
msgid "1 Year"
|
||||
msgstr "1 Year"
|
||||
|
||||
msgctxt "help:investment.asset,change_12month:"
|
||||
msgid "percentage change in value during 1 year"
|
||||
msgstr "percentage change in value during 1 year"
|
||||
|
||||
msgctxt "model:investment.source,name:"
|
||||
msgid "Online Source"
|
||||
msgstr "Online Source"
|
||||
|
|
|
@ -111,6 +111,54 @@ class AssetTestCase(ModuleTestCase):
|
|||
}])
|
||||
self.assertEqual(asset.rec_name, 'Product 1 - 2.4500 usd/Unit [05/15/2022]')
|
||||
|
||||
@with_transaction()
|
||||
def test_asset_percentges(self):
|
||||
""" create asset, add rates, check percentages
|
||||
"""
|
||||
Asset = Pool().get('investment.asset')
|
||||
|
||||
company = self.prep_asset_company()
|
||||
product = self.prep_asset_product(
|
||||
name='Product 1',
|
||||
description='some asset')
|
||||
|
||||
asset1 = self.prep_asset_item(
|
||||
company=company,
|
||||
product = product)
|
||||
asset2 = self.prep_asset_item(
|
||||
company=company,
|
||||
product = product)
|
||||
|
||||
self.assertEqual(asset1.rec_name, 'Product 1 - - usd/Unit [-]')
|
||||
self.assertEqual(asset2.rec_name, 'Product 1 - - usd/Unit [-]')
|
||||
|
||||
Asset.write(*[
|
||||
[asset1],
|
||||
{
|
||||
'rates': [('create', [{
|
||||
'date': date(2022, 5, 15),
|
||||
'rate': Decimal('2.45'),
|
||||
}, {
|
||||
'date': date(2022, 5, 16),
|
||||
'rate': Decimal('2.6'),
|
||||
}])],
|
||||
},
|
||||
[asset2],
|
||||
{
|
||||
'rates': [('create', [{
|
||||
'date': date(2022, 5, 14),
|
||||
'rate': Decimal('5.75'),
|
||||
}, {
|
||||
'date': date(2022, 5, 15),
|
||||
'rate': Decimal('5.25'),
|
||||
}])],
|
||||
},
|
||||
])
|
||||
self.assertEqual(asset1.rec_name, 'Product 1 - 2.6000 usd/Unit [05/16/2022]')
|
||||
self.assertEqual(asset2.rec_name, 'Product 1 - 5.2500 usd/Unit [05/15/2022]')
|
||||
self.assertEqual(asset1.change_today, Decimal('6.1'))
|
||||
self.assertEqual(asset2.change_today, Decimal('-8.7'))
|
||||
|
||||
@with_transaction()
|
||||
def test_asset_check_onlinesource_onoff(self):
|
||||
""" create asset, switch online-source on/off
|
||||
|
|
|
@ -10,4 +10,6 @@ full copyright notices and license terms. -->
|
|||
<field name="rate" expand="1"/>
|
||||
<field name="currency"/>
|
||||
<field name="uom" />
|
||||
<field name="change_today" />
|
||||
|
||||
</tree>
|
||||
|
|
Loading…
Reference in a new issue