fix group testing
[auf_rh_dae.git] / project / rh / views.py
CommitLineData
e9bbd6ba 1# -*- encoding: utf-8 -*-
c9122f1d 2from datetime import date
9b0a8554 3
fbe60a9d 4from django.db.models import Q
3411ac33 5from django.contrib.auth.decorators import login_required
fd009814 6from django.utils.encoding import smart_str
e9bbd6ba 7from django.shortcuts import redirect, render_to_response, get_object_or_404
8from django.template import RequestContext
a184c555 9from sendfile import sendfile
e9bbd6ba 10
d04d084c 11from datamaster_modeles import models as ref
d04d084c 12from project.lib import get_employe_from_id
ae936f8d 13
d04d084c 14from rh import models as rh
e2c0b1ac 15from rh.lib import calc_remun
02c1b3dc 16from rh.decorators import drh_or_admin_required
a9faef67 17
9817fed5
OL
18# pas de reference a DAE devrait etre refactorisé
19from dae.utils import get_employe_from_user
20from dae.decorators import redirect_interdiction
21from dae.workflow import grp_drh, grp_correspondants_rh
22
a184c555 23@login_required
9817fed5 24def piece(request, filename):
a184c555 25 """Téléchargement d'une pièce jointe à un poste."""
9817fed5
OL
26 model, id, f = filename.split('/')
27 if model == 'employe':
28 # TODO definir peut-être un controle d'accès
29 piece = get_object_or_404(rh.EmployePiece, fichier=filename)
a184c555 30 return sendfile(request, piece.fichier.path)
9817fed5
OL
31 if model == 'poste':
32 piece = get_object_or_404(rh.PostePiece, fichier=filename)
33 if rh.Poste.objects.ma_region_ou_service(request.user).filter(id=piece.poste_id).exists():
34 return sendfile(request, piece.fichier.path)
35 else:
36 return redirect_interdiction(request)
a184c555 37
3411ac33 38# homes
c9122f1d 39@login_required
40def profil(request):
41 """Profil personnel de l'employé - éditable"""
42 rc = RequestContext(request)
43 c = {}
d04d084c 44
c9122f1d 45 employe = rc['this_employe']
65f9fac8 46
c9122f1d 47 c['user'] = request.user
48 c['employe'] = employe
c9122f1d 49 return render_to_response('rh/profil.html', c, rc)
a9faef67 50
51# employes
3411ac33 52@login_required
a9faef67 53def employes_liste(request):
54 """Liste des employés."""
5ea6b5bb 55 today = date.today()
56 employes = rh.Employe.objects \
57 .filter(actif=True, supprime=False) \
58 .filter(dossiers__actif=True, dossiers__supprime=False) \
59 .exclude(dossiers__date_debut__gt=today) \
60 .exclude(dossiers__date_fin__lt=today) \
61 .order_by('nom')
a9faef67 62 c = {
63 'user':request.user,
64 'employes':employes,
65 }
66 return render_to_response('rh/employes_liste.html', c, RequestContext(request))
67
3411ac33 68@login_required
a9faef67 69def employe(request, id):
70 """Information publique sur un employé."""
71 try:
72 employe = rh.Employe.objects.get(pk=id)
73 except:
74 employe = rh.Employe.objects.none()
75 c = {
76 'user':request.user,
77 'employe':employe,
78 }
79 return render_to_response('rh/employe.html', c, RequestContext(request))
63e17dff
PP
80
81
82# Rapports
83
84@login_required
02c1b3dc 85@drh_or_admin_required
63e17dff
PP
86def rapports_poste(request):
87
fd009814
PP
88 lookup_params = dict(request.GET.items())
89
7821915f
PP
90 comble = 'all'
91
fd009814
PP
92 for key, value in lookup_params.items():
93 if not isinstance(key, str):
94 # 'key' will be used as a keyword argument later, so Python
95 # requires it to be a string.
96 del lookup_params[key]
97 lookup_params[smart_str(key)] = value
98
7821915f
PP
99 if key == 'comble':
100 comble = value
101 del lookup_params[key]
102
63e17dff 103 postes = rh.Poste.actifs.select_related('implantation').all()
fd009814
PP
104 postes = postes.filter(**lookup_params)
105
63e17dff
PP
106 poste_ids = set([p.pk for p in postes])
107
108 dossiers = rh.Dossier.objects.select_related('poste', 'employe').filter(poste__in=poste_ids)
109
110 employes = dict((d.poste.id, d.employe) for d in dossiers)
111
112 out = []
113
114 for p in postes:
7821915f
PP
115 employe = employes.get(p.id)
116
117 if comble == 'c' and not employe:
118 continue
119
120 if comble == 'n' and employe:
121 continue
122
63e17dff
PP
123 line = {}
124 out.append(line)
125
126 line['id'] = p.id
127 line['nom'] = p.nom
128 line['implantation'] = p.implantation.nom
63e17dff
PP
129 if employe:
130 line['employe_id'] = employe.id
131 line['employe_nom'] = employe.nom
132 line['employe_prenom'] = employe.prenom
133
134 c = {
135 'title': 'Rapport des postes',
136 'postes': out,
08fabc85 137 'count': len(out),
63e17dff
PP
138 }
139
140 return render_to_response('rh/rapports/postes.html', c, RequestContext(request))
f2d65e83 141
02c1b3dc
JPC
142@login_required
143@drh_or_admin_required
f2d65e83
PP
144def rapports_contrat(request):
145
146 lookup_params = dict(request.GET.items())
147
148 for key, value in lookup_params.items():
149 if not isinstance(key, str):
150 # 'key' will be used as a keyword argument later, so Python
151 # requires it to be a string.
152 del lookup_params[key]
153 lookup_params[smart_str(key)] = value
154
155 contrats = rh.Contrat.objects.select_related('dossier', 'dossier__poste', 'dossier__poste__implantation', 'type_contrat', 'dossier__employe').all()
156 contrats = contrats.filter(**lookup_params)
157
8ee135b7
JPC
158 employes = set([c.dossier.employe_id for c in contrats])
159
f2d65e83
PP
160 c = {
161 'title': 'Rapport des contrats',
162 'contrats': contrats,
8ee135b7
JPC
163 'count': len(contrats),
164 'count_employe' : len(employes),
f2d65e83
PP
165 }
166
167 return render_to_response('rh/rapports/contrats.html', c, RequestContext(request))
e2c0b1ac
PP
168
169
02c1b3dc
JPC
170@login_required
171@drh_or_admin_required
e2c0b1ac
PP
172def rapports_remuneration(request):
173
174 lookup_params = dict(request.GET.items())
175
176 for key, value in lookup_params.items():
177 if not isinstance(key, str):
178 # 'key' will be used as a keyword argument later, so Python
179 # requires it to be a string.
180 del lookup_params[key]
181 lookup_params[smart_str(key)] = value
182
183 employes = rh.Employe.objects.all()
184 employes = employes.filter(**lookup_params)
185
186 output = []
187
188 for employe in employes:
189 line = {}
190 output.append(line)
191
da6c523f 192 dossiers = employe.rh_dossiers.all()
e2c0b1ac
PP
193
194 remun = {}
195 remun_sum_euro = 0
196
197 for dossier in dossiers:
198 this_remun, this_remun_sum, this_remun_sum_euro = calc_remun(dossier)
199
200 for item in this_remun:
201 if item not in remun:
202 remun[item] = this_remun[item]
203 else:
204 remun[item][0] += this_remun[item][0]
205 remun[item][1] += this_remun[item][1]
206
207 remun_sum_euro += this_remun_sum_euro
208
209 line['remun_sum_euro'] = remun_sum_euro
210
211 for r in remun:
212 if r == u'Indemnité':
e2c0b1ac
PP
213 line['Indemnite'] = remun[r][1]
214 else:
215 line[r] = remun[r][1]
216
217 line['id'] = employe.id
218 line['nom'] = employe.nom
219 line['prenom'] = employe.prenom
220
221
222 c = {
223 'title': 'Rapport de remuneration',
224 'employes': output,
225 }
226
227 return render_to_response('rh/rapports/remuneration.html', c, RequestContext(request))
3ebc0952 228
32373f2e
OL
229def region_protected(model):
230 def wrapper(func):
231 def wrapped(request, id):
32373f2e
OL
232 if request.user.is_superuser:
233 return func(request, id)
234 user_groups = request.user.groups.all()
235 if grp_drh in user_groups:
236 return func(request, id)
237 if grp_correspondants_rh in user_groups:
238 employe = get_employe_from_user(request.user)
239 q = Q(**{model.prefix_implantation: employe.implantation.region})
240 qs = model.objects.filter(q)
718abc4d 241 if int(id) in [o.id for o in qs]:
32373f2e
OL
242 return func(request, id)
243 return redirect_interdiction(request)
244 return wrapped
245 return wrapper
246
247
248@region_protected(rh.Dossier)
3ebc0952
OL
249def dossier_apercu(request, dossier_id):
250 c = {
251 'is_popup' : request.GET.get('_popup', False),
252 'dossier' : get_object_or_404(rh.Dossier, pk=dossier_id)
253
254 }
255 return render_to_response('admin/rh/dossier/apercu.html', c, RequestContext(request))
4a1f2ece 256
fbe60a9d
OL
257@region_protected(rh.Poste)
258def poste_apercu(request, poste_id):
259 c = {
260 'is_popup' : request.GET.get('_popup', False),
261 'poste' : get_object_or_404(rh.Poste, pk=poste_id)
262
263 }
264 return render_to_response('admin/rh/poste/apercu.html', c, RequestContext(request))
265
4a1f2ece 266def employe_apercu(request, employe_id):
fbe60a9d 267 from dae.workflow import grp_drh, grp_correspondants_rh
4a1f2ece 268 employe = get_object_or_404(rh.Employe, pk=employe_id)
fbe60a9d
OL
269 user_groups = request.user.groups.all()
270
271 dernier_dossier = None
272 dossiers = None
273
274 if request.user.is_superuser or \
275 grp_drh in user_groups:
276 q = Q(employe=employe)
277 if grp_correspondants_rh in user_groups:
7eeafa57 278 regions = [d.poste.implantation.region for d in employe.rh_dossiers.all()]
fbe60a9d
OL
279 q = Q(employe=employe) & Q(implantation__region__in=regions)
280
281 dossiers = rh.Dossier.objects.filter(employe=employe).order_by('-date_debut')
282
4a1f2ece
OL
283 c = {
284 'is_popup' : request.GET.get('_popup', False),
285 'employe' : employe,
fbe60a9d 286 'dossiers' : dossiers,
4a1f2ece
OL
287 }
288 return render_to_response('admin/rh/employe/apercu.html', c, RequestContext(request))
150d83ec 289
32373f2e 290