2016-08-04 05:21:04 +02:00
|
|
|
from django.shortcuts import render, get_object_or_404, redirect
|
2016-08-07 17:02:01 +02:00
|
|
|
from django.core.exceptions import PermissionDenied, ValidationError
|
2016-08-22 05:41:31 +02:00
|
|
|
from django.core.cache import cache
|
2016-08-04 05:21:04 +02:00
|
|
|
from django.views.generic import ListView, DetailView
|
|
|
|
from django.views.generic.edit import CreateView, UpdateView, DeleteView
|
|
|
|
from django.core.urlresolvers import reverse_lazy
|
|
|
|
from django.contrib import messages
|
|
|
|
from django.contrib.messages.views import SuccessMessageMixin
|
2016-08-20 19:35:45 +02:00
|
|
|
from django.contrib.auth import authenticate, login
|
2016-08-02 10:40:46 +02:00
|
|
|
from django.contrib.auth.decorators import login_required, permission_required
|
2016-08-21 02:53:35 +02:00
|
|
|
from django.contrib.auth.models import User, Permission, Group
|
2016-08-06 22:19:52 +02:00
|
|
|
from django.http import HttpResponse, JsonResponse, Http404
|
|
|
|
from django.forms import modelformset_factory
|
2016-08-08 00:24:46 +02:00
|
|
|
from django.db import IntegrityError, transaction
|
2016-08-23 18:15:41 +02:00
|
|
|
from django.db.models import F, Sum
|
2016-08-23 20:31:31 +02:00
|
|
|
from django.db.models.functions import Coalesce
|
2016-08-08 07:44:05 +02:00
|
|
|
from django.utils import timezone
|
2016-08-20 19:35:45 +02:00
|
|
|
from django.utils.crypto import get_random_string
|
2016-08-02 10:40:46 +02:00
|
|
|
from gestioncof.models import CofProfile, Clipper
|
2016-08-11 15:14:23 +02:00
|
|
|
from kfet.models import (Account, Checkout, Article, Settings, AccountNegative,
|
2016-08-20 19:35:45 +02:00
|
|
|
CheckoutStatement, GenericTeamToken)
|
2016-08-03 04:38:54 +02:00
|
|
|
from kfet.forms import *
|
2016-08-06 22:19:52 +02:00
|
|
|
from collections import defaultdict
|
2016-08-14 19:59:36 +02:00
|
|
|
from kfet import consumers
|
2016-08-23 18:15:41 +02:00
|
|
|
import datetime
|
2016-08-20 21:08:33 +02:00
|
|
|
import django_cas_ng
|
2016-08-02 10:40:46 +02:00
|
|
|
|
|
|
|
@login_required
|
|
|
|
def home(request):
|
|
|
|
return render(request, "kfet/base.html")
|
|
|
|
|
2016-08-20 19:35:45 +02:00
|
|
|
@permission_required('kfet.is_team')
|
|
|
|
def login_genericteam(request):
|
2016-08-20 21:08:33 +02:00
|
|
|
profile, _ = CofProfile.objects.get_or_create(user=request.user)
|
|
|
|
logout_cas = ''
|
|
|
|
if profile.login_clipper:
|
|
|
|
logout_cas = django_cas_ng.views.logout(request)
|
|
|
|
|
2016-08-20 19:35:45 +02:00
|
|
|
token = GenericTeamToken.objects.create(token=get_random_string(50))
|
|
|
|
user = authenticate(username="kfet_genericteam", token=token.token)
|
|
|
|
login(request, user)
|
2016-08-20 21:08:33 +02:00
|
|
|
|
|
|
|
if logout_cas:
|
|
|
|
return logout_cas
|
|
|
|
|
2016-08-20 19:35:45 +02:00
|
|
|
return render(request, "kfet/login_genericteam.html")
|
|
|
|
|
2016-08-02 10:40:46 +02:00
|
|
|
def put_cleaned_data_in_dict(dict, form):
|
|
|
|
for field in form.cleaned_data:
|
|
|
|
dict[field] = form.cleaned_data[field]
|
|
|
|
|
2016-08-04 05:21:04 +02:00
|
|
|
# -----
|
|
|
|
# Account views
|
|
|
|
# -----
|
|
|
|
|
|
|
|
# Account - General
|
|
|
|
|
2016-08-02 10:40:46 +02:00
|
|
|
@login_required
|
2016-08-08 12:46:43 +02:00
|
|
|
@permission_required('kfet.is_team')
|
2016-08-03 04:38:54 +02:00
|
|
|
def account(request):
|
2016-08-04 05:21:04 +02:00
|
|
|
accounts = Account.objects.order_by('trigramme')
|
2016-08-03 04:38:54 +02:00
|
|
|
return render(request, "kfet/account.html", { 'accounts' : accounts })
|
|
|
|
|
2016-08-04 05:21:04 +02:00
|
|
|
@login_required
|
|
|
|
@permission_required('kfet.is_team')
|
|
|
|
def account_is_validandfree_ajax(request):
|
2016-08-15 01:48:22 +02:00
|
|
|
if not request.GET.get("trigramme", ''):
|
2016-08-04 05:21:04 +02:00
|
|
|
raise Http404
|
|
|
|
trigramme = request.GET.get("trigramme")
|
|
|
|
data = Account.is_validandfree(trigramme)
|
2016-08-06 22:19:52 +02:00
|
|
|
return JsonResponse(data)
|
2016-08-04 05:21:04 +02:00
|
|
|
|
|
|
|
# Account - Create
|
|
|
|
|
2016-08-03 04:38:54 +02:00
|
|
|
@login_required
|
2016-08-08 12:46:43 +02:00
|
|
|
@permission_required('kfet.is_team')
|
2016-08-03 04:38:54 +02:00
|
|
|
def account_create(request):
|
|
|
|
|
2016-08-02 10:40:46 +02:00
|
|
|
# A envoyer au template
|
|
|
|
data_template = {
|
2016-08-03 04:38:54 +02:00
|
|
|
'account_trigramme_form': AccountTriForm(),
|
2016-08-02 10:40:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
# Enregistrement
|
|
|
|
if request.method == "POST":
|
|
|
|
# Pour indiquer la tentative d'enregistrement au template
|
|
|
|
|
2016-08-03 04:38:54 +02:00
|
|
|
# Checking permission
|
|
|
|
if not request.user.has_perm('kfet.add_account'):
|
|
|
|
raise PermissionDenied
|
|
|
|
|
2016-08-02 10:40:46 +02:00
|
|
|
# Peuplement des forms
|
|
|
|
username = request.POST.get('username')
|
|
|
|
try:
|
2016-08-03 04:38:54 +02:00
|
|
|
user = User.objects.get(username=username)
|
2016-08-02 10:40:46 +02:00
|
|
|
(cof, _) = CofProfile.objects.get_or_create(user=user)
|
2016-08-03 04:38:54 +02:00
|
|
|
user_form = UserForm(request.POST, instance=user)
|
|
|
|
cof_form = CofForm(request.POST, instance=cof)
|
2016-08-02 10:40:46 +02:00
|
|
|
except User.DoesNotExist:
|
|
|
|
user_form = UserForm(request.POST)
|
|
|
|
cof_form = CofForm(request.POST)
|
2016-08-03 04:38:54 +02:00
|
|
|
trigramme_form = AccountTriForm(request.POST)
|
|
|
|
account_form = AccountNoTriForm(request.POST)
|
|
|
|
|
2016-08-02 10:40:46 +02:00
|
|
|
if all((user_form.is_valid(), cof_form.is_valid(),
|
|
|
|
trigramme_form.is_valid(), account_form.is_valid())):
|
|
|
|
data = {}
|
2016-08-03 04:38:54 +02:00
|
|
|
# Fill data for Account.save()
|
2016-08-02 10:40:46 +02:00
|
|
|
put_cleaned_data_in_dict(data, user_form)
|
|
|
|
put_cleaned_data_in_dict(data, cof_form)
|
|
|
|
|
|
|
|
try:
|
2016-08-03 04:38:54 +02:00
|
|
|
account = trigramme_form.save(data = data)
|
|
|
|
account_form = AccountNoTriForm(request.POST, instance=account)
|
|
|
|
account_form.save()
|
2016-08-04 05:21:04 +02:00
|
|
|
messages.success(request, 'Compte créé : %s' % account.trigramme)
|
2016-08-02 10:40:46 +02:00
|
|
|
except Account.UserHasAccount as e:
|
2016-08-04 05:21:04 +02:00
|
|
|
messages.error(request, \
|
|
|
|
"Cet utilisateur a déjà un compte K-Fêt : %s" % e.trigramme)
|
2016-08-21 02:53:35 +02:00
|
|
|
else:
|
|
|
|
messages.error(request, user_form.errors)
|
|
|
|
messages.error(request, cof_form.errors)
|
|
|
|
messages.error(request, trigramme_form.errors)
|
|
|
|
messages.error(request, account_form.errors)
|
2016-08-02 10:40:46 +02:00
|
|
|
|
2016-08-03 04:38:54 +02:00
|
|
|
return render(request, "kfet/account_create.html", data_template)
|
2016-08-02 10:40:46 +02:00
|
|
|
|
2016-08-03 04:38:54 +02:00
|
|
|
def account_form_set_readonly_fields(user_form, cof_form):
|
2016-08-02 10:40:46 +02:00
|
|
|
user_form.fields['username'].widget.attrs['readonly'] = True
|
|
|
|
cof_form.fields['login_clipper'].widget.attrs['readonly'] = True
|
|
|
|
cof_form.fields['is_cof'].widget.attrs['disabled'] = True
|
|
|
|
|
2016-08-03 04:38:54 +02:00
|
|
|
@login_required
|
|
|
|
@permission_required('kfet.is_team')
|
|
|
|
def account_create_ajax(request, username=None, login_clipper=None):
|
2016-08-02 10:40:46 +02:00
|
|
|
user = None
|
|
|
|
if login_clipper:
|
|
|
|
# à partir d'un clipper
|
2016-08-15 01:48:22 +02:00
|
|
|
# le user associé à ce clipper ne devrait pas encore exister
|
2016-08-02 10:40:46 +02:00
|
|
|
clipper = get_object_or_404(Clipper, username = login_clipper)
|
|
|
|
try:
|
|
|
|
# Vérification que clipper ne soit pas déjà dans User
|
2016-08-03 04:38:54 +02:00
|
|
|
user = User.objects.get(username=login_clipper)
|
2016-08-02 10:40:46 +02:00
|
|
|
# Ici, on nous a menti, le user existe déjà
|
|
|
|
username = user.username
|
|
|
|
login_clipper = None
|
|
|
|
except User.DoesNotExist:
|
|
|
|
# Clipper (sans user déjà existant)
|
|
|
|
|
|
|
|
# UserForm - Prefill + Création
|
|
|
|
user_initial_data = {
|
|
|
|
'username' : login_clipper,
|
2016-08-15 01:48:22 +02:00
|
|
|
'email' : "%s@clipper.ens.fr" % login_clipper}
|
2016-08-02 10:40:46 +02:00
|
|
|
if clipper.fullname:
|
|
|
|
# Prefill du nom et prénom
|
|
|
|
names = clipper.fullname.split()
|
|
|
|
# Le premier, c'est le prénom
|
|
|
|
user_initial_data['first_name'] = names[0]
|
|
|
|
if len(names) > 1:
|
|
|
|
# Si d'autres noms -> tous dans le nom de famille
|
|
|
|
user_initial_data['last_name'] = " ".join(names[1:])
|
|
|
|
user_form = UserForm(initial = user_initial_data)
|
|
|
|
|
|
|
|
# CofForm - Prefill + Création
|
|
|
|
cof_initial_data = { 'login_clipper': login_clipper }
|
|
|
|
cof_form = CofForm(initial = cof_initial_data)
|
|
|
|
|
|
|
|
# AccountForm
|
|
|
|
account_form = AccountForm()
|
|
|
|
|
|
|
|
# Protection (read-only) des champs username et login_clipper
|
2016-08-03 04:38:54 +02:00
|
|
|
account_form_set_readonly_fields(user_form, cof_form)
|
2016-08-02 10:40:46 +02:00
|
|
|
if username:
|
|
|
|
# le user existe déjà
|
|
|
|
user = get_object_or_404(User, username=username)
|
|
|
|
# récupération du profil cof
|
|
|
|
(cof, _) = CofProfile.objects.get_or_create(user=user)
|
|
|
|
# UserForm + CofForm - Création à partir des instances existantes
|
|
|
|
user_form = UserForm(instance = user)
|
|
|
|
cof_form = CofForm(instance = cof)
|
|
|
|
# AccountForm
|
2016-08-03 04:38:54 +02:00
|
|
|
account_form = AccountNoTriForm()
|
2016-08-02 10:40:46 +02:00
|
|
|
# Protection (read-only) des champs username et login_clipper
|
2016-08-03 04:38:54 +02:00
|
|
|
account_form_set_readonly_fields(user_form, cof_form)
|
2016-08-02 10:40:46 +02:00
|
|
|
elif not login_clipper:
|
|
|
|
# connaît pas du tout, faut tout remplir
|
|
|
|
user_form = UserForm()
|
|
|
|
cof_form = CofForm()
|
2016-08-03 04:38:54 +02:00
|
|
|
account_form = AccountNoTriForm()
|
2016-08-02 10:40:46 +02:00
|
|
|
|
2016-08-03 04:38:54 +02:00
|
|
|
return render(request, "kfet/account_create_form.html", {
|
2016-08-02 10:40:46 +02:00
|
|
|
'account_form' : account_form,
|
|
|
|
'cof_form' : cof_form,
|
|
|
|
'user_form' : user_form,
|
|
|
|
})
|
|
|
|
|
2016-08-04 05:21:04 +02:00
|
|
|
# Account - Read
|
2016-08-03 04:38:54 +02:00
|
|
|
|
|
|
|
@login_required
|
|
|
|
def account_read(request, trigramme):
|
2016-08-15 01:48:22 +02:00
|
|
|
account = get_object_or_404(Account, trigramme=trigramme)
|
2016-08-03 04:38:54 +02:00
|
|
|
|
|
|
|
# Checking permissions
|
|
|
|
if not request.user.has_perm('kfet.is_team') \
|
2016-08-04 05:21:04 +02:00
|
|
|
and request.user != account.user:
|
2016-08-03 04:38:54 +02:00
|
|
|
raise PermissionDenied
|
|
|
|
|
2016-08-20 17:18:41 +02:00
|
|
|
history = (Operation.objects
|
|
|
|
.select_related('group', 'group__valid_by', 'addcost_for', 'article')
|
|
|
|
.filter(group__on_acc=account)
|
|
|
|
.order_by('-group__at'))
|
|
|
|
|
2016-08-23 18:15:41 +02:00
|
|
|
addcosts = (OperationGroup.objects
|
|
|
|
.filter(opes__addcost_for=account,opes__canceled_at=None)
|
|
|
|
.extra({'date':"date(at)"})
|
|
|
|
.values('date')
|
|
|
|
.annotate(sum_addcosts=Sum('opes__addcost_amount'))
|
|
|
|
.order_by('-date'))
|
|
|
|
|
2016-08-20 17:18:41 +02:00
|
|
|
return render(request, "kfet/account_read.html", {
|
|
|
|
'account' : account,
|
|
|
|
'history' : history,
|
2016-08-23 18:15:41 +02:00
|
|
|
'addcosts': addcosts,
|
2016-08-20 17:18:41 +02:00
|
|
|
})
|
2016-08-03 04:38:54 +02:00
|
|
|
|
2016-08-04 05:21:04 +02:00
|
|
|
# Account - Update
|
|
|
|
|
2016-08-03 04:38:54 +02:00
|
|
|
@login_required
|
|
|
|
def account_update(request, trigramme):
|
2016-08-15 01:48:22 +02:00
|
|
|
account = get_object_or_404(Account, trigramme=trigramme)
|
2016-08-03 04:38:54 +02:00
|
|
|
|
|
|
|
# Checking permissions
|
2016-08-04 05:21:04 +02:00
|
|
|
if not request.user.has_perm('kfet.is_team') \
|
|
|
|
and request.user != account.user:
|
2016-08-03 04:38:54 +02:00
|
|
|
raise PermissionDenied
|
|
|
|
|
2016-08-21 02:53:35 +02:00
|
|
|
if request.user.has_perm('kfet.is_team'):
|
|
|
|
user_form = UserRestrictTeamForm(instance=account.user)
|
|
|
|
group_form = UserGroupForm(instance=account.user)
|
|
|
|
account_form = AccountForm(instance=account)
|
|
|
|
cof_form = CofRestrictForm(instance=account.cofprofile)
|
2016-08-22 20:07:01 +02:00
|
|
|
if hasattr(account, 'negative'):
|
|
|
|
negative_form = AccountNegativeForm(instance=account.negative)
|
|
|
|
else:
|
|
|
|
negative_form = None
|
2016-08-21 02:53:35 +02:00
|
|
|
else:
|
|
|
|
user_form = UserRestrictForm(instance=account.user)
|
|
|
|
account_form = None
|
|
|
|
cof_form = None
|
|
|
|
group_form = None
|
2016-08-22 20:07:01 +02:00
|
|
|
negative_form = None
|
2016-08-21 02:53:35 +02:00
|
|
|
|
2016-08-03 04:38:54 +02:00
|
|
|
if request.method == "POST":
|
|
|
|
# Update attempt
|
2016-08-21 02:53:35 +02:00
|
|
|
success = False
|
|
|
|
missing_perm = False
|
2016-08-03 04:38:54 +02:00
|
|
|
|
2016-08-21 02:53:35 +02:00
|
|
|
if request.user.has_perm('kfet.is_team'):
|
|
|
|
account_form = AccountForm(request.POST, instance=account)
|
|
|
|
cof_form = CofRestrictForm(request.POST, instance=account.cofprofile)
|
|
|
|
user_form = UserRestrictTeamForm(request.POST, instance=account.user)
|
|
|
|
group_form = UserGroupForm(request.POST, instance=account.user)
|
2016-08-22 20:07:01 +02:00
|
|
|
if hasattr(account, 'negative'):
|
|
|
|
negative_form = AccountNegativeForm(request.POST, instance=account.negative)
|
2016-08-21 02:53:35 +02:00
|
|
|
|
|
|
|
if (request.user.has_perm('kfet.change_account')
|
|
|
|
and account_form.is_valid() and cof_form.is_valid()
|
|
|
|
and user_form.is_valid()):
|
2016-08-20 23:31:30 +02:00
|
|
|
data = {}
|
|
|
|
# Fill data for Account.save()
|
|
|
|
put_cleaned_data_in_dict(data, user_form)
|
|
|
|
put_cleaned_data_in_dict(data, cof_form)
|
2016-08-22 20:07:01 +02:00
|
|
|
print(vars(account.negative))
|
2016-08-20 23:31:30 +02:00
|
|
|
|
|
|
|
# Updating
|
|
|
|
account_form.save(data = data)
|
2016-08-21 02:53:35 +02:00
|
|
|
|
|
|
|
# Checking perm to manage perms
|
|
|
|
if (request.user.has_perm('kfet.manage_perms')
|
|
|
|
and group_form.is_valid()):
|
|
|
|
group_form.save()
|
2016-08-22 20:07:01 +02:00
|
|
|
print(vars(account.negative))
|
|
|
|
|
|
|
|
# Checking perm to manage negative
|
|
|
|
if hasattr(account, 'negative'):
|
|
|
|
balance_offset_old = 0
|
|
|
|
if account.negative.balance_offset:
|
|
|
|
balance_offset_old = account.negative.balance_offset
|
|
|
|
if (hasattr(account, 'negative')
|
|
|
|
and request.user.has_perm('kfet.change_accountnegative')
|
|
|
|
and negative_form.is_valid()):
|
|
|
|
balance_offset_new = negative_form.cleaned_data['balance_offset']
|
|
|
|
if not balance_offset_new:
|
|
|
|
balance_offset_new = 0
|
|
|
|
balance_offset_diff = balance_offset_old - balance_offset_new
|
|
|
|
Account.objects.filter(pk=account.pk).update(
|
|
|
|
balance = F('balance') + balance_offset_diff)
|
|
|
|
negative_form.save()
|
|
|
|
if not balance_offset_new and Account.objects.get(pk=account.pk).balance >= 0:
|
|
|
|
AccountNegative.objects.get(account=account).delete()
|
2016-08-21 02:53:35 +02:00
|
|
|
|
|
|
|
success = True
|
|
|
|
messages.success(request,
|
|
|
|
'Informations du compte %s mises à jour' % account.trigramme)
|
|
|
|
elif not request.user.has_perm('kfet.change_account'):
|
|
|
|
missing_perm = True
|
|
|
|
|
|
|
|
if request.user == account.user:
|
|
|
|
missing_perm = False
|
|
|
|
user_form = UserRestrictForm(request.POST, instance=account.user)
|
|
|
|
|
|
|
|
if user_form.is_valid():
|
|
|
|
user_form.save()
|
|
|
|
success = True
|
|
|
|
messages.success(request, 'Vos informations ont été mises à jour')
|
|
|
|
|
|
|
|
if missing_perm:
|
|
|
|
messages.error('Permission refusée')
|
|
|
|
if success:
|
|
|
|
return redirect('kfet.account.read', account.trigramme)
|
2016-08-03 04:38:54 +02:00
|
|
|
else:
|
2016-08-21 02:53:35 +02:00
|
|
|
messages.error('Informations non mises à jour. Corrigez les erreurs')
|
2016-08-03 04:38:54 +02:00
|
|
|
|
|
|
|
return render(request, "kfet/account_update.html", {
|
2016-08-20 23:31:30 +02:00
|
|
|
'account' : account,
|
|
|
|
'account_form' : account_form,
|
|
|
|
'cof_form' : cof_form,
|
|
|
|
'user_form' : user_form,
|
2016-08-21 02:53:35 +02:00
|
|
|
'group_form' : group_form,
|
2016-08-22 20:07:01 +02:00
|
|
|
'negative_form': negative_form,
|
2016-08-20 23:31:30 +02:00
|
|
|
})
|
2016-08-04 05:21:04 +02:00
|
|
|
|
2016-08-21 02:53:35 +02:00
|
|
|
@permission_required('kfet.manage_perms')
|
|
|
|
def account_group(request):
|
|
|
|
groups = (Group.objects
|
|
|
|
.filter(name__icontains='K-Fêt')
|
|
|
|
.prefetch_related('permissions', 'user_set__profile__account_kfet')
|
|
|
|
)
|
|
|
|
return render(request, 'kfet/account_group.html', { 'groups': groups })
|
|
|
|
|
2016-08-21 05:51:55 +02:00
|
|
|
class AccountGroupCreate(SuccessMessageMixin, CreateView):
|
|
|
|
model = Group
|
|
|
|
template_name = 'kfet/account_group_form.html'
|
|
|
|
form_class = GroupForm
|
|
|
|
success_message = 'Nouveau groupe : %(name)s'
|
|
|
|
success_url = reverse_lazy('kfet.account.group')
|
|
|
|
|
|
|
|
class AccountGroupUpdate(UpdateView):
|
|
|
|
queryset = Group.objects.filter(name__icontains='K-Fêt')
|
|
|
|
template_name = 'kfet/account_group_form.html'
|
|
|
|
form_class = GroupForm
|
|
|
|
success_message = 'Groupe modifié : %(name)s'
|
|
|
|
success_url = reverse_lazy('kfet.account.group')
|
|
|
|
|
2016-08-23 20:31:31 +02:00
|
|
|
class AccountNegativeList(ListView):
|
|
|
|
queryset = (AccountNegative.objects
|
|
|
|
.select_related('account', 'account__cofprofile__user'))
|
|
|
|
template_name = 'kfet/account_negative.html'
|
|
|
|
context_object_name = 'negatives'
|
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = super(AccountNegativeList, self).get_context_data(**kwargs)
|
|
|
|
context['settings'] = {
|
|
|
|
'overdraft_amount': Settings.OVERDRAFT_AMOUNT(),
|
|
|
|
'overdraft_duration': Settings.OVERDRAFT_DURATION(),
|
|
|
|
}
|
|
|
|
negs_sum = (AccountNegative.objects
|
|
|
|
.aggregate(
|
|
|
|
bal = Coalesce(Sum('account__balance'),0),
|
|
|
|
offset = Coalesce(Sum('balance_offset'),0),
|
|
|
|
)
|
|
|
|
)
|
|
|
|
context['negatives_sum'] = negs_sum['bal'] + negs_sum['offset']
|
|
|
|
return context
|
|
|
|
|
2016-08-04 05:21:04 +02:00
|
|
|
# -----
|
|
|
|
# Checkout views
|
|
|
|
# -----
|
|
|
|
|
|
|
|
# Checkout - General
|
|
|
|
|
|
|
|
class CheckoutList(ListView):
|
|
|
|
model = Checkout
|
|
|
|
template_name = 'kfet/checkout.html'
|
|
|
|
context_object_name = 'checkouts'
|
|
|
|
|
|
|
|
# Checkout - Create
|
|
|
|
|
|
|
|
class CheckoutCreate(SuccessMessageMixin, CreateView):
|
|
|
|
model = Checkout
|
|
|
|
template_name = 'kfet/checkout_create.html'
|
|
|
|
form_class = CheckoutForm
|
|
|
|
success_message = 'Nouvelle caisse : %(name)s'
|
|
|
|
|
|
|
|
# Surcharge de la validation
|
|
|
|
def form_valid(self, form):
|
|
|
|
# Checking permission
|
2016-08-15 01:48:22 +02:00
|
|
|
if not self.request.user.has_perm('kfet.add_checkout'):
|
2016-08-22 02:52:59 +02:00
|
|
|
form.add_error(None, 'Permission refusée')
|
|
|
|
return self.form_invalid(form)
|
2016-08-04 05:21:04 +02:00
|
|
|
# Creating
|
|
|
|
form.instance.created_by = self.request.user.profile.account_kfet
|
|
|
|
return super(CheckoutCreate, self).form_valid(form)
|
|
|
|
|
|
|
|
# Checkout - Read
|
|
|
|
|
|
|
|
class CheckoutRead(DetailView):
|
2016-08-04 08:23:34 +02:00
|
|
|
model = Checkout
|
2016-08-04 05:21:04 +02:00
|
|
|
template_name = 'kfet/checkout_read.html'
|
|
|
|
context_object_name = 'checkout'
|
|
|
|
|
2016-08-23 03:00:09 +02:00
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = super(CheckoutRead, self).get_context_data(**kwargs)
|
|
|
|
context['statements'] = context['checkout'].statements.order_by('-at')
|
|
|
|
return context
|
|
|
|
|
2016-08-04 05:21:04 +02:00
|
|
|
# Checkout - Update
|
|
|
|
|
|
|
|
class CheckoutUpdate(SuccessMessageMixin, UpdateView):
|
|
|
|
model = Checkout
|
|
|
|
template_name = 'kfet/checkout_update.html'
|
|
|
|
form_class = CheckoutRestrictForm
|
|
|
|
success_message = 'Informations mises à jour pour la caisse : %(name)s'
|
|
|
|
|
|
|
|
# Surcharge de la validation
|
|
|
|
def form_valid(self, form):
|
|
|
|
# Checking permission
|
2016-08-15 01:48:22 +02:00
|
|
|
if not self.request.user.has_perm('kfet.change_checkout'):
|
2016-08-22 02:52:59 +02:00
|
|
|
form.add_error(None, 'Permission refusée')
|
|
|
|
return self.form_invalid(form)
|
2016-08-04 05:21:04 +02:00
|
|
|
# Updating
|
|
|
|
return super(CheckoutUpdate, self).form_valid(form)
|
2016-08-04 08:23:34 +02:00
|
|
|
|
2016-08-11 15:14:23 +02:00
|
|
|
# -----
|
|
|
|
# Checkout Statement views
|
|
|
|
# -----
|
|
|
|
|
|
|
|
# Checkout Statement - General
|
|
|
|
|
|
|
|
class CheckoutStatementList(ListView):
|
|
|
|
model = CheckoutStatement
|
|
|
|
queryset = CheckoutStatement.objects.order_by('-at')
|
|
|
|
template_name = 'kfet/checkoutstatement.html'
|
|
|
|
context_object_name= 'checkoutstatements'
|
|
|
|
|
|
|
|
# Checkout Statement - Create
|
|
|
|
|
2016-08-23 00:15:17 +02:00
|
|
|
def getAmountTaken(data):
|
|
|
|
return Decimal(data.taken_001 * 0.01 + data.taken_002 * 0.02
|
|
|
|
+ data.taken_005 * 0.05 + data.taken_01 * 0.1
|
|
|
|
+ data.taken_02 * 0.2 + data.taken_05 * 0.5
|
|
|
|
+ data.taken_1 * 1 + data.taken_2 * 2
|
|
|
|
+ data.taken_5 * 5 + data.taken_10 * 10
|
|
|
|
+ data.taken_20 * 20 + data.taken_50 * 50
|
|
|
|
+ data.taken_100 * 100 + data.taken_200 * 200
|
2016-08-23 02:45:49 +02:00
|
|
|
+ data.taken_500 * 500 + float(data.taken_cheque))
|
2016-08-23 00:15:17 +02:00
|
|
|
|
|
|
|
def getAmountBalance(data):
|
|
|
|
return Decimal(data['balance_001'] * 0.01 + data['balance_002'] * 0.02
|
|
|
|
+ data['balance_005'] * 0.05 + data['balance_01'] * 0.1
|
|
|
|
+ data['balance_02'] * 0.2 + data['balance_05'] * 0.5
|
|
|
|
+ data['balance_1'] * 1 + data['balance_2'] * 2
|
|
|
|
+ data['balance_5'] * 5 + data['balance_10'] * 10
|
|
|
|
+ data['balance_20'] * 20 + data['balance_50'] * 50
|
|
|
|
+ data['balance_100'] * 100 + data['balance_200'] * 200
|
|
|
|
+ data['balance_500'] * 500)
|
|
|
|
|
2016-08-11 15:14:23 +02:00
|
|
|
class CheckoutStatementCreate(SuccessMessageMixin, CreateView):
|
|
|
|
model = CheckoutStatement
|
|
|
|
template_name = 'kfet/checkoutstatement_create.html'
|
2016-08-23 00:15:17 +02:00
|
|
|
form_class = CheckoutStatementCreateForm
|
2016-08-11 15:14:23 +02:00
|
|
|
success_message = 'Nouveau relevé : %(checkout)s - %(at)s'
|
|
|
|
|
|
|
|
def get_success_url(self):
|
|
|
|
return reverse_lazy('kfet.checkout.read', kwargs={'pk':self.kwargs['pk_checkout']})
|
|
|
|
|
|
|
|
def get_success_message(self, cleaned_data):
|
|
|
|
return self.success_message % dict(
|
|
|
|
cleaned_data,
|
|
|
|
checkout = self.object.checkout.name,
|
|
|
|
at = self.object.at)
|
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = super(CheckoutStatementCreate, self).get_context_data(**kwargs)
|
|
|
|
checkout = Checkout.objects.get(pk=self.kwargs['pk_checkout'])
|
|
|
|
context['checkout'] = checkout
|
|
|
|
return context
|
|
|
|
|
|
|
|
def form_valid(self, form):
|
|
|
|
# Checking permission
|
|
|
|
if not self.request.user.has_perm('kfet.add_checkoutstatement'):
|
2016-08-22 01:57:28 +02:00
|
|
|
form.add_error(None, 'Permission refusée')
|
|
|
|
return self.form_invalid(form)
|
2016-08-11 15:14:23 +02:00
|
|
|
# Creating
|
2016-08-23 00:15:17 +02:00
|
|
|
form.instance.amount_taken = getAmountTaken(form.instance)
|
2016-08-23 02:45:49 +02:00
|
|
|
if not form.instance.not_count:
|
|
|
|
form.instance.balance_new = getAmountBalance(form.cleaned_data)
|
2016-08-11 15:14:23 +02:00
|
|
|
form.instance.checkout_id = self.kwargs['pk_checkout']
|
|
|
|
form.instance.by = self.request.user.profile.account_kfet
|
|
|
|
return super(CheckoutStatementCreate, self).form_valid(form)
|
|
|
|
|
2016-08-23 00:15:17 +02:00
|
|
|
class CheckoutStatementUpdate(SuccessMessageMixin, UpdateView):
|
|
|
|
model = CheckoutStatement
|
|
|
|
template_name = 'kfet/checkoutstatement_update.html'
|
|
|
|
form_class = CheckoutStatementUpdateForm
|
|
|
|
success_message = 'Relevé modifié'
|
|
|
|
|
|
|
|
def get_success_url(self):
|
|
|
|
return reverse_lazy('kfet.checkout.read', kwargs={'pk':self.kwargs['pk_checkout']})
|
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = super(CheckoutStatementUpdate, self).get_context_data(**kwargs)
|
|
|
|
checkout = Checkout.objects.get(pk=self.kwargs['pk_checkout'])
|
|
|
|
context['checkout'] = checkout
|
|
|
|
return context
|
|
|
|
|
|
|
|
def form_valid(self, form):
|
|
|
|
# Checking permission
|
|
|
|
if not self.request.user.has_perm('kfet.change_checkoutstatement'):
|
|
|
|
form.add_error(None, 'Permission refusée')
|
|
|
|
return self.form_invalid(form)
|
|
|
|
# Updating
|
|
|
|
form.instance.amount_taken = getAmountTaken(form.instance)
|
|
|
|
return super(CheckoutStatementUpdate, self).form_valid(form)
|
|
|
|
|
2016-08-04 08:23:34 +02:00
|
|
|
# -----
|
|
|
|
# Article views
|
|
|
|
# -----
|
|
|
|
|
|
|
|
# Article - General
|
|
|
|
|
|
|
|
class ArticleList(ListView):
|
|
|
|
model = Article
|
|
|
|
queryset = Article.objects.order_by('category', '-is_sold', 'name')
|
|
|
|
template_name = 'kfet/article.html'
|
|
|
|
context_object_name = 'articles'
|
|
|
|
|
|
|
|
# Article - Create
|
|
|
|
|
|
|
|
class ArticleCreate(SuccessMessageMixin, CreateView):
|
|
|
|
model = Article
|
|
|
|
template_name = 'kfet/article_create.html'
|
|
|
|
form_class = ArticleForm
|
|
|
|
success_message = 'Nouvel item : %(category)s - %(name)s'
|
|
|
|
|
|
|
|
# Surcharge de la validation
|
|
|
|
def form_valid(self, form):
|
|
|
|
# Checking permission
|
2016-08-21 18:10:35 +02:00
|
|
|
if not self.request.user.has_perm('kfet.add_article'):
|
|
|
|
form.add_error(None, 'Permission refusée')
|
|
|
|
return self.form_invalid(form)
|
|
|
|
|
2016-08-04 08:23:34 +02:00
|
|
|
# Creating
|
|
|
|
return super(ArticleCreate, self).form_valid(form)
|
|
|
|
|
|
|
|
# Article - Read
|
|
|
|
|
|
|
|
class ArticleRead(DetailView):
|
|
|
|
model = Article
|
|
|
|
template_name = 'kfet/article_read.html'
|
|
|
|
context_object_name = 'article'
|
|
|
|
|
|
|
|
# Article - Update
|
|
|
|
|
2016-08-22 03:57:13 +02:00
|
|
|
class ArticleUpdate(SuccessMessageMixin, UpdateView):
|
2016-08-04 08:23:34 +02:00
|
|
|
model = Article
|
|
|
|
template_name = 'kfet/article_update.html'
|
|
|
|
form_class = ArticleRestrictForm
|
|
|
|
success_message = "Informations mises à jour pour l'article : %(name)s"
|
|
|
|
|
|
|
|
# Surcharge de la validation
|
|
|
|
def form_valid(self, form):
|
|
|
|
# Checking permission
|
2016-08-21 18:10:35 +02:00
|
|
|
if not self.request.user.has_perm('kfet.change_article'):
|
|
|
|
form.add_error(None, 'Permission refusée')
|
|
|
|
return self.form_invalid(form)
|
2016-08-04 08:23:34 +02:00
|
|
|
# Updating
|
|
|
|
return super(ArticleUpdate, self).form_valid(form)
|
2016-08-06 22:19:52 +02:00
|
|
|
|
|
|
|
# -----
|
|
|
|
# K-Psul
|
|
|
|
# -----
|
|
|
|
|
|
|
|
@permission_required('kfet.is_team')
|
|
|
|
def kpsul(request):
|
|
|
|
data = {}
|
|
|
|
data['operationgroup_form'] = KPsulOperationGroupForm()
|
|
|
|
data['trigramme_form'] = KPsulAccountForm()
|
|
|
|
data['checkout_form'] = KPsulCheckoutForm()
|
2016-08-06 23:44:58 +02:00
|
|
|
operation_formset = KPsulOperationFormSet(queryset=Operation.objects.none())
|
2016-08-06 22:19:52 +02:00
|
|
|
data['operation_formset'] = operation_formset
|
2016-08-22 16:08:21 +02:00
|
|
|
addcost_for = Settings.ADDCOST_FOR()
|
2016-08-06 22:19:52 +02:00
|
|
|
return render(request, 'kfet/kpsul.html', data)
|
|
|
|
|
2016-08-22 16:08:21 +02:00
|
|
|
@permission_required('kfet.is_team')
|
|
|
|
def kpsul_get_settings(request):
|
|
|
|
addcost_for = Settings.ADDCOST_FOR()
|
|
|
|
data = {
|
|
|
|
'subvention_cof': Settings.SUBVENTION_COF(),
|
|
|
|
'addcost_for' : addcost_for and addcost_for.trigramme or '',
|
|
|
|
'addcost_amount': Settings.ADDCOST_AMOUNT(),
|
|
|
|
}
|
|
|
|
return JsonResponse(data)
|
|
|
|
|
2016-08-06 22:19:52 +02:00
|
|
|
@permission_required('kfet.is_team')
|
|
|
|
def kpsul_account_data(request):
|
|
|
|
trigramme = request.POST.get('trigramme', '')
|
|
|
|
account = get_object_or_404(Account, trigramme=trigramme)
|
2016-08-19 02:56:45 +02:00
|
|
|
data = { 'id': account.pk, 'name': account.name, 'email': account.email,
|
2016-08-06 22:19:52 +02:00
|
|
|
'is_cof': account.is_cof, 'promo': account.promo,
|
|
|
|
'balance': account.balance, 'is_frozen': account.is_frozen,
|
2016-08-16 03:36:14 +02:00
|
|
|
'departement': account.departement, 'nickname': account.nickname,
|
|
|
|
'trigramme': account.trigramme }
|
2016-08-06 22:19:52 +02:00
|
|
|
return JsonResponse(data)
|
|
|
|
|
|
|
|
@permission_required('kfet.is_team')
|
|
|
|
def kpsul_checkout_data(request):
|
2016-08-12 10:03:39 +02:00
|
|
|
pk = request.POST.get('pk', 0)
|
|
|
|
try:
|
|
|
|
data = (Checkout.objects
|
|
|
|
.annotate(
|
|
|
|
last_statement_by_first_name=F('statements__by__cofprofile__user__first_name'),
|
|
|
|
last_statement_by_last_name=F('statements__by__cofprofile__user__last_name'),
|
|
|
|
last_statement_by_trigramme=F('statements__by__trigramme'),
|
|
|
|
last_statement_balance=F('statements__balance_new'),
|
|
|
|
last_statement_at=F('statements__at'))
|
|
|
|
.values(
|
|
|
|
'id', 'name', 'balance', 'valid_from', 'valid_to',
|
|
|
|
'last_statement_balance', 'last_statement_at',
|
|
|
|
'last_statement_by_trigramme', 'last_statement_by_last_name',
|
|
|
|
'last_statement_by_first_name')
|
|
|
|
.select_related(
|
|
|
|
'statements'
|
|
|
|
'statements__by',
|
|
|
|
'statements__by__cofprofile__user')
|
|
|
|
.filter(pk=pk)
|
|
|
|
.order_by('statements__at')
|
|
|
|
.last())
|
|
|
|
except Checkout.DoesNotExist:
|
|
|
|
raise http404
|
2016-08-06 22:19:52 +02:00
|
|
|
return JsonResponse(data)
|
|
|
|
|
2016-08-22 05:41:31 +02:00
|
|
|
@permission_required('kfet.is_team')
|
|
|
|
def kpsul_update_addcost(request):
|
|
|
|
addcost_form = AddcostForm(request.POST)
|
|
|
|
|
|
|
|
if not addcost_form.is_valid():
|
2016-08-22 16:29:12 +02:00
|
|
|
data = { 'errors': { 'addcost': list(addcost_form.errors) } }
|
|
|
|
return JsonResponse(data, status=400)
|
2016-08-22 05:41:31 +02:00
|
|
|
required_perms = ['kfet.manage_addcosts']
|
|
|
|
if not request.user.has_perms(required_perms):
|
|
|
|
data = {
|
|
|
|
'errors': {
|
|
|
|
'missing_perms': get_missing_perms(required_perms, request.user)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return JsonResponse(data, status=403)
|
|
|
|
|
|
|
|
trigramme = addcost_form.cleaned_data['trigramme']
|
|
|
|
account = trigramme and Account.objects.get(trigramme=trigramme) or None
|
|
|
|
Settings.objects.filter(name='ADDCOST_FOR').update(value_account=account)
|
|
|
|
Settings.objects.filter(name='ADDCOST_AMOUNT').update(value_decimal=addcost_form.cleaned_data['amount'])
|
|
|
|
cache.delete('ADDCOST_FOR')
|
|
|
|
cache.delete('ADDCOST_AMOUNT')
|
|
|
|
data = {
|
|
|
|
'addcost': {
|
|
|
|
'for': trigramme and account.trigramme or None,
|
|
|
|
'amount': addcost_form.cleaned_data['amount'],
|
|
|
|
}
|
|
|
|
}
|
|
|
|
consumers.KPsul.group_send('kfet.kpsul', data)
|
|
|
|
return JsonResponse(data)
|
|
|
|
|
2016-08-09 11:02:26 +02:00
|
|
|
def get_missing_perms(required_perms, user):
|
|
|
|
missing_perms_codenames = [ (perm.split('.'))[1]
|
|
|
|
for perm in required_perms if not user.has_perm(perm)]
|
|
|
|
missing_perms = list(
|
|
|
|
Permission.objects
|
|
|
|
.filter(codename__in=missing_perms_codenames)
|
|
|
|
.values_list('name', flat=True))
|
|
|
|
return missing_perms
|
|
|
|
|
2016-08-06 22:19:52 +02:00
|
|
|
@permission_required('kfet.is_team')
|
|
|
|
def kpsul_perform_operations(request):
|
2016-08-07 17:02:01 +02:00
|
|
|
# Initializing response data
|
2016-08-14 19:59:36 +02:00
|
|
|
data = { 'operationgroup': 0, 'operations': [],
|
2016-08-11 06:13:31 +02:00
|
|
|
'warnings': {}, 'errors': {} }
|
2016-08-06 22:19:52 +02:00
|
|
|
|
2016-08-07 17:02:01 +02:00
|
|
|
# Checking operationgroup
|
|
|
|
operationgroup_form = KPsulOperationGroupForm(request.POST)
|
|
|
|
if not operationgroup_form.is_valid():
|
2016-08-11 06:13:31 +02:00
|
|
|
data['errors']['operation_group'] = list(operationgroup_form.errors)
|
2016-08-07 17:02:01 +02:00
|
|
|
|
|
|
|
# Checking operation_formset
|
|
|
|
operation_formset = KPsulOperationFormSet(request.POST)
|
|
|
|
if not operation_formset.is_valid():
|
2016-08-11 06:13:31 +02:00
|
|
|
data['errors']['operations'] = list(operation_formset.errors)
|
2016-08-07 17:02:01 +02:00
|
|
|
|
2016-08-07 23:41:46 +02:00
|
|
|
# Returning BAD REQUEST if errors
|
2016-08-11 06:13:31 +02:00
|
|
|
if data['errors']:
|
2016-08-06 22:19:52 +02:00
|
|
|
return JsonResponse(data, status=400)
|
|
|
|
|
2016-08-07 17:22:39 +02:00
|
|
|
# Pre-saving (no commit)
|
2016-08-07 17:02:01 +02:00
|
|
|
operationgroup = operationgroup_form.save(commit = False)
|
|
|
|
operations = operation_formset.save(commit = False)
|
|
|
|
|
2016-08-07 18:37:06 +02:00
|
|
|
# Retrieving COF grant
|
|
|
|
cof_grant = Settings.SUBVENTION_COF()
|
2016-08-08 02:50:04 +02:00
|
|
|
# Retrieving addcosts data
|
|
|
|
addcost_amount = Settings.ADDCOST_AMOUNT()
|
|
|
|
addcost_for = Settings.ADDCOST_FOR()
|
2016-08-07 18:37:06 +02:00
|
|
|
|
2016-08-08 02:50:04 +02:00
|
|
|
# Initializing vars
|
2016-08-11 06:13:31 +02:00
|
|
|
required_perms = set() # Required perms to perform all operations
|
2016-08-08 02:50:04 +02:00
|
|
|
cof_grant_divisor = 1 + cof_grant / 100
|
2016-08-11 06:13:31 +02:00
|
|
|
to_addcost_for_balance = 0 # For balance of addcost_for
|
|
|
|
to_checkout_balance = 0 # For balance of selected checkout
|
|
|
|
to_articles_stocks = defaultdict(lambda:0) # For stocks articles
|
2016-08-08 02:50:04 +02:00
|
|
|
is_addcost = (addcost_for and addcost_amount
|
|
|
|
and addcost_for != operationgroup.on_acc)
|
2016-08-11 06:13:31 +02:00
|
|
|
|
|
|
|
# Filling data of each operations + operationgroup + calculating other stuffs
|
2016-08-06 22:19:52 +02:00
|
|
|
for operation in operations:
|
2016-08-07 17:22:39 +02:00
|
|
|
if operation.type == Operation.PURCHASE:
|
|
|
|
operation.amount = - operation.article.price * operation.article_nb
|
2016-08-08 02:50:04 +02:00
|
|
|
if is_addcost:
|
2016-08-11 06:13:31 +02:00
|
|
|
operation.addcost_for = addcost_for
|
2016-08-08 02:50:04 +02:00
|
|
|
operation.addcost_amount = addcost_amount * operation.article_nb
|
2016-08-14 19:59:36 +02:00
|
|
|
operation.amount -= operation.addcost_amount
|
2016-08-11 06:13:31 +02:00
|
|
|
to_addcost_for_balance += operation.addcost_amount
|
2016-08-08 12:46:43 +02:00
|
|
|
if operationgroup.on_acc.is_cash:
|
|
|
|
to_checkout_balance += -operation.amount
|
2016-08-07 18:37:06 +02:00
|
|
|
if operationgroup.on_acc.is_cof:
|
2016-08-22 18:08:44 +02:00
|
|
|
if is_addcost:
|
|
|
|
operation.addcost_amount = operation.addcost_amount / cof_grant_divisor
|
2016-08-07 18:37:06 +02:00
|
|
|
operation.amount = operation.amount / cof_grant_divisor
|
2016-08-11 06:13:31 +02:00
|
|
|
to_articles_stocks[operation.article] -= operation.article_nb
|
2016-08-08 12:46:43 +02:00
|
|
|
else:
|
2016-08-11 06:13:31 +02:00
|
|
|
if operationgroup.on_acc.is_cash:
|
|
|
|
data['errors']['account'] = 'Charge et retrait impossible sur LIQ'
|
2016-08-08 12:46:43 +02:00
|
|
|
to_checkout_balance += operation.amount
|
2016-08-08 00:41:31 +02:00
|
|
|
operationgroup.amount += operation.amount
|
2016-08-07 23:41:46 +02:00
|
|
|
if operation.type == Operation.DEPOSIT:
|
2016-08-09 11:02:26 +02:00
|
|
|
required_perms.add('kfet.perform_deposit')
|
2016-08-22 18:08:44 +02:00
|
|
|
if operationgroup.on_acc.is_cof:
|
|
|
|
to_addcost_for_balance = to_addcost_for_balance / cof_grant_divisor
|
2016-08-07 23:41:46 +02:00
|
|
|
|
2016-08-11 06:13:31 +02:00
|
|
|
(perms, stop) = operationgroup.on_acc.perms_to_perform_operation(
|
|
|
|
amount = operationgroup.amount)
|
|
|
|
required_perms |= perms
|
|
|
|
|
2016-08-23 15:43:16 +02:00
|
|
|
if operationgroup.on_acc.need_comment:
|
|
|
|
operationgroup.comment = operationgroup.comment.strip()
|
|
|
|
if not operationgroup.comment:
|
|
|
|
data['errors']['need_comment'] = True
|
|
|
|
return JsonResponse(data, status=400)
|
|
|
|
|
2016-08-11 06:13:31 +02:00
|
|
|
if stop or not request.user.has_perms(required_perms):
|
|
|
|
missing_perms = get_missing_perms(required_perms, request.user)
|
|
|
|
if missing_perms:
|
|
|
|
data['errors']['missing_perms'] = missing_perms
|
|
|
|
if stop:
|
|
|
|
data['errors']['negative'] = True
|
|
|
|
return JsonResponse(data, status=403)
|
|
|
|
|
|
|
|
# If 1 perm is required, filling who perform the operations
|
|
|
|
if required_perms:
|
|
|
|
operationgroup.valid_by = request.user.profile.account_kfet
|
|
|
|
# Filling cof status for statistics
|
|
|
|
operationgroup.is_cof = operationgroup.on_acc.is_cof
|
|
|
|
|
2016-08-08 03:32:48 +02:00
|
|
|
# Starting transaction to ensure data consistency
|
2016-08-11 06:13:31 +02:00
|
|
|
with transaction.atomic():
|
|
|
|
# If not cash account,
|
|
|
|
# saving account's balance and adding to Negative if not in
|
|
|
|
if not operationgroup.on_acc.is_cash:
|
|
|
|
Account.objects.filter(pk=operationgroup.on_acc.pk).update(
|
|
|
|
balance = F('balance') + operationgroup.amount)
|
|
|
|
operationgroup.on_acc.refresh_from_db()
|
|
|
|
if operationgroup.on_acc.balance < 0:
|
2016-08-20 01:20:06 +02:00
|
|
|
if hasattr(operationgroup.on_acc, 'negative'):
|
|
|
|
if not operationgroup.on_acc.negative.start:
|
|
|
|
operationgroup.on_acc.negative.start = timezone.now()
|
|
|
|
operationgroup.on_acc.negative.save()
|
|
|
|
else:
|
|
|
|
negative = AccountNegative(
|
|
|
|
account = operationgroup.on_acc, start = timezone.now())
|
|
|
|
negative.save()
|
2016-08-20 17:18:41 +02:00
|
|
|
elif (hasattr(operationgroup.on_acc, 'negative')
|
|
|
|
and not operationgroup.on_acc.negative.balance_offset):
|
|
|
|
operationgroup.on_acc.negative.delete()
|
2016-08-08 12:46:43 +02:00
|
|
|
|
2016-08-11 06:13:31 +02:00
|
|
|
# Updating checkout's balance
|
|
|
|
if to_checkout_balance:
|
|
|
|
Checkout.objects.filter(pk=operationgroup.checkout.pk).update(
|
|
|
|
balance = F('balance') + to_checkout_balance)
|
2016-08-08 07:44:05 +02:00
|
|
|
|
2016-08-11 06:13:31 +02:00
|
|
|
# Saving addcost_for with new balance if there is one
|
|
|
|
if is_addcost and to_addcost_for_balance:
|
|
|
|
Account.objects.filter(pk=addcost_for.pk).update(
|
|
|
|
balance = F('balance') + to_addcost_for_balance)
|
|
|
|
|
|
|
|
# Saving operation group
|
|
|
|
operationgroup.save()
|
|
|
|
data['operationgroup'] = operationgroup.pk
|
|
|
|
|
|
|
|
# Filling operationgroup id for each operations and saving
|
|
|
|
for operation in operations:
|
|
|
|
operation.group = operationgroup
|
|
|
|
operation.save()
|
|
|
|
data['operations'].append(operation.pk)
|
|
|
|
|
|
|
|
# Updating articles stock
|
|
|
|
for article in to_articles_stocks:
|
|
|
|
Article.objects.filter(pk=article.pk).update(
|
|
|
|
stock = F('stock') + to_articles_stocks[article])
|
2016-08-06 22:19:52 +02:00
|
|
|
|
2016-08-14 19:59:36 +02:00
|
|
|
# Websocket data
|
|
|
|
websocket_data = {}
|
|
|
|
websocket_data['opegroups'] = [{
|
|
|
|
'add': True,
|
|
|
|
'id': operationgroup.pk,
|
|
|
|
'amount': operationgroup.amount,
|
|
|
|
'checkout__name': operationgroup.checkout.name,
|
|
|
|
'at': operationgroup.at,
|
2016-08-18 18:26:58 +02:00
|
|
|
'is_cof': operationgroup.is_cof,
|
2016-08-23 16:22:19 +02:00
|
|
|
'comment': operationgroup.comment,
|
2016-08-14 19:59:36 +02:00
|
|
|
'valid_by__trigramme': ( operationgroup.valid_by and
|
|
|
|
operationgroup.valid_by.trigramme or None),
|
|
|
|
'on_acc__trigramme': operationgroup.on_acc.trigramme,
|
|
|
|
'opes': [],
|
|
|
|
}]
|
|
|
|
for operation in operations:
|
|
|
|
ope_data = {
|
|
|
|
'id': operation.pk, 'type': operation.type, 'amount': operation.amount,
|
|
|
|
'addcost_amount': operation.addcost_amount,
|
2016-08-22 17:57:08 +02:00
|
|
|
'addcost_for__trigramme': is_addcost and addcost_for.trigramme or None,
|
2016-08-14 19:59:36 +02:00
|
|
|
'is_checkout': operation.is_checkout,
|
|
|
|
'article__name': operation.article and operation.article.name or None,
|
|
|
|
'article_nb': operation.article_nb,
|
|
|
|
'group_id': operationgroup.pk,
|
|
|
|
'canceled_by__trigramme': None, 'canceled_at': None,
|
|
|
|
}
|
|
|
|
websocket_data['opegroups'][0]['opes'].append(ope_data)
|
2016-08-14 23:37:05 +02:00
|
|
|
# Need refresh from db cause we used update on queryset
|
2016-08-14 19:59:36 +02:00
|
|
|
operationgroup.checkout.refresh_from_db()
|
|
|
|
websocket_data['checkouts'] = [{
|
|
|
|
'id': operationgroup.checkout.pk,
|
|
|
|
'balance': operationgroup.checkout.balance,
|
|
|
|
}]
|
2016-08-14 23:37:05 +02:00
|
|
|
websocket_data['articles'] = []
|
|
|
|
# Need refresh from db cause we used update on querysets
|
|
|
|
articles_pk = [ article.pk for article in to_articles_stocks]
|
|
|
|
articles = Article.objects.values('id', 'stock').filter(pk__in=articles_pk)
|
|
|
|
for article in articles:
|
|
|
|
websocket_data['articles'].append({
|
|
|
|
'id': article['id'],
|
|
|
|
'stock': article['stock']
|
|
|
|
})
|
2016-08-14 19:59:36 +02:00
|
|
|
consumers.KPsul.group_send('kfet.kpsul', websocket_data)
|
2016-08-06 22:19:52 +02:00
|
|
|
return JsonResponse(data)
|
2016-08-09 11:02:26 +02:00
|
|
|
|
|
|
|
@permission_required('kfet.is_team')
|
|
|
|
def kpsul_cancel_operations(request):
|
|
|
|
# Pour la réponse
|
|
|
|
data = { 'canceled': [], 'warnings': {}, 'errors': {}}
|
|
|
|
|
|
|
|
# Checking if BAD REQUEST (opes_pk not int or not existing)
|
|
|
|
try:
|
|
|
|
# Set pour virer les doublons
|
2016-08-18 18:26:58 +02:00
|
|
|
opes_post = set(map(int, filter(None, request.POST.getlist('operations[]', []))))
|
2016-08-09 11:02:26 +02:00
|
|
|
except ValueError:
|
|
|
|
return JsonResponse(data, status=400)
|
2016-08-09 11:06:42 +02:00
|
|
|
opes_all = (
|
|
|
|
Operation.objects
|
|
|
|
.select_related('group', 'group__on_acc', 'group__on_acc__negative')
|
|
|
|
.filter(pk__in=opes_post))
|
2016-08-09 11:02:26 +02:00
|
|
|
opes_pk = [ ope.pk for ope in opes_all ]
|
|
|
|
opes_notexisting = [ ope for ope in opes_post if ope not in opes_pk ]
|
|
|
|
if opes_notexisting:
|
|
|
|
data['errors']['opes_notexisting'] = opes_notexisting
|
|
|
|
return JsonResponse(data, status=400)
|
|
|
|
|
|
|
|
opes_already_canceled = [] # Déjà annulée
|
|
|
|
opes = [] # Pas déjà annulée
|
|
|
|
required_perms = set()
|
|
|
|
stop_all = False
|
|
|
|
cancel_duration = Settings.CANCEL_DURATION()
|
|
|
|
to_accounts_balances = defaultdict(lambda:0) # Modifs à faire sur les balances des comptes
|
|
|
|
to_groups_amounts = defaultdict(lambda:0) # ------ sur les montants des groupes d'opé
|
|
|
|
to_checkouts_balances = defaultdict(lambda:0) # ------ sur les balances de caisses
|
|
|
|
to_articles_stocks = defaultdict(lambda:0) # ------ sur les stocks d'articles
|
|
|
|
for ope in opes_all:
|
|
|
|
if ope.canceled_at:
|
|
|
|
# Opération déjà annulée, va pour un warning en Response
|
|
|
|
opes_already_canceled.append(ope.pk)
|
|
|
|
else:
|
|
|
|
opes.append(ope.pk)
|
|
|
|
# Si opé il y a plus de CANCEL_DURATION, permission requise
|
|
|
|
if ope.group.at + cancel_duration < timezone.now():
|
|
|
|
required_perms.add('kfet.cancel_old_operations')
|
|
|
|
|
|
|
|
# Calcul de toutes modifs à faire en cas de validation
|
|
|
|
|
|
|
|
# Pour les balances de comptes
|
|
|
|
if not ope.group.on_acc.is_cash:
|
|
|
|
to_accounts_balances[ope.group.on_acc] -= ope.amount
|
|
|
|
if ope.addcost_for and ope.addcost_amount:
|
|
|
|
to_accounts_balances[ope.addcost_for] -= ope.addcost_amount
|
|
|
|
# Pour les groupes d'opés
|
|
|
|
to_groups_amounts[ope.group] -= ope.amount
|
2016-08-23 03:27:02 +02:00
|
|
|
|
2016-08-09 11:02:26 +02:00
|
|
|
# Pour les balances de caisses
|
2016-08-23 03:27:02 +02:00
|
|
|
# Les balances de caisses dont il y a eu un relevé depuis la date
|
|
|
|
# de la commande ne doivent pas être modifiées
|
|
|
|
# TODO ? : Maj les balance_old de relevés pour modifier l'erreur
|
|
|
|
last_statement = (CheckoutStatement.objects
|
|
|
|
.filter(checkout=ope.group.checkout)
|
|
|
|
.order_by('at')
|
|
|
|
.last())
|
|
|
|
if not last_statement or last_statement.at < ope.group.at:
|
|
|
|
if ope.type == Operation.PURCHASE:
|
|
|
|
if ope.group.on_acc.is_cash:
|
|
|
|
to_checkouts_balances[ope.group.checkout] -= - ope.amount
|
|
|
|
else:
|
|
|
|
to_checkouts_balances[ope.group.checkout] -= ope.amount
|
|
|
|
|
2016-08-09 11:02:26 +02:00
|
|
|
# Pour les stocks d'articles
|
|
|
|
if ope.article and ope.article_nb:
|
|
|
|
to_articles_stocks[ope.article] += ope.article_nb
|
|
|
|
|
|
|
|
if not opes:
|
|
|
|
data['warnings']['already_canceled'] = opes_already_canceled
|
|
|
|
return JsonResponse(data)
|
|
|
|
|
|
|
|
# Checking permissions or stop
|
|
|
|
overdraft_duration_max = Settings.OVERDRAFT_DURATION()
|
|
|
|
overdraft_amount_max = Settings.OVERDRAFT_AMOUNT()
|
|
|
|
for account in to_accounts_balances:
|
|
|
|
(perms, stop) = account.perms_to_perform_operation(
|
|
|
|
amount = to_accounts_balances[account],
|
|
|
|
overdraft_duration_max = overdraft_duration_max,
|
|
|
|
overdraft_amount_max = overdraft_amount_max)
|
|
|
|
required_perms |= perms
|
|
|
|
stop_all = stop_all or stop
|
|
|
|
|
|
|
|
if stop_all or not request.user.has_perms(required_perms):
|
|
|
|
missing_perms = get_missing_perms(required_perms, request.user)
|
|
|
|
if missing_perms:
|
|
|
|
data['errors']['missing_perms'] = missing_perms
|
|
|
|
if stop_all:
|
|
|
|
data['errors']['negative'] = True
|
|
|
|
return JsonResponse(data, status=403)
|
|
|
|
|
2016-08-14 19:59:36 +02:00
|
|
|
canceled_by = required_perms and request.user.profile.account_kfet or None
|
|
|
|
canceled_at = timezone.now()
|
|
|
|
|
2016-08-09 11:02:26 +02:00
|
|
|
with transaction.atomic():
|
|
|
|
(Operation.objects.filter(pk__in=opes)
|
2016-08-14 19:59:36 +02:00
|
|
|
.update(canceled_by=canceled_by, canceled_at=canceled_at))
|
2016-08-09 11:02:26 +02:00
|
|
|
for account in to_accounts_balances:
|
|
|
|
Account.objects.filter(pk=account.pk).update(
|
|
|
|
balance = F('balance') + to_accounts_balances[account])
|
|
|
|
for checkout in to_checkouts_balances:
|
|
|
|
Checkout.objects.filter(pk=checkout.pk).update(
|
|
|
|
balance = F('balance') + to_checkouts_balances[checkout])
|
|
|
|
for group in to_groups_amounts:
|
|
|
|
OperationGroup.objects.filter(pk=group.pk).update(
|
|
|
|
amount = F('amount') + to_groups_amounts[group])
|
|
|
|
for article in to_articles_stocks:
|
|
|
|
Article.objects.filter(pk=article.pk).update(
|
|
|
|
stock = F('stock') + to_articles_stocks[article])
|
|
|
|
|
2016-08-14 19:59:36 +02:00
|
|
|
# Websocket data
|
2016-08-14 23:37:05 +02:00
|
|
|
websocket_data = { 'opegroups': [], 'opes': [], 'checkouts': [], 'articles': [] }
|
|
|
|
# Need refresh from db cause we used update on querysets
|
|
|
|
opegroups_pk = [ opegroup.pk for opegroup in to_groups_amounts ]
|
|
|
|
opegroups = (OperationGroup.objects
|
2016-08-19 01:08:57 +02:00
|
|
|
.values('id','amount','on_acc__cofprofile__is_cof').filter(pk__in=opegroups_pk))
|
2016-08-14 23:37:05 +02:00
|
|
|
for opegroup in opegroups:
|
2016-08-14 19:59:36 +02:00
|
|
|
websocket_data['opegroups'].append({
|
|
|
|
'cancellation': True,
|
2016-08-14 23:37:05 +02:00
|
|
|
'id': opegroup['id'],
|
|
|
|
'amount': opegroup['amount'],
|
2016-08-19 01:08:57 +02:00
|
|
|
'on_acc__is_cof': opegroup['on_acc__cofprofile__is_cof'],
|
2016-08-14 19:59:36 +02:00
|
|
|
})
|
2016-08-19 06:39:25 +02:00
|
|
|
canceled_by__trigramme = canceled_by and canceled_by.trigramme or None
|
2016-08-14 19:59:36 +02:00
|
|
|
for ope in opes:
|
|
|
|
websocket_data['opes'].append({
|
|
|
|
'cancellation': True,
|
|
|
|
'id': ope,
|
2016-08-20 17:18:41 +02:00
|
|
|
'canceled_by__trigramme': canceled_by__trigramme,
|
2016-08-14 19:59:36 +02:00
|
|
|
'canceled_at': canceled_at,
|
|
|
|
})
|
2016-08-14 23:37:05 +02:00
|
|
|
# Need refresh from db cause we used update on querysets
|
|
|
|
checkouts_pk = [ checkout.pk for checkout in to_checkouts_balances]
|
|
|
|
checkouts = (Checkout.objects
|
|
|
|
.values('id', 'balance').filter(pk__in=checkouts_pk))
|
|
|
|
for checkout in checkouts:
|
|
|
|
websocket_data['checkouts'].append({
|
|
|
|
'id': checkout['id'],
|
|
|
|
'balance': checkout['balance']})
|
|
|
|
# Need refresh from db cause we used update on querysets
|
|
|
|
articles_pk = [ article.pk for articles in to_articles_stocks]
|
|
|
|
articles = Article.objects.values('id', 'stock').filter(pk__in=articles_pk)
|
|
|
|
for article in articles:
|
|
|
|
websocket_data['articles'].append({
|
|
|
|
'id': article['id'],
|
|
|
|
'stock': article['stock']})
|
2016-08-14 19:59:36 +02:00
|
|
|
consumers.KPsul.group_send('kfet.kpsul', websocket_data)
|
|
|
|
|
2016-08-09 11:02:26 +02:00
|
|
|
data['canceled'] = opes
|
|
|
|
if opes_already_canceled:
|
|
|
|
data['warnings']['already_canceled'] = opes_already_canceled
|
|
|
|
return JsonResponse(data)
|
2016-08-14 19:59:36 +02:00
|
|
|
|
|
|
|
@permission_required('kfet.is_team')
|
|
|
|
def kpsul_history(request):
|
|
|
|
opegroups_list = (OperationGroup.objects
|
|
|
|
.values(
|
2016-08-23 16:22:19 +02:00
|
|
|
'id', 'amount', 'at', 'checkout_id', 'is_cof', 'comment',
|
2016-08-14 19:59:36 +02:00
|
|
|
'valid_by__trigramme', 'on_acc__trigramme')
|
|
|
|
.select_related('valid_by', 'on_acc')
|
2016-08-23 18:15:41 +02:00
|
|
|
.filter(at__gt=timezone.now()-datetime.timedelta(hours=24)))
|
2016-08-14 19:59:36 +02:00
|
|
|
opegroups = { opegroup['id']:opegroup for opegroup in opegroups_list }
|
|
|
|
for opegroup in opegroups:
|
|
|
|
opegroups[opegroup]['opes'] = []
|
|
|
|
opes = (Operation.objects
|
|
|
|
.values(
|
|
|
|
'id', 'type', 'amount', 'addcost_amount', 'is_checkout',
|
|
|
|
'article__name', 'canceled_by__trigramme', 'canceled_at',
|
|
|
|
'addcost_for__trigramme', 'article_nb', 'group_id')
|
|
|
|
.filter(group__in=opegroups.keys()))
|
|
|
|
for ope in opes:
|
|
|
|
opegroups[ope['group_id']]['opes'].append(ope)
|
2016-08-14 23:37:05 +02:00
|
|
|
return JsonResponse(opegroups)
|
|
|
|
|
|
|
|
@permission_required('kfet.is_team')
|
|
|
|
def kpsul_articles_data(request):
|
|
|
|
articles = (
|
|
|
|
Article.objects
|
|
|
|
.values('id', 'name', 'price', 'stock', 'category_id', 'category__name')
|
|
|
|
.filter(is_sold=True))
|
|
|
|
return JsonResponse({ 'articles': list(articles) })
|
2016-08-22 03:57:13 +02:00
|
|
|
|
|
|
|
# -----
|
|
|
|
# Settings views
|
|
|
|
# -----
|
|
|
|
|
|
|
|
class SettingsList(ListView):
|
|
|
|
model = Settings
|
|
|
|
context_object_name = 'settings'
|
|
|
|
template_name = 'kfet/settings.html'
|
|
|
|
|
2016-08-22 04:21:10 +02:00
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
Settings.create_missing()
|
|
|
|
return super(SettingsList, self).get_context_data(**kwargs)
|
|
|
|
|
2016-08-22 03:57:13 +02:00
|
|
|
class SettingsUpdate(SuccessMessageMixin, UpdateView):
|
|
|
|
model = Settings
|
|
|
|
form_class = SettingsForm
|
|
|
|
template_name = 'kfet/settings_update.html'
|
|
|
|
success_message = 'Paramètre %(name)s mis à jour'
|
|
|
|
success_url = reverse_lazy('kfet.settings')
|
|
|
|
|
|
|
|
def form_valid(self, form):
|
|
|
|
# Checking permission
|
|
|
|
if not self.request.user.has_perm('kfet.change_settings'):
|
|
|
|
form.add_error(None, 'Permission refusée')
|
|
|
|
return self.form_invalid(form)
|
|
|
|
# Creating
|
|
|
|
return super(SettingsUpdate, self).form_valid(form)
|
|
|
|
|