book: added fields to book-form

This commit is contained in:
Frederik Jaeckel 2023-02-22 20:33:12 +01:00
parent 91de15e093
commit 20f465ae3a
4 changed files with 183 additions and 36 deletions

99
book.py
View file

@ -10,6 +10,8 @@ from trytond.modules.cashbook.book import STATES2, DEPENDS2
from trytond.transaction import Transaction from trytond.transaction import Transaction
from trytond.report import Report from trytond.report import Report
from decimal import Decimal from decimal import Decimal
from datetime import timedelta
from sql import Literal
from sql.functions import CurrentDate from sql.functions import CurrentDate
from sql.aggregate import Sum from sql.aggregate import Sum
from sql.conditionals import Case, Coalesce from sql.conditionals import Case, Coalesce
@ -121,38 +123,46 @@ class Book(SymbolMixin, metaclass=PoolMeta):
yield_dividend_total = fields.Function(fields.Numeric(string='Dividend', yield_dividend_total = fields.Function(fields.Numeric(string='Dividend',
help='Total dividends received', help='Total dividends received',
readonly=True, digits=(16, Eval('currency_digits', 2)), readonly=True, digits=(16, Eval('currency_digits', 2)),
depends=['currency_digits']), states={
'get_yield_data') 'invisible': Eval('feature', '') != 'asset',
}, depends=['currency_digits', 'feature']), 'get_yield_data')
yield_dividend_12m = fields.Function(fields.Numeric(string='Dividend 1y', yield_dividend_12m = fields.Function(fields.Numeric(string='Dividend 1y',
help='Dividends received in the last twelve months', help='Dividends received in the last twelve months',
readonly=True, digits=(16, Eval('currency_digits', 2)), readonly=True, digits=(16, Eval('currency_digits', 2)),
depends=['currency_digits']), states={
'get_yield_data') 'invisible': Eval('feature', '') != 'asset',
}, depends=['currency_digits', 'feature']), 'get_yield_data')
yield_fee_total = fields.Function(fields.Numeric(string='Trade Fee', yield_fee_total = fields.Function(fields.Numeric(string='Trade Fee',
help='Total trade fees payed', help='Total trade fees payed',
readonly=True, digits=(16, Eval('currency_digits', 2)), readonly=True, digits=(16, Eval('currency_digits', 2)),
depends=['currency_digits']), states={
'get_yield_data') 'invisible': Eval('feature', '') != 'asset',
}, depends=['currency_digits', 'feature']), 'get_yield_data')
yield_fee_12m = fields.Function(fields.Numeric(string='Trade Fee 1y', yield_fee_12m = fields.Function(fields.Numeric(string='Trade Fee 1y',
help='Trade fees payed in the last twelve month', help='Trade fees payed in the last twelve month',
readonly=True, digits=(16, Eval('currency_digits', 2)), readonly=True, digits=(16, Eval('currency_digits', 2)),
depends=['currency_digits']), states={
'get_yield_data') 'invisible': Eval('feature', '') != 'asset',
}, depends=['currency_digits', 'feature']), 'get_yield_data')
yield_sales = fields.Function(fields.Numeric(string='Sales', yield_sales = fields.Function(fields.Numeric(string='Sales',
help='Total profit or loss on sale of shares.', help='Total profit or loss on sale of shares.',
readonly=True, digits=(16, Eval('currency_digits', 2)), readonly=True, digits=(16, Eval('currency_digits', 2)),
depends=['currency_digits']), states={
'get_yield_data') 'invisible': Eval('feature', '') != 'asset',
}, depends=['currency_digits', 'feature']), 'get_yield_data')
yield_sales_12m = fields.Function(fields.Numeric(string='Sales 1y', yield_sales_12m = fields.Function(fields.Numeric(string='Sales 1y',
help='Total profit or loss on sale of shares in the last twelve month.', help='Total profit or loss on sale of shares in the last twelve month.',
readonly=True, digits=(16, Eval('currency_digits', 2)), readonly=True, digits=(16, Eval('currency_digits', 2)),
depends=['currency_digits']), states={
'get_yield_data') 'invisible': Eval('feature', '') != 'asset',
}, depends=['currency_digits', 'feature']), 'get_yield_data')
yield_balance = fields.Function(fields.Numeric(string='Total Yield', yield_balance = fields.Function(fields.Numeric(string='Total Yield',
help='Total income: price gain + dividends + sales gains - fees', help='Total income: price gain + dividends + sales gains - fees',
readonly=True, digits=(16, Eval('currency_digits', 2)), readonly=True, digits=(16, Eval('currency_digits', 2)),
depends=['currency_digits']), states={
'get_yield_data') 'invisible': Eval('feature', '') != 'asset',
}, depends=['currency_digits', 'feature']),
'on_change_with_yield_balance')
@classmethod @classmethod
def view_attributes(cls): def view_attributes(cls):
@ -190,8 +200,15 @@ class Book(SymbolMixin, metaclass=PoolMeta):
""" """
return 4 return 4
@fields.depends('yield_sales', 'yield_fee_total', 'yield_dividend_total', 'diff_amount')
def on_change_with_yield_balance(self, name=None):
""" calculate yield total
"""
return self.diff_amount + self.yield_dividend_total + \
self.yield_sales - self.yield_fee_total
@classmethod @classmethod
def get_yield_data_sql(cls, date_from=none, date_to=None): def get_yield_data_sql(cls, date_from=None, date_to=None):
""" collect yield data """ collect yield data
""" """
pool = Pool() pool = Pool()
@ -201,7 +218,7 @@ class Book(SymbolMixin, metaclass=PoolMeta):
tab_book = cls.__table__() tab_book = cls.__table__()
tab_line = Line.__table__() tab_line = Line.__table__()
where = True where = Literal(True)
if date_from: if date_from:
where &= tab_line.date >= date_from where &= tab_line.date >= date_from
if date_to: if date_to:
@ -218,7 +235,8 @@ class Book(SymbolMixin, metaclass=PoolMeta):
Sum(tab_line_yield.fee).as_('fee'), Sum(tab_line_yield.fee).as_('fee'),
Sum(tab_line_yield.dividend).as_('dividend'), Sum(tab_line_yield.dividend).as_('dividend'),
Sum(tab_line_gainloss.gainloss).as_('gainloss'), Sum(tab_line_gainloss.gainloss).as_('gainloss'),
having=where group_by=[tab_book.id],
where=where
) )
return (tab_book, query) return (tab_book, query)
@ -226,13 +244,12 @@ class Book(SymbolMixin, metaclass=PoolMeta):
def get_yield_data(cls, cashbooks, names): def get_yield_data(cls, cashbooks, names):
""" collect yield data """ collect yield data
""" """
pool = Pool()
CashBook = pool.get('cashbook.book')
IrDate = pool.get('ir.date')
cursor = Transaction().connection.cursor() cursor = Transaction().connection.cursor()
(tab_book, query) = cls.get_yield_data_sql() context = Transaction().context
result = {x:{y.id: None for y in cashbooks} for x in names} result = {x:{y.id: Decimal('0.0') 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): def quantize_val(value, line):
""" quantize... """ quantize...
@ -241,14 +258,34 @@ class Book(SymbolMixin, metaclass=PoolMeta):
value or Decimal('0.0') value or Decimal('0.0')
).quantize(Decimal(str(1/10**line.currency_digits))) ).quantize(Decimal(str(1/10**line.currency_digits)))
for record in records: # results for 'total'
line = Line2(record[0]) (tab_book1, query_total) = cls.get_yield_data_sql()
values = { query_total.where &= tab_book1.id.in_([x.id for x in cashbooks])
'trade_fee': quantize_val(record[1], line), cursor.execute(*query_total)
'asset_dividend': quantize_val(record[2], line), records_total = cursor.fetchall()
}
for name in list(name_set): # results for 12 months
result[name][record[0]] = values[name] query_date = context.get('date', IrDate.today())
(tab_book2, query_12m) = cls.get_yield_data_sql(
date_to = query_date,
date_from = query_date - timedelta(days=365),
)
query_12m.where &= tab_book2.id.in_([x.id for x in cashbooks])
cursor.execute(*query_12m)
records_12m = cursor.fetchall()
result = {x:{y.id: Decimal('0.0') for y in cashbooks} for x in names}
for record in records_total:
book = CashBook(record[0])
result['yield_fee_total'][record[0]] = quantize_val(record[1], book)
result['yield_dividend_total'][record[0]] = quantize_val(record[2], book)
result['yield_sales'][record[0]] = quantize_val(record[3], book)
for record in records_12m:
book = CashBook(record[0])
result['yield_fee_12m'][record[0]] = quantize_val(record[1], book)
result['yield_dividend_12m'][record[0]] = quantize_val(record[2], book)
result['yield_sales_12m'][record[0]] = quantize_val(record[3], book)
return result return result

