From 7a4fd94981910e89810b9a75443dd256f41556a0 Mon Sep 17 00:00:00 2001 From: Frederik Jaeckel Date: Mon, 24 Jul 2023 17:31:34 +0200 Subject: [PATCH] optimize search-queries, line: fix state-selection --- book.py | 2 +- line.py | 16 ++++++++-------- reconciliation.py | 12 ++++++------ splitline.py | 4 ++-- wizard_booking.py | 2 +- wizard_runreport.py | 4 ++-- 6 files changed, 20 insertions(+), 20 deletions(-) diff --git a/book.py b/book.py index 3e3fa0f..a4fd5a7 100644 --- a/book.py +++ b/book.py @@ -574,7 +574,7 @@ class Book(tree(separator='/'), Workflow, ModelSQL, ModelView): 'defbook', 'book1', 'book2', 'book3', 'book4', 'book5']: cfg1 = ConfigUser.search([ - ('iduser.id', '=', book.owner.id), + ('iduser', '=', book.owner.id), ('%s.id' % x, '=', book.id)]) if len(cfg1) > 0: to_write_config.extend([cfg1, {x: None}]) diff --git a/line.py b/line.py index 6a202f5..41c6c9a 100644 --- a/line.py +++ b/line.py @@ -23,7 +23,7 @@ sel_payee = [ ('party.party', 'Party') ] -sel_linetype = [ +sel_linestate = [ ('edit', 'Edit'), ('check', 'Checked'), ('recon', 'Reconciled'), @@ -197,7 +197,7 @@ class Line(SecondCurrencyMixin, MemCacheIndexMx, Workflow, ModelSQL, ModelView): state = fields.Selection( string='State', required=True, readonly=True, - selection=sel_linetype) + selection=sel_linestate) state_string = state.translated('state') state_cashbook = fields.Function(fields.Selection( string='State of Cashbook', @@ -375,7 +375,7 @@ class Line(SecondCurrencyMixin, MemCacheIndexMx, Workflow, ModelSQL, ModelView): # allow cashbook-line at range-limits if Recon.search_count([ ('state', 'in', ['check', 'done']), - ('cashbook.id', '=', line.cashbook.id), + ('cashbook', '=', line.cashbook.id), ('date_from', '<', line.date), ('date_to', '>', line.date)]) > 0: raise UserError(gettext( @@ -386,7 +386,7 @@ class Line(SecondCurrencyMixin, MemCacheIndexMx, Workflow, ModelSQL, ModelView): # reconciliations exist if Recon.search_count([ ('state', 'in', ['check', 'done']), - ('cashbook.id', '=', line.cashbook.id), + ('cashbook', '=', line.cashbook.id), ['OR', ('date_from', '=', line.date), ('date_to', '=', line.date)]]) > 1: @@ -751,7 +751,7 @@ class Line(SecondCurrencyMixin, MemCacheIndexMx, Workflow, ModelSQL, ModelView): end_value = None recons = Reconciliation.search([ - ('cashbook.id', '=', line.cashbook.id), + ('cashbook', '=', line.cashbook.id), ('date_to', '<=', line2.date), ('state', '=', 'done'), ], order=[('date_from', 'DESC')], limit=1) @@ -761,14 +761,14 @@ class Line(SecondCurrencyMixin, MemCacheIndexMx, Workflow, ModelSQL, ModelView): ('date', '<=', line2.date), ['OR', ('reconciliation', '=', None), - ('reconciliation.id', '!=', recons[0])], + ('reconciliation', '!=', recons[0])], ]) end_value = getattr(recons[0], 'end_%s' % field_name) return (query2, end_value) if line.cashbook: query = [ - ('cashbook.id', '=', line.cashbook.id), + ('cashbook', '=', line.cashbook.id), ] balance = Decimal('0.0') @@ -778,7 +778,7 @@ class Line(SecondCurrencyMixin, MemCacheIndexMx, Workflow, ModelSQL, ModelView): if line.reconciliation: if line.reconciliation.state == 'done': query.append( - ('reconciliation.id', '=', line.reconciliation.id), + ('reconciliation', '=', line.reconciliation.id), ) balance = getattr( line.reconciliation, 'start_%s' % field_name) diff --git a/reconciliation.py b/reconciliation.py index 044f85c..e27e0c9 100644 --- a/reconciliation.py +++ b/reconciliation.py @@ -183,7 +183,7 @@ class Reconciliation(Workflow, ModelSQL, ModelView): if Line.search_count([ ('date', '>', reconciliation.date_from), ('date', '<', reconciliation.date_to), - ('cashbook.id', '=', reconciliation.cashbook.id), + ('cashbook', '=', reconciliation.cashbook.id), ('state', 'not in', ['check', 'recon']), ]) > 0: raise UserError(gettext( @@ -224,7 +224,7 @@ class Reconciliation(Workflow, ModelSQL, ModelView): lines = Line.search([ ('date', '>=', reconciliation.date_from), ('date', '<=', reconciliation.date_to), - ('cashbook.id', '=', reconciliation.cashbook.id), + ('cashbook', '=', reconciliation.cashbook.id), ('reconciliation', '=', None), ('state', 'in', ['check', 'recon']), ]) @@ -315,7 +315,7 @@ class Reconciliation(Workflow, ModelSQL, ModelView): # deny if there are lines not linked to reconciliation if Line.search_count([ - ('cashbook.id', '=', reconciliation.cashbook.id), + ('cashbook', '=', reconciliation.cashbook.id), ('reconciliation', '=', None), ['OR', [ # lines inside of date-range @@ -405,7 +405,7 @@ class Reconciliation(Workflow, ModelSQL, ModelView): if self.cashbook: if self.date_from is not None: reconciliations = Recon.search([ - ('cashbook.id', '=', self.cashbook.id), + ('cashbook', '=', self.cashbook.id), ('date_from', '<', self.date_from), ], order=[('date_from', 'DESC')], limit=1) if len(reconciliations) > 0: @@ -457,7 +457,7 @@ class Reconciliation(Workflow, ModelSQL, ModelView): # set date_from to date_to of predecessor recons = Recon.search([ - ('cashbook.id', '=', id_cashbook), + ('cashbook', '=', id_cashbook), ], order=[('date_to', 'DESC')], limit=1) if len(recons) > 0: values['date_from'] = recons[0].date_to @@ -466,7 +466,7 @@ class Reconciliation(Workflow, ModelSQL, ModelView): # set date_to to day of last 'checked'-booking in selected cashbook lines = Line.search([ - ('cashbook.id', '=', id_cashbook), + ('cashbook', '=', id_cashbook), ('state', '=', 'check'), ('reconciliation', '=', None), ], order=[('date', 'DESC')], limit=1) diff --git a/splitline.py b/splitline.py index a09bd77..79e78df 100644 --- a/splitline.py +++ b/splitline.py @@ -9,7 +9,7 @@ from trytond.pool import Pool from trytond.pyson import Eval, If from trytond.report import Report 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 .mixin import SecondCurrencyMixin, MemCacheIndexMx @@ -90,7 +90,7 @@ class SplitLine(SecondCurrencyMixin, MemCacheIndexMx, ModelSQL, ModelView): selection=sel_bookingtype), 'on_change_with_bookingtype') state = fields.Function(fields.Selection( string='State', readonly=True, - selection=sel_linetype), 'on_change_with_state') + selection=sel_linestate), 'on_change_with_state') cashbook = fields.Function(fields.Many2One( string='Cashbook', readonly=True, states={'invisible': True}, model_name='cashbook.book'), diff --git a/wizard_booking.py b/wizard_booking.py index ef27f32..429deae 100644 --- a/wizard_booking.py +++ b/wizard_booking.py @@ -152,7 +152,7 @@ class EnterBookingWizard(Wizard): 'cashbooks': [x.id for x in Cashbook.search([ ('state', '=', 'open'), ('btype', '!=', None), - ('owner.id', '=', Transaction().user), + ('owner', '=', Transaction().user), ('id', 'in', book_ids), ])], 'bookingtype': getattr(self.start, 'bookingtype', 'out'), diff --git a/wizard_runreport.py b/wizard_runreport.py index 534f521..d2b4fc2 100644 --- a/wizard_runreport.py +++ b/wizard_runreport.py @@ -55,7 +55,7 @@ class RunCbReportStart(ModelView): if self.cashbook: recons = Recon2.search([ - ('cashbook.id', '=', self.cashbook.id), + ('cashbook', '=', self.cashbook.id), ], order=[('date_from', 'DESC')]) return [x.id for x in recons] @@ -103,7 +103,7 @@ class RunCbReport(Wizard): result['cashbook'] = result['cashbooks'][0] recons = Recon2.search([ - ('cashbook.id', '=', result['cashbook']), + ('cashbook', '=', result['cashbook']), ], order=[('date_from', 'DESC')]) if len(recons) > 0: result['reconciliations'] = [x.id for x in recons]