From 7070129addac805f904e00f90723809341d010f2 Mon Sep 17 00:00:00 2001 From: Qwann Date: Wed, 21 Dec 2016 11:51:08 +0100 Subject: [PATCH] evolution de la balance\n premier jet --- kfet/templates/kfet/account_stat_balance.html | 52 ++++++ kfet/urls.py | 19 +++ kfet/views.py | 160 +++++++++++++++++- 3 files changed, 228 insertions(+), 3 deletions(-) create mode 100644 kfet/templates/kfet/account_stat_balance.html diff --git a/kfet/templates/kfet/account_stat_balance.html b/kfet/templates/kfet/account_stat_balance.html new file mode 100644 index 00000000..54d18505 --- /dev/null +++ b/kfet/templates/kfet/account_stat_balance.html @@ -0,0 +1,52 @@ + + + + + + + diff --git a/kfet/urls.py b/kfet/urls.py index 73437fb1..53f593c6 100644 --- a/kfet/urls.py +++ b/kfet/urls.py @@ -78,6 +78,25 @@ urlpatterns = [ views.AccountStatLastDay.as_view(), name = 'kfet.account.stat.last.day'), + url('^accounts/(?P.{3})/stat/balance/$', + views.AccountStatBalanceAll.as_view(), + name = 'kfet.account.stat.balance'), + url('^accounts/(?P.{3})/stat/balance/month/$', + views.AccountStatBalanceMonth.as_view(), + name = 'kfet.account.stat.balance.month'), + url('^accounts/(?P.{3})/stat/balance/treemonths/$', + views.AccountStatBalanceThreeMonths.as_view(), + name = 'kfet.account.stat.balance.treemonths'), + url('^accounts/(?P.{3})/stat/balance/sixmonths/$', + views.AccountStatBalanceSixMonths.as_view(), + name = 'kfet.account.stat.balance.sixmonths'), + url('^accounts/(?P.{3})/stat/balance/year/$', + views.AccountStatBalanceYear.as_view(), + name = 'kfet.account.stat.balance.year'), + url('^accounts/(?P.{3})/stat/balance/anytime/$', + views.AccountStatBalanceAnytime.as_view(), + name = 'kfet.account.stat.balance.anytime'), + # ----- # Checkout urls # ----- diff --git a/kfet/views.py b/kfet/views.py index 33afe7bd..ca24dbc4 100644 --- a/kfet/views.py +++ b/kfet/views.py @@ -2025,13 +2025,13 @@ class ObjectResumeStat(DetailView): stat_labels = ['stat_1', 'stat_2'] stat_urls = ['url_1', 'url_2'] + # sert à renverser les urls + # utile de le surcharger quand l'url prend d'autres arguments que l'id def get_object_url_kwargs(self, **kwargs): return {'pk': self.object.id} def get_context_data(self, **kwargs): - # On hérite - # Pas besoin, c'est essentiellement inutile - # context = super(ObjectResumeStat, self).get_context_data(**kwargs) + # On hérite pas object_id = self.object.id context = {} stats = {} @@ -2053,6 +2053,160 @@ class ObjectResumeStat(DetailView): return context +# ----------------------- +# Evolution Balance perso +# ----------------------- +ID_PREFIX_ACC_BALANCE = "balance_acc" +ID_PREFIX_ACC_BALANCE_MONTH = "balance_month_acc" +ID_PREFIX_ACC_BALANCE_THREE_MONTHS = "balance_three_months_acc" +ID_PREFIX_ACC_BALANCE_SIX_MONTHS = "balance_six_months_acc" +ID_PREFIX_ACC_BALANCE_YEAR = "balance_year_acc" +ID_PREFIX_ACC_BALANCE_ANYTIME = "balance_anytime_acc" + + +# Un résumé de toutes les vues ArticleStatBalance +# NE REND PAS DE JSON +class AccountStatBalanceAll(ObjectResumeStat): + model = Account + context_object_name = 'account' + trigramme_url_kwarg = 'trigramme' + id_prefix = ID_PREFIX_ACC_BALANCE + nb_stat = 5 + nb_default = 4 + stat_labels = ["Tout le temps", "1 an", "6 mois", "3 mois", "30 jours"] + stat_urls = ['kfet.account.stat.balance.anytime', + 'kfet.account.stat.balance.year', + 'kfet.account.stat.balance.sixmonths', + 'kfet.account.stat.balance.treemonths', + 'kfet.account.stat.balance.month'] + + def get_object(self, **kwargs): + trigramme = self.kwargs.get(self.trigramme_url_kwarg) + return get_object_or_404(Account, trigramme=trigramme) + + def get_object_url_kwargs(self, **kwargs): + return {'trigramme': self.object.trigramme} + + +# Rend un graphe (ou un json) de l'évolution de la balance personelle +# entre begin_date et end_date +# prend en compte les opérations et les transferts +# ne prend pas en compte les autorisations de négatif (TODO?) +class AccountStatBalance(HybridDetailView): + model = Account + trigramme_url_kwarg = 'trigramme' + template_name = 'kfet/account_stat_balance.html' + context_object_name = 'account' + begin_date = this_morning() + end_date = timezone.now() + id_prefix = "lol" + + def get_object(self, **kwargs): + trigramme = self.kwargs.get(self.trigramme_url_kwarg) + return get_object_or_404(Account, trigramme=trigramme) + + def get_changes_list(self, **kwargs): + account = self.object + # On récupère les opérations + opgroups = list(OperationGroup.objects + .filter(on_acc=account) + .filter(at__gte=self.begin_date) + .filter(at__lte=self.end_date) + ) + # On récupère les transferts reçus + received_transfers = list(Transfer.objects + .filter(to_acc=account) + .filter(canceled_at=None) + .filter(transfers__at__gte=self.begin_date) + .filter(transfers__at__lte=self.end_date) + ) + # On récupère les transferts émis + emitted_transfers = list(Transfer.objects + .filter(to_acc=account) + .filter(canceled_at=None) + .filter(transfers__at__gte=self.begin_date) + .filter(transfers__at__lte=self.end_date) + ) + # On transforme tout ça en une liste de dictionnaires sous la forme + # {'at': date, + # 'amount': changement de la balance (négatif si diminue la balance, + # positif si l'augmente), + # 'label': text descriptif, + # 'balance': état de la balance après l'action (0 pour le moment, + # sera mis à jour lors d'une + # autre passe) + # } + actions=[] + for op in opgroups: + action = { + 'at': op.at, + 'amount': op.amount, + 'label': "opération", #TODO + 'balance': 0, + } + actions.append(action) + for tr in received_transfers: + action = { + 'at': tr.transfers.at, + 'amount': re.amount, + 'label': "Transfert", #TODO + 'balance': 0, + } + actions.append(action) + for tr in emitted_transfers: + action = { + 'at': tr.transfers.at, + 'amount': -re.amount, + 'label': "Transfert", #TODO + 'balance': 0, + } + actions.append(action) + # 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'] + return actions + + def get_context_data(self, **kwargs): + context = {} + changes = self.get_changes_list() + context['changes'] = changes + # TODO: offset + return context + + +# Rend l'évolution de la balance perso de ces 30 derniers jours +class AccountStatBalanceMonth(AccountStatBalance): + begin_date = this_morning() - timezone.timedelta(days=30) + id_prefix = ID_PREFIX_ACC_BALANCE_MONTH + + +# Rend l'évolution de la balance perso de ces 3 derniers mois +class AccountStatBalanceThreeMonths(AccountStatBalance): + begin_date = this_morning() - timezone.timedelta(days=30*3) + id_prefix = ID_PREFIX_ACC_BALANCE_THREE_MONTHS + + +# Rend l'évolution de la balance perso de ces 6 derniers mois +class AccountStatBalanceSixMonths(AccountStatBalance): + begin_date = this_morning() - timezone.timedelta(days=30*6) + id_prefix = ID_PREFIX_ACC_BALANCE_SIX_MONTHS + + +# Rend l'évolution de la balance perso de la dernière annnée +class AccountStatBalanceYear(AccountStatBalance): + begin_date = this_morning() - timezone.timedelta(days=365) + id_prefix = ID_PREFIX_ACC_BALANCE_YEAR + + +# Rend l'évolution de la balance perso depuis toujours +class AccountStatBalanceAnytime(AccountStatBalance): + begin_date = timezone.datetime(year=1980, month=1, day=1) + id_prefix = ID_PREFIX_ACC_BALANCE_ANYTIME + + # ------------------------ # Consommation personnelle # ------------------------