View file

@ -50,6 +50,10 @@ msgctxt "view:cashbook.book:"
msgid "Quantity" msgid "Quantity"
msgstr "Anzahl" msgstr "Anzahl"
msgctxt "view:cashbook.book:"
msgid "Fees and dividends"
msgstr "Gebühren und Dividenden"
msgctxt "view:cashbook.book:" msgctxt "view:cashbook.book:"
msgid "Current valuation of the investment" msgid "Current valuation of the investment"
msgstr "aktuelle Bewertung der Vermögensanlage" msgstr "aktuelle Bewertung der Vermögensanlage"

View file

@ -34,6 +34,10 @@ msgctxt "view:cashbook.book:"
msgid "Quantity" msgid "Quantity"
msgstr "Quantity" msgstr "Quantity"
msgctxt "view:cashbook.book:"
msgid "Fees and dividends"
msgstr "Fees and dividends"
msgctxt "view:cashbook.book:" msgctxt "view:cashbook.book:"
msgid "Current valuation of the investment" msgid "Current valuation of the investment"
msgstr "Current valuation of the investment" msgstr "Current valuation of the investment"
@ -122,6 +126,62 @@ msgctxt "help:cashbook.book,current_rate:"
msgid "Rate per unit of investment based on current stock exchange price." msgid "Rate per unit of investment based on current stock exchange price."
msgstr "Rate per unit of investment based on current stock exchange price." msgstr "Rate per unit of investment based on current stock exchange price."
msgctxt "field:cashbook.book,yield_dividend_total:"
msgid "Dividend"
msgstr "Dividend"
msgctxt "help:cashbook.book,yield_dividend_total:"
msgid "Total dividends received"
msgstr "Total dividends received"
msgctxt "field:cashbook.book,yield_dividend_12m:"
msgid "Dividend 1y"
msgstr "Dividend 1y"
msgctxt "help:cashbook.book,yield_dividend_12m:"
msgid "Dividends received in the last twelve months"
msgstr "Dividends received in the last twelve months"
msgctxt "field:cashbook.book,yield_fee_total:"
msgid "Trade Fee"
msgstr "Trade Fee"
msgctxt "help:cashbook.book,yield_fee_total:"
msgid "Total trade fees payed"
msgstr "Total trade fees payed"
msgctxt "field:cashbook.book,yield_fee_12m:"
msgid "Trade Fee 1y"
msgstr "Trade Fee 1y"
msgctxt "help:cashbook.book,yield_fee_12m:"
msgid "Trade fees payed in the last twelve month"
msgstr "Trade fees payed in the last twelve month"
msgctxt "field:cashbook.book,yield_sales:"
msgid "Sales"
msgstr "Sales"
msgctxt "help:cashbook.book,yield_sales:"
msgid "Total profit or loss on sale of shares."
msgstr "Total profit or loss on sale of shares."
msgctxt "field:cashbook.book,yield_sales_12m:"
msgid "Sales 1y"
msgstr "Sales 1y"
msgctxt "help:cashbook.book,yield_sales_12m:"
msgid "Total profit or loss on sale of shares in the last twelve month."
msgstr "Total profit or loss on sale of shares in the last twelve month."
msgctxt "field:cashbook.book,yield_balance:"
msgid "Total Yield"
msgstr "Total Yield"
msgctxt "help:cashbook.book,yield_balance:"
msgid "Total income: price gain + dividends + sales gains - fees"
msgstr "Total income: price gain + dividends + sales gains - fees"
msgctxt "field:cashbook.split,quantity_digits:" msgctxt "field:cashbook.split,quantity_digits:"
msgid "Digits" msgid "Digits"
msgstr "Digits" msgstr "Digits"
@ -238,6 +298,30 @@ msgctxt "help:cashbook.line,diff_percent:"
msgid "percentage performance since acquisition" msgid "percentage performance since acquisition"
msgstr "percentage performance since acquisition" msgstr "percentage performance since acquisition"
msgctxt "field:cashbook.line,trade_fee:"
msgid "Fee"
msgstr "Fee"
msgctxt "help:cashbook.line,trade_fee:"
msgid "Trading fee for the current booking line."
msgstr "Trading fee for the current booking line."
msgctxt "field:cashbook.line,asset_dividend:"
msgid "Dividend"
msgstr "Dividend"
msgctxt "help:cashbook.line,asset_dividend:"
msgid "Dividend received at the current booking line."
msgstr "Dividend received at the current booking line."
msgctxt "field:cashbook.line,asset_gainloss:"
msgid "Profit/Loss"
msgstr "Profit/Loss"
msgctxt "help:cashbook.line,asset_gainloss:"
msgid "Profit or loss on sale on the current booking line."
msgstr "Profit or loss on sale on the current booking line."
msgctxt "field:cashbook.recon,start_quantity:" msgctxt "field:cashbook.recon,start_quantity:"
msgid "Start Quantity" msgid "Start Quantity"
msgstr "Start Quantity" msgstr "Start Quantity"

