book/category: hierarchische sortierung, form optimiert

This commit is contained in:
Frederik Jaeckel 2022-09-22 15:40:23 +02:00
parent 0572e18b64
commit 7f1b826590
5 changed files with 76 additions and 62 deletions

10
book.py
View file

@ -13,6 +13,7 @@ from trytond.report import Report
from decimal import Decimal from decimal import Decimal
from sql.aggregate import Sum from sql.aggregate import Sum
from sql.conditionals import Case from sql.conditionals import Case
from .model import order_name_hierarchical
STATES = { STATES = {
@ -136,7 +137,7 @@ class Book(tree(separator='/'), Workflow, ModelSQL, ModelView):
@classmethod @classmethod
def __setup__(cls): def __setup__(cls):
super(Book, cls).__setup__() super(Book, cls).__setup__()
cls._order.insert(0, ('name', 'ASC')) cls._order.insert(0, ('rec_name', 'ASC'))
cls._order.insert(0, ('state', 'ASC')) cls._order.insert(0, ('state', 'ASC'))
t = cls.__table__() t = cls.__table__()
cls._sql_constraints.extend([ cls._sql_constraints.extend([
@ -225,6 +226,13 @@ class Book(tree(separator='/'), Workflow, ModelSQL, ModelView):
) )
return [query] return [query]
@staticmethod
def order_rec_name(tables):
""" order by pos
a recursive sorting
"""
return order_name_hierarchical('cashbook.book', tables)
def get_rec_name(self, name): def get_rec_name(self, name):
""" name, balance, state """ name, balance, state
""" """

View file

@ -10,39 +10,7 @@ from trytond.pyson import Eval, If, Bool
from trytond.exceptions import UserError from trytond.exceptions import UserError
from trytond.i18n import gettext from trytond.i18n import gettext
from sql.operators import Equal from sql.operators import Equal
from sql.functions import Function from .model import order_name_hierarchical
from sql import With, Literal
class ArrayApppend(Function):
""" sql: array_append
"""
__slots__ = ()
_function = 'ARRAY_APPEND'
# end ArrayApppend
class ArrayToString(Function):
""" sql: array_to_string
"""
__slots__ = ()
_function = 'ARRAY_TO_STRING'
# end ArrayToString
class Array(Function):
""" sql: array-type
"""
__slots__ = ()
_function = 'ARRAY'
def __str__(self):
return self._function + '[' + ', '.join(
map(self._format, self.args)) + ']'
# end Array
sel_categorytype = [ sel_categorytype = [
@ -128,28 +96,7 @@ class Category(tree(separator='/'), ModelSQL, ModelView):
""" order by pos """ order by pos
a recursive sorting a recursive sorting
""" """
Category2 = Pool().get('cashbook.category') return order_name_hierarchical('cashbook.category', tables)
tab_cat = Category2.__table__()
tab_cat2 = Category2.__table__()
table, _ = tables[None]
categories = With('id', 'name', 'name_path', recursive=True)
categories.query = tab_cat.select(
tab_cat.id, tab_cat.name, Array(tab_cat.name),
where = tab_cat.parent==None,
)
categories.query |= tab_cat2.join(categories,
condition=categories.id==tab_cat2.parent,
).select(
tab_cat2.id, tab_cat2.name, ArrayApppend(categories.name_path, tab_cat2.name),
)
categories.query.all_ = True
query = categories.select(
ArrayToString(categories.name_path, '/').as_('rec_name'),
where = table.id==categories.id,
with_ = [categories])
return [query]
@fields.depends('parent', '_parent_parent.cattype') @fields.depends('parent', '_parent_parent.cattype')
def on_change_with_parent_cattype(self, name=None): def on_change_with_parent_cattype(self, name=None):

View file

@ -5,6 +5,68 @@
from trytond.model import MultiValueMixin, ValueMixin, fields, Unique from trytond.model import MultiValueMixin, ValueMixin, fields, Unique
from trytond.transaction import Transaction from trytond.transaction import Transaction
from trytond.pool import Pool
from sql import With, Literal
from sql.functions import Function
class ArrayApppend(Function):
""" sql: array_append
"""
__slots__ = ()
_function = 'ARRAY_APPEND'
# end ArrayApppend
class ArrayToString(Function):
""" sql: array_to_string
"""
__slots__ = ()
_function = 'ARRAY_TO_STRING'
# end ArrayToString
class Array(Function):
""" sql: array-type
"""
__slots__ = ()
_function = 'ARRAY'
def __str__(self):
return self._function + '[' + ', '.join(
map(self._format, self.args)) + ']'
# end Array
def order_name_hierarchical(model_name, tables):
""" order by pos
a recursive sorting
"""
Model2 = Pool().get(model_name)
tab_mod = Model2.__table__()
tab_mod2 = Model2.__table__()
table, _ = tables[None]
lines = With('id', 'name', 'name_path', recursive=True)
lines.query = tab_mod.select(
tab_mod.id, tab_mod.name, Array(tab_mod.name),
where = tab_mod.parent==None,
)
lines.query |= tab_mod2.join(lines,
condition=lines.id==tab_mod2.parent,
).select(
tab_mod2.id, tab_mod2.name, ArrayApppend(lines.name_path, tab_mod2.name),
)
lines.query.all_ = True
query = lines.select(
ArrayToString(lines.name_path, '/').as_('rec_name'),
where = table.id==lines.id,
with_ = [lines])
return [query]
class UserValueMixin(ValueMixin): class UserValueMixin(ValueMixin):

View file

@ -44,8 +44,6 @@ full copyright notices and license terms. -->
<field name="start_date"/> <field name="start_date"/>
<label name="currency"/> <label name="currency"/>
<field name="currency"/> <field name="currency"/>
<label id="phaccount" colspan="2" string=" "/>
</page> </page>
<page id="pgperm" string="Owner and Authorizeds" col="4"> <page id="pgperm" string="Owner and Authorizeds" col="4">
<label name="owner"/> <label name="owner"/>

View file

@ -2,17 +2,16 @@
<!-- This file is part of the cashbook-module from m-ds for Tryton. <!-- 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 The COPYRIGHT file at the top level of this repository contains the
full copyright notices and license terms. --> full copyright notices and license terms. -->
<form col="6"> <form col="4">
<label name="name"/> <label name="name"/>
<field name="name"/> <field name="name"/>
<label name="cattype"/> <label name="cattype"/>
<field name="cattype"/> <field name="cattype"/>
<label id="phaccount" colspan="2" string=" "/>
<label name="description"/> <label name="description"/>
<field name="description" colspan="5"/> <field name="description" colspan="3"/>
<notebook colspan="6"> <notebook colspan="4">
<page string="General Information" id="general" col="4"> <page string="General Information" id="general" col="4">
<label name="company"/> <label name="company"/>
<field name="company"/> <field name="company"/>