Skip to content

Commit

Permalink
[IMP] account: add cumulated balance for general ledger list view
Browse files Browse the repository at this point in the history
  • Loading branch information
william-andre committed Oct 10, 2019
1 parent 9784236 commit 1d304ce
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
37 changes: 37 additions & 0 deletions addons/account/models/account_move.py
Original file line number Diff line number Diff line change
Expand Up @@ -2386,6 +2386,10 @@ class AccountMoveLine(models.Model):
currency_field='company_currency_id',
compute='_compute_balance',
help="Technical field holding the debit - credit in order to open meaningful graph views from reports")
cumulated_balance = fields.Monetary(string='Cumulated Balance', store=False,
currency_field='company_currency_id',
compute='_compute_cumulated_balance',
help="Technical field holding the debit - credit in order to open meaningful graph views from reports")
amount_currency = fields.Monetary(string='Amount in Currency', store=True, copy=True,
help="The amount expressed in an optional other currency if it is a multi-currency entry.")
price_subtotal = fields.Monetary(string='Subtotal', store=True, readonly=True,
Expand Down Expand Up @@ -2886,6 +2890,39 @@ def _compute_balance(self):
for line in self:
line.balance = line.debit - line.credit

@api.model
def search_read(self, domain=None, fields=None, offset=0, limit=None, order=None):
return super(AccountMoveLine, self.with_context(domain=domain or [], order_by=order)).search_read(domain, fields, offset, limit, order)

def _compute_cumulated_balance(self):
# invert the order by
order_by = [x.strip().split(' ') for x in (self.env.context['order_by'] or self._order).split(',')]
for o in order_by:
o[0] = 'account_move_line.' + o[0]
if len(o) == 1:
o += ['DESC']
elif o[1].upper() == 'ASC':
o[1] = 'DESC'
else:
o[1] = "ASC"
order_by = ', '.join([' '.join(o) for o in order_by])

# get the where clause
domain = [x if 'date' not in x[0] else ['id', '>', 0] for x in self.env.context['domain']]
from_clause, where_clause, where_clause_params = self._where_calc(domain).get_sql()
sql = """
SELECT account_move_line.id, SUM(account_move_line.debit-account_move_line.credit) OVER (
ORDER BY %(order_by)s
ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
)
FROM %(from)s
WHERE %(where)s
""" % {'from': from_clause, 'where': where_clause, 'order_by': order_by}
self.env.cr.execute(sql, where_clause_params)
result = {r[0]: r[1] for r in self.env.cr.fetchall()}
for record in self:
record.cumulated_balance = result[record.id]

@api.depends('debit', 'credit', 'account_id', 'amount_currency', 'currency_id', 'matched_debit_ids', 'matched_credit_ids', 'matched_debit_ids.amount', 'matched_credit_ids.amount', 'move_id.state', 'company_id')
def _amount_residual(self):
""" Computes the residual amount of a move line from a reconcilable account in the company currency and the line's currency.
Expand Down
4 changes: 4 additions & 0 deletions addons/account/views/account_move_views.xml
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,10 @@
<attribute name="optional">hide</attribute>
<attribute name="readonly">1</attribute>
</field>
<field name="credit" position="after">
<field name="balance" optional="hide"/>
<field name="cumulated_balance" optional="show"/>
</field>
</field>
</record>

Expand Down

0 comments on commit 1d304ce

Please sign in to comment.