add profit/loss-values for selection of cashbooks by btype

This commit is contained in:
Frederik Jaeckel 2023-02-06 20:22:07 +01:00
parent d01bd0dc0a
commit 131b9f0c8d
7 changed files with 119 additions and 31 deletions

View file

@ -10,7 +10,7 @@ from trytond.pool import Pool
from trytond.i18n import gettext
from .colors import sel_color as sel_bgcolor
from .templates import template_view_graph, template_view_line, \
cashbook_types, category_types
cashbook_types, category_types, booktype_types
sel_chart = [
@ -65,7 +65,7 @@ class Evaluation(sequence_ordered(), ModelSQL, ModelView):
relation_name='cashbook_report.eval_line',
origin='evaluation', target='dtype',
states={
'invisible': Eval('dtype', '') != 'types',
'invisible': ~Eval('dtype', '').in_(booktype_types),
}, depends=['dtype'])
currencies = fields.Many2Many(string='Currencies',
relation_name='cashbook_report.eval_line',
@ -316,4 +316,3 @@ class Evaluation(sequence_ordered(), ModelSQL, ModelView):
super(Evaluation, cls).delete(evaluations)
# end Evaluation

View file

@ -23,6 +23,9 @@ class InvestmentEvaluation(metaclass=PoolMeta):
('category_gldiff', gettext('cashbook_report.msg_dtype_category_gldiff')),
('category_glvalue', gettext('cashbook_report.msg_dtype_category_glvalue')),
('category_glperc', gettext('cashbook_report.msg_dtype_category_glperc')),
('types_gldiff', gettext('cashbook_report.msg_dtype_types_gldiff')),
('types_glvalue', gettext('cashbook_report.msg_dtype_types_glvalue')),
('types_glperc', gettext('cashbook_report.msg_dtype_types_glperc')),
])
return result
@ -32,19 +35,36 @@ class InvestmentEvaluation(metaclass=PoolMeta):
class InvestmentLine(metaclass=PoolMeta):
__name__ = 'cashbook_report.eval_line'
def get_value_types_glperc(self):
""" get percent of profit/loss by type
"""
if self.dtype is None:
return None
return self.get_percent_by_query([
('btype.id', '=', self.dtype.id),
])
def get_value_category_glperc(self):
""" get percent of profit/loss by category
"""
if self.category is None:
return None
return self.get_percent_by_query([
('categories.id', '=', self.category.id),
])
def get_percent_by_query(self, query):
""" get percentual difference of bookings in categories
converted to currency of evaluation
"""
Book = Pool().get('cashbook.book')
if self.category is None:
return None
query2 = [('state', '=', 'open')]
query2.extend(query)
books = Book.search(query2)
books = Book.search([
('state', '=', 'open'),
('categories.id', '=', self.category.id),
])
value = Decimal('0.0')
amount = Decimal('0.0')
@ -60,19 +80,36 @@ class InvestmentLine(metaclass=PoolMeta):
)
return Decimal('0.0')
def get_value_types_glvalue(self):
""" get current value by type
"""
if self.dtype is None:
return None
return self.get_currentvalue_by_query([
('btype.id', '=', self.dtype.id),
])
def get_value_category_glvalue(self):
""" get current value by category
"""
if self.category is None:
return None
return self.get_currentvalue_by_query([
('categories.id', '=', self.category.id),
])
def get_currentvalue_by_query(self, query):
""" get current value of bookings in categories
converted to currency of evaluation
"""
Book = Pool().get('cashbook.book')
if self.category is None:
return None
query2 = [('state', '=', 'open')]
query2.extend(query)
books = Book.search(query2)
books = Book.search([
('state', '=', 'open'),
('categories.id', '=', self.category.id),
])
result = Decimal('0.0')
if len(books) > 0:
for book in books:
@ -86,19 +123,36 @@ class InvestmentLine(metaclass=PoolMeta):
books[0].company.currency, result)
return result
def get_value_types_gldiff(self):
""" get difference amount by type
"""
if self.dtype is None:
return None
return self.get_difference_by_query([
('btype.id', '=', self.dtype.id),
])
def get_value_category_gldiff(self):
""" get difference amount by category
"""
if self.category is None:
return None
return self.get_difference_by_query([
('categories.id', '=', self.category.id),
])
def get_difference_by_query(self, query):
""" get difference amount of bookings in categories
converted to currency of evaluation
"""
Book = Pool().get('cashbook.book')
if self.category is None:
return None
query2 = [('state', '=', 'open')]
query2.extend(query)
books = Book.search(query2)
books = Book.search([
('state', '=', 'open'),
('categories.id', '=', self.category.id),
])
result = Decimal('0.0')
if len(books) > 0:
result = sum([x.current_value_ref - x.balance_ref for x in books

11
line.py
View file

@ -11,7 +11,7 @@ from trytond.transaction import Transaction
from trytond.i18n import gettext
from trytond.exceptions import UserError
from trytond.pool import Pool
from .templates import cashbook_types, category_types
from .templates import cashbook_types, category_types, booktype_types
class EvaluationLine(ModelSQL, ModelView):
@ -29,7 +29,7 @@ class EvaluationLine(ModelSQL, ModelView):
dtype = fields.Many2One(string='Type', select=True, ondelete='CASCADE',
model_name='cashbook.type',
states={
'required': Eval('eval_dtype', '') == 'types',
'required': Eval('eval_dtype', '').in_(booktype_types),
}, depends=['eval_dtype'])
currency = fields.Many2One(string='Currency', select=True, ondelete='CASCADE',
model_name='currency.currency',
@ -96,7 +96,7 @@ class EvaluationLine(ModelSQL, ModelView):
@fields.depends('evaluation', '_parent_evaluation.dtype')
def on_change_with_eval_dtype(self, name=None):
""" get dtape from parent
""" get dtype from parent
"""
if self.evaluation:
return self.evaluation.dtype
@ -128,9 +128,10 @@ class EvaluationLine(ModelSQL, ModelView):
# otherwise use rec_name of linked record
if self.eval_dtype:
dtype_sel = {'types': 'dtype', 'currencies': 'currency'}
dtype_sel = {'currencies': 'currency'}
dtype_sel.update({x:'cashbook' for x in cashbook_types})
dtype_sel.update({x:'category' for x in category_types})
dtype_sel.update({x:'dtype' for x in booktype_types})
return getattr(
getattr(self, dtype_sel[self.eval_dtype], None),
@ -160,7 +161,7 @@ class EvaluationLine(ModelSQL, ModelView):
'cashbook_report.msg_invalid_dtype',
typename = gettext('cashbook_report.msg_dtype_cashbook'),
))
if (record.evaluation.dtype != 'types') and \
if (record.evaluation.dtype not in booktype_types) and \
(record.dtype is not None):
raise UserError(gettext(
'cashbook_report.msg_invalid_dtype',

View file

@ -27,8 +27,20 @@ msgid "Cashbooks [Current Value]"
msgstr "Kassenbücher [aktueller Wert]"
msgctxt "model:ir.message,text:msg_dtype_type"
msgid "Types of Cashbooks"
msgstr "Typen von Kassenbüchern"
msgid "Types of Cashbooks [Amount]"
msgstr "Typen von Kassenbüchern [Betrag]"
msgctxt "model:ir.message,text:msg_dtype_types_gldiff"
msgid "Types of Cashbooks [Amount of Profit/Loss]"
msgstr "Typen von Kassenbüchern [Betrag Gewinn/Verlust]"
msgctxt "model:ir.message,text:msg_dtype_types_glvalue"
msgid "Types of Cashbooks [Current Value]"
msgstr "Typen von Kassenbüchern [aktueller Wert]"
msgctxt "model:ir.message,text:msg_dtype_types_glperc"
msgid "Types of Cashbooks [Percent of Profit/Loss]"
msgstr "Typen von Kassenbüchern [Prozent Gewinn/Verlust]"
msgctxt "model:ir.message,text:msg_dtype_currency"
msgid "Currencies"

View file

@ -23,8 +23,20 @@ msgid "Cashbooks [Current Value]"
msgstr "Cashbooks [Current Value]"
msgctxt "model:ir.message,text:msg_dtype_type"
msgid "Types of Cashbooks"
msgstr "Types of Cashbooks"
msgid "Types of Cashbooks [Amount]"
msgstr "Types of Cashbooks [Amount]"
msgctxt "model:ir.message,text:msg_dtype_types_gldiff"
msgid "Types of Cashbooks [Amount of Profit/Loss]"
msgstr "Types of Cashbooks [Amount of Profit/Loss]"
msgctxt "model:ir.message,text:msg_dtype_types_glvalue"
msgid "Types of Cashbooks [Current Value]"
msgstr "Types of Cashbooks [Current Value]"
msgctxt "model:ir.message,text:msg_dtype_types_glperc"
msgid "Types of Cashbooks [Percent of Profit/Loss]"
msgstr "Types of Cashbooks [Percent of Profit/Loss]"
msgctxt "model:ir.message,text:msg_dtype_currency"
msgid "Currencies"

View file

@ -21,7 +21,16 @@ full copyright notices and license terms. -->
<field name="text">Cashbooks [Current Value]</field>
</record>
<record model="ir.message" id="msg_dtype_type">
<field name="text">Types of Cashbooks</field>
<field name="text">Types of Cashbooks [Amount]</field>
</record>
<record model="ir.message" id="msg_dtype_types_gldiff">
<field name="text">Types of Cashbooks [Amount of Profit/Loss]</field>
</record>
<record model="ir.message" id="msg_dtype_types_glvalue">
<field name="text">ypes of Cashbooks [Current Value]</field>
</record>
<record model="ir.message" id="msg_dtype_types_glperc">
<field name="text">Types of Cashbooks [Percent of Profit/Loss]</field>
</record>
<record model="ir.message" id="msg_dtype_currency">
<field name="text">Currencies</field>

View file

@ -5,6 +5,7 @@
cashbook_types = ['cashbooks', 'cashbooks_gldiff', 'cashbooks_glperc', 'cashbooks_glvalue']
category_types = ['categories', 'category_gldiff', 'category_glvalue', 'category_glperc']
booktype_types = ['types', 'types_gldiff', 'types_glvalue', 'types_glperc']
template_view_line = '<field name="balance" fill="%(fill)s" empty="0" string="%(string)s"/>'