evolution de la balance\n premier jet

This commit is contained in:
Qwann 2016-12-21 11:51:08 +01:00
parent ccf7c4a484
commit 7070129add
3 changed files with 228 additions and 3 deletions

View file

@ -0,0 +1,52 @@
<!doctype html>
<body>
<canvas id="{{ chart_id }}"></canvas>
<script>
jQuery(document).ready(function() {
var ctx1 = $({{ chart_id }});
var myChart = new Chart(ctx1, {
type: 'line',
data: {
labels: [
{% for k,label in labels.items %}
{% if forloop.last %}
"{{ label }}"
{% else %}
"{{ label }}",
{% endif %}
{% endfor %}
],
datasets: [{
label: 'Nb items achetés',
borderColor: 'rgb(255, 99, 132)',
backgroundColor: 'rgb(255, 99, 132)',
data: [
{% for k,nb in nb_ventes.items %}
{% if forloop.last %}
"{{ nb }}"
{% else %}
"{{ nb }}",
{% endif %}
{% endfor %}
],
fill: false,
lineTension: 0,
}]
},
options: {
responsive: true,
tooltips: {
mode: 'index',
intersect: false,
},
hover: {
mode: 'nearest',
intersect: false,
},
}
});
});
</script>
</body>

View file

@ -78,6 +78,25 @@ urlpatterns = [
views.AccountStatLastDay.as_view(),
name = 'kfet.account.stat.last.day'),
url('^accounts/(?P<trigramme>.{3})/stat/balance/$',
views.AccountStatBalanceAll.as_view(),
name = 'kfet.account.stat.balance'),
url('^accounts/(?P<trigramme>.{3})/stat/balance/month/$',
views.AccountStatBalanceMonth.as_view(),
name = 'kfet.account.stat.balance.month'),
url('^accounts/(?P<trigramme>.{3})/stat/balance/treemonths/$',
views.AccountStatBalanceThreeMonths.as_view(),
name = 'kfet.account.stat.balance.treemonths'),
url('^accounts/(?P<trigramme>.{3})/stat/balance/sixmonths/$',
views.AccountStatBalanceSixMonths.as_view(),
name = 'kfet.account.stat.balance.sixmonths'),
url('^accounts/(?P<trigramme>.{3})/stat/balance/year/$',
views.AccountStatBalanceYear.as_view(),
name = 'kfet.account.stat.balance.year'),
url('^accounts/(?P<trigramme>.{3})/stat/balance/anytime/$',
views.AccountStatBalanceAnytime.as_view(),
name = 'kfet.account.stat.balance.anytime'),
# -----
# Checkout urls
# -----

View file

@ -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
# ------------------------