diff --git a/l10n_nl_xaf_auditfile_export/models/xaf_auditfile_export.py b/l10n_nl_xaf_auditfile_export/models/xaf_auditfile_export.py index 242b18a6a..8b11c66d0 100644 --- a/l10n_nl_xaf_auditfile_export/models/xaf_auditfile_export.py +++ b/l10n_nl_xaf_auditfile_export/models/xaf_auditfile_export.py @@ -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 @@ -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(