Organigramem d'employé: affichage des postes en corus
[auf_rh_dae.git] / project / rh / views.py
CommitLineData
e9bbd6ba 1# -*- encoding: utf-8 -*-
c9122f1d 2from datetime import date
a7de62d7 3from itertools import izip
08faf06e 4from base64 import b64encode
fa1624f6 5import networkx as nx
9b0a8554 6
fbe60a9d 7from django.db.models import Q
3411ac33 8from django.contrib.auth.decorators import login_required
fd009814 9from django.utils.encoding import smart_str
e9bbd6ba 10from django.shortcuts import redirect, render_to_response, get_object_or_404
11from django.template import RequestContext
9d53bc73 12from django.http import Http404
e9bbd6ba 13
d04d084c 14from datamaster_modeles import models as ref
d04d084c 15from project.lib import get_employe_from_id
ae936f8d 16
d04d084c 17from rh import models as rh
e2c0b1ac 18from rh.lib import calc_remun
02c1b3dc 19from rh.decorators import drh_or_admin_required
9d53bc73 20from rh.templatetags.rapports import SortHeaders
f0f6b03e 21from rh.change_list import RechercheTemporelle
a9faef67 22
9817fed5
OL
23# pas de reference a DAE devrait etre refactorisé
24from dae.utils import get_employe_from_user
25from dae.decorators import redirect_interdiction
26from dae.workflow import grp_drh, grp_correspondants_rh
baf9b78c 27from django.conf import settings
9817fed5 28
3411ac33 29# homes
c9122f1d 30@login_required
31def profil(request):
32 """Profil personnel de l'employé - éditable"""
33 rc = RequestContext(request)
34 c = {}
d04d084c 35
c9122f1d 36 employe = rc['this_employe']
65f9fac8 37
c9122f1d 38 c['user'] = request.user
39 c['employe'] = employe
c9122f1d 40 return render_to_response('rh/profil.html', c, rc)
a9faef67 41
42# employes
3411ac33 43@login_required
a9faef67 44def employes_liste(request):
45 """Liste des employés."""
5ea6b5bb 46 today = date.today()
47 employes = rh.Employe.objects \
5ea6b5bb 48 .exclude(dossiers__date_debut__gt=today) \
49 .exclude(dossiers__date_fin__lt=today) \
50 .order_by('nom')
a9faef67 51 c = {
52 'user':request.user,
53 'employes':employes,
54 }
55 return render_to_response('rh/employes_liste.html', c, RequestContext(request))
56
3411ac33 57@login_required
a9faef67 58def employe(request, id):
59 """Information publique sur un employé."""
60 try:
61 employe = rh.Employe.objects.get(pk=id)
62 except:
63 employe = rh.Employe.objects.none()
64 c = {
65 'user':request.user,
66 'employe':employe,
67 }
68 return render_to_response('rh/employe.html', c, RequestContext(request))
63e17dff
PP
69
70
71# Rapports
72
73@login_required
02c1b3dc 74@drh_or_admin_required
48487f09
JPC
75def rapports_postes_hierarchie(request):
76
77 c = {}
ce18dd0a 78 graph = nx.DiGraph()
fa1624f6
JPC
79 postes = rh.Poste.objects.all()
80 postes_id = dict((p.id, p) for p in postes)
ce18dd0a 81 graph.add_node(p for p in postes)
48487f09 82
fa1624f6
JPC
83 for p in postes:
84 if p.responsable_id != p.id:
ce18dd0a 85 graph.add_edge(postes_id[p.responsable_id], p)
48487f09 86
fa1624f6 87 c['postes'] = nx.to_dict_of_lists(graph)
48487f09
JPC
88
89 return render_to_response('rh/rapports/postes_hierarchie.html', c, RequestContext(request))
90
91@login_required
92@drh_or_admin_required
08faf06e
JPC
93def hierarchie_poste(request):
94 pass
95
96@login_required
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')
f0f6b03e
OL
176
177 cl = RechercheTemporelle(request, rh.Contrat)
178 lookup_params = cl.purge_params(lookup_params)
821b9889 179 contrats = contrats.filter(**lookup_params).exclude(dossier__employe__supprime=1)
f0f6b03e
OL
180 contrats = cl.filter_temporel(contrats)
181
65e02621
JPC
182 if 'o' in request.GET:
183 contrats = contrats.order_by("%s%s" % ('-' if 'ot' in request.GET and request.GET['ot'] == "desc" else '', request.GET['o']))
f2d65e83 184
8ee135b7
JPC
185 employes = set([c.dossier.employe_id for c in contrats])
186
65e02621
JPC
187 headers = [
188 ("dossier__employe__id", u"# de l'employé"),
189 ("dossier__employe__nom", u"Nom"),
190 ("dossier__employe__prenom", u"Prénom"),
191 ("type_contrat__nom", u"Poste"),
192 ("type_contrat__nom", u"Type de contrat"),
193 ("date_debut", u"Date début"),
194 ("date_fin", u"Date fin"),
195 ]
196 h = SortHeaders(request, headers, order_field_type="ot", order_field="o")
197
f2d65e83 198 c = {
f0f6b03e 199 'cl' : cl,
f2d65e83
PP
200 'title': 'Rapport des contrats',
201 'contrats': contrats,
8ee135b7
JPC
202 'count': len(contrats),
203 'count_employe' : len(employes),
65e02621 204 'headers': list(h.headers()),
f2d65e83
PP
205 }
206
207 return render_to_response('rh/rapports/contrats.html', c, RequestContext(request))
e2c0b1ac
PP
208
209
02c1b3dc
JPC
210@login_required
211@drh_or_admin_required
e2c0b1ac
PP
212def rapports_remuneration(request):
213
214 lookup_params = dict(request.GET.items())
9202c5db
JPC
215 if 'ot' in lookup_params:
216 del lookup_params['ot']
217 if 'o' in lookup_params:
218 del lookup_params['o']
e2c0b1ac
PP
219
220 for key, value in lookup_params.items():
221 if not isinstance(key, str):
222 # 'key' will be used as a keyword argument later, so Python
223 # requires it to be a string.
224 del lookup_params[key]
225 lookup_params[smart_str(key)] = value
226
227 employes = rh.Employe.objects.all()
9202c5db
JPC
228 if 'o' in request.GET:
229 employes = employes.order_by("%s%s" % ('-' if 'ot' in request.GET and request.GET['ot'] == "desc" else '', request.GET['o']))
230
e2c0b1ac
PP
231 employes = employes.filter(**lookup_params)
232
233 output = []
9202c5db
JPC
234 headers = [
235 ("id", u"# de l'employé"),
236 ("nom", u"Nom"),
237 ("prenom", u"Prénom"),
238 ("", u"Salaire"),
239 ("", u"RAS"),
240 ("", u"Indemnités"),
241 ("", u"Accessoire"),
242 ("", u"Charges patronales"),
243 ("", u"Total"),
244 ]
245 h = SortHeaders(request, headers, order_field_type="ot", order_field="o")
e2c0b1ac
PP
246
247 for employe in employes:
248 line = {}
249 output.append(line)
250
da6c523f 251 dossiers = employe.rh_dossiers.all()
e2c0b1ac
PP
252
253 remun = {}
254 remun_sum_euro = 0
255
256 for dossier in dossiers:
257 this_remun, this_remun_sum, this_remun_sum_euro = calc_remun(dossier)
258
259 for item in this_remun:
260 if item not in remun:
261 remun[item] = this_remun[item]
262 else:
263 remun[item][0] += this_remun[item][0]
264 remun[item][1] += this_remun[item][1]
265
266 remun_sum_euro += this_remun_sum_euro
267
268 line['remun_sum_euro'] = remun_sum_euro
269
270 for r in remun:
271 if r == u'Indemnité':
e2c0b1ac
PP
272 line['Indemnite'] = remun[r][1]
273 else:
274 line[r] = remun[r][1]
275
276 line['id'] = employe.id
277 line['nom'] = employe.nom
278 line['prenom'] = employe.prenom
279
280
281 c = {
282 'title': 'Rapport de remuneration',
283 'employes': output,
9202c5db 284 'headers': list(h.headers()),
e2c0b1ac
PP
285 }
286
287 return render_to_response('rh/rapports/remuneration.html', c, RequestContext(request))
3ebc0952 288
783e077a
JPC
289@login_required
290@drh_or_admin_required
c8b22fd1
JPC
291def rapports_employe_sans_contrat(request):
292
293 lookup_params = dict(request.GET.items())
294 if 'ot' in lookup_params:
295 del lookup_params['ot']
296 if 'o' in lookup_params:
297 del lookup_params['o']
298
299 for key, value in lookup_params.items():
300 if not isinstance(key, str):
301 # 'key' will be used as a keyword argument later, so Python
302 # requires it to be a string.
303 del lookup_params[key]
304 lookup_params[smart_str(key)] = value
305
306 employes_query = rh.Employe.objects
307 if 'o' in request.GET:
308 employes_query = employes_query.order_by("%s%s" % ('-' if 'ot' in request.GET and request.GET['ot'] == "desc" else '', request.GET['o']))
309
7c5645f0 310 employes = {}
00c9d7c2
OL
311
312 dossiers_en_cours = rh.Dossier.objects.filter(Q(date_fin=None) | Q(date_fin__gt=date.today()))
313 tous_contrats_echus = rh.Contrat.objects.filter(date_fin__lt=date.today(), dossier__in=dossiers_en_cours)
314 contrats = tous_contrats_echus.filter(**lookup_params).all()
315 for c in contrats:
316 if c.dossier.employe.id not in employes.keys():
317 employes[c.dossier.employe.id] = {'employe': c.dossier.employe, 'dossiers': []}
318 employes[c.dossier.employe.id]['dossiers'] += [c.dossier,]
319
320
c8b22fd1
JPC
321
322 headers = [
1d2d4dc2
JPC
323 ("id", u"# de l'employé"),
324 ("nom", u"Nom"),
325 ("prenom", u"Prénom"),
80518280 326 ("dossier", u"Dossiers"),
c8b22fd1 327 ]
80518280 328 h = SortHeaders(request, headers, order_field_type="ot", order_field="o", not_sortable=('dossier',))
c8b22fd1
JPC
329
330 c = {
331 'title': u'Rapport des employés sans contrat',
332 'employes': employes,
333 'count': len(employes),
334 'headers': list(h.headers()),
335 }
336
337 return render_to_response('rh/rapports/employes_sans_contrat.html', c, RequestContext(request))
338
339@login_required
340@drh_or_admin_required
857b5c24
JPC
341def rapports_postes_modelisation(request):
342 c = {}
343 data = []
3c1ba807
JPC
344
345 for f in rh.FamilleEmploi.objects.all():
346 types = rh.TypePoste.objects.filter(famille_emploi=f)
347 data_types = []
348 for t in types.all():
349 postes = rh.Poste.objects.filter(type_poste=t)
350 data_types.append({
351 'num_postes': postes.count(),
352 'postes': postes.all(),
353 'type': f,
857b5c24 354 })
3c1ba807 355
857b5c24 356 data.append({
3c1ba807
JPC
357 'famille': f,
358 'nb_types': types.count(),
359 'types' : data_types
857b5c24
JPC
360 })
361
362 c['data'] = data
363
364
365 return render_to_response('rh/rapports/postes_modelisation.html', c, RequestContext(request))
366
367
368@login_required
369@drh_or_admin_required
783e077a
JPC
370def rapports_postes_implantation(request):
371 c = {}
372 data = []
373 for r in ref.Region.objects.all():
374 implantations = []
375 for i in ref.Implantation.objects.filter(region=r):
376 implantations.append({
377 'implantation': i,
378 'postes': rh.Poste.objects.filter(implantation=i),
379 'num_postes': rh.Poste.objects.filter(implantation=i).count(),
380 })
381 data.append({
382 'region': r,
383 'implantations': implantations
384 })
385
386 c['data'] = data
387
388
389 return render_to_response('rh/rapports/postes_implantation.html', c, RequestContext(request))
390
391
392@login_required
393@drh_or_admin_required
394def rapports_postes_service(request):
395 c = {}
9988aafb
JPC
396 data = []
397 for s in rh.Service.objects.all():
398 postes = rh.Poste.objects.filter(service=s).all()
399 num_postes = rh.Poste.objects.filter(service=s).count()
400 data.append({'service': s, 'num_postes': num_postes, 'postes': postes})
783e077a 401
9988aafb
JPC
402 c['data'] = data
403 return render_to_response('rh/rapports/postes_service.html', c, RequestContext(request))
783e077a
JPC
404
405
32373f2e
OL
406def region_protected(model):
407 def wrapper(func):
408 def wrapped(request, id):
32373f2e
OL
409 if request.user.is_superuser:
410 return func(request, id)
411 user_groups = request.user.groups.all()
412 if grp_drh in user_groups:
413 return func(request, id)
414 if grp_correspondants_rh in user_groups:
415 employe = get_employe_from_user(request.user)
416 q = Q(**{model.prefix_implantation: employe.implantation.region})
417 qs = model.objects.filter(q)
718abc4d 418 if int(id) in [o.id for o in qs]:
32373f2e
OL
419 return func(request, id)
420 return redirect_interdiction(request)
421 return wrapped
422 return wrapper
423
424
425@region_protected(rh.Dossier)
3ebc0952 426def dossier_apercu(request, dossier_id):
baf9b78c 427 d = get_object_or_404(rh.Dossier, pk=dossier_id)
3ebc0952
OL
428 c = {
429 'is_popup' : request.GET.get('_popup', False),
baf9b78c 430 'dossier' : d,
520ab455
JPC
431 'pieces' : rh.DossierPiece.objects.filter(dossier__exact=d),
432 'contrats' : rh.Contrat.objects.filter(dossier__exact=d),
2a36598b 433 'commentaires' : rh.DossierCommentaire.objects.filter(dossier__exact=d).all(),
abf91905 434 'media_url': settings.PRIVE_MEDIA_URL,
3ebc0952
OL
435 }
436 return render_to_response('admin/rh/dossier/apercu.html', c, RequestContext(request))
4a1f2ece 437
fbe60a9d
OL
438@region_protected(rh.Poste)
439def poste_apercu(request, poste_id):
440 c = {
441 'is_popup' : request.GET.get('_popup', False),
0dcfc806
JPC
442 'poste' : get_object_or_404(rh.Poste, pk=poste_id),
443 'financements' : rh.PosteFinancement.objects.filter(poste=poste_id).all(),
444 'pieces' : rh.PostePiece.objects.filter(poste=poste_id).all(),
445 'dossiers' : rh.Dossier.objects.filter(poste=poste_id).order_by("-date_debut").all(),
446 'comparaisons' : rh.PosteComparaison.objects.filter(poste=poste_id).all(),
447 'commentaires' : rh.PosteCommentaire.objects.filter(poste=poste_id).all(),
abf91905 448 'media_url': settings.PRIVE_MEDIA_URL,
fbe60a9d
OL
449 }
450 return render_to_response('admin/rh/poste/apercu.html', c, RequestContext(request))
451
4a1f2ece 452def employe_apercu(request, employe_id):
fbe60a9d 453 from dae.workflow import grp_drh, grp_correspondants_rh
4a1f2ece 454 employe = get_object_or_404(rh.Employe, pk=employe_id)
fbe60a9d
OL
455 user_groups = request.user.groups.all()
456
457 dernier_dossier = None
458 dossiers = None
459
460 if request.user.is_superuser or \
461 grp_drh in user_groups:
462 q = Q(employe=employe)
463 if grp_correspondants_rh in user_groups:
7eeafa57 464 regions = [d.poste.implantation.region for d in employe.rh_dossiers.all()]
fbe60a9d
OL
465 q = Q(employe=employe) & Q(implantation__region__in=regions)
466
467 dossiers = rh.Dossier.objects.filter(employe=employe).order_by('-date_debut')
468
4a1f2ece
OL
469 c = {
470 'is_popup' : request.GET.get('_popup', False),
471 'employe' : employe,
fbe60a9d 472 'dossiers' : dossiers,
abf91905 473 'media_url': settings.PRIVE_MEDIA_URL,
4a1f2ece
OL
474 }
475 return render_to_response('admin/rh/employe/apercu.html', c, RequestContext(request))
150d83ec 476
08faf06e
JPC
477def organigrammes_employe(request, id):
478
479 poste = get_object_or_404(rh.Poste, pk=id)
480
481 graph = nx.DiGraph()
13ad5ad5 482 postes = rh.Poste.objects.select_related('implantation').filter((Q(date_fin__gt=date.today()) | Q(date_fin=None)) & (Q(date_debut__lt=date.today()) | Q(date_debut=None)) ).exclude(supprime=True).exclude(responsable=None).all()
08faf06e 483 dossiers_by_poste = dict((d.poste_id, d) for d in rh.Dossier.objects.select_related('employe', 'poste').all())
13ad5ad5
JPC
484 postes_by_id = dict((p.id, p) for p in rh.Poste.objects.all())
485
486 for p in rh.Poste.objects.filter((Q(date_fin__gt=date.today()) | Q(date_fin=None)) & (Q(date_debut__lt=date.today()) | Q(date_debut=None)) ).exclude(supprime=True).exclude(responsable=None).all():
08faf06e
JPC
487 graph.add_node(p.id)
488
13ad5ad5
JPC
489 for p in postes:
490 if p.responsable_id != p.id:
491 graph.add_edge(dossiers_by_poste[p.responsable_id].poste_id, p.id)
32373f2e 492
835748f9 493 graph = nx.bfs_tree(graph, poste.id)
08faf06e
JPC
494 a = nx.to_agraph(graph)
495 for n in a.nodes():
13ad5ad5
JPC
496 p = postes_by_id[int(n)]
497 d = rh.Dossier.objects.select_related('employe').filter((Q(date_fin__gt=date.today()) | Q(date_fin=None)) & (Q(date_debut__lt=date.today()) | Q(date_debut=None)) & Q(poste=p)).exclude(supprime=True).exclude(poste__responsable=None).all()[0]
498
499 n.attr['label'] = u"%s %s\\n%s\\n%s" % (d.id, d.poste.nom, "%s %s" % (d.employe.nom.upper(), d.employe.prenom), d.poste.implantation)
08faf06e
JPC
500
501 a.graph_attr['normalize'] = True
502 a.graph_attr['level'] = 2
503 a.layout(prog='dot')
504
505 png = a.draw(format='png')
506
507
508 c = {
509 'png': b64encode(png)
510 }
511 return render_to_response('rh/organigrammes/employe.html', c, RequestContext(request))