optimize search-queries, line: fix state-selection

This commit is contained in:
Frederik Jaeckel 2023-07-24 17:31:34 +02:00
parent 50ce7f5dbc
commit 0e30725d1f
6 changed files with 19 additions and 19 deletions

View file

@ -546,7 +546,7 @@ class Book(tree(separator='/'), Workflow, ModelSQL, ModelView):
'defbook', 'book1', 'book2', 'book3', 'defbook', 'book1', 'book2', 'book3',
'book4', 'book5']: 'book4', 'book5']:
cfg1 = ConfigUser.search([ cfg1 = ConfigUser.search([
('iduser.id', '=', book.owner.id), ('iduser', '=', book.owner.id),
('%s.id' % x, '=', book.id)]) ('%s.id' % x, '=', book.id)])
if len(cfg1) > 0: if len(cfg1) > 0:
to_write_config.extend([cfg1, {x: None}]) to_write_config.extend([cfg1, {x: None}])

14
line.py
View file

@ -23,7 +23,7 @@ sel_payee = [
('party.party', 'Party') ('party.party', 'Party')
] ]
sel_linetype = [ sel_linestate = [
('edit', 'Edit'), ('edit', 'Edit'),
('check', 'Checked'), ('check', 'Checked'),
('recon', 'Reconciled'), ('recon', 'Reconciled'),
@ -351,7 +351,7 @@ class Line(SecondCurrencyMixin, MemCacheIndexMx, Workflow, ModelSQL, ModelView):
# allow cashbook-line at range-limits # allow cashbook-line at range-limits
if Recon.search_count([ if Recon.search_count([
('state', 'in', ['check', 'done']), ('state', 'in', ['check', 'done']),
('cashbook.id', '=', line.cashbook.id), ('cashbook', '=', line.cashbook.id),
('date_from', '<', line.date), ('date_from', '<', line.date),
('date_to', '>', line.date)]) > 0: ('date_to', '>', line.date)]) > 0:
raise UserError(gettext( raise UserError(gettext(
@ -362,7 +362,7 @@ class Line(SecondCurrencyMixin, MemCacheIndexMx, Workflow, ModelSQL, ModelView):
# reconciliations exist # reconciliations exist
if Recon.search_count([ if Recon.search_count([
('state', 'in', ['check', 'done']), ('state', 'in', ['check', 'done']),
('cashbook.id', '=', line.cashbook.id), ('cashbook', '=', line.cashbook.id),
['OR', ['OR',
('date_from', '=', line.date), ('date_from', '=', line.date),
('date_to', '=', line.date)]]) > 1: ('date_to', '=', line.date)]]) > 1:
@ -727,7 +727,7 @@ class Line(SecondCurrencyMixin, MemCacheIndexMx, Workflow, ModelSQL, ModelView):
end_value = None end_value = None
recons = Reconciliation.search([ recons = Reconciliation.search([
('cashbook.id', '=', line.cashbook.id), ('cashbook', '=', line.cashbook.id),
('date_to', '<=', line2.date), ('date_to', '<=', line2.date),
('state', '=', 'done'), ('state', '=', 'done'),
], order=[('date_from', 'DESC')], limit=1) ], order=[('date_from', 'DESC')], limit=1)
@ -737,14 +737,14 @@ class Line(SecondCurrencyMixin, MemCacheIndexMx, Workflow, ModelSQL, ModelView):
('date', '<=', line2.date), ('date', '<=', line2.date),
['OR', ['OR',
('reconciliation', '=', None), ('reconciliation', '=', None),
('reconciliation.id', '!=', recons[0])], ('reconciliation', '!=', recons[0])],
]) ])
end_value = getattr(recons[0], 'end_%s' % field_name) end_value = getattr(recons[0], 'end_%s' % field_name)
return (query2, end_value) return (query2, end_value)
if line.cashbook: if line.cashbook:
query = [ query = [
('cashbook.id', '=', line.cashbook.id), ('cashbook', '=', line.cashbook.id),
] ]
balance = Decimal('0.0') balance = Decimal('0.0')
@ -754,7 +754,7 @@ class Line(SecondCurrencyMixin, MemCacheIndexMx, Workflow, ModelSQL, ModelView):
if line.reconciliation: if line.reconciliation:
if line.reconciliation.state == 'done': if line.reconciliation.state == 'done':
query.append( query.append(
('reconciliation.id', '=', line.reconciliation.id), ('reconciliation', '=', line.reconciliation.id),
) )
balance = getattr( balance = getattr(
line.reconciliation, 'start_%s' % field_name) line.reconciliation, 'start_%s' % field_name)

View file

@ -165,7 +165,7 @@ class Reconciliation(Workflow, ModelSQL, ModelView):
if Line.search_count([ if Line.search_count([
('date', '>', reconciliation.date_from), ('date', '>', reconciliation.date_from),
('date', '<', reconciliation.date_to), ('date', '<', reconciliation.date_to),
('cashbook.id', '=', reconciliation.cashbook.id), ('cashbook', '=', reconciliation.cashbook.id),
('state', 'not in', ['check', 'recon']), ('state', 'not in', ['check', 'recon']),
]) > 0: ]) > 0:
raise UserError(gettext( raise UserError(gettext(
@ -206,7 +206,7 @@ class Reconciliation(Workflow, ModelSQL, ModelView):
lines = Line.search([ lines = Line.search([
('date', '>=', reconciliation.date_from), ('date', '>=', reconciliation.date_from),
('date', '<=', reconciliation.date_to), ('date', '<=', reconciliation.date_to),
('cashbook.id', '=', reconciliation.cashbook.id), ('cashbook', '=', reconciliation.cashbook.id),
('reconciliation', '=', None), ('reconciliation', '=', None),
('state', 'in', ['check', 'recon']), ('state', 'in', ['check', 'recon']),
]) ])
@ -297,7 +297,7 @@ class Reconciliation(Workflow, ModelSQL, ModelView):
# deny if there are lines not linked to reconciliation # deny if there are lines not linked to reconciliation
if Line.search_count([ if Line.search_count([
('cashbook.id', '=', reconciliation.cashbook.id), ('cashbook', '=', reconciliation.cashbook.id),
('reconciliation', '=', None), ('reconciliation', '=', None),
['OR', ['OR',
[ # lines inside of date-range [ # lines inside of date-range
@ -387,7 +387,7 @@ class Reconciliation(Workflow, ModelSQL, ModelView):
if self.cashbook: if self.cashbook:
if self.date_from is not None: if self.date_from is not None:
reconciliations = Recon.search([ reconciliations = Recon.search([
('cashbook.id', '=', self.cashbook.id), ('cashbook', '=', self.cashbook.id),
('date_from', '<', self.date_from), ('date_from', '<', self.date_from),
], order=[('date_from', 'DESC')], limit=1) ], order=[('date_from', 'DESC')], limit=1)
if len(reconciliations) > 0: if len(reconciliations) > 0:
@ -439,7 +439,7 @@ class Reconciliation(Workflow, ModelSQL, ModelView):
# set date_from to date_to of predecessor # set date_from to date_to of predecessor
recons = Recon.search([ recons = Recon.search([
('cashbook.id', '=', id_cashbook), ('cashbook', '=', id_cashbook),
], order=[('date_to', 'DESC')], limit=1) ], order=[('date_to', 'DESC')], limit=1)
if len(recons) > 0: if len(recons) > 0:
values['date_from'] = recons[0].date_to values['date_from'] = recons[0].date_to
@ -448,7 +448,7 @@ class Reconciliation(Workflow, ModelSQL, ModelView):
# set date_to to day of last 'checked'-booking in selected cashbook # set date_to to day of last 'checked'-booking in selected cashbook
lines = Line.search([ lines = Line.search([
('cashbook.id', '=', id_cashbook), ('cashbook', '=', id_cashbook),
('state', '=', 'check'), ('state', '=', 'check'),
('reconciliation', '=', None), ('reconciliation', '=', None),
], order=[('date', 'DESC')], limit=1) ], order=[('date', 'DESC')], limit=1)

View file

@ -9,7 +9,7 @@ from trytond.pool import Pool
from trytond.pyson import Eval, If from trytond.pyson import Eval, If
from trytond.report import Report from trytond.report import Report
from trytond.i18n import gettext from trytond.i18n import gettext
from .line import sel_bookingtype, STATES, DEPENDS from .line import sel_bookingtype, sel_linestate, STATES, DEPENDS
from .book import sel_state_book from .book import sel_state_book
from .mixin import SecondCurrencyMixin, MemCacheIndexMx from .mixin import SecondCurrencyMixin, MemCacheIndexMx
@ -90,7 +90,7 @@ class SplitLine(SecondCurrencyMixin, MemCacheIndexMx, ModelSQL, ModelView):
selection=sel_bookingtype), 'on_change_with_bookingtype') selection=sel_bookingtype), 'on_change_with_bookingtype')
state = fields.Function(fields.Selection( state = fields.Function(fields.Selection(
string='State', readonly=True, string='State', readonly=True,
selection=sel_linetype), 'on_change_with_state') selection=sel_linestate), 'on_change_with_state')
cashbook = fields.Function(fields.Many2One( cashbook = fields.Function(fields.Many2One(
string='Cashbook', string='Cashbook',
readonly=True, states={'invisible': True}, model_name='cashbook.book'), readonly=True, states={'invisible': True}, model_name='cashbook.book'),

View file

@ -152,7 +152,7 @@ class EnterBookingWizard(Wizard):
'cashbooks': [x.id for x in Cashbook.search([ 'cashbooks': [x.id for x in Cashbook.search([
('state', '=', 'open'), ('state', '=', 'open'),
('btype', '!=', None), ('btype', '!=', None),
('owner.id', '=', Transaction().user), ('owner', '=', Transaction().user),
('id', 'in', book_ids), ('id', 'in', book_ids),
])], ])],
'bookingtype': getattr(self.start, 'bookingtype', 'out'), 'bookingtype': getattr(self.start, 'bookingtype', 'out'),

View file

@ -55,7 +55,7 @@ class RunCbReportStart(ModelView):
if self.cashbook: if self.cashbook:
recons = Recon2.search([ recons = Recon2.search([
('cashbook.id', '=', self.cashbook.id), ('cashbook', '=', self.cashbook.id),
], order=[('date_from', 'DESC')]) ], order=[('date_from', 'DESC')])
return [x.id for x in recons] return [x.id for x in recons]
@ -103,7 +103,7 @@ class RunCbReport(Wizard):
result['cashbook'] = result['cashbooks'][0] result['cashbook'] = result['cashbooks'][0]
recons = Recon2.search([ recons = Recon2.search([
('cashbook.id', '=', result['cashbook']), ('cashbook', '=', result['cashbook']),
], order=[('date_from', 'DESC')]) ], order=[('date_from', 'DESC')])
if len(recons) > 0: if len(recons) > 0:
result['reconciliations'] = [x.id for x in recons] result['reconciliations'] = [x.id for x in recons]