cashbook: yield-info prepared

This commit is contained in:
Frederik Jaeckel 2023-02-22 16:21:23 +01:00
parent 6177b5d691
commit 91de15e093
3 changed files with 163 additions and 1 deletions

99
book.py
View file

@ -117,6 +117,43 @@ class Book(SymbolMixin, metaclass=PoolMeta):
'invisible': Eval('feature', '') != 'asset',
}, depends=['currency_digits', 'feature']), 'get_asset_quantity')
# yield
yield_dividend_total = fields.Function(fields.Numeric(string='Dividend',
help='Total dividends received',
readonly=True, digits=(16, Eval('currency_digits', 2)),
depends=['currency_digits']),
'get_yield_data')
yield_dividend_12m = fields.Function(fields.Numeric(string='Dividend 1y',
help='Dividends received in the last twelve months',
readonly=True, digits=(16, Eval('currency_digits', 2)),
depends=['currency_digits']),
'get_yield_data')
yield_fee_total = fields.Function(fields.Numeric(string='Trade Fee',
help='Total trade fees payed',
readonly=True, digits=(16, Eval('currency_digits', 2)),
depends=['currency_digits']),
'get_yield_data')
yield_fee_12m = fields.Function(fields.Numeric(string='Trade Fee 1y',
help='Trade fees payed in the last twelve month',
readonly=True, digits=(16, Eval('currency_digits', 2)),
depends=['currency_digits']),
'get_yield_data')
yield_sales = fields.Function(fields.Numeric(string='Sales',
help='Total profit or loss on sale of shares.',
readonly=True, digits=(16, Eval('currency_digits', 2)),
depends=['currency_digits']),
'get_yield_data')
yield_sales_12m = fields.Function(fields.Numeric(string='Sales 1y',
help='Total profit or loss on sale of shares in the last twelve month.',
readonly=True, digits=(16, Eval('currency_digits', 2)),
depends=['currency_digits']),
'get_yield_data')
yield_balance = fields.Function(fields.Numeric(string='Total Yield',
help='Total income: price gain + dividends + sales gains - fees',
readonly=True, digits=(16, Eval('currency_digits', 2)),
depends=['currency_digits']),
'get_yield_data')
@classmethod
def view_attributes(cls):
return super(Book, cls).view_attributes() + [
@ -153,6 +190,68 @@ class Book(SymbolMixin, metaclass=PoolMeta):
"""
return 4
@classmethod
def get_yield_data_sql(cls, date_from=none, date_to=None):
""" collect yield data
"""
pool = Pool()
Line = pool.get('cashbook.line')
(tab_line1, tab_line_yield) = Line.get_yield_data_sql()
(tab_line2, tab_line_gainloss) = Line.get_gainloss_data_sql()
tab_book = cls.__table__()
tab_line = Line.__table__()
where = True
if date_from:
where &= tab_line.date >= date_from
if date_to:
where &= tab_line.date <= date_to
query = tab_book.join(tab_line,
condition=tab_line.cashbook==tab_book.id,
).join(tab_line_yield,
condition=tab_line_yield.id==tab_line.id,
).join(tab_line_gainloss,
condition=tab_line_gainloss.id==tab_line.id,
).select(
tab_book.id,
Sum(tab_line_yield.fee).as_('fee'),
Sum(tab_line_yield.dividend).as_('dividend'),
Sum(tab_line_gainloss.gainloss).as_('gainloss'),
having=where
)
return (tab_book, query)
@classmethod
def get_yield_data(cls, cashbooks, names):
""" collect yield data
"""
cursor = Transaction().connection.cursor()
(tab_book, query) = cls.get_yield_data_sql()
result = {x:{y.id: None for y in cashbooks} for x in names}
query.where = tab_book.id.in_([x.id for x in cashbooks])
cursor.execute(*query)
records = cursor.fetchall()
def quantize_val(value, line):
""" quantize...
"""
return (
value or Decimal('0.0')
).quantize(Decimal(str(1/10**line.currency_digits)))
for record in records:
line = Line2(record[0])
values = {
'trade_fee': quantize_val(record[1], line),
'asset_dividend': quantize_val(record[2], line),
}
for name in list(name_set):
result[name][record[0]] = values[name]
return result
@classmethod
def get_asset_quantity_sql(cls):
""" get table of asset and its value, rate, ...

View file

@ -203,6 +203,7 @@ class Line(SecondUomMixin, metaclass=PoolMeta):
tab_spmv_counterpart.credit - tab_spmv_counterpart.debit,
Decimal('0.0'),
) * Decimal('-1.0')).as_('gainloss'),
tab_line.cashbook,
)
return (tab_line, query)
@ -306,7 +307,8 @@ class Line(SecondUomMixin, metaclass=PoolMeta):
),
Decimal('0.0'),
)).as_('dividend'),
group_by=[tab_line.id],
tab_line.cashbook,
group_by=[tab_line.id, tab_line.cashbook],
)
return (tab_line, query)

