Begin/end balance stat graph

- Anytime begin at account creation datetime
- Others doesn't take care of account creation
- Add check to avoid to bug on actions list length
This commit is contained in:
Aurélien Delobelle 2017-04-03 16:07:31 +02:00
parent 769c37634d
commit 87bc90ec8b

View file

@ -2186,7 +2186,22 @@ class AccountStatBalance(PkUrlMixin, JSONDetailView):
# autre passe)
# }
actions = [
actions = []
actions.append({
'at': (begin_date or account.created_at).isoformat(),
'amount': 0,
'label': 'début',
'balance': 0,
})
actions.append({
'at': (end_date or timezone.now()).isoformat(),
'amount': 0,
'label': 'fin',
'balance': 0,
})
actions += [
{
'at': ope_grp.at.isoformat(),
'amount': ope_grp.amount,
@ -2210,11 +2225,12 @@ class AccountStatBalance(PkUrlMixin, JSONDetailView):
]
# Maintenant on trie la liste des actions par ordre du plus récent
# an plus ancien et on met à jour la balance
actions = sorted(actions, key=lambda k: k['at'], reverse=True)
actions[0]['balance'] = account.balance
for i in range(len(actions)-1):
actions[i+1]['balance'] = \
actions[i]['balance'] - actions[i+1]['amount']
if len(actions) > 1:
actions = sorted(actions, key=lambda k: k['at'], reverse=True)
actions[0]['balance'] = account.balance
for i in range(len(actions)-1):
actions[i+1]['balance'] = \
actions[i]['balance'] - actions[i+1]['amount']
return actions
def get_context_data(self, *args, **kwargs):