diff --git a/kfet/forms.py b/kfet/forms.py index 9419d9f8..896621f0 100644 --- a/kfet/forms.py +++ b/kfet/forms.py @@ -7,6 +7,7 @@ from django.core import validators from django.core.exceptions import ValidationError from django.forms import modelformset_factory from django.utils import timezone +from django.utils.translation import ugettext_lazy as _ from djconfig.forms import ConfigForm from gestioncof.models import CofProfile @@ -482,10 +483,23 @@ class KFetConfigForm(ConfigForm): class FilterHistoryForm(forms.Form): - checkouts = forms.ModelMultipleChoiceField(queryset=Checkout.objects.all()) - accounts = forms.ModelMultipleChoiceField(queryset=Account.objects.all()) - from_date = forms.DateTimeField(widget=DateTimeWidget) - to_date = forms.DateTimeField(widget=DateTimeWidget) + start = forms.DateTimeField(label=_("De"), widget=DateTimeWidget, required=False) + end = forms.DateTimeField(label=_("À"), widget=DateTimeWidget, required=False) + checkout = forms.ModelChoiceField( + label=_("Caisse"), + queryset=Checkout.objects.all(), + required=False, + empty_label=_("Toutes les caisses"), + ) + account = forms.ModelChoiceField( + label=_("Compte"), + queryset=Account.objects.all(), + required=False, + empty_label=_("Tous les comptes"), + ) + + transfers_only = forms.BooleanField(widget=forms.HiddenInput, required=False) + opes_only = forms.BooleanField(widget=forms.HiddenInput, required=False) # ----- diff --git a/kfet/static/kfet/css/history.css b/kfet/static/kfet/css/history.css index 42e73527..437fcd71 100644 --- a/kfet/static/kfet/css/history.css +++ b/kfet/static/kfet/css/history.css @@ -109,3 +109,7 @@ #history .entry .glyphicon { padding-left:15px; } + +#history-form .form-group { + position: relative; +} diff --git a/kfet/static/kfet/js/history.js b/kfet/static/kfet/js/history.js index 540c8239..06b10d17 100644 --- a/kfet/static/kfet/js/history.js +++ b/kfet/static/kfet/js/history.js @@ -205,17 +205,21 @@ function KHistory(options = {}) { $group.find('.amount').text(amount); } - this.fetch = function (fetch_options) { - options = $.extend({}, this.fetch_options, fetch_options); - var that = this; + this.fetch = function (fetch_options = {}) { + if (typeof (fetch_options) == "string") + data = fetch_options + else + data = $.extend({}, this.fetch_options, fetch_options); + return $.ajax({ + context: this, dataType: "json", url: django_urls["kfet.history.json"](), - method: "POST", - data: options, + method: "GET", + data: data, }).done(function (data) { for (let group of data['groups']) { - that.add_history_group(group); + this.add_history_group(group); } }); } diff --git a/kfet/templates/kfet/account_read.html b/kfet/templates/kfet/account_read.html index d1af035a..07a32661 100644 --- a/kfet/templates/kfet/account_read.html +++ b/kfet/templates/kfet/account_read.html @@ -95,7 +95,7 @@ $(document).ready(function() { khistory = new KHistory({ display_trigramme: false, fetch_options: { - 'accounts': [{{ account.pk }}], + account: {{ account.pk }}, } }); diff --git a/kfet/templates/kfet/history.html b/kfet/templates/kfet/history.html index 94bba48c..c3ebc8b0 100644 --- a/kfet/templates/kfet/history.html +++ b/kfet/templates/kfet/history.html @@ -1,5 +1,5 @@ {% extends 'kfet/base_col_2.html' %} -{% load l10n staticfiles widget_tweaks %} +{% load l10n staticfiles widget_tweaks bootstrap %} {% block extra_head %} @@ -20,13 +20,8 @@ opérations -
- +
+ {{ filter_form|bootstrap}}
@@ -48,30 +43,13 @@ $(document).ready(function() { khistory = new KHistory(); - var $from_date = $('#id_from_date'); - var $to_date = $('#id_to_date'); - var $checkouts = $('#id_checkouts'); - var $accounts = $('#id_accounts'); - - function getSelectedMultiple($el) { - var selected = []; - $el.find(':selected').each(function() { - selected.push($(this).val()) - }); - return selected; - } + var $from_date = $('#id_start'); + var $to_date = $('#id_end'); + var $checkout = $('#id_checkout'); + var $account = $('#id_account'); function getHistory() { - var data = {}; - if ($from_date.val()) - data['from'] = moment($from_date.val()).format('YYYY-MM-DD HH:mm:ss'); - if ($to_date.val()) - data['to'] = moment($to_date.val()).format('YYYY-MM-DD HH:mm:ss'); - var checkouts = getSelectedMultiple($checkouts); - if ($checkouts) - data['checkouts'] = checkouts; - var accounts = getSelectedMultiple($accounts); - data['accounts'] = accounts; + data = $("#history-form").find("input, select, textarea").serialize(); khistory.fetch(data).done(function () { var nb_opes = khistory.$container.find('.entry:not(.canceled)').length; @@ -84,7 +62,11 @@ $(document).ready(function() { format : 'YYYY-MM-DD HH:mm', stepping : 5, locale : 'fr', - showTodayButton: true + showTodayButton: true, + widgetPositioning: { + horizontal: "left", + vertical: "bottom", + } }; $from_date.datetimepicker($.extend({}, defaults_datetimepicker, { @@ -101,14 +83,6 @@ $(document).ready(function() { $('#from_date').data("DateTimePicker").maxDate(e.date); }); - $("select").multipleSelect({ - width: '100%', - filter: true, - allSelected: " ", - selectAllText: "Tout-te-s", - countSelected: "# sur %" - }); - $("#btn-fetch").on('click', function() { khistory.reset(); getHistory(); diff --git a/kfet/templates/kfet/kpsul.html b/kfet/templates/kfet/kpsul.html index 7b292087..3d6ba2d1 100644 --- a/kfet/templates/kfet/kpsul.html +++ b/kfet/templates/kfet/kpsul.html @@ -1142,8 +1142,8 @@ $(document).ready(function() { khistory = new KHistory({ fetch_options: { - from: moment().subtract(1, 'days').format('YYYY-MM-DD HH:mm:ss'), - opesonly: true, + start: moment().subtract(1, 'days').format('YYYY-MM-DD HH:mm:ss'), + opes_only: true, }, }); diff --git a/kfet/templates/kfet/transfers.html b/kfet/templates/kfet/transfers.html index 83f20c70..f8bef33d 100644 --- a/kfet/templates/kfet/transfers.html +++ b/kfet/templates/kfet/transfers.html @@ -36,7 +36,7 @@ $(document).ready(function() { var khistory = new KHistory({ fetch_options:{ - transfersonly: true, + transfers_only: true, } }); diff --git a/kfet/views.py b/kfet/views.py index 3a3c8cd0..2afbfbc5 100644 --- a/kfet/views.py +++ b/kfet/views.py @@ -15,7 +15,7 @@ from django.contrib.messages.views import SuccessMessageMixin from django.db import transaction from django.db.models import Count, F, Prefetch, Q, Sum from django.forms import formset_factory -from django.http import Http404, JsonResponse +from django.http import Http404, HttpResponseBadRequest, JsonResponse from django.shortcuts import get_object_or_404, redirect, render from django.urls import reverse, reverse_lazy from django.utils import timezone @@ -1407,12 +1407,17 @@ def cancel_operations(request): @login_required def history_json(request): # Récupération des paramètres - from_date = request.POST.get("from", None) - to_date = request.POST.get("to", None) - checkouts = request.POST.getlist("checkouts[]", None) - accounts = request.POST.getlist("accounts[]", None) - transfers_only = request.POST.get("transfersonly", False) - opes_only = request.POST.get("opesonly", False) + form = FilterHistoryForm(request.GET) + + if not form.is_valid(): + return HttpResponseBadRequest() + + start = form.cleaned_data["start"] + end = form.cleaned_data["end"] + account = form.cleaned_data["account"] + checkout = form.cleaned_data["checkout"] + transfers_only = form.cleaned_data["transfers_only"] + opes_only = form.cleaned_data["opes_only"] # Construction de la requête (sur les transferts) pour le prefetch @@ -1421,9 +1426,9 @@ def history_json(request): ) # Le check sur les comptes est dans le prefetch pour les transferts - if accounts: + if account: transfer_queryset_prefetch = transfer_queryset_prefetch.filter( - Q(from_acc__in=accounts) | Q(to_acc__in=accounts) + Q(from_acc=account) | Q(to_acc=account) ) if not request.user.has_perm("kfet.is_team"): @@ -1458,21 +1463,21 @@ def history_json(request): ) # Application des filtres - if from_date: - opegroups = opegroups.filter(at__gte=from_date) - transfergroups = transfergroups.filter(at__gte=from_date) - if to_date: - opegroups = opegroups.filter(at__lt=to_date) - transfergroups = transfergroups.filter(at__lt=to_date) - if checkouts: - opegroups = opegroups.filter(checkout__in=checkouts) + if start: + opegroups = opegroups.filter(at__gte=start) + transfergroups = transfergroups.filter(at__gte=start) + if end: + opegroups = opegroups.filter(at__lt=end) + transfergroups = transfergroups.filter(at__lt=end) + if checkout: + opegroups = opegroups.filter(checkout__in=checkout) transfergroups = TransferGroup.objects.none() if transfers_only: opegroups = OperationGroup.objects.none() if opes_only: transfergroups = TransferGroup.objects.none() - if accounts: - opegroups = opegroups.filter(on_acc__in=accounts) + if account: + opegroups = opegroups.filter(on_acc=account) # Un non-membre de l'équipe n'a que accès à son historique if not request.user.has_perm("kfet.is_team"): opegroups = opegroups.filter(on_acc=request.user.profile.account_kfet)