line: zeilen-saldo optimiert
This commit is contained in:
parent
f76f91b35a
commit
bb24a94cd1
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', \
|
@fields.depends('id', 'date', 'cashbook', \
|
||||||
'_parent_cashbook.start_balance', '_parent_cashbook.id',\
|
'_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):
|
def on_change_with_balance(self, name=None):
|
||||||
""" compute balance until current line, with current sort order,
|
""" compute balance until current line, with current sort order,
|
||||||
try to use a reconciliation as start to speed up calculation
|
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')
|
Reconciliation = pool.get('cashbook.recon')
|
||||||
Line = pool.get('cashbook.line')
|
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:
|
if self.cashbook:
|
||||||
query = [
|
query = [
|
||||||
('cashbook.id', '=', self.cashbook.id),
|
('cashbook.id', '=', self.cashbook.id),
|
||||||
|
@ -567,28 +594,29 @@ class Line(Workflow, ModelSQL, ModelView):
|
||||||
# get existing reconciliation, starting before current line
|
# get existing reconciliation, starting before current line
|
||||||
# this will speed up calculation of by-line-balance
|
# this will speed up calculation of by-line-balance
|
||||||
if self.date is not None:
|
if self.date is not None:
|
||||||
recons = Reconciliation.search([
|
if self.reconciliation:
|
||||||
('cashbook.id', '=', self.cashbook.id),
|
if self.reconciliation.state == 'done':
|
||||||
('date_from', '<=', self.date),
|
query.append(
|
||||||
('state', '=', 'done'),
|
('reconciliation.id', '=', self.reconciliation.id),
|
||||||
], order=[('date_from', 'DESC')], limit=1)
|
)
|
||||||
if len(recons) > 0:
|
balance = self.reconciliation.start_amount
|
||||||
query.extend([
|
else :
|
||||||
['OR',
|
(query2, balance) = get_from_last_recon(self)
|
||||||
('date', '>', recons[0].date_from),
|
query.extend(query2)
|
||||||
[
|
else :
|
||||||
('date', '=', recons[0].date_from),
|
(query2, balance) = get_from_last_recon(self)
|
||||||
('reconciliation.id', '=',recons[0].id),
|
query.extend(query2)
|
||||||
],
|
|
||||||
]
|
|
||||||
])
|
|
||||||
balance = recons[0].start_amount
|
|
||||||
|
|
||||||
|
print('-- 1:', (datetime.now() - dt1))
|
||||||
|
|
||||||
|
print('-- 2:', (datetime.now() - dt1), ', query:', query)
|
||||||
lines = Line.search(query)
|
lines = Line.search(query)
|
||||||
|
print('-- 3:', (datetime.now() - dt1), ', lines:',len(lines))
|
||||||
for line in lines:
|
for line in lines:
|
||||||
balance += line.credit - line.debit
|
balance += line.credit - line.debit
|
||||||
if line.id == self.id:
|
if line.id == self.id:
|
||||||
break
|
break
|
||||||
|
print('-- 4:', (datetime.now() - dt1))
|
||||||
return balance
|
return balance
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
|
|
Loading…
Reference in a new issue