forked from DGNum/gestioCOF
Inventory deletion view
This commit is contained in:
parent
cc3a436750
commit
59dacda37d
1 changed files with 27 additions and 1 deletions
|
@ -13,7 +13,7 @@ from django.contrib.auth.models import Permission, User
|
|||
from django.contrib.messages.views import SuccessMessageMixin
|
||||
from django.core.exceptions import SuspiciousOperation
|
||||
from django.db import transaction
|
||||
from django.db.models import Count, F, Prefetch, Q, Sum
|
||||
from django.db.models import Count, F, Max, OuterRef, Prefetch, Q, Subquery, Sum
|
||||
from django.forms import formset_factory
|
||||
from django.http import Http404, HttpResponseBadRequest, JsonResponse
|
||||
from django.shortcuts import get_object_or_404, redirect, render
|
||||
|
@ -1909,6 +1909,32 @@ class InventoryRead(DetailView):
|
|||
return context
|
||||
|
||||
|
||||
class InventoryDelete(PermissionRequiredMixin, DeleteView):
|
||||
model = Inventory
|
||||
success_url = reverse_lazy("kfet.inventory")
|
||||
success_message = "Inventaire annulé avec succès !"
|
||||
permission_required = "kfet.delete_inventory"
|
||||
|
||||
def get(self, request, *args, **kwargs):
|
||||
return redirect("kfet.inventory.read", self.kwargs.get(self.pk_url_kwarg))
|
||||
|
||||
def delete(self, request, *args, **kwargs):
|
||||
inv = self.get_object()
|
||||
# On met à jour les articles dont c'est le dernier inventaire
|
||||
# .get() ne marche pas avec OuterRef, donc on utilise .filter() avec [:1]
|
||||
update_subquery = InventoryArticle.objects.filter(
|
||||
inventory=inv, article=OuterRef("pk")
|
||||
).values("stock_old")[:1]
|
||||
|
||||
Article.objects.annotate(last_env=Max("inventories__at")).filter(
|
||||
last_env=inv.at
|
||||
).update(stock=Subquery(update_subquery))
|
||||
|
||||
# On a tout mis à jour, on peut delete (avec un message)
|
||||
messages.success(request, self.success_message)
|
||||
return super().delete(request, *args, **kwargs)
|
||||
|
||||
|
||||
# -----
|
||||
# Order views
|
||||
# -----
|
||||
|
|
Loading…
Reference in a new issue