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