book: list-view - farbige asset-zeilen,

book: Felder diff-prozent/amount + tests
This commit is contained in:
Frederik Jaeckel 2022-12-23 19:15:20 +01:00
parent 8f8da805d4
commit d501bd1700
8 changed files with 249 additions and 31 deletions

59
book.py
View file

@ -4,9 +4,8 @@
# full copyright notices and license terms.
from trytond.model import fields, SymbolMixin
from trytond.exceptions import UserError
from trytond.pool import PoolMeta, Pool
from trytond.pyson import Eval, Or, Len
from trytond.pyson import Eval, Or, Len, Bool, If
from trytond.modules.cashbook.book import STATES2, DEPENDS2
from trytond.transaction import Transaction
from decimal import Decimal
@ -75,18 +74,44 @@ class Book(SymbolMixin, metaclass=PoolMeta):
}, depends=['quantity_digits', 'feature']),
'get_asset_quantity')
current_value = fields.Function(fields.Numeric(string='Value',
help='Valuation of the investment based on the current stock market price.',
readonly=True, digits=(16, Eval('currency_digits', 2)),
states={
'invisible': Eval('feature', '') != 'asset',
}, depends=['currency_digits', 'feature']),
'get_asset_quantity')
current_value_ref = fields.Function(fields.Numeric(string='Value (Ref.)',
help='Valuation of the investment based on the current stock exchange price, converted into the company currency.',
readonly=True, digits=(16, Eval('currency_digits', 2)),
states={
'invisible': Eval('feature', '') != 'asset',
}, depends=['currency_digits', 'feature']),
'invisible': Or(
Eval('feature', '') != 'asset',
~Bool(Eval('company_currency', -1)),
),
}, depends=['currency_digits', 'feature', 'company_currency']),
'get_asset_quantity')
# performance
diff_amount = fields.Function(fields.Numeric(string='Difference',
help='Difference between acquisition value and current value',
readonly=True, digits=(16, Eval('currency_digits', 2)),
depends=['currency_digits']), 'get_asset_quantity')
diff_percent = fields.Function(fields.Numeric(string='Percent',
help='percentage performance since acquisition',
readonly=True, digits=(16, Eval('currency_digits', 2)),
depends=['currency_digits']), 'get_asset_quantity')
@classmethod
def view_attributes(cls):
return super(Book, cls).view_attributes() + [
('/tree', 'visual',
If(Eval('feature', '') == 'asset',
If(Eval('diff_percent', 0) < 0, 'danger',
If(Eval('diff_percent', 0) > 0, 'success', '')
), '')
),
]
@fields.depends('asset', 'quantity_uom')
def on_change_asset(self):
""" get uom from asset
@ -115,6 +140,7 @@ class Book(SymbolMixin, metaclass=PoolMeta):
tab_cur = Currency.__table__()
tab_asset = Asset.__table__()
(tab_rate, tab2) = Asset.get_rate_data_sql()
(tab_balance, tab2) = CBook.get_balance_of_cashbook_sql()
cursor = Transaction().connection.cursor()
context = Transaction().context
@ -128,6 +154,9 @@ class Book(SymbolMixin, metaclass=PoolMeta):
condition=tab_book.currency==tab_cur.id,
).join(tab_asset,
condition=tab_book.asset==tab_asset.id,
).join(tab_balance,
condition=tab_book.id==tab_balance.cashbook,
type_ = 'LEFT OUTER',
).join(tab_rate,
condition=tab_book.asset==tab_rate.id,
type_ = 'LEFT OUTER',
@ -144,9 +173,11 @@ class Book(SymbolMixin, metaclass=PoolMeta):
tab_asset.uom, # 6
tab_book.quantity_uom, # 7
tab_asset.currency.as_('asset_currency'), #8
Coalesce(tab_balance.balance, Decimal('0.0')).as_('balance'), #9
group_by=[tab_book.id, tab_rate.rate,
tab_book.currency, tab_cur.digits, tab_asset.uom,
tab_book.quantity_uom, tab_asset.currency],
tab_book.quantity_uom, tab_asset.currency,
tab_balance.balance],
)
cursor.execute(*query)
records = cursor.fetchall()
@ -160,19 +191,27 @@ class Book(SymbolMixin, metaclass=PoolMeta):
Uom.compute_qty(Uom(record[6]), 1.0, Uom(record[7]), round=False)
)
current_value = Currency.compute(
record[8],
record[3] * record[1] / uom_factor,
record[4]
)
values = {
'quantity': record[1],
'quantity_all': record[2],
'current_value': Currency.compute(
record[8],
record[3] * record[1] / uom_factor,
record[4]
),
'current_value': current_value,
'current_value_ref': Currency.compute(
record[8],
record[3] * record[1] / uom_factor,
company_currency if company_currency is not None else record[8],
),
'diff_amount': current_value - record[9],
'diff_percent': (
Decimal('100.0') * current_value / \
record[9] - Decimal('100.0')
).quantize(Decimal(str(1/10**record[5]))) \
if record[9] != Decimal('0.0') else None,
}
for name in names: