[#3188] genre dans apercu employé
[auf_rh_dae.git] / project / rh / views.py
CommitLineData
e9bbd6ba 1# -*- encoding: utf-8 -*-
22343fe7 2
e90b19fd 3import urllib
c9122f1d 4from datetime import date
a7de62d7 5from itertools import izip
eabb27fd 6import StringIO
9b0a8554 7
9f55f18a 8import pygraphviz as pgv
e949324b 9
e90b19fd 10from django import forms
4ba84959 11from django.conf import settings
e90b19fd 12from django.contrib.auth.decorators import login_required
eabb27fd 13from django.core.servers.basehttp import FileWrapper
dcd1b959 14from django.core.urlresolvers import reverse
fbe60a9d 15from django.db.models import Q
4ba84959 16from django.http import HttpResponse
890b80e7 17from django.shortcuts import render, get_object_or_404
e9bbd6ba 18
4c17e518 19from auf.django.references import models as ref
e9bbd6ba 20
acbc95a1 21from project.decorators import redirect_interdiction
018c8eaf 22from project.decorators import drh_or_admin_required
ae99002a 23from project.decorators import region_protected
afd3be54 24from project.groups import get_employe_from_user
a4198848 25from project.groups import grp_drh, grp_correspondants_rh
e9bbd6ba 26
4ba84959 27from project.rh import models as rh
4ba84959 28from project.rh import graph as rh_graph
890b80e7 29from project.rh.change_list import RechercheTemporelle
2cb9e555 30from project.rh.lib import calc_remun, get_lookup_params
4ba84959 31from project.rh.masse_salariale import MasseSalariale
890b80e7 32from project.rh.templatetags.rapports import SortHeaders
9817fed5 33
22343fe7 34
c9122f1d 35@login_required
36def profil(request):
37 """Profil personnel de l'employé - éditable"""
7d0c2206
DB
38 employe = get_employe_from_user(user)
39 c = {
8b3f6ff5
DB
40 'user': request.user,
41 'employe': employe,
7d0c2206 42 }
890b80e7 43 return render(request, 'rh/profil.html', c)
22343fe7
OL
44
45
3411ac33 46@login_required
a9faef67 47def employes_liste(request):
48 """Liste des employés."""
5ea6b5bb 49 today = date.today()
50 employes = rh.Employe.objects \
5ea6b5bb 51 .exclude(dossiers__date_debut__gt=today) \
52 .exclude(dossiers__date_fin__lt=today) \
53 .order_by('nom')
a9faef67 54 c = {
22343fe7
OL
55 'user': request.user,
56 'employes': employes,
a9faef67 57 }
890b80e7 58 return render(request, 'rh/employes_liste.html', c)
22343fe7
OL
59
60
3411ac33 61@login_required
a9faef67 62def employe(request, id):
63 """Information publique sur un employé."""
64 try:
65 employe = rh.Employe.objects.get(pk=id)
66 except:
67 employe = rh.Employe.objects.none()
68 c = {
22343fe7
OL
69 'user': request.user,
70 'employe': employe,
a9faef67 71 }
890b80e7 72 return render(request, 'rh/employe.html', c)
63e17dff
PP
73
74
08faf06e
JPC
75@login_required
76@drh_or_admin_required
f2d65e83 77def rapports_contrat(request):
dcd1b959 78 if 'HTTP_REFERER' in request.META.keys():
58348377
EMS
79 referer = request.META['HTTP_REFERER']
80 referer = "/".join(referer.split('/')[3:])
81 referer = "/%s" % referer.split('?')[0]
82 if referer != reverse('rhr_contrats'):
83 params = request.GET.copy()
84 params.update({'statut': 'Actif'})
85 request.GET = params
f2d65e83 86
1a7b9e36 87 lookup_params = get_lookup_params(request)
f2d65e83 88
00ca4d9f
EMS
89 contrats = rh.Contrat.objects.select_related(
90 'dossier', 'dossier__poste', 'dossier__poste__implantation',
91 'type_contrat', 'dossier__employe'
92 )
22343fe7 93
6bee05ff 94 cl = RechercheTemporelle(dict(request.GET.items()), rh.Contrat)
f0f6b03e 95 lookup_params = cl.purge_params(lookup_params)
dcd1b959
OL
96 q_temporel = cl.get_q_temporel(contrats)
97 q = Q(**lookup_params) & q_temporel
98 contrats = contrats.filter(q).exclude(dossier__employe__supprime=1)
f0f6b03e 99
65e02621 100 if 'o' in request.GET:
00ca4d9f
EMS
101 contrats = contrats.order_by(
102 ('-' if request.GET.get('ot') == "desc" else '') + request.GET['o']
103 )
f2d65e83 104
8ee135b7
JPC
105 employes = set([c.dossier.employe_id for c in contrats])
106
65e02621
JPC
107 headers = [
108 ("dossier__employe__id", u"# de l'employé"),
d50a5e5d 109 ("dossier__employe__nom", u"Employé"),
1a7b9e36 110 ("dossier__poste__nom", u"Poste"),
d50a5e5d
OL
111 ("dossier__poste__implantation__region", u"Région"),
112 ("dossier__poste__implantation", u"Implantation"),
65e02621 113 ("type_contrat__nom", u"Type de contrat"),
58348377 114 ("dossier__statut_residence", u"Statut de résidence"),
65e02621
JPC
115 ("date_debut", u"Date début"),
116 ("date_fin", u"Date fin"),
117 ]
118 h = SortHeaders(request, headers, order_field_type="ot", order_field="o")
119
f2d65e83 120 c = {
00ca4d9f 121 'cl': cl,
f2d65e83
PP
122 'title': 'Rapport des contrats',
123 'contrats': contrats,
8ee135b7 124 'count': len(contrats),
00ca4d9f 125 'count_employe': len(employes),
65e02621 126 'headers': list(h.headers()),
f2d65e83
PP
127 }
128
890b80e7 129 return render(request, 'rh/rapports/contrats.html', c)
e2c0b1ac
PP
130
131
02c1b3dc
JPC
132@login_required
133@drh_or_admin_required
98d6eb6c 134def rapports_masse_salariale(request):
df37184c 135
e90b19fd 136 class RechercheTemporelle(forms.Form):
aecb222b 137 CHOICE_ANNEES = range(
00ca4d9f
EMS
138 rh.Remuneration.objects.exclude(date_debut=None)
139 .order_by('date_debut')[0].date_debut.year,
140 date.today().year + 1
141 )
142
143 annee = forms.CharField(
144 initial=date.today().year,
145 widget=forms.Select(
146 choices=((a, a) for a in reversed(CHOICE_ANNEES))
147 )
444f3223 148 )
aecb222b 149
48c0abfe
JPC
150 region = forms.CharField(
151 widget=forms.Select(choices=[('', '')] +
152 [(i.id, i) for i in ref.Region.objects.all()]
153 )
154 )
155
156 implantation = forms.CharField(
157 widget=forms.Select(choices=[('', '')] +
158 [(i.id, i) for i in ref.Implantation.objects.all()]
159 )
160 )
161
aecb222b
JPC
162 #date_debut = forms.DateField(widget=adminwidgets.AdminDateWidget)
163 #date_fin = forms.DateField(widget=adminwidgets.AdminDateWidget)
e90b19fd 164
e90b19fd 165 form = RechercheTemporelle(request.GET)
00ca4d9f
EMS
166 get_filtre = [
167 (k, v) for k, v in request.GET.items()
168 if k not in ('date_debut', 'date_fin', 'implantation')
169 ]
e90b19fd
OL
170 query_string = urllib.urlencode(get_filtre)
171
aecb222b
JPC
172 date_debut = None
173 date_fin = None
444f3223
JPC
174 if request.GET.get('annee', None):
175 date_debut = "01-01-%s" % request.GET.get('annee', None)
176 date_fin = "31-12-%s" % request.GET.get('annee', None)
df37184c 177
48c0abfe
JPC
178 implantation = request.GET.get('implantation')
179 region = request.GET.get('region')
df37184c
JPC
180
181 custom_filter = {}
182 if implantation:
183 custom_filter['dossier__poste__implantation'] = implantation
184 if region:
185 custom_filter['dossier__poste__implantation__region'] = region
186
df37184c 187 c = {
e90b19fd
OL
188 'title': 'Rapport de masse salariale',
189 'form': form,
df37184c 190 'headers': [],
e90b19fd 191 'query_string': query_string,
df37184c 192 }
22b53270 193 if date_debut or date_fin:
c99116c3
JPC
194 masse = MasseSalariale(date_debut, date_fin, custom_filter,
195 request.GET.get('ne_pas_grouper', False))
515b83c3 196 if masse.rapport:
eabb27fd 197 if request.GET.get('ods'):
00ca4d9f
EMS
198 for h in (
199 h for h in masse.headers if 'background-color' in h[2]
200 ):
eabb27fd
JPC
201 del h[2]['background-color']
202 masse.ods()
203 output = StringIO.StringIO()
204 masse.doc.save(output)
205 output.seek(0)
206
00ca4d9f
EMS
207 response = HttpResponse(
208 FileWrapper(output),
209 content_type=(
210 'application/vnd.oasis.opendocument.spreadsheet'
211 )
212 )
eabb27fd
JPC
213 response['Content-Disposition'] = \
214 'attachment; filename=Masse Salariale %s.ods' % \
215 masse.annee
216 return response
217 else:
218 c['rapport'] = masse.rapport
219 c['header_keys'] = [h[0] for h in masse.headers]
220 #on enleve le background pour le header
00ca4d9f
EMS
221 for h in (
222 h for h in masse.headers if 'background-color' in h[2]
223 ):
eabb27fd
JPC
224 h[2]['background'] = 'none'
225 h = SortHeaders(request, masse.headers, order_field_type="ot",
226 not_sortable=c['header_keys'], order_field="o")
227 c['headers'] = list(h.headers())
228 for key, nom, opts in masse.headers:
229 c['headers']
230 c['total'] = masse.grand_totaux[0]
231 c['total_euro'] = masse.grand_totaux[1]
232 c['colspan'] = len(c['header_keys']) - 1
233 get_filtre.append(('ods', True))
234 query_string = urllib.urlencode(get_filtre)
235 c['url_ods'] = "%s?%s" % (
236 reverse('rhr_masse_salariale'), query_string)
22b53270 237
890b80e7 238 return render(request, 'rh/rapports/masse_salariale.html', c)
98d6eb6c
JPC
239
240
241@login_required
242@drh_or_admin_required
6b7cf654 243def rapports_employes_sans_contrat(request):
1a7b9e36 244 lookup_params = get_lookup_params(request)
c8b22fd1 245
1a7b9e36
DB
246 # contrats échus
247 contrats_echus = rh.Contrat.objects.filter(
248 date_fin__lt=date.today()
249 ).filter(
250 **lookup_params
251 )
252
253 # dossiers en cours sans contrat
254 dossiers_sans_contrat = rh.Dossier.objects.filter(
255 Q(date_fin=None) | Q(date_fin__gt=date.today()),
256 ).exclude(
257 date_debut__gt=date.today()
258 ).filter(
259 rh_contrats__in=contrats_echus
260 )
261
262 # employés sans contrat
263 employes = rh.Employe.objects.filter(rh_dossiers__in=dossiers_sans_contrat).distinct()
c8b22fd1 264 if 'o' in request.GET:
1a7b9e36 265 employes = employes.order_by(
00ca4d9f
EMS
266 ('-' if request.GET.get('ot') == "desc" else '') + request.GET['o']
267 )
c8b22fd1 268
6b7cf654 269 # affichage
c8b22fd1 270 headers = [
1d2d4dc2 271 ("id", u"# de l'employé"),
1a7b9e36
DB
272 ("nom", u"Employé"),
273 ("dossier", u"Dossier : Poste"),
274 ("dossier_date_debut", u"Début occupation poste"),
275 ("dossier_date_fin", u"Fin occupation poste"),
c8b22fd1 276 ]
00ca4d9f
EMS
277 h = SortHeaders(
278 request, headers, order_field_type="ot", order_field="o",
1a7b9e36 279 not_sortable=('dossier','dossier_date_debut','dossier_date_fin',)
00ca4d9f 280 )
c8b22fd1
JPC
281
282 c = {
283 'title': u'Rapport des employés sans contrat',
284 'employes': employes,
1a7b9e36 285 'dossiers_sans_contrat':dossiers_sans_contrat,
c8b22fd1
JPC
286 'headers': list(h.headers()),
287 }
288
890b80e7 289 return render(request, 'rh/rapports/employes_sans_contrat.html', c)
00ca4d9f 290
c8b22fd1
JPC
291
292@login_required
293@drh_or_admin_required
857b5c24
JPC
294def rapports_postes_modelisation(request):
295 c = {}
296 data = []
3c1ba807 297
7bf28694
EMS
298 for categorie in rh.CategorieEmploi.objects.all():
299 types = rh.TypePoste.objects.filter(categorie_emploi=categorie)
3c1ba807
JPC
300 data_types = []
301 for t in types.all():
302 postes = rh.Poste.objects.filter(type_poste=t)
303 data_types.append({
304 'num_postes': postes.count(),
305 'postes': postes.all(),
7bf28694 306 'type': categorie,
857b5c24 307 })
3c1ba807 308
857b5c24 309 data.append({
7bf28694 310 'categorie': categorie,
3c1ba807 311 'nb_types': types.count(),
00ca4d9f
EMS
312 'types': data_types
313 })
857b5c24
JPC
314
315 c['data'] = data
316
890b80e7 317 return render(request, 'rh/rapports/postes_modelisation.html', c)
857b5c24
JPC
318
319
320@login_required
321@drh_or_admin_required
783e077a
JPC
322def rapports_postes_implantation(request):
323 c = {}
324 data = []
325 for r in ref.Region.objects.all():
326 implantations = []
327 for i in ref.Implantation.objects.filter(region=r):
328 implantations.append({
329 'implantation': i,
330 'postes': rh.Poste.objects.filter(implantation=i),
331 'num_postes': rh.Poste.objects.filter(implantation=i).count(),
332 })
333 data.append({
334 'region': r,
335 'implantations': implantations
336 })
337
338 c['data'] = data
339
890b80e7 340 return render(request, 'rh/rapports/postes_implantation.html', c)
783e077a
JPC
341
342
343@login_required
344@drh_or_admin_required
345def rapports_postes_service(request):
346 c = {}
9988aafb
JPC
347 data = []
348 for s in rh.Service.objects.all():
349 postes = rh.Poste.objects.filter(service=s).all()
350 num_postes = rh.Poste.objects.filter(service=s).count()
22343fe7 351 data.append({'service': s, 'num_postes': num_postes, 'postes': postes})
783e077a 352
9988aafb 353 c['data'] = data
890b80e7 354 return render(request, 'rh/rapports/postes_service.html', c)
32373f2e
OL
355
356@region_protected(rh.Dossier)
3ebc0952 357def dossier_apercu(request, dossier_id):
baf9b78c 358 d = get_object_or_404(rh.Dossier, pk=dossier_id)
3ebc0952 359 c = {
838bc59d 360 'title': u"Dossier %s" % (d, ),
00ca4d9f
EMS
361 'is_popup': request.GET.get('_popup', False),
362 'dossier': d,
363 'pieces': rh.DossierPiece.objects.filter(dossier__exact=d),
364 'contrats': rh.Contrat.objects.filter(dossier__exact=d),
365 'commentaires': rh.DossierCommentaire.objects.filter(dossier=d).all(),
abf91905 366 'media_url': settings.PRIVE_MEDIA_URL,
3ebc0952 367 }
890b80e7 368 return render(request, 'admin/rh/dossier/apercu.html', c)
00ca4d9f 369
4a1f2ece 370
fbe60a9d
OL
371@region_protected(rh.Poste)
372def poste_apercu(request, poste_id):
c8cb18e3 373 p = get_object_or_404(rh.Poste, pk=poste_id)
fbe60a9d 374 c = {
c8cb18e3 375 'title': u"Poste %s" % (p, ),
00ca4d9f
EMS
376 'is_popup': request.GET.get('_popup', False),
377 'poste': p,
378 'financements': (
379 rh.PosteFinancement.objects.filter(poste=poste_id).all()
380 ),
381 'pieces': rh.PostePiece.objects.filter(poste=poste_id).all(),
382 'dossiers': (
383 rh.Dossier.objects.filter(poste=poste_id)
384 .order_by("-date_debut").all()
385 ),
386 'comparaisons': (
387 rh.PosteComparaison.objects.filter(poste=poste_id).all()
388 ),
389 'commentaires': (
390 rh.PosteCommentaire.objects.filter(poste=poste_id).all()
391 ),
abf91905 392 'media_url': settings.PRIVE_MEDIA_URL,
fbe60a9d 393 }
890b80e7 394 return render(request, 'admin/rh/poste/apercu.html', c)
00ca4d9f 395
fbe60a9d 396
4a1f2ece
OL
397def employe_apercu(request, employe_id):
398 employe = get_object_or_404(rh.Employe, pk=employe_id)
fbe60a9d 399 user_groups = request.user.groups.all()
fbe60a9d
OL
400 dossiers = None
401
402 if request.user.is_superuser or \
403 grp_drh in user_groups:
404 q = Q(employe=employe)
405 if grp_correspondants_rh in user_groups:
00ca4d9f
EMS
406 regions = [
407 d.poste.implantation.region for d in employe.rh_dossiers.all()
408 ]
fbe60a9d 409 q = Q(employe=employe) & Q(implantation__region__in=regions)
22343fe7
OL
410
411 dossiers = rh.Dossier.objects.filter(q).order_by('-date_debut')
412
4a1f2ece 413 c = {
ce740bb5 414 'title': u"Employe %s" % (employe, ),
00ca4d9f
EMS
415 'is_popup': request.GET.get('_popup', False),
416 'employe': employe,
417 'dossiers': dossiers,
abf91905 418 'media_url': settings.PRIVE_MEDIA_URL,
4a1f2ece 419 }
890b80e7 420 return render(request, 'admin/rh/employe/apercu.html', c)
00ca4d9f 421
150d83ec 422
77bd83d1
JPC
423@login_required
424@drh_or_admin_required
56264a85 425def organigrammes_employe(request, id, level="all"):
08faf06e
JPC
426
427 poste = get_object_or_404(rh.Poste, pk=id)
00ca4d9f
EMS
428 dossiers_by_poste = dict(
429 (d.poste_id, d)
430 for d in rh.Dossier.objects.select_related('employe', 'poste').all()
431 )
5c0f1778
JPC
432 postes_by_id = dict((p.id, p) for p in rh.Poste.objects.all())
433
434 e = dossiers_by_poste[poste.id].employe
435 name = u"Organigramme de [%s] %s %s" % (e.id, e.nom.upper(), e.prenom)
58014aec 436 graph = pgv.AGraph()
5c0f1778 437
f187a10f 438 if rh.Poste.objects.filter(responsable=poste).count() > 0:
58014aec
JPC
439 postes_handle = [poste]
440 while postes_handle:
00ca4d9f
EMS
441 postes_handle = rh.Poste.objects.select_related('implantation') \
442 .filter(
443 Q(date_fin__gt=date.today()) | Q(date_fin=None),
444 Q(date_debut__lt=date.today()) | Q(date_debut=None),
445 responsable__in=postes_handle
446 ).exclude(supprime=True).exclude(responsable=None).all()
32373f2e 447
58014aec
JPC
448 for p in postes_handle:
449 if p.responsable_id != p.id:
00ca4d9f
EMS
450 graph.add_edge(
451 dossiers_by_poste[p.responsable_id].poste_id, p.id
452 )
13ad5ad5 453
f187a10f 454 else:
5c0f1778
JPC
455 graph.add_node(poste.id)
456
4afec2e1
JPC
457 if level != "all":
458 postes_niveau = [poste.id]
459 for niveau in range(int(level)):
00ca4d9f
EMS
460 postes_niveau = [
461 p.id for p in
462 rh.Poste.objects.filter(responsable__in=postes_niveau).all()
463 ]
4afec2e1
JPC
464
465 while postes_niveau:
00ca4d9f
EMS
466 postes_niveau = [
467 p.id for p in
468 rh.Poste.objects.filter(responsable__in=postes_niveau).all()
469 ]
4afec2e1
JPC
470 if postes_niveau:
471 for p in postes_niveau:
472 if graph.has_node(p):
4cc81304 473 graph.delete_node(p)
4afec2e1 474
58014aec 475 a = graph
4afec2e1 476 a.name = name.encode('ascii', 'xmlcharrefreplace')
5c0f1778
JPC
477
478 poste_remontant = poste
479 while poste_remontant.responsable_id:
480 a.add_edge(poste_remontant.responsable_id, poste_remontant.id)
481 poste_remontant = poste_remontant.responsable
482
483 rh_graph.bind_poste_to_graph(a, postes_by_id)
484 #a.graph_attr['normalize'] = True
485 #a.graph_attr['level'] = 2
486 a.layout(prog='dot')
487
488 svg = a.draw(format='svg')
489
490 c = {
491 'svg': svg
492 }
a251ac8d
JPC
493
494 if 'forcer' in request.GET:
890b80e7 495 response = HttpResponse(svg, content_type='image/svg+xml')
00ca4d9f
EMS
496 response['Content-Disposition'] = \
497 'attachment; filename=organigramme.svg'
a251ac8d
JPC
498 return response
499
890b80e7
DB
500 return render(request, 'rh/organigrammes/employe.html', c,
501 content_type="image/svg+xml"
00ca4d9f 502 )
5c0f1778
JPC
503
504
505@login_required
506@drh_or_admin_required
507def organigrammes_service(request, id):
508
509 service = get_object_or_404(rh.Service, pk=id)
82af5c19
JPC
510 svg = rh_graph.organigramme_postes_cluster( \
511 cluster_filter={"service": service}, \
512 titre=u"Organigramme du service %s" % service.nom,
513 cluster_titre=service.nom)
5c0f1778 514
82af5c19
JPC
515 c = {
516 'svg': svg
517 }
5c0f1778 518
890b80e7
DB
519 return render(request, 'rh/organigrammes/vide.html', c,
520 content_type="image/svg+xml"
00ca4d9f
EMS
521 )
522
5c0f1778 523
82af5c19
JPC
524@login_required
525@drh_or_admin_required
526def organigrammes_implantation(request, id):
5c0f1778 527
82af5c19
JPC
528 implantation = get_object_or_404(ref.Implantation, pk=id)
529 svg = rh_graph.organigramme_postes_cluster( \
530 cluster_filter={"implantation": implantation}, \
531 titre=u"Organigramme de l'implantation %s" % implantation.nom,
532 cluster_titre=implantation.nom)
5c0f1778
JPC
533
534 c = {
535 'svg': svg
536 }
537
890b80e7
DB
538 return render(request, 'rh/organigrammes/vide.html', c,
539 content_type="image/svg+xml"
00ca4d9f
EMS
540 )
541
5c0f1778 542
9da4c195
JPC
543@login_required
544@drh_or_admin_required
545def organigrammes_region(request, id):
546
547 region = get_object_or_404(ref.Region, pk=id)
548 svg = rh_graph.organigramme_postes_cluster( \
549 cluster_filter={"implantation__region": region}, \
550 titre=u"Organigramme du bureau de %s" % region.nom,
551 cluster_titre=region.nom)
552
553 c = {
554 'svg': svg
555 }
556
890b80e7
DB
557 return render(request,
558 'rh/organigrammes/vide.html', c,
559 content_type="image/svg+xml"
00ca4d9f 560 )