Choix 6 mois/12 mois

This commit is contained in:
Tom Hubrecht 2021-06-26 14:25:39 +02:00
parent ad8c3143b6
commit 44c729dfee
3 changed files with 30 additions and 7 deletions

View file

@ -25,6 +25,21 @@
</a> </a>
</div> </div>
<aside>
<div class="heading">
<span>Export des ventes</span>
</div>
<div class="buttons">
<a class="btn btn-primary" href="{% url 'kfet.purchases' 6 %}">
6 derniers mois
</a>
<a class="btn btn-primary" href="{% url 'kfet.purchases' %}">
12 derniers mois
</a>
</div>
</aside>
{% endblock %} {% endblock %}
{% block main %} {% block main %}

View file

@ -239,6 +239,9 @@ urlpatterns = [
# Sales history urls # Sales history urls
# ----- # -----
path("purchases", views.SalesStatList.as_view(), name="kfet.purchases"), path("purchases", views.SalesStatList.as_view(), name="kfet.purchases"),
path(
"purchases/<int:n_months>", views.SalesStatList.as_view(), name="kfet.purchases"
),
# ----- # -----
# JSON urls # JSON urls
# ----- # -----

View file

@ -2469,32 +2469,37 @@ class SalesStatList(BaseListView):
model = Operation model = Operation
def get_queryset(self): 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): def get_context_data(self, **kwargs):
ctx = super().get_context_data(**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() qs = self.get_queryset()
articles = list(Article.objects.order_by("name")) articles = list(Article.objects.order_by("name"))
indexes = {a.name: index for index, a in enumerate(articles)} indexes = {a.name: index for index, a in enumerate(articles)}
ventes = scale.chunkify_qs( ventes = scale.chunkify_qs(
qs.values_list( qs.values_list("article__name").annotate(Sum("article_nb")),
"article__name", "group__at__month", "group__at__year"
).annotate(Sum("article_nb")),
field="group__at", field="group__at",
) )
ctx["header"] = ["Mois"] + [a.name for a in articles] ctx["header"] = ["Mois"] + [a.name for a in articles]
ctx["ventes"] = [] ctx["ventes"] = []
ctx["total"] = [0] * len(articles) ctx["total"] = [0] * len(articles)
labels = scale.get_labels()
for v in ventes: for v in ventes:
m_ventes = [0] * len(articles) 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] index = indexes[a_name]
m_ventes[index] = nb_ventes m_ventes[index] = nb_ventes
ctx["total"][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 return ctx