[FIX] account: aged partner balance fixed. Work at a given date, in case of partial reconciliation, fixed totals too
This commit is contained in:
@@ -61,8 +61,8 @@ class ReportAgedPartnerBalance(models.AbstractModel):
|
||||
AND (COALESCE(l.date_maturity,l.date) > %s)\
|
||||
AND (l.partner_id IN %s)\
|
||||
AND l.reconciled IS FALSE\
|
||||
AND (l.date <= %s)\
|
||||
GROUP BY l.partner_id', (tuple(move_state), tuple(account_type), date_from, tuple(partner_ids), date_from,))
|
||||
AND l.date <= %s \
|
||||
GROUP BY l.partner_id', (tuple(move_state), tuple(account_type), date_from, tuple(partner_ids), date_from))
|
||||
partner_totals = cr.fetchall()
|
||||
for partner_id, amount in partner_totals:
|
||||
future_past[partner_id] = amount
|
||||
@@ -85,50 +85,29 @@ class ReportAgedPartnerBalance(models.AbstractModel):
|
||||
args_list += (form[str(i)]['stop'],)
|
||||
args_list += (date_from,)
|
||||
|
||||
cr.execute('''SELECT l.partner_id, SUM(l.debit - l.credit), l.id
|
||||
query = '''SELECT l.id
|
||||
FROM account_move_line AS l, account_account, account_move am
|
||||
WHERE (l.account_id = account_account.id) AND (l.move_id = am.id)
|
||||
AND (am.state IN %s)
|
||||
AND (account_account.internal_type IN %s)
|
||||
AND (l.partner_id IN %s)
|
||||
AND l.reconciled IS FALSE
|
||||
AND ''' + dates_query + '''
|
||||
AND (l.date <= %s)
|
||||
GROUP BY l.partner_id, l.id''', args_list)
|
||||
partners_partial = cr.fetchall()
|
||||
partners_amount = dict((partner_id, 0) for partner_id, amount, line_id in partners_partial)
|
||||
|
||||
for partner_id, amount, line_id in partners_partial:
|
||||
partial_reconcile_ids = []
|
||||
line = self.env['account.move.line'].browse(line_id)
|
||||
GROUP BY l.partner_id, l.id'''
|
||||
cr.execute(query, args_list)
|
||||
partners_amount = {}
|
||||
aml_ids = cr.fetchall()
|
||||
aml_ids = aml_ids and aml_ids[0] or []
|
||||
for line in self.env['account.move.line'].browse(aml_ids):
|
||||
if line.partner_id.id not in partners_amount:
|
||||
partners_amount[line.partner_id.id] = 0.0
|
||||
line_amount = line.amount_residual
|
||||
for partial_line in (line.matched_debit_ids + line.matched_credit_ids):
|
||||
if not partial_line.credit_move_id.id in partial_reconcile_ids:
|
||||
partial_reconcile_ids.append(partial_line.credit_move_id.id)
|
||||
if not partial_line.debit_move_id.id in partial_reconcile_ids:
|
||||
partial_reconcile_ids.append(partial_line.debit_move_id.id)
|
||||
if partial_reconcile_ids:
|
||||
# in case of partial reconciliation, we want to keep the left amount in the oldest period
|
||||
cr.execute('''SELECT MIN(COALESCE(date_maturity, date)) from account_move_line where id = %s''', (line_id,))
|
||||
date = cr.fetchall()
|
||||
partial = False
|
||||
if 'BETWEEN' in dates_query:
|
||||
partial = date and args_list[-3] <= date[0][0] <= args_list[-2]
|
||||
elif '>=' in dates_query:
|
||||
partial = date and date[0][0] >= form[str(i)]['start']
|
||||
else:
|
||||
partial = date and date[0][0] <= form[str(i)]['stop']
|
||||
if partial:
|
||||
# partial reconcilation
|
||||
limit_date = 'COALESCE(l.date_maturity, l.date) <= %s'
|
||||
cr.execute('''SELECT SUM(l.debit - l.credit)
|
||||
FROM account_move_line AS l, account_move AS am
|
||||
WHERE l.move_id = am.id AND am.state IN %s
|
||||
AND l.id IN %s
|
||||
AND ''' + limit_date, (tuple(move_state), tuple(partial_reconcile_ids), date_from))
|
||||
unreconciled_amount = cr.fetchall()
|
||||
partners_amount[partner_id] += unreconciled_amount[0][0]
|
||||
else:
|
||||
partners_amount[partner_id] += amount
|
||||
if partial_line.create_date > date_from + ' 23:59:59':
|
||||
line_amount = line.balance
|
||||
break
|
||||
|
||||
partners_amount[line.partner_id.id] += line_amount
|
||||
history.append(partners_amount)
|
||||
|
||||
for partner in partners:
|
||||
@@ -148,12 +127,9 @@ class ReportAgedPartnerBalance(models.AbstractModel):
|
||||
# Adding counter
|
||||
self.total_account[(i)] = self.total_account[(i)] + (during and during[0] or 0)
|
||||
values[str(i)] = during and during[0] or 0.0
|
||||
total = False
|
||||
if totals.has_key( partner['id'] ):
|
||||
total = [ totals[partner['id']] ]
|
||||
values['total'] = total and total[0] or 0.0
|
||||
values['total'] = sum([values['direction']] + [values[str(i)] for i in range(5)])
|
||||
## Add for total
|
||||
self.total_account[(i+1)] = self.total_account[(i+1)] + (total and total[0] or 0.0)
|
||||
self.total_account[(i + 1)] += values['total']
|
||||
values['name'] = partner['name']
|
||||
|
||||
res.append(values)
|
||||
|
||||
Reference in New Issue
Block a user