cashbook_report/evaluation.py

225 lines
7.1 KiB
Python
Raw Normal View History

2022-10-28 11:27:33 +00:00
# -*- coding: utf-8 -*-
# This file is part of the cashbook-module from m-ds for Tryton.
# The COPYRIGHT file at the top level of this repository contains the
# full copyright notices and license terms.
from trytond.model import ModelView, ModelSQL, fields, Check
from trytond.pyson import Eval, Or, Bool, Id, Len
from trytond.transaction import Transaction
from trytond.i18n import gettext
from trytond.exceptions import UserError
2022-10-28 11:27:33 +00:00
from .colors import sel_color as sel_bgcolor
sel_etype = [
('cashbooks', 'Cashbooks'),
('types', 'Types of Cashbooks'),
('currencies', 'Currencys'),
#('category', 'Category'),
2022-10-28 11:27:33 +00:00
]
sel_chart = [
('vbar', 'Vertical Bars'),
('hbar', 'Horizontal Bars'),
('pie', 'Pie'),
('line', 'Line'),
]
sel_maincolor = [
('default', 'Default'),
('red', 'Red'),
('green', 'Green'),
('grey', 'Grey'),
('black', 'Black'),
('darkcyan', 'Dark Cyan'),
]
class Evaluation(ModelSQL, ModelView):
'Evaluation'
__name__ = 'cashbook_report.evaluation'
company = fields.Many2One(string='Company', model_name='company.company',
required=True, ondelete="RESTRICT")
name = fields.Char(string='Name', required=True)
dtype = fields.Selection(string='Data type', required=True,
sort=False, selection=sel_etype,
help='Type of data displayed')
chart = fields.Selection(string='Chart type', required=True,
sort=False, selection=sel_chart,
help='Type of graphical presentation.')
legend = fields.Boolean(string='Legend')
maincolor = fields.Selection(string='Color scheme', required=True,
help='The color scheme determines the hue of all components of the chart.',
selection=sel_maincolor, sort=False)
bgcolor = fields.Selection(string='Background Color', required=True,
help='Background color of the chart area.', sort=False,
selection=sel_bgcolor)
posted = fields.Boolean(string='Posted', help='Posted amounts only.')
cashbooks = fields.Many2Many(string='Cashbooks',
relation_name='cashbook_report.eval_book',
origin='evaluation', target='cashbook',
states={
'invisible': Eval('dtype', '') != 'cashbooks',
}, depends=['dtype'])
types = fields.Many2Many(string='Types',
relation_name='cashbook_report.eval_type',
origin='evaluation', target='dtype',
states={
'invisible': Eval('dtype', '') != 'types',
}, depends=['dtype'])
currencies = fields.Many2Many(string='Currencies',
relation_name='cashbook_report.eval_currency',
origin='evaluation', target='currency',
filter=[('cashbook_hasbookings', '=', True)],
states={
'invisible': Eval('dtype', '') != 'currencies',
}, depends=['dtype'])
currency_values = fields.One2Many(string='Currency Values',
field='evaluation', readonly=True,
model_name='cashbook_report.eval_currency')
2022-10-28 11:27:33 +00:00
@staticmethod
def default_company():
return Transaction().context.get('company') or None
@classmethod
def default_posted(cls):
""" default: False
"""
return False
2022-10-28 11:27:33 +00:00
@classmethod
def default_bgcolor(cls):
""" default: Yellow 5
"""
return '#ffffc0'
@classmethod
def default_maincolor(cls):
""" default: 'default'
"""
return 'default'
@classmethod
def default_legend(cls):
""" default True
"""
return True
@classmethod
def default_dtype(cls):
2022-10-28 11:27:33 +00:00
""" default 'book'
"""
return 'cashbooks'
2022-10-28 11:27:33 +00:00
@classmethod
def default_chart(cls):
""" default 'pie'
2022-10-28 11:27:33 +00:00
"""
return 'pie'
@classmethod
def write(cls, *args):
""" unlink records if dtype changes
"""
to_write = []
actions = iter(args)
for evaluations, values in zip(actions, actions):
if 'dtype' in values.keys():
for evaluation in evaluations:
if evaluation.dtype == values['dtype']:
continue
if (values['dtype'] != 'cashbooks') and \
(len(evaluation.cashbooks) > 0):
to_write.extend([
[evaluation],
{
'cashbooks': [
('remove', [x.id for x in evaluation.cashbooks])
],
}])
if (values['dtype'] != 'types') and (len(evaluation.types) > 0):
to_write.extend([
[evaluation],
{
'types': [
('remove', [x.id for x in evaluation.types])
],
}])
if (values['dtype'] != 'currencies') and (len(evaluation.currencies) > 0):
to_write.extend([
[evaluation],
{
'currencies': [
('remove', [x.id for x in evaluation.currencies])
],
}])
args = list(args)
args.extend(to_write)
super(Evaluation, cls).write(*args)
2022-10-28 11:27:33 +00:00
# end Evaluation
class EvaluationCashbookRel(ModelSQL):
'Evaluation Cashbook Relation'
__name__ = 'cashbook_report.eval_book'
evaluation = fields.Many2One(string='Evaluation', required=True,
select=True, ondelete='CASCADE',
model_name='cashbook_report.evaluation')
cashbook = fields.Many2One(string='Cashbook', required=True,
select=True, ondelete='CASCADE',
model_name='cashbook.book')
@classmethod
def validate(cls, records):
""" check parent record
"""
super(EvaluationCashbookRel, cls).validate(records)
for record in records:
if record.evaluation.dtype != 'cashbooks':
raise UserError(gettext(
'cashbook_report.msg_invalid_dtype',
dtype = gettext('cashbook_report.msg_dtype_cashbook'),
))
# end EvaluationCashbookRel
class EvaluationTypeRel(ModelSQL):
'Evaluation Type Relation'
__name__ = 'cashbook_report.eval_type'
evaluation = fields.Many2One(string='Evaluation', required=True,
select=True, ondelete='CASCADE',
model_name='cashbook_report.evaluation')
dtype = fields.Many2One(string='Type', required=True,
select=True, ondelete='CASCADE',
model_name='cashbook.type')
# end EvaluationTypeRel
class EvaluationCurrencyRel(ModelSQL):
'Evaluation Currency Relation'
__name__ = 'cashbook_report.eval_currency'
evaluation = fields.Many2One(string='Evaluation', required=True,
select=True, ondelete='CASCADE',
model_name='cashbook_report.evaluation')
currency = fields.Many2One(string='Currency', required=True,
select=True, ondelete='CASCADE',
model_name='currency.currency')
# end EvaluationCurrencyRel