Skip to content

Commit

Permalink
fixup! [IMP] calculate opening debit, credit from balance of opening …
Browse files Browse the repository at this point in the history
…lines
  • Loading branch information
hbrunn committed Mar 19, 2024
1 parent 16b4b78 commit 98272e5
Showing 1 changed file with 18 additions and 36 deletions.
54 changes: 18 additions & 36 deletions l10n_nl_xaf_auditfile_export/models/xaf_auditfile_export.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,46 +251,21 @@ def get_taxes(self):
('company_id', '=', self.company_id.id),
])

@api.multi
def get_ob_sql_fragment(self):
"""
return a from clause and a where clause selecting the
relevant lines for the opening balance
"""
return self.env.cr.mogrify(
"from account_move_line l, account_account a, "
" account_move m, account_account_type t "
"where a.user_type_id = t.id "
"and a.id = l.account_id and l.date < %s "
"and l.move_id = m.id and m.state = 'posted' "
"and l.company_id=%s "
"and t.include_initial_balance = true ",
(self.date_start, self.company_id.id),
).decode('utf8')

@api.multi
def get_ob_totals(self):
"""return totals of opening balance"""
self.env.cr.execute(
# pylint: disable=sql-injection
"select sum(l.balance), count(distinct a.id) " +
self.get_ob_sql_fragment() +
"and l.balance < 0"
)
row = self.env.cr.fetchall()[0]
result = dict(
credit=abs(round(row[0] or 0.0, 2)),
count=row[1] or 0,
)
self.env.cr.execute(
# pylint: disable=sql-injection
"select sum(l.balance), count(distinct a.id) " +
self.get_ob_sql_fragment() +
"and l.balance > 0"
credit=0.0,
debit=0.0,
count=0
)
row = self.env.cr.fetchall()[0]
result['debit'] = round(row[0] or 0.0, 2)
result['count'] += row[1] or 0
for line in self.get_ob_lines():
balance = line['balance']
if balance > 0:
result['debit'] += balance
else:
result['credit'] -= balance
result['count'] += 1
return result

@api.multi
Expand All @@ -299,8 +274,15 @@ def get_ob_lines(self):
self.env.cr.execute(
# pylint: disable=sql-injection
"select a.id, a.code, sum(l.balance) " +
self.get_ob_sql_fragment() +
"from account_move_line l, account_account a, "
" account_move m, account_account_type t "
"where a.user_type_id = t.id "
"and a.id = l.account_id and l.date < %s "
"and l.move_id = m.id and m.state = 'posted' "
"and l.company_id=%s "
"and t.include_initial_balance = true "
"group by a.id, a.code",
(self.date_start, self.company_id.id),
)
for result in self.env.cr.fetchall():
yield dict(
Expand Down

0 comments on commit 98272e5

Please sign in to comment.