View file

@ -138,6 +138,67 @@ msgctxt "help:cashbook.book,current_rate:"
msgid "Rate per unit of investment based on current stock exchange price."
msgstr "Kurs pro Einheit der Investition anhand des aktuellen Börsenkurses."
msgctxt "field:cashbook.book,yield_dividend_total:"
msgid "Dividend"
msgstr "Dividende"
msgctxt "help:cashbook.book,yield_dividend_total:"
msgid "Total dividends received"
msgstr "insgesamt erhaltene Dividenden"
msgctxt "field:cashbook.book,yield_dividend_12m:"
msgid "Dividend 1y"
msgstr "Dividende 1J"
msgctxt "help:cashbook.book,yield_dividend_12m:"
msgid "Dividends received in the last twelve months"
msgstr "in den letzten zwölf Monaten erhaltene Dividenden"
msgctxt "field:cashbook.book,yield_fee_total:"
msgid "Trade Fee"
msgstr "Handelsgebühr"
msgctxt "help:cashbook.book,yield_fee_total:"
msgid "Total trade fees payed"
msgstr "insgesamt bezahlte Handelsgebühr"
msgctxt "field:cashbook.book,yield_fee_12m:"
msgid "Trade Fee 1y"
msgstr "Handelsgebühr 1J"
msgctxt "help:cashbook.book,yield_fee_12m:"
msgid "Trade fees payed in the last twelve month"
msgstr "in den letzten zwölf Monaten bezahlte Handelsgebühr"
msgctxt "field:cashbook.book,yield_sales:"
msgid "Sales"
msgstr "Verkäufe"
msgctxt "help:cashbook.book,yield_sales:"
msgid "Total profit or loss on sale of shares."
msgstr "Gesamter Gewinn oder Verlust bei Verkauf von Anteilen."
msgctxt "field:cashbook.book,yield_sales_12m:"
msgid "Sales 1y"
msgstr "Verkäufe 1J"
msgctxt "help:cashbook.book,yield_sales_12m:"
msgid "Total profit or loss on sale of shares in the last twelve month."
msgstr "Gesamter Gewinn oder Verlust bei Verkauf von Anteilen in den letzten zwölf Monaten."
msgctxt "field:cashbook.book,yield_balance:"
msgid "Total Yield"
msgstr "Gesamtertrag"
msgctxt "help:cashbook.book,yield_balance:"
msgid "Total income: price gain + dividends + sales gains - fees"
msgstr "Gesamtertrag: Kursgewinn + Dividenden + Verkaufsgewinne - Gebühren"
# Total return - share price gain plus dividend minus fees
# Total return - price gain + sales gain + dividend - fees
# Rendite insgesamt - Kursgewinn + Verkaufserfolg + Dividende - Gebühren
##################
# cashbook.split #