kategorie: sperrt 'Typ' wenn parent existiert, korrekte Anpassung

der sub-kategorien bei Änderung + test
Line: Kategoriefeld prüft Inhalt + tests
This commit is contained in:
Frederik Jaeckel 2022-08-11 11:06:28 +02:00
parent 52ffa0536e
commit 8fd6e0d339
7 changed files with 137 additions and 46 deletions

View file

@ -6,6 +6,7 @@
from trytond.model import ModelView, ModelSQL, fields, Unique, tree, sequence_ordered
from trytond.transaction import Transaction
from trytond.pool import Pool
from trytond.pyson import Eval, If, Bool
from trytond.exceptions import UserError
from trytond.i18n import gettext
@ -23,7 +24,15 @@ class Category(tree(separator='/'), sequence_ordered(), ModelSQL, ModelView):
name = fields.Char(string='Name', required=True, translate=True)
description = fields.Char(string='Description', translate=True)
cattype = fields.Selection(string='Type', required=True,
help='Type of Category', selection=sel_categorytype)
help='Type of Category', selection=sel_categorytype,
states={'readonly': Bool(Eval('parent_cattype'))},
domain=[If(Bool(Eval('parent_cattype')),
('cattype', '=', Eval('parent_cattype', '')),
())],
depends=['parent_cattype'])
parent_cattype = fields.Function(fields.Char(string='Parent Category Type',
readonly=True, states={'invisible': True}),
'on_change_with_parent_cattype')
account = fields.Many2One(string='Account', select=True,
model_name='account.account', ondelete='RESTRICT')
@ -98,6 +107,13 @@ class Category(tree(separator='/'), sequence_ordered(), ModelSQL, ModelView):
('account.code',) + tuple(clause[1:]),
]
@fields.depends('parent', '_parent_parent.cattype')
def on_change_with_parent_cattype(self, name=None):
""" get type of parent category or None
"""
if self.parent:
return self.parent.cattype
@fields.depends('account')
def on_change_with_account_code(self, name=None):
""" get code of account
@ -111,34 +127,56 @@ class Category(tree(separator='/'), sequence_ordered(), ModelSQL, ModelView):
"""
return [('account.code',) + tuple(clause[1:])]
@classmethod
def check_category_hierarchy(cls, categories):
""" check if current category-type is equal to parent
"""
for category in categories:
if category.parent:
if category.parent.cattype != category.cattype:
raise UserError(gettext(
'cashbook.msg_category_type_not_like_parent',
parentname = category.parent.rec_name,
catname = category.rec_name,
))
@classmethod
def create(cls, vlist):
""" add debit/credit
"""
records = super(Category, cls).create(vlist)
cls.check_category_hierarchy(records)
return records
@classmethod
def write(cls, *args):
""" parent.cattape == cattype,
update sub-categories
"""
to_write = []
actions = iter(args)
to_check = []
to_write = []
to_write2 = []
for categories, values in zip(actions, actions):
to_write2.extend([categories, values])
if 'cattype' in values.keys():
for category in categories:
if category.parent:
if category.parent.cattype != values['cattype']:
raise UserError(gettext(
'cashbook.msg_category_type_not_like_parent',
parentname = category.parent.rec_name,
catname = category.rec_name,
))
cats = Category.search([('parent', 'child_of', [x.id for x in categories])])
# update sub-categories
cats = Category.search([
('parent', 'child_of', [x.id for x in categories])
])
if len(cats) > 0:
to_write.extend([
cats,
{
'cattype': values['cattype'],
}])
to_check.extend(categories)
to_check.extend(cats)
super(Category, cls).write(*args)
if len(to_write) > 0:
print('\n## to_write:',to_write)
Category.write(*to_write)
# add category-updates after regulary writes
to_write2.extend(to_write)
super(Category, cls).write(*to_write2)
cls.check_category_hierarchy(to_check)
# end Category