View file

@ -6,18 +6,20 @@ full copyright notices and license terms. -->
<xpath expr="/form/notebook/page[@name='balance']/field[@name='balance_ref']" <xpath expr="/form/notebook/page[@name='balance']/field[@name='balance_ref']"
position="after"> position="after">
<separator colspan="4" name="quantity" string="Quantity"/> <newline/>
<separator colspan="2" name="quantity" string="Quantity"/>
<separator colspan="4" name="current_value" string="Current valuation of the investment"/>
<label name="quantity"/> <label name="quantity"/>
<field name="quantity" symbol="quantity_uom"/> <field name="quantity" symbol="quantity_uom"/>
<label name="quantity_all"/>
<field name="quantity_all" symbol="quantity_uom"/>
<separator colspan="4" name="current_value" string="Current valuation of the investment"/>
<label name="current_value"/> <label name="current_value"/>
<field name="current_value" symbol="currency"/> <field name="current_value" symbol="currency"/>
<label name="current_value_ref"/> <label name="current_value_ref"/>
<field name="current_value_ref" symbol="company_currency"/> <field name="current_value_ref" symbol="company_currency"/>
<label name="quantity_all"/>
<field name="quantity_all" symbol="quantity_uom"/>
<label name="diff_amount"/> <label name="diff_amount"/>
<field name="diff_amount" symbol="currency"/> <field name="diff_amount" symbol="currency"/>
<label name="diff_percent"/> <label name="diff_percent"/>
@ -25,8 +27,28 @@ full copyright notices and license terms. -->
<field name="diff_percent" xexpand="0"/> <field name="diff_percent" xexpand="0"/>
<label name="diff_percent" xalign="0.0" string="%" xexpand="1"/> <label name="diff_percent" xalign="0.0" string="%" xexpand="1"/>
</group> </group>
<label id="lab1" colspan="2" string=" "/>
<label name="yield_balance"/>
<field name="yield_balance" symbol="currency"/>
<label name="current_rate"/> <label name="current_rate"/>
<field name="current_rate" symbol="asset_symbol"/> <field name="current_rate" symbol="asset_symbol"/>
<separator name="yield_dividend_total" colspan="6" string="Fees and dividends"/>
<label name="yield_sales"/>
<field name="yield_sales" symbol="currency"/>
<label name="yield_dividend_total"/>
<field name="yield_dividend_total" symbol="currency"/>
<label name="yield_fee_total"/>
<field name="yield_fee_total" symbol="currency"/>
<label name="yield_sales_12m"/>
<field name="yield_sales_12m" symbol="currency"/>
<label name="yield_dividend_12m"/>
<field name="yield_dividend_12m" symbol="currency"/>
<label name="yield_fee_12m"/>
<field name="yield_fee_12m" symbol="currency"/>
</xpath> </xpath>
<xpath expr="/form/notebook/page[@id='pggeneral']" position="after"> <xpath expr="/form/notebook/page[@id='pggeneral']" position="after">