Merge branch 'dev' of ssh://git.auf/auf_rh_dae into dev
[auf_rh_dae.git] / project / rh / views.py
CommitLineData
e9bbd6ba 1# -*- encoding: utf-8 -*-
c9122f1d 2from datetime import date
a7de62d7 3from itertools import izip
9b0a8554 4
fbe60a9d 5from django.db.models import Q
3411ac33 6from django.contrib.auth.decorators import login_required
fd009814 7from django.utils.encoding import smart_str
e9bbd6ba 8from django.shortcuts import redirect, render_to_response, get_object_or_404
9from django.template import RequestContext
9d53bc73 10from django.http import Http404
a184c555 11from sendfile import sendfile
e9bbd6ba 12
d04d084c 13from datamaster_modeles import models as ref
d04d084c 14from project.lib import get_employe_from_id
ae936f8d 15
d04d084c 16from rh import models as rh
e2c0b1ac 17from rh.lib import calc_remun
02c1b3dc 18from rh.decorators import drh_or_admin_required
9d53bc73 19from rh.templatetags.rapports import SortHeaders
a9faef67 20
9817fed5
OL
21# pas de reference a DAE devrait etre refactorisé
22from dae.utils import get_employe_from_user
23from dae.decorators import redirect_interdiction
24from dae.workflow import grp_drh, grp_correspondants_rh
baf9b78c 25from django.conf import settings
9817fed5 26
a184c555 27@login_required
9817fed5 28def piece(request, filename):
a184c555 29 """Téléchargement d'une pièce jointe à un poste."""
9817fed5 30 model, id, f = filename.split('/')
4ba73558
OL
31 if model == 'contrat':
32 # TODO definir peut-être un controle d'accès
33 piece = get_object_or_404(rh.Contrat, fichier=filename)
34 return sendfile(request, piece.fichier.path)
9817fed5
OL
35 if model == 'employe':
36 # TODO definir peut-être un controle d'accès
37 piece = get_object_or_404(rh.EmployePiece, fichier=filename)
a184c555 38 return sendfile(request, piece.fichier.path)
9817fed5
OL
39 if model == 'poste':
40 piece = get_object_or_404(rh.PostePiece, fichier=filename)
41 if rh.Poste.objects.ma_region_ou_service(request.user).filter(id=piece.poste_id).exists():
42 return sendfile(request, piece.fichier.path)
43 else:
44 return redirect_interdiction(request)
0ce4c68b
OL
45 if model == 'dossier':
46 piece = get_object_or_404(rh.DossierPiece, fichier=filename)
47 if rh.Dossier.objects.ma_region_ou_service(request.user).filter(id=piece.dossier_id).exists():
48 return sendfile(request, piece.fichier.path)
49 else:
50 return redirect_interdiction(request)
a184c555 51
3411ac33 52# homes
c9122f1d 53@login_required
54def profil(request):
55 """Profil personnel de l'employé - éditable"""
56 rc = RequestContext(request)
57 c = {}
d04d084c 58
c9122f1d 59 employe = rc['this_employe']
65f9fac8 60
c9122f1d 61 c['user'] = request.user
62 c['employe'] = employe
c9122f1d 63 return render_to_response('rh/profil.html', c, rc)
a9faef67 64
65# employes
3411ac33 66@login_required
a9faef67 67def employes_liste(request):
68 """Liste des employés."""
5ea6b5bb 69 today = date.today()
70 employes = rh.Employe.objects \
5ea6b5bb 71 .exclude(dossiers__date_debut__gt=today) \
72 .exclude(dossiers__date_fin__lt=today) \
73 .order_by('nom')
a9faef67 74 c = {
75 'user':request.user,
76 'employes':employes,
77 }
78 return render_to_response('rh/employes_liste.html', c, RequestContext(request))
79
3411ac33 80@login_required
a9faef67 81def employe(request, id):
82 """Information publique sur un employé."""
83 try:
84 employe = rh.Employe.objects.get(pk=id)
85 except:
86 employe = rh.Employe.objects.none()
87 c = {
88 'user':request.user,
89 'employe':employe,
90 }
91 return render_to_response('rh/employe.html', c, RequestContext(request))
63e17dff
PP
92
93
94# Rapports
95
96@login_required
02c1b3dc 97@drh_or_admin_required
63e17dff
PP
98def rapports_poste(request):
99
fd009814
PP
100 lookup_params = dict(request.GET.items())
101
7821915f
PP
102 comble = 'all'
103
fd009814 104 for key, value in lookup_params.items():
9d53bc73
JPC
105 if key == 'o' or key == 'ot':
106 del lookup_params[key]
107 continue
fd009814
PP
108 if not isinstance(key, str):
109 # 'key' will be used as a keyword argument later, so Python
110 # requires it to be a string.
111 del lookup_params[key]
112 lookup_params[smart_str(key)] = value
113
7821915f
PP
114 if key == 'comble':
115 comble = value
116 del lookup_params[key]
117
ed3cbb32
OL
118 postes = rh.Poste.objects.select_related('implantation') \
119 .extra(select={'employe_id':'select group_concat(employe separator "|") from rh_dossier where poste=rh_poste.id and rh_dossier.date_fin is null %s' % \
9202c5db
JPC
120 ("%s" % (('order by employe %s' % ('asc' if 'ot' in request.GET and request.GET['ot'] == "asc" else "desc"))) if 'o' in request.GET and request.GET['o'] == "employe_id" else "") \
121 }) \
ed3cbb32 122 .extra(select={'employe_nom':'select group_concat(rh_employe.nom separator "|") from rh_dossier inner join rh_employe on rh_dossier.employe = rh_employe.id where poste=rh_poste.id and rh_dossier.date_fin is null %s' % \
9202c5db
JPC
123 ("%s" % (('order by rh_employe.nom %s' % ('asc' if 'ot' in request.GET and request.GET['ot'] == "asc" else "desc"))) if 'o' in request.GET and request.GET['o'] == "employe_nom" else "") \
124 }) \
ed3cbb32 125 .extra(select={'employe_prenom':'select group_concat(rh_employe.prenom separator "|") from rh_dossier inner join rh_employe on rh_dossier.employe = rh_employe.id where poste=rh_poste.id and rh_dossier.date_fin is null %s' % \
9202c5db
JPC
126 ("%s" % (('order by rh_employe.prenom %s' % ('asc' if 'ot' in request.GET and request.GET['ot'] == "asc" else "desc"))) if 'o' in request.GET and request.GET['o'] == "employe_prenom" else "") \
127 })
9d53bc73 128
65e02621 129 postes = postes.filter(**lookup_params)
9d53bc73 130 if 'o' in request.GET:
65e02621
JPC
131 postes = postes.order_by("%s%s" % ('-' if 'ot' in request.GET and request.GET['ot'] == "desc" else '', request.GET['o']))
132
a7de62d7
JPC
133 out = []
134 for p in postes:
135 out.append({
136 'id': p.id,
137 'nom': p.nom,
138 'implantation': p.implantation,
139 'employes': [] if not p.employe_id else [{'id': id, 'nom': nom, 'prenom': prenom} for id, nom, prenom in izip(p.employe_id.split('|'), p.employe_nom.split('|'), p.employe_prenom.split('|'))] })
140
141
4dade240
JPC
142 headers = [
143 ("id", u"# du poste"),
144 ("nom", u"Nom du poste"),
65e02621 145 ("implantation__id", u"Implantation"),
4dade240 146 ("employe_nom", u"Nom"),
4dade240
JPC
147 ]
148 h = SortHeaders(request, headers, order_field_type="ot", order_field="o")
63e17dff
PP
149 c = {
150 'title': 'Rapport des postes',
a7de62d7
JPC
151 'postes': out,
152 'count': len(out),
65e02621 153 'headers': list(h.headers()),
63e17dff
PP
154 }
155
156 return render_to_response('rh/rapports/postes.html', c, RequestContext(request))
f2d65e83 157
02c1b3dc
JPC
158@login_required
159@drh_or_admin_required
f2d65e83
PP
160def rapports_contrat(request):
161
162 lookup_params = dict(request.GET.items())
65e02621
JPC
163 if 'ot' in lookup_params:
164 del lookup_params['ot']
165 if 'o' in lookup_params:
166 del lookup_params['o']
f2d65e83
PP
167
168 for key, value in lookup_params.items():
169 if not isinstance(key, str):
170 # 'key' will be used as a keyword argument later, so Python
171 # requires it to be a string.
172 del lookup_params[key]
173 lookup_params[smart_str(key)] = value
174
65e02621 175 contrats = rh.Contrat.objects.select_related('dossier', 'dossier__poste', 'dossier__poste__implantation', 'type_contrat', 'dossier__employe')
821b9889 176 contrats = contrats.filter(**lookup_params).exclude(dossier__employe__supprime=1)
65e02621
JPC
177 if 'o' in request.GET:
178 contrats = contrats.order_by("%s%s" % ('-' if 'ot' in request.GET and request.GET['ot'] == "desc" else '', request.GET['o']))
f2d65e83 179
8ee135b7
JPC
180 employes = set([c.dossier.employe_id for c in contrats])
181
65e02621
JPC
182 headers = [
183 ("dossier__employe__id", u"# de l'employé"),
184 ("dossier__employe__nom", u"Nom"),
185 ("dossier__employe__prenom", u"Prénom"),
186 ("type_contrat__nom", u"Poste"),
187 ("type_contrat__nom", u"Type de contrat"),
188 ("date_debut", u"Date début"),
189 ("date_fin", u"Date fin"),
190 ]
191 h = SortHeaders(request, headers, order_field_type="ot", order_field="o")
192
f2d65e83
PP
193 c = {
194 'title': 'Rapport des contrats',
195 'contrats': contrats,
8ee135b7
JPC
196 'count': len(contrats),
197 'count_employe' : len(employes),
65e02621 198 'headers': list(h.headers()),
f2d65e83
PP
199 }
200
201 return render_to_response('rh/rapports/contrats.html', c, RequestContext(request))
e2c0b1ac
PP
202
203
02c1b3dc
JPC
204@login_required
205@drh_or_admin_required
e2c0b1ac
PP
206def rapports_remuneration(request):
207
208 lookup_params = dict(request.GET.items())
9202c5db
JPC
209 if 'ot' in lookup_params:
210 del lookup_params['ot']
211 if 'o' in lookup_params:
212 del lookup_params['o']
e2c0b1ac
PP
213
214 for key, value in lookup_params.items():
215 if not isinstance(key, str):
216 # 'key' will be used as a keyword argument later, so Python
217 # requires it to be a string.
218 del lookup_params[key]
219 lookup_params[smart_str(key)] = value
220
221 employes = rh.Employe.objects.all()
9202c5db
JPC
222 if 'o' in request.GET:
223 employes = employes.order_by("%s%s" % ('-' if 'ot' in request.GET and request.GET['ot'] == "desc" else '', request.GET['o']))
224
e2c0b1ac
PP
225 employes = employes.filter(**lookup_params)
226
227 output = []
9202c5db
JPC
228 headers = [
229 ("id", u"# de l'employé"),
230 ("nom", u"Nom"),
231 ("prenom", u"Prénom"),
232 ("", u"Salaire"),
233 ("", u"RAS"),
234 ("", u"Indemnités"),
235 ("", u"Accessoire"),
236 ("", u"Charges patronales"),
237 ("", u"Total"),
238 ]
239 h = SortHeaders(request, headers, order_field_type="ot", order_field="o")
e2c0b1ac
PP
240
241 for employe in employes:
242 line = {}
243 output.append(line)
244
da6c523f 245 dossiers = employe.rh_dossiers.all()
e2c0b1ac
PP
246
247 remun = {}
248 remun_sum_euro = 0
249
250 for dossier in dossiers:
251 this_remun, this_remun_sum, this_remun_sum_euro = calc_remun(dossier)
252
253 for item in this_remun:
254 if item not in remun:
255 remun[item] = this_remun[item]
256 else:
257 remun[item][0] += this_remun[item][0]
258 remun[item][1] += this_remun[item][1]
259
260 remun_sum_euro += this_remun_sum_euro
261
262 line['remun_sum_euro'] = remun_sum_euro
263
264 for r in remun:
265 if r == u'Indemnité':
e2c0b1ac
PP
266 line['Indemnite'] = remun[r][1]
267 else:
268 line[r] = remun[r][1]
269
270 line['id'] = employe.id
271 line['nom'] = employe.nom
272 line['prenom'] = employe.prenom
273
274
275 c = {
276 'title': 'Rapport de remuneration',
277 'employes': output,
9202c5db 278 'headers': list(h.headers()),
e2c0b1ac
PP
279 }
280
281 return render_to_response('rh/rapports/remuneration.html', c, RequestContext(request))
3ebc0952 282
783e077a
JPC
283@login_required
284@drh_or_admin_required
857b5c24
JPC
285def rapports_postes_modelisation(request):
286 c = {}
287 data = []
3c1ba807
JPC
288
289 for f in rh.FamilleEmploi.objects.all():
290 types = rh.TypePoste.objects.filter(famille_emploi=f)
291 data_types = []
292 for t in types.all():
293 postes = rh.Poste.objects.filter(type_poste=t)
294 data_types.append({
295 'num_postes': postes.count(),
296 'postes': postes.all(),
297 'type': f,
857b5c24 298 })
3c1ba807 299
857b5c24 300 data.append({
3c1ba807
JPC
301 'famille': f,
302 'nb_types': types.count(),
303 'types' : data_types
857b5c24
JPC
304 })
305
306 c['data'] = data
307
308
309 return render_to_response('rh/rapports/postes_modelisation.html', c, RequestContext(request))
310
311
312@login_required
313@drh_or_admin_required
783e077a
JPC
314def rapports_postes_implantation(request):
315 c = {}
316 data = []
317 for r in ref.Region.objects.all():
318 implantations = []
319 for i in ref.Implantation.objects.filter(region=r):
320 implantations.append({
321 'implantation': i,
322 'postes': rh.Poste.objects.filter(implantation=i),
323 'num_postes': rh.Poste.objects.filter(implantation=i).count(),
324 })
325 data.append({
326 'region': r,
327 'implantations': implantations
328 })
329
330 c['data'] = data
331
332
333 return render_to_response('rh/rapports/postes_implantation.html', c, RequestContext(request))
334
335
336@login_required
337@drh_or_admin_required
338def rapports_postes_service(request):
339 c = {}
9988aafb
JPC
340 data = []
341 for s in rh.Service.objects.all():
342 postes = rh.Poste.objects.filter(service=s).all()
343 num_postes = rh.Poste.objects.filter(service=s).count()
344 data.append({'service': s, 'num_postes': num_postes, 'postes': postes})
783e077a 345
9988aafb
JPC
346 c['data'] = data
347 return render_to_response('rh/rapports/postes_service.html', c, RequestContext(request))
783e077a
JPC
348
349
32373f2e
OL
350def region_protected(model):
351 def wrapper(func):
352 def wrapped(request, id):
32373f2e
OL
353 if request.user.is_superuser:
354 return func(request, id)
355 user_groups = request.user.groups.all()
356 if grp_drh in user_groups:
357 return func(request, id)
358 if grp_correspondants_rh in user_groups:
359 employe = get_employe_from_user(request.user)
360 q = Q(**{model.prefix_implantation: employe.implantation.region})
361 qs = model.objects.filter(q)
718abc4d 362 if int(id) in [o.id for o in qs]:
32373f2e
OL
363 return func(request, id)
364 return redirect_interdiction(request)
365 return wrapped
366 return wrapper
367
368
369@region_protected(rh.Dossier)
3ebc0952 370def dossier_apercu(request, dossier_id):
baf9b78c 371 d = get_object_or_404(rh.Dossier, pk=dossier_id)
3ebc0952
OL
372 c = {
373 'is_popup' : request.GET.get('_popup', False),
baf9b78c 374 'dossier' : d,
520ab455
JPC
375 'pieces' : rh.DossierPiece.objects.filter(dossier__exact=d),
376 'contrats' : rh.Contrat.objects.filter(dossier__exact=d),
2a36598b 377 'commentaires' : rh.DossierCommentaire.objects.filter(dossier__exact=d).all(),
abf91905 378 'media_url': settings.PRIVE_MEDIA_URL,
3ebc0952
OL
379 }
380 return render_to_response('admin/rh/dossier/apercu.html', c, RequestContext(request))
4a1f2ece 381
fbe60a9d
OL
382@region_protected(rh.Poste)
383def poste_apercu(request, poste_id):
384 c = {
385 'is_popup' : request.GET.get('_popup', False),
0dcfc806
JPC
386 'poste' : get_object_or_404(rh.Poste, pk=poste_id),
387 'financements' : rh.PosteFinancement.objects.filter(poste=poste_id).all(),
388 'pieces' : rh.PostePiece.objects.filter(poste=poste_id).all(),
389 'dossiers' : rh.Dossier.objects.filter(poste=poste_id).order_by("-date_debut").all(),
390 'comparaisons' : rh.PosteComparaison.objects.filter(poste=poste_id).all(),
391 'commentaires' : rh.PosteCommentaire.objects.filter(poste=poste_id).all(),
abf91905 392 'media_url': settings.PRIVE_MEDIA_URL,
fbe60a9d
OL
393 }
394 return render_to_response('admin/rh/poste/apercu.html', c, RequestContext(request))
395
4a1f2ece 396def employe_apercu(request, employe_id):
fbe60a9d 397 from dae.workflow import grp_drh, grp_correspondants_rh
4a1f2ece 398 employe = get_object_or_404(rh.Employe, pk=employe_id)
fbe60a9d
OL
399 user_groups = request.user.groups.all()
400
401 dernier_dossier = None
402 dossiers = None
403
404 if request.user.is_superuser or \
405 grp_drh in user_groups:
406 q = Q(employe=employe)
407 if grp_correspondants_rh in user_groups:
7eeafa57 408 regions = [d.poste.implantation.region for d in employe.rh_dossiers.all()]
fbe60a9d
OL
409 q = Q(employe=employe) & Q(implantation__region__in=regions)
410
411 dossiers = rh.Dossier.objects.filter(employe=employe).order_by('-date_debut')
412
4a1f2ece
OL
413 c = {
414 'is_popup' : request.GET.get('_popup', False),
415 'employe' : employe,
fbe60a9d 416 'dossiers' : dossiers,
abf91905 417 'media_url': settings.PRIVE_MEDIA_URL,
4a1f2ece
OL
418 }
419 return render_to_response('admin/rh/employe/apercu.html', c, RequestContext(request))
150d83ec 420
32373f2e 421