Merge branch 'aureplop/less_prices_history' into 'master'

Less articles prices history

Closes #142

See merge request !224
This commit is contained in:
Ludovic Stephan 2017-04-25 14:32:18 +02:00
commit 5c5fc6da1b

View file

@ -1946,6 +1946,7 @@ class OrderRead(DetailView):
context['mail'] = mail
return context
@teamkfet_required
def order_to_inventory(request, pk):
order = get_object_or_404(Order, pk=pk)
@ -1953,28 +1954,36 @@ def order_to_inventory(request, pk):
if hasattr(order, 'inventory'):
raise Http404
articles = (Article.objects
.filter(orders=order.pk)
.select_related('category')
.prefetch_related(Prefetch('orderarticle_set',
queryset = OrderArticle.objects.filter(order=order),
to_attr = 'order'))
.prefetch_related(Prefetch('supplierarticle_set',
queryset = (SupplierArticle.objects
.filter(supplier=order.supplier)
.order_by('-at')),
to_attr = 'supplier'))
.order_by('category__name', 'name'))
supplier_prefetch = Prefetch(
'article__supplierarticle_set',
queryset=(
SupplierArticle.objects
.filter(supplier=order.supplier)
.order_by('-at')
),
to_attr='supplier',
)
order_articles = (
OrderArticle.objects
.filter(order=order.pk)
.select_related('article', 'article__category')
.prefetch_related(
supplier_prefetch,
)
.order_by('article__category__name', 'article__name')
)
initial = []
for article in articles:
for order_article in order_articles:
article = order_article.article
initial.append({
'article': article.pk,
'name': article.name,
'category': article.category_id,
'category__name': article.category.name,
'quantity_ordered': article.order[0].quantity_ordered,
'quantity_received': article.order[0].quantity_ordered,
'quantity_ordered': order_article.quantity_ordered,
'quantity_received': order_article.quantity_ordered,
'price_HT': article.supplier[0].price_HT,
'TVA': article.supplier[0].TVA,
'rights': article.supplier[0].rights,
@ -1989,31 +1998,50 @@ def order_to_inventory(request, pk):
messages.error(request, 'Permission refusée')
elif formset.is_valid():
with transaction.atomic():
inventory = Inventory()
inventory.order = order
inventory.by = request.user.profile.account_kfet
inventory.save()
inventory = Inventory.objects.create(
order=order, by=request.user.profile.account_kfet,
)
new_supplierarticle = []
new_inventoryarticle = []
for form in formset:
q_received = form.cleaned_data['quantity_received']
article = form.cleaned_data['article']
SupplierArticle.objects.create(
supplier = order.supplier,
article = article,
price_HT = form.cleaned_data['price_HT'],
TVA = form.cleaned_data['TVA'],
rights = form.cleaned_data['rights'])
(OrderArticle.objects
.filter(order=order, article=article)
.update(quantity_received = q_received))
InventoryArticle.objects.create(
inventory = inventory,
article = article,
stock_old = article.stock,
stock_new = article.stock + q_received)
price_HT = form.cleaned_data['price_HT']
TVA = form.cleaned_data['TVA']
rights = form.cleaned_data['rights']
if any((form.initial['price_HT'] != price_HT,
form.initial['TVA'] != TVA,
form.initial['rights'] != rights)):
new_supplierarticle.append(
SupplierArticle(
supplier=order.supplier,
article=article,
price_HT=price_HT,
TVA=TVA,
rights=rights,
)
)
(
OrderArticle.objects
.filter(order=order, article=article)
.update(quantity_received=q_received)
)
new_inventoryarticle.append(
InventoryArticle(
inventory=inventory,
article=article,
stock_old=article.stock,
stock_new=article.stock + q_received,
)
)
article.stock += q_received
if q_received > 0:
article.is_sold = True
article.save()
SupplierArticle.objects.bulk_create(new_supplierarticle)
InventoryArticle.objects.bulk_create(new_inventoryarticle)
messages.success(request, "C'est tout bon !")
return redirect('kfet.order')
else: