From bb24a94cd188fdf7b7bb9962c36a015f3aa2a376 Mon Sep 17 00:00:00 2001 From: Frederik Jaeckel Date: Fri, 2 Sep 2022 16:04:31 +0200 Subject: [PATCH] line: zeilen-saldo optimiert --- line.py | 62 +++++++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 45 insertions(+), 17 deletions(-) diff --git a/line.py b/line.py index f8de1e9..2333dd6 100644 --- a/line.py +++ b/line.py @@ -549,7 +549,8 @@ class Line(Workflow, ModelSQL, ModelView): @fields.depends('id', 'date', 'cashbook', \ '_parent_cashbook.start_balance', '_parent_cashbook.id',\ - 'reconciliation', '_parent_reconciliation.start_amount') + 'reconciliation', '_parent_reconciliation.start_amount', + '_parent_reconciliation.state') def on_change_with_balance(self, name=None): """ compute balance until current line, with current sort order, try to use a reconciliation as start to speed up calculation @@ -558,6 +559,32 @@ class Line(Workflow, ModelSQL, ModelView): Reconciliation = pool.get('cashbook.recon') Line = pool.get('cashbook.line') + def get_from_last_recon(line2): + """ search last reconciliation in state 'done', + generate query + """ + query2 = [] + + recons = Reconciliation.search([ + ('cashbook.id', '=', self.cashbook.id), + ('date_to', '<=', line2.date), + ('state', '=', 'done'), + ], order=[('date_from', 'DESC')], limit=1) + if len(recons) > 0: + query2.append([ + ('date', '>=', recons[0].date_to), + ('date', '<=', line2.date), + ['OR', + ('reconciliation', '=', None), + ('reconciliation.id', '!=', recons[0]), + ], + ]) + return (query2, recons[0].end_amount) + + from datetime import datetime + dt1 = datetime.now() + print('\n## on_change_with_balance', dt1) + if self.cashbook: query = [ ('cashbook.id', '=', self.cashbook.id), @@ -567,28 +594,29 @@ class Line(Workflow, ModelSQL, ModelView): # get existing reconciliation, starting before current line # this will speed up calculation of by-line-balance if self.date is not None: - recons = Reconciliation.search([ - ('cashbook.id', '=', self.cashbook.id), - ('date_from', '<=', self.date), - ('state', '=', 'done'), - ], order=[('date_from', 'DESC')], limit=1) - if len(recons) > 0: - query.extend([ - ['OR', - ('date', '>', recons[0].date_from), - [ - ('date', '=', recons[0].date_from), - ('reconciliation.id', '=',recons[0].id), - ], - ] - ]) - balance = recons[0].start_amount + if self.reconciliation: + if self.reconciliation.state == 'done': + query.append( + ('reconciliation.id', '=', self.reconciliation.id), + ) + balance = self.reconciliation.start_amount + else : + (query2, balance) = get_from_last_recon(self) + query.extend(query2) + else : + (query2, balance) = get_from_last_recon(self) + query.extend(query2) + print('-- 1:', (datetime.now() - dt1)) + + print('-- 2:', (datetime.now() - dt1), ', query:', query) lines = Line.search(query) + print('-- 3:', (datetime.now() - dt1), ', lines:',len(lines)) for line in lines: balance += line.credit - line.debit if line.id == self.id: break + print('-- 4:', (datetime.now() - dt1)) return balance @classmethod