Add two weeks, count in negative

This commit is contained in:
Hugo Manet 2017-04-16 20:47:45 +02:00
parent a0fe94e2f4
commit 96dbfc22da
3 changed files with 70 additions and 44 deletions

View file

@ -517,11 +517,13 @@ class OrderArticleForm(forms.Form):
self.category = kwargs['initial']['category'] self.category = kwargs['initial']['category']
self.category_name = kwargs['initial']['category__name'] self.category_name = kwargs['initial']['category__name']
self.box_capacity = kwargs['initial']['box_capacity'] self.box_capacity = kwargs['initial']['box_capacity']
self.v_s1 = kwargs['initial']['v_s1'] self.v_s_7 = kwargs['initial']['v_s_7']
self.v_s2 = kwargs['initial']['v_s2'] self.v_s_6 = kwargs['initial']['v_s_6']
self.v_s3 = kwargs['initial']['v_s3'] self.v_s_5 = kwargs['initial']['v_s_5']
self.v_s4 = kwargs['initial']['v_s4'] self.v_s_4 = kwargs['initial']['v_s_4']
self.v_s5 = kwargs['initial']['v_s5'] 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_moy = kwargs['initial']['v_moy']
self.v_et = kwargs['initial']['v_et'] self.v_et = kwargs['initial']['v_et']
self.v_prev = kwargs['initial']['v_prev'] self.v_prev = kwargs['initial']['v_prev']

View file

@ -14,7 +14,7 @@
<thead> <thead>
<tr> <tr>
<td rowspan="2">Article</td> <td rowspan="2">Article</td>
<td colspan="5">Ventes <td colspan="7">Ventes
<span class='glyphicon glyphicon-question-sign' title="Ventes des 5 dernières semaines" data-placement="bottom"></span> <span class='glyphicon glyphicon-question-sign' title="Ventes des 5 dernières semaines" data-placement="bottom"></span>
</td> </td>
<td rowspan="2">V. moy. <td rowspan="2">V. moy.
@ -33,11 +33,13 @@
<td rowspan="2">Commande</td> <td rowspan="2">Commande</td>
</tr> </tr>
<tr> <tr>
<td>S1</td> <td>S -7</td>
<td>S2</td> <td>S -6</td>
<td>S3</td> <td>S -5</td>
<td>S4</td> <td>S -4</td>
<td>S5</td> <td>S -3</td>
<td>S -2</td>
<td>S -1</td>
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
@ -51,11 +53,13 @@
<tr> <tr>
{{ form.article }} {{ form.article }}
<td>{{ form.name }}</td> <td>{{ form.name }}</td>
<td>{{ form.v_s1 }}</td> <td>{{ form.v_s_7 }}</td>
<td>{{ form.v_s2 }}</td> <td>{{ form.v_s_6 }}</td>
<td>{{ form.v_s3 }}</td> <td>{{ form.v_s_5 }}</td>
<td>{{ form.v_s4 }}</td> <td>{{ form.v_s_4 }}</td>
<td>{{ form.v_s5 }}</td> <td>{{ form.v_s_3 }}</td>
<td>{{ form.v_s_2 }}</td>
<td>{{ form.v_s_1 }}</td>
<td>{{ form.v_moy }}</td> <td>{{ form.v_moy }}</td>
<td>{{ form.v_et }}</td> <td>{{ form.v_et }}</td>
<td>{{ form.v_prev }}</td> <td>{{ form.v_prev }}</td>

View file

@ -1793,47 +1793,65 @@ def order_create(request, pk):
.select_related('group') .select_related('group')
.filter(article__in=articles, canceled_at=None) .filter(article__in=articles, canceled_at=None)
.values('article')) .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( .filter(
group__at__gte = today-timedelta(weeks=5), 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')) .annotate(nb=Sum('article_nb'))
) )
sales_s1 = { d['article']:d['nb'] for d in sales_s1 } sales_s_5 = { d['article']:d['nb'] for d in sales_s_5 }
sales_s2 = (sales_q sales_s_4 = (sales_q
.filter( .filter(
group__at__gte = today-timedelta(weeks=4), 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')) .annotate(nb=Sum('article_nb'))
) )
sales_s2 = { d['article']:d['nb'] for d in sales_s2 } sales_s_4 = { d['article']:d['nb'] for d in sales_s_4 }
sales_s3 = (sales_q sales_s_3 = (sales_q
.filter( .filter(
group__at__gte = today-timedelta(weeks=3), 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')) .annotate(nb=Sum('article_nb'))
) )
sales_s3 = { d['article']:d['nb'] for d in sales_s3 } sales_s_3 = { d['article']:d['nb'] for d in sales_s_3 }
sales_s4 = (sales_q sales_s_2 = (sales_q
.filter( .filter(
group__at__gte = today-timedelta(weeks=2), 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')) .annotate(nb=Sum('article_nb'))
) )
sales_s4 = { d['article']:d['nb'] for d in sales_s4 } sales_s_2 = { d['article']:d['nb'] for d in sales_s_2 }
sales_s5 = (sales_q sales_s_1 = (sales_q
.filter(group__at__gte = today-timedelta(weeks=1)) .filter(
group__at__gte = today-timedelta(weeks=1),
group__at__lt = today
.annotate(nb=Sum('article_nb')) .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: for article in articles:
v_s1 = sales_s1.get(article.pk, 0) v_s_7 = sales_s_7.get(article.pk, 0)
v_s2 = sales_s2.get(article.pk, 0) v_s_6 = sales_s_6.get(article.pk, 0)
v_s3 = sales_s3.get(article.pk, 0) v_s_5 = sales_s_5.get(article.pk, 0)
v_s4 = sales_s4.get(article.pk, 0) v_s_4 = sales_s_4.get(article.pk, 0)
v_s5 = sales_s5.get(article.pk, 0) v_s_3 = sales_s_3.get(article.pk, 0)
v_all = [v_s1, v_s2, v_s3, v_s4, v_s5] 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_3max = heapq.nlargest(3, v_all)
v_moy = statistics.mean(v_3max) v_moy = statistics.mean(v_3max)
v_et = statistics.pstdev(v_3max, v_moy) v_et = statistics.pstdev(v_3max, v_moy)
@ -1856,15 +1874,17 @@ def order_create(request, pk):
'category__name': article.category.name, 'category__name': article.category.name,
'stock': article.stock, 'stock': article.stock,
'box_capacity': article.box_capacity, 'box_capacity': article.box_capacity,
'v_s1': v_s1, 'v_s_7': v_s_7,
'v_s2': v_s2, 'v_s_6': v_s_6,
'v_s3': v_s3, 'v_s_5': v_s_5,
'v_s4': v_s4, 'v_s_4': v_s_4,
'v_s5': v_s5, 'v_s_3': v_s_3,
'v_s_2': v_s_2,
'v_s_1': v_s_1,
'v_moy': round(v_moy), 'v_moy': round(v_moy),
'v_et': round(v_et), 'v_et': round(v_et),
'v_prev': round(v_prev), '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( cls_formset = formset_factory(