convert results to evaluation-currency, fix states

This commit is contained in:
Frederik Jaeckel 2023-02-05 18:16:49 +01:00
parent 0c3a130ef5
commit e3a41b821b
3 changed files with 51 additions and 39 deletions

View file

@ -78,7 +78,7 @@ class Evaluation(sequence_ordered(), ModelSQL, ModelView):
relation_name='cashbook_report.eval_line',
origin='evaluation', target='category',
states={
'invisible': Eval('dtype', '').in_(category_types),
'invisible': ~Eval('dtype', '').in_(category_types),
}, depends=['dtype'])
line_values = fields.One2Many(string='Line Values',

View file

@ -49,12 +49,15 @@ class InvestmentLine(metaclass=PoolMeta):
amount = Decimal('0.0')
if len(books) > 0:
value = sum([x.current_value_ref for x in books])
amount = sum([x.balance_ref for x in books])
value = sum([x.current_value_ref for x in books
if (x.current_value_ref is not None) and (x.feature == 'asset')])
amount = sum([x.balance_ref for x in books
if (x.balance_ref is not None) and (x.feature == 'asset')])
if amount != Decimal('0.0'):
return (
Decimal('100.0') * value / amount - Decimal('100.0')
).quantize(Decimal(str(1 / 10 ** self.currency_digits)))
return self.convert_to_evalcurrency(
books[0].company.currency,
Decimal('100.0') * value / amount - Decimal('100.0'),
)
return Decimal('0.0')
def get_value_category_glvalue(self):
@ -72,7 +75,15 @@ class InvestmentLine(metaclass=PoolMeta):
])
result = Decimal('0.0')
if len(books) > 0:
result = sum([x.current_value_ref for x in books])
for book in books:
if book.feature == 'asset':
if book.current_value_ref is not None:
result += book.current_value_ref
else :
if book.balance_ref is not None:
result += book.balance_ref
return self.convert_to_evalcurrency(
books[0].company.currency, result)
return result
def get_value_category_gldiff(self):
@ -90,7 +101,12 @@ class InvestmentLine(metaclass=PoolMeta):
])
result = Decimal('0.0')
if len(books) > 0:
result = sum([x.current_value_ref - x.balance_ref for x in books])
result = sum([x.current_value_ref - x.balance_ref for x in books
if (x.current_value_ref is not None) and \
(x.balance_ref is not None) and \
(x.feature == 'asset')])
result = self.convert_to_evalcurrency(
books[0].company.currency, result)
return result
def get_value_cashbooks_gldiff(self):
@ -100,12 +116,9 @@ class InvestmentLine(metaclass=PoolMeta):
if self.cashbook:
if self.cashbook.feature == 'asset':
exp = Decimal(Decimal(1) / 10 ** self.currency_digits)
return Currency.compute(
return self.convert_to_evalcurrency(
self.cashbook.currency,
self.cashbook.diff_amount,
self.eval_currency,
).quantize(exp)
self.cashbook.diff_amount)
def get_value_cashbooks_glvalue(self):
""" current value of cashbooks
@ -114,12 +127,9 @@ class InvestmentLine(metaclass=PoolMeta):
if self.cashbook:
if self.cashbook.feature == 'asset':
exp = Decimal(Decimal(1) / 10 ** self.currency_digits)
return Currency.compute(
return self.convert_to_evalcurrency(
self.cashbook.currency,
self.cashbook.current_value,
self.eval_currency,
).quantize(exp)
self.cashbook.current_value)
def get_value_cashbooks_glperc(self):
""" percent of profit/loss of cashbooks

42
line.py
View file

@ -128,18 +128,26 @@ class EvaluationLine(ModelSQL, ModelView):
# otherwise use rec_name of linked record
if self.eval_dtype:
dtype_sel = {'types': 'dtype', 'currencies': 'currency'}
dtype_sel.update({x:'cashbook' for x in cashbook_types})
dtype_sel.update({x:'category' for x in category_types})
return getattr(
getattr(self, {
'cashbooks': 'cashbook',
'cashbooks_gldiff': 'cashbook',
'cashbooks_glperc': 'cashbook',
'cashbooks_glvalue': 'cashbook',
'categories': 'category',
'types': 'dtype',
'currencies': 'currency',
}[self.eval_dtype], None),
getattr(self, dtype_sel[self.eval_dtype], None),
'rec_name', None)
def convert_to_evalcurrency(self, from_currency, amount):
""" convert amount to current evaluation-currency
"""
Currency = Pool().get('currency.currency')
exp = Decimal(Decimal(1) / 10 ** self.currency_digits)
return Currency.compute(
from_currency,
amount,
self.eval_currency,
).quantize(exp)
@classmethod
def validate(cls, records):
""" check parent record
@ -202,13 +210,9 @@ class EvaluationLine(ModelSQL, ModelView):
(id_currency, bal1) = balance
if bal1 is not None:
total_amount += Currency.compute(
Currency(id_currency),
bal1,
self.eval_currency,
)
exp = Decimal(Decimal(1) / 10 ** self.currency_digits)
return total_amount.quantize(exp)
total_amount += self.convert_to_evalcurrency(
Currency(id_currency), bal1)
return total_amount
def get_value_cashbooks(self):
""" balance of cashbooks
@ -216,12 +220,10 @@ class EvaluationLine(ModelSQL, ModelView):
Currency = Pool().get('currency.currency')
if self.cashbook:
exp = Decimal(Decimal(1) / 10 ** self.currency_digits)
return Currency.compute(
return self.convert_to_evalcurrency(
self.cashbook.currency,
self.cashbook.balance,
self.eval_currency,
).quantize(exp)
)
def get_value_categories(self):
""" get balance of bookings in categories