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