optimize searcher/sort, add search/sort to balance_ref

This commit is contained in:
Frederik Jaeckel 2023-12-29 14:51:39 +01:00
parent 5d8f924960
commit 5ef3a52fdc
6 changed files with 272 additions and 21 deletions

88
book.py
View file

@ -139,7 +139,7 @@ class Book(tree(separator='/'), Workflow, ModelSQL, ModelView):
states={
'invisible': ~Bool(Eval('company_currency')),
}, depends=['company_currency_digits', 'company_currency']),
'get_balance_cashbook')
'get_balance_cashbook', searcher='search_balance')
company_currency = fields.Function(fields.Many2One(
readonly=True,
string='Company Currency', states={'invisible': True},
@ -346,44 +346,92 @@ class Book(tree(separator='/'), Workflow, ModelSQL, ModelView):
)
return (query, tab_line)
@classmethod
def work_order_balance(cls, tables, field_name):
""" get order-query
"""
pool = Pool()
Book2 = pool.get('cashbook.book')
ValueStore = pool.get('cashbook.values')
context = Transaction().context
query_date = context.get('date', None)
table, _ = tables[None]
if query_date is not None:
if field_name == 'balance_ref':
raise UserError(gettext(
'cashbook.msg_nosearch_with_date',
fname=field_name, model=Book2.__name__))
(tab_book, tab2) = Book2.get_balance_of_cashbook_sql()
query = tab_book.select(
getattr(tab_book, field_name),
where=tab_book.cashbook == table.id)
return [query]
else:
tab_val = ValueStore.__table__()
tab_book = Book2.__table__()
query = tab_book.join(
tab_val,
condition=(
tab_book.id == tab_val.cashbook) & (
tab_val.field_name == field_name),
).select(
tab_val.numvalue,
where=tab_book.id == table.id)
return [query]
@staticmethod
def order_balance(tables):
""" order by balance
"""
Book2 = Pool().get('cashbook.book')
(tab_book, tab2) = Book2.get_balance_of_cashbook_sql()
table, _ = tables[None]
query = tab_book.select(
tab_book.balance,
where=tab_book.cashbook == table.id)
return [query]
return Book2.work_order_balance(tables, 'balance')
@staticmethod
def order_balance_all(tables):
""" order by balance-all
"""
Book2 = Pool().get('cashbook.book')
(tab_book, tab2) = Book2.get_balance_of_cashbook_sql()
table, _ = tables[None]
return Book2.work_order_balance(tables, 'balance_all')
query = tab_book.select(
tab_book.balance_all,
where=tab_book.cashbook == table.id)
return [query]
@staticmethod
def order_balance_ref(tables):
""" order by balance-all
"""
Book2 = Pool().get('cashbook.book')
return Book2.work_order_balance(tables, 'balance_ref')
@classmethod
def search_balance(cls, name, clause):
""" search in 'balance'
"""
(tab_line, tab2) = cls.get_balance_of_cashbook_sql()
ValueStore = Pool().get('cashbook.values')
Operator = fields.SQL_OPERATORS[clause[1]]
context = Transaction().context
query = tab_line.select(
tab_line.cashbook,
where=Operator(
getattr(tab_line, name), clause[2]))
return [('id', 'in', query)]
query_date = context.get('date', None)
if query_date is not None:
if name == 'balance_ref':
raise UserError(gettext(
'cashbook.msg_nosearch_with_date',
fname=name, model=cls.__name__))
(tab_line, tab2) = cls.get_balance_of_cashbook_sql()
query = tab_line.select(
tab_line.cashbook,
where=Operator(
getattr(tab_line, name), clause[2]))
return [('id', 'in', query)]
else:
value_query = ValueStore.search([
('field_name', '=', clause[0]),
('numvalue',) + tuple(clause[1:]),
],
query=True)
return [('value_store', 'in', value_query)]
@classmethod
def valuestore_delete_records(cls, records):