diff --git a/book.py b/book.py
index 0a79fb8..2393bab 100644
--- a/book.py
+++ b/book.py
@@ -10,6 +10,8 @@ from trytond.modules.cashbook.book import STATES2, DEPENDS2
from trytond.transaction import Transaction
from trytond.report import Report
from decimal import Decimal
+from datetime import timedelta
+from sql import Literal
from sql.functions import CurrentDate
from sql.aggregate import Sum
from sql.conditionals import Case, Coalesce
@@ -121,38 +123,46 @@ class Book(SymbolMixin, metaclass=PoolMeta):
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')
+ states={
+ 'invisible': Eval('feature', '') != 'asset',
+ }, depends=['currency_digits', 'feature']), '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')
+ states={
+ 'invisible': Eval('feature', '') != 'asset',
+ }, depends=['currency_digits', 'feature']), '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')
+ states={
+ 'invisible': Eval('feature', '') != 'asset',
+ }, depends=['currency_digits', 'feature']), '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')
+ states={
+ 'invisible': Eval('feature', '') != 'asset',
+ }, depends=['currency_digits', 'feature']), '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')
+ states={
+ 'invisible': Eval('feature', '') != 'asset',
+ }, depends=['currency_digits', 'feature']), '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')
+ states={
+ 'invisible': Eval('feature', '') != 'asset',
+ }, depends=['currency_digits', 'feature']), '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')
+ states={
+ 'invisible': Eval('feature', '') != 'asset',
+ }, depends=['currency_digits', 'feature']),
+ 'on_change_with_yield_balance')
@classmethod
def view_attributes(cls):
@@ -190,8 +200,15 @@ class Book(SymbolMixin, metaclass=PoolMeta):
"""
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
- 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
"""
pool = Pool()
@@ -201,7 +218,7 @@ class Book(SymbolMixin, metaclass=PoolMeta):
tab_book = cls.__table__()
tab_line = Line.__table__()
- where = True
+ where = Literal(True)
if date_from:
where &= tab_line.date >= date_from
if date_to:
@@ -218,7 +235,8 @@ class Book(SymbolMixin, metaclass=PoolMeta):
Sum(tab_line_yield.fee).as_('fee'),
Sum(tab_line_yield.dividend).as_('dividend'),
Sum(tab_line_gainloss.gainloss).as_('gainloss'),
- having=where
+ group_by=[tab_book.id],
+ where=where
)
return (tab_book, query)
@@ -226,13 +244,12 @@ class Book(SymbolMixin, metaclass=PoolMeta):
def get_yield_data(cls, cashbooks, names):
""" collect yield data
"""
+ pool = Pool()
+ CashBook = pool.get('cashbook.book')
+ IrDate = pool.get('ir.date')
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()
+ context = Transaction().context
+ result = {x:{y.id: Decimal('0.0') for y in cashbooks} for x in names}
def quantize_val(value, line):
""" quantize...
@@ -241,14 +258,34 @@ class Book(SymbolMixin, metaclass=PoolMeta):
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]
+ # results for 'total'
+ (tab_book1, query_total) = cls.get_yield_data_sql()
+ query_total.where &= tab_book1.id.in_([x.id for x in cashbooks])
+ cursor.execute(*query_total)
+ records_total = cursor.fetchall()
+
+ # results for 12 months
+ 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
diff --git a/locale/de.po b/locale/de.po
index c6d627a..f00f63e 100644
--- a/locale/de.po
+++ b/locale/de.po
@@ -50,6 +50,10 @@ msgctxt "view:cashbook.book:"
msgid "Quantity"
msgstr "Anzahl"
+msgctxt "view:cashbook.book:"
+msgid "Fees and dividends"
+msgstr "Gebühren und Dividenden"
+
msgctxt "view:cashbook.book:"
msgid "Current valuation of the investment"
msgstr "aktuelle Bewertung der Vermögensanlage"
diff --git a/locale/en.po b/locale/en.po
index 585c663..b75f750 100644
--- a/locale/en.po
+++ b/locale/en.po
@@ -34,6 +34,10 @@ msgctxt "view:cashbook.book:"
msgid "Quantity"
msgstr "Quantity"
+msgctxt "view:cashbook.book:"
+msgid "Fees and dividends"
+msgstr "Fees and dividends"
+
msgctxt "view:cashbook.book:"
msgid "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."
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:"
msgid "Digits"
msgstr "Digits"
@@ -238,6 +298,30 @@ msgctxt "help:cashbook.line,diff_percent:"
msgid "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:"
msgid "Start Quantity"
msgstr "Start Quantity"
diff --git a/view/book_form.xml b/view/book_form.xml
index 8d98082..fffd775 100644
--- a/view/book_form.xml
+++ b/view/book_form.xml
@@ -6,18 +6,20 @@ full copyright notices and license terms. -->
-
+
+
+
+
-
-
-
-
+
+
+
@@ -25,8 +27,28 @@ full copyright notices and license terms. -->
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+