From 44c729dfeefc36c8819f62501201b153c562d401 Mon Sep 17 00:00:00 2001 From: Tom Hubrecht Date: Sat, 26 Jun 2021 14:25:39 +0200 Subject: [PATCH] Choix 6 mois/12 mois --- kfet/templates/kfet/article.html | 15 +++++++++++++++ kfet/urls.py | 3 +++ kfet/views.py | 19 ++++++++++++------- 3 files changed, 30 insertions(+), 7 deletions(-) diff --git a/kfet/templates/kfet/article.html b/kfet/templates/kfet/article.html index 6b48ddbb..eefd9fc3 100644 --- a/kfet/templates/kfet/article.html +++ b/kfet/templates/kfet/article.html @@ -25,6 +25,21 @@ + + {% endblock %} {% block main %} diff --git a/kfet/urls.py b/kfet/urls.py index 439be38c..bf0e85bf 100644 --- a/kfet/urls.py +++ b/kfet/urls.py @@ -239,6 +239,9 @@ urlpatterns = [ # Sales history urls # ----- path("purchases", views.SalesStatList.as_view(), name="kfet.purchases"), + path( + "purchases/", views.SalesStatList.as_view(), name="kfet.purchases" + ), # ----- # JSON urls # ----- diff --git a/kfet/views.py b/kfet/views.py index 1ac394ad..c7ca72f3 100644 --- a/kfet/views.py +++ b/kfet/views.py @@ -2469,32 +2469,37 @@ class SalesStatList(BaseListView): model = Operation def get_queryset(self): - return super().get_queryset().filter(type=Operation.PURCHASE, canceled_at=None) + return ( + super() + .get_queryset() + .filter(type=Operation.PURCHASE, canceled_at=None, article__isnull=False) + ) def get_context_data(self, **kwargs): ctx = super().get_context_data(**kwargs) - scale = MonthScale(n_steps=12, last=True) + scale = MonthScale(n_steps=self.kwargs.get("n_months", 12), last=True) qs = self.get_queryset() articles = list(Article.objects.order_by("name")) indexes = {a.name: index for index, a in enumerate(articles)} ventes = scale.chunkify_qs( - qs.values_list( - "article__name", "group__at__month", "group__at__year" - ).annotate(Sum("article_nb")), + qs.values_list("article__name").annotate(Sum("article_nb")), field="group__at", ) ctx["header"] = ["Mois"] + [a.name for a in articles] ctx["ventes"] = [] ctx["total"] = [0] * len(articles) + + labels = scale.get_labels() + for v in ventes: m_ventes = [0] * len(articles) - for (a_name, month, year, nb_ventes) in v: + for (a_name, nb_ventes) in v: index = indexes[a_name] m_ventes[index] = nb_ventes ctx["total"][index] += nb_ventes - ctx["ventes"].append(["{} {}".format(MONTHS[month], year)] + m_ventes) + ctx["ventes"].append([labels.pop(0)] + m_ventes) return ctx