forked from DGNum/gestioCOF
Fetch transfers in history_json
This commit is contained in:
parent
fcf29fe6df
commit
a3b0ea9b8d
1 changed files with 39 additions and 8 deletions
|
@ -12,7 +12,7 @@ from django.contrib.auth.mixins import PermissionRequiredMixin
|
||||||
from django.contrib.auth.models import Permission, User
|
from django.contrib.auth.models import Permission, User
|
||||||
from django.contrib.messages.views import SuccessMessageMixin
|
from django.contrib.messages.views import SuccessMessageMixin
|
||||||
from django.db import transaction
|
from django.db import transaction
|
||||||
from django.db.models import Count, F, Prefetch, Sum
|
from django.db.models import Count, F, Prefetch, Q, Sum
|
||||||
from django.forms import formset_factory
|
from django.forms import formset_factory
|
||||||
from django.http import Http404, JsonResponse
|
from django.http import Http404, JsonResponse
|
||||||
from django.shortcuts import get_object_or_404, redirect, render
|
from django.shortcuts import get_object_or_404, redirect, render
|
||||||
|
@ -1416,42 +1416,73 @@ def history_json(request):
|
||||||
# Récupération des paramètres
|
# Récupération des paramètres
|
||||||
from_date = request.POST.get("from", None)
|
from_date = request.POST.get("from", None)
|
||||||
to_date = request.POST.get("to", None)
|
to_date = request.POST.get("to", None)
|
||||||
limit = request.POST.get("limit", None)
|
|
||||||
checkouts = request.POST.getlist("checkouts[]", None)
|
checkouts = request.POST.getlist("checkouts[]", None)
|
||||||
accounts = request.POST.getlist("accounts[]", None)
|
accounts = request.POST.getlist("accounts[]", None)
|
||||||
|
transfers_only = request.POST.get("transfersonly", None)
|
||||||
|
opes_only = request.POST.get("opesonly", None)
|
||||||
|
|
||||||
# Construction de la requête (sur les opérations) pour le prefetch
|
# Construction de la requête (sur les opérations) pour le prefetch
|
||||||
queryset_prefetch = Operation.objects.select_related(
|
ope_queryset_prefetch = Operation.objects.select_related(
|
||||||
"article", "canceled_by", "addcost_for"
|
"article", "canceled_by", "addcost_for"
|
||||||
)
|
)
|
||||||
|
ope_prefetch = Prefetch("opes", queryset=ope_queryset_prefetch)
|
||||||
|
|
||||||
|
transfer_queryset_prefetch = Transfer.objects.select_related(
|
||||||
|
"from_acc", "to_acc", "canceled_by"
|
||||||
|
)
|
||||||
|
|
||||||
|
if accounts:
|
||||||
|
transfer_queryset_prefetch = transfer_queryset_prefetch.filter(
|
||||||
|
Q(from_acc__trigramme__in=accounts) | Q(to_acc__trigramme__in=accounts)
|
||||||
|
)
|
||||||
|
|
||||||
|
if not request.user.has_perm("kfet.is_team"):
|
||||||
|
acc = request.user.profile.account_kfet
|
||||||
|
transfer_queryset_prefetch = transfer_queryset_prefetch.filter(
|
||||||
|
Q(from_acc=acc) | Q(to_acc=acc)
|
||||||
|
)
|
||||||
|
|
||||||
|
transfer_prefetch = Prefetch(
|
||||||
|
"transfers", queryset=transfer_queryset_prefetch, to_attr="filtered_transfers"
|
||||||
|
)
|
||||||
|
|
||||||
# Construction de la requête principale
|
# Construction de la requête principale
|
||||||
opegroups = (
|
opegroups = (
|
||||||
OperationGroup.objects.prefetch_related(
|
OperationGroup.objects.prefetch_related(ope_prefetch)
|
||||||
Prefetch("opes", queryset=queryset_prefetch)
|
|
||||||
)
|
|
||||||
.select_related("on_acc", "valid_by")
|
.select_related("on_acc", "valid_by")
|
||||||
.order_by("at")
|
.order_by("at")
|
||||||
)
|
)
|
||||||
|
transfergroups = (
|
||||||
|
TransferGroup.objects.prefetch_related(transfer_prefetch)
|
||||||
|
.select_related("valid_by")
|
||||||
|
.order_by("at")
|
||||||
|
)
|
||||||
|
|
||||||
# Application des filtres
|
# Application des filtres
|
||||||
if from_date:
|
if from_date:
|
||||||
opegroups = opegroups.filter(at__gte=from_date)
|
opegroups = opegroups.filter(at__gte=from_date)
|
||||||
|
transfergroups = transfergroups.filter(at__gte=from_date)
|
||||||
if to_date:
|
if to_date:
|
||||||
opegroups = opegroups.filter(at__lt=to_date)
|
opegroups = opegroups.filter(at__lt=to_date)
|
||||||
|
transfergroups = transfergroups.filter(at__lt=to_date)
|
||||||
if checkouts:
|
if checkouts:
|
||||||
opegroups = opegroups.filter(checkout_id__in=checkouts)
|
opegroups = opegroups.filter(checkout_id__in=checkouts)
|
||||||
|
transfergroups = TransferGroup.objects.none()
|
||||||
|
if transfers_only:
|
||||||
|
opegroups = OperationGroup.objects.none()
|
||||||
|
if opes_only:
|
||||||
|
transfergroups = TransferGroup.objects.none()
|
||||||
if accounts:
|
if accounts:
|
||||||
opegroups = opegroups.filter(on_acc_id__in=accounts)
|
opegroups = opegroups.filter(on_acc_id__in=accounts)
|
||||||
# Un non-membre de l'équipe n'a que accès à son historique
|
# Un non-membre de l'équipe n'a que accès à son historique
|
||||||
if not request.user.has_perm("kfet.is_team"):
|
if not request.user.has_perm("kfet.is_team"):
|
||||||
opegroups = opegroups.filter(on_acc=request.user.profile.account_kfet)
|
opegroups = opegroups.filter(on_acc=request.user.profile.account_kfet)
|
||||||
if limit:
|
|
||||||
opegroups = opegroups[:limit]
|
|
||||||
|
|
||||||
# Construction de la réponse
|
# Construction de la réponse
|
||||||
opegroups_list = []
|
opegroups_list = []
|
||||||
for opegroup in opegroups:
|
for opegroup in opegroups:
|
||||||
opegroup_dict = {
|
opegroup_dict = {
|
||||||
|
"type": "opegroup",
|
||||||
"id": opegroup.id,
|
"id": opegroup.id,
|
||||||
"amount": opegroup.amount,
|
"amount": opegroup.amount,
|
||||||
"at": opegroup.at,
|
"at": opegroup.at,
|
||||||
|
|
Loading…
Reference in a new issue