line: zeilen-saldo optimiert
This commit is contained in:
parent
ba3892aa03
commit
8abe8fc164
1 changed files with 45 additions and 17 deletions
62
line.py
62
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
|
||||
|
|
Loading…
Reference in a new issue