Less articles prices history

- Prices given with order_to_inventory are saved to db only if they are
  updated (it doesn't create a new price row each time)

Fixes #142.
This commit is contained in:
Aurélien Delobelle 2017-04-13 16:34:29 +02:00
parent a725f8020c
commit 7db497d095

View file

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