From 96dbfc22da49d546d83cbc4af3cc47868a954c36 Mon Sep 17 00:00:00 2001 From: Hugo Manet Date: Sun, 16 Apr 2017 20:47:45 +0200 Subject: [PATCH 1/2] Add two weeks, count in negative --- kfet/forms.py | 12 +++-- kfet/templates/kfet/order_create.html | 26 +++++---- kfet/views.py | 76 +++++++++++++++++---------- 3 files changed, 70 insertions(+), 44 deletions(-) diff --git a/kfet/forms.py b/kfet/forms.py index f89b8f08..e61773f9 100644 --- a/kfet/forms.py +++ b/kfet/forms.py @@ -517,11 +517,13 @@ class OrderArticleForm(forms.Form): self.category = kwargs['initial']['category'] self.category_name = kwargs['initial']['category__name'] self.box_capacity = kwargs['initial']['box_capacity'] - self.v_s1 = kwargs['initial']['v_s1'] - self.v_s2 = kwargs['initial']['v_s2'] - self.v_s3 = kwargs['initial']['v_s3'] - self.v_s4 = kwargs['initial']['v_s4'] - self.v_s5 = kwargs['initial']['v_s5'] + self.v_s_7 = kwargs['initial']['v_s_7'] + self.v_s_6 = kwargs['initial']['v_s_6'] + self.v_s_5 = kwargs['initial']['v_s_5'] + self.v_s_4 = kwargs['initial']['v_s_4'] + self.v_s_3 = kwargs['initial']['v_s_3'] + self.v_s_2 = kwargs['initial']['v_s_2'] + self.v_s_1 = kwargs['initial']['v_s_1'] self.v_moy = kwargs['initial']['v_moy'] self.v_et = kwargs['initial']['v_et'] self.v_prev = kwargs['initial']['v_prev'] diff --git a/kfet/templates/kfet/order_create.html b/kfet/templates/kfet/order_create.html index c2d07f76..92f53823 100644 --- a/kfet/templates/kfet/order_create.html +++ b/kfet/templates/kfet/order_create.html @@ -14,7 +14,7 @@ Article - Ventes + Ventes V. moy. @@ -33,11 +33,13 @@ Commande - S1 - S2 - S3 - S4 - S5 + S -7 + S -6 + S -5 + S -4 + S -3 + S -2 + S -1 @@ -51,11 +53,13 @@ {{ form.article }} {{ form.name }} - {{ form.v_s1 }} - {{ form.v_s2 }} - {{ form.v_s3 }} - {{ form.v_s4 }} - {{ form.v_s5 }} + {{ form.v_s_7 }} + {{ form.v_s_6 }} + {{ form.v_s_5 }} + {{ form.v_s_4 }} + {{ form.v_s_3 }} + {{ form.v_s_2 }} + {{ form.v_s_1 }} {{ form.v_moy }} {{ form.v_et }} {{ form.v_prev }} diff --git a/kfet/views.py b/kfet/views.py index 60dbb44b..afe26c70 100644 --- a/kfet/views.py +++ b/kfet/views.py @@ -1793,47 +1793,65 @@ def order_create(request, pk): .select_related('group') .filter(article__in=articles, canceled_at=None) .values('article')) - sales_s1 = (sales_q + sales_s_7 = (sales_q + .filter( + group__at__gte = today-timedelta(weeks=7), + group__at__lt = today-timedelta(weeks=7-1)) + .annotate(nb=Sum('article_nb')) + ) + sales_s_7 = { d['article']:d['nb'] for d in sales_s_7 } + sales_s_6 = (sales_q + .filter( + group__at__gte = today-timedelta(weeks=6), + group__at__lt = today-timedelta(weeks=6-1)) + .annotate(nb=Sum('article_nb')) + ) + sales_s_6 = { d['article']:d['nb'] for d in sales_s_6 } + sales_s_5 = (sales_q .filter( group__at__gte = today-timedelta(weeks=5), - group__at__lt = today-timedelta(weeks=4)) + group__at__lt = today-timedelta(weeks=5-1)) .annotate(nb=Sum('article_nb')) ) - sales_s1 = { d['article']:d['nb'] for d in sales_s1 } - sales_s2 = (sales_q + sales_s_5 = { d['article']:d['nb'] for d in sales_s_5 } + sales_s_4 = (sales_q .filter( group__at__gte = today-timedelta(weeks=4), - group__at__lt = today-timedelta(weeks=3)) + group__at__lt = today-timedelta(weeks=4-1)) .annotate(nb=Sum('article_nb')) ) - sales_s2 = { d['article']:d['nb'] for d in sales_s2 } - sales_s3 = (sales_q + sales_s_4 = { d['article']:d['nb'] for d in sales_s_4 } + sales_s_3 = (sales_q .filter( group__at__gte = today-timedelta(weeks=3), - group__at__lt = today-timedelta(weeks=2)) + group__at__lt = today-timedelta(weeks=3-1)) .annotate(nb=Sum('article_nb')) ) - sales_s3 = { d['article']:d['nb'] for d in sales_s3 } - sales_s4 = (sales_q + sales_s_3 = { d['article']:d['nb'] for d in sales_s_3 } + sales_s_2 = (sales_q .filter( group__at__gte = today-timedelta(weeks=2), - group__at__lt = today-timedelta(weeks=1)) + group__at__lt = today-timedelta(weeks=2-1)) .annotate(nb=Sum('article_nb')) ) - sales_s4 = { d['article']:d['nb'] for d in sales_s4 } - sales_s5 = (sales_q - .filter(group__at__gte = today-timedelta(weeks=1)) + sales_s_2 = { d['article']:d['nb'] for d in sales_s_2 } + sales_s_1 = (sales_q + .filter( + group__at__gte = today-timedelta(weeks=1), + group__at__lt = today .annotate(nb=Sum('article_nb')) ) - sales_s5 = { d['article']:d['nb'] for d in sales_s5 } - + sales_s_1 = { d['article']:d['nb'] for d in sales_s_1 } + for article in articles: - v_s1 = sales_s1.get(article.pk, 0) - v_s2 = sales_s2.get(article.pk, 0) - v_s3 = sales_s3.get(article.pk, 0) - v_s4 = sales_s4.get(article.pk, 0) - v_s5 = sales_s5.get(article.pk, 0) - v_all = [v_s1, v_s2, v_s3, v_s4, v_s5] + v_s_7 = sales_s_7.get(article.pk, 0) + v_s_6 = sales_s_6.get(article.pk, 0) + v_s_5 = sales_s_5.get(article.pk, 0) + v_s_4 = sales_s_4.get(article.pk, 0) + v_s_3 = sales_s_3.get(article.pk, 0) + v_s_2 = sales_s_2.get(article.pk, 0) + v_s_1 = sales_s_1.get(article.pk, 0) + v_all = [v_s_7, v_s_6, v_s_5, v_s_4, v_s_3, v_s_2, v_s_1] v_3max = heapq.nlargest(3, v_all) v_moy = statistics.mean(v_3max) v_et = statistics.pstdev(v_3max, v_moy) @@ -1856,15 +1874,17 @@ def order_create(request, pk): 'category__name': article.category.name, 'stock': article.stock, 'box_capacity': article.box_capacity, - 'v_s1': v_s1, - 'v_s2': v_s2, - 'v_s3': v_s3, - 'v_s4': v_s4, - 'v_s5': v_s5, + 'v_s_7': v_s_7, + 'v_s_6': v_s_6, + 'v_s_5': v_s_5, + 'v_s_4': v_s_4, + 'v_s_3': v_s_3, + 'v_s_2': v_s_2, + 'v_s_1': v_s_1, 'v_moy': round(v_moy), 'v_et': round(v_et), 'v_prev': round(v_prev), - 'c_rec': article.box_capacity and c_rec or round(c_rec_tot), + 'c_rec': c_rec if article.box_capacity else round(c_rec_tot), }) cls_formset = formset_factory( -- 2.47.0 From d9411c5b5a275f1096fbe0fe7018041ef621839c Mon Sep 17 00:00:00 2001 From: Hugo Manet Date: Tue, 18 Apr 2017 11:43:27 +0200 Subject: [PATCH 2/2] Add box_capacity to template + minor changes --- kfet/templates/kfet/order_create.html | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/kfet/templates/kfet/order_create.html b/kfet/templates/kfet/order_create.html index 92f53823..67ed437e 100644 --- a/kfet/templates/kfet/order_create.html +++ b/kfet/templates/kfet/order_create.html @@ -15,7 +15,7 @@ Article Ventes - + V. moy. @@ -27,6 +27,9 @@ Stock + Box + + Rec. @@ -47,7 +50,7 @@ {% ifchanged form.category %} {{ form.category_name }} - + {% endifchanged %} @@ -64,6 +67,7 @@ {{ form.v_et }} {{ form.v_prev }} {{ form.stock }} + {{ form.box_capacity }} {{ form.c_rec }} {{ form.quantity_ordered | add_class:"form-control" }} -- 2.47.0