redirect_interdiction niveau project
[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
4ba84959
EMS
10from django import forms
11from django.conf import settings
12from django.contrib.auth.decorators import login_required
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
4ba84959 18from django.utils.encoding import smart_str
a9faef67 19
4c17e518 20from auf.django.references import models as ref
e9bbd6ba 21
7d0c2206 22from project.lib import get_employe_from_user
acbc95a1 23from project.decorators import redirect_interdiction
7d0c2206 24
4ba84959 25from project.rh import models as rh
4ba84959 26from project.rh import graph as rh_graph
890b80e7
DB
27from project.rh.change_list import RechercheTemporelle
28from project.rh.decorators import drh_or_admin_required
29from project.rh.lib import calc_remun
4ba84959 30from project.rh.masse_salariale import MasseSalariale
890b80e7 31from project.rh.templatetags.rapports import SortHeaders
9817fed5 32
9817fed5 33# pas de reference a DAE devrait etre refactorisé
890b80e7 34from project.dae.utils import get_employe_from_user
75f0e87b 35from project.dae.workflow import grp_drh, grp_correspondants_rh
22343fe7 36
22343fe7 37
c9122f1d 38@login_required
39def profil(request):
40 """Profil personnel de l'employé - éditable"""
7d0c2206
DB
41 employe = get_employe_from_user(user)
42 c = {
43 'user' = request.user,
44 'employe' = employe,
45 }
890b80e7 46 return render(request, 'rh/profil.html', c)
22343fe7
OL
47
48
3411ac33 49@login_required
a9faef67 50def employes_liste(request):
51 """Liste des employés."""
5ea6b5bb 52 today = date.today()
53 employes = rh.Employe.objects \
5ea6b5bb 54 .exclude(dossiers__date_debut__gt=today) \
55 .exclude(dossiers__date_fin__lt=today) \
56 .order_by('nom')
a9faef67 57 c = {
22343fe7
OL
58 'user': request.user,
59 'employes': employes,
a9faef67 60 }
890b80e7 61 return render(request, 'rh/employes_liste.html', c)
22343fe7
OL
62
63
3411ac33 64@login_required
a9faef67 65def employe(request, id):
66 """Information publique sur un employé."""
67 try:
68 employe = rh.Employe.objects.get(pk=id)
69 except:
70 employe = rh.Employe.objects.none()
71 c = {
22343fe7
OL
72 'user': request.user,
73 'employe': employe,
a9faef67 74 }
890b80e7 75 return render(request, 'rh/employe.html', c)
63e17dff
PP
76
77
08faf06e
JPC
78@login_required
79@drh_or_admin_required
63e17dff
PP
80def rapports_poste(request):
81
fd009814
PP
82 lookup_params = dict(request.GET.items())
83
84 for key, value in lookup_params.items():
9d53bc73
JPC
85 if key == 'o' or key == 'ot':
86 del lookup_params[key]
87 continue
fd009814
PP
88 if not isinstance(key, str):
89 # 'key' will be used as a keyword argument later, so Python
90 # requires it to be a string.
91 del lookup_params[key]
92 lookup_params[smart_str(key)] = value
93
7821915f 94 if key == 'comble':
7821915f
PP
95 del lookup_params[key]
96
00ca4d9f
EMS
97 sort_order = 'ASC' if request.GET.get('ot') == 'asc' else 'DESC'
98
ed3cbb32 99 postes = rh.Poste.objects.select_related('implantation') \
00ca4d9f
EMS
100 .extra(select={
101 'employe_id':
102 'SELECT GROUP_CONCAT(employe SEPARATOR "|") '
103 'FROM rh_dossier '
104 'WHERE poste = rh_poste.id '
105 'AND rh_dossier.date_fin IS NULL ' + (
106 'ORDER BY employe ' + sort_order
107 if request.GET.get('o') == 'employe_id' else ''
108 )
9202c5db 109 }) \
00ca4d9f
EMS
110 .extra(select={
111 'employe_nom':
112 'SELECT GROUP_CONCAT(rh_employe.nom SEPARATOR "|") '
113 'FROM rh_dossier INNER JOIN rh_employe '
114 'ON rh_dossier.employe = rh_employe.id '
115 'WHERE poste = rh_poste.id '
116 'AND rh_dossier.date_fin IS NULL ' + (
117 'ORDER BY rh_employe.nom ' + sort_order
118 if request.GET.get('o') == 'employe_nom' else ''
119 )
9202c5db 120 }) \
00ca4d9f
EMS
121 .extra(select={
122 'employe_prenom':
123 'SELECT GROUP_CONCAT(rh_employe.prenom SEPARATOR "|") '
124 'FROM rh_dossier INNER JOIN rh_employe '
125 'ON rh_dossier.employe = rh_employe.id '
126 'WHERE poste = rh_poste.id '
127 'AND rh_dossier.date_fin IS NULL ' + (
128 'ORDER BY rh_employe.prenom ' + sort_order
129 if request.GET.get('o') == 'employe_prenom' else ''
130 )
9202c5db 131 })
9d53bc73 132
65e02621 133 postes = postes.filter(**lookup_params)
9d53bc73 134 if 'o' in request.GET:
00ca4d9f
EMS
135 postes = postes.order_by(
136 ('-' if sort_order == "DESC" else '') + request.GET['o']
137 )
65e02621 138
a7de62d7
JPC
139 out = []
140 for p in postes:
141 out.append({
142 'id': p.id,
143 'nom': p.nom,
144 'implantation': p.implantation,
00ca4d9f
EMS
145 'employes': [] if not p.employe_id else [
146 {'id': id, 'nom': nom, 'prenom': prenom}
147 for id, nom, prenom in izip(
148 p.employe_id.split('|'),
149 p.employe_nom.split('|'),
150 p.employe_prenom.split('|')
151 )
152 ]
153 })
a7de62d7 154
4dade240
JPC
155 headers = [
156 ("id", u"# du poste"),
157 ("nom", u"Nom du poste"),
65e02621 158 ("implantation__id", u"Implantation"),
4dade240 159 ("employe_nom", u"Nom"),
4dade240
JPC
160 ]
161 h = SortHeaders(request, headers, order_field_type="ot", order_field="o")
63e17dff
PP
162 c = {
163 'title': 'Rapport des postes',
a7de62d7
JPC
164 'postes': out,
165 'count': len(out),
65e02621 166 'headers': list(h.headers()),
63e17dff
PP
167 }
168
890b80e7 169 return render(request, 'rh/rapports/postes.html', c)
00ca4d9f 170
f2d65e83 171
02c1b3dc
JPC
172@login_required
173@drh_or_admin_required
f2d65e83 174def rapports_contrat(request):
dcd1b959
OL
175 if 'HTTP_REFERER' in request.META.keys():
176 referer = request.META['HTTP_REFERER']
177 referer = "/".join(referer.split('/')[3:])
178 referer = "/%s" % referer.split('?')[0]
179 if referer != reverse('rhr_contrats'):
180 params = request.GET.copy()
181 params.update({'statut': 'Actif'})
182 request.GET = params
f2d65e83
PP
183
184 lookup_params = dict(request.GET.items())
65e02621
JPC
185 if 'ot' in lookup_params:
186 del lookup_params['ot']
187 if 'o' in lookup_params:
188 del lookup_params['o']
f2d65e83
PP
189
190 for key, value in lookup_params.items():
191 if not isinstance(key, str):
192 # 'key' will be used as a keyword argument later, so Python
193 # requires it to be a string.
194 del lookup_params[key]
195 lookup_params[smart_str(key)] = value
196
00ca4d9f
EMS
197 contrats = rh.Contrat.objects.select_related(
198 'dossier', 'dossier__poste', 'dossier__poste__implantation',
199 'type_contrat', 'dossier__employe'
200 )
22343fe7 201
6bee05ff 202 cl = RechercheTemporelle(dict(request.GET.items()), rh.Contrat)
f0f6b03e 203 lookup_params = cl.purge_params(lookup_params)
dcd1b959
OL
204 q_temporel = cl.get_q_temporel(contrats)
205 q = Q(**lookup_params) & q_temporel
206 contrats = contrats.filter(q).exclude(dossier__employe__supprime=1)
f0f6b03e 207
65e02621 208 if 'o' in request.GET:
00ca4d9f
EMS
209 contrats = contrats.order_by(
210 ('-' if request.GET.get('ot') == "desc" else '') + request.GET['o']
211 )
f2d65e83 212
8ee135b7
JPC
213 employes = set([c.dossier.employe_id for c in contrats])
214
65e02621
JPC
215 headers = [
216 ("dossier__employe__id", u"# de l'employé"),
d50a5e5d 217 ("dossier__employe__nom", u"Employé"),
65e02621 218 ("type_contrat__nom", u"Poste"),
d50a5e5d
OL
219 ("dossier__poste__implantation__region", u"Région"),
220 ("dossier__poste__implantation", u"Implantation"),
65e02621
JPC
221 ("type_contrat__nom", u"Type de contrat"),
222 ("date_debut", u"Date début"),
223 ("date_fin", u"Date fin"),
224 ]
225 h = SortHeaders(request, headers, order_field_type="ot", order_field="o")
226
f2d65e83 227 c = {
00ca4d9f 228 'cl': cl,
f2d65e83
PP
229 'title': 'Rapport des contrats',
230 'contrats': contrats,
8ee135b7 231 'count': len(contrats),
00ca4d9f 232 'count_employe': len(employes),
65e02621 233 'headers': list(h.headers()),
f2d65e83
PP
234 }
235
890b80e7 236 return render(request, 'rh/rapports/contrats.html', c)
e2c0b1ac
PP
237
238
02c1b3dc
JPC
239@login_required
240@drh_or_admin_required
98d6eb6c 241def rapports_masse_salariale(request):
df37184c 242
e90b19fd 243 class RechercheTemporelle(forms.Form):
aecb222b 244 CHOICE_ANNEES = range(
00ca4d9f
EMS
245 rh.Remuneration.objects.exclude(date_debut=None)
246 .order_by('date_debut')[0].date_debut.year,
247 date.today().year + 1
248 )
249
250 annee = forms.CharField(
251 initial=date.today().year,
252 widget=forms.Select(
253 choices=((a, a) for a in reversed(CHOICE_ANNEES))
254 )
444f3223 255 )
aecb222b 256
48c0abfe
JPC
257 region = forms.CharField(
258 widget=forms.Select(choices=[('', '')] +
259 [(i.id, i) for i in ref.Region.objects.all()]
260 )
261 )
262
263 implantation = forms.CharField(
264 widget=forms.Select(choices=[('', '')] +
265 [(i.id, i) for i in ref.Implantation.objects.all()]
266 )
267 )
268
aecb222b
JPC
269 #date_debut = forms.DateField(widget=adminwidgets.AdminDateWidget)
270 #date_fin = forms.DateField(widget=adminwidgets.AdminDateWidget)
e90b19fd 271
e90b19fd 272 form = RechercheTemporelle(request.GET)
00ca4d9f
EMS
273 get_filtre = [
274 (k, v) for k, v in request.GET.items()
275 if k not in ('date_debut', 'date_fin', 'implantation')
276 ]
e90b19fd
OL
277 query_string = urllib.urlencode(get_filtre)
278
aecb222b
JPC
279 date_debut = None
280 date_fin = None
444f3223
JPC
281 if request.GET.get('annee', None):
282 date_debut = "01-01-%s" % request.GET.get('annee', None)
283 date_fin = "31-12-%s" % request.GET.get('annee', None)
df37184c 284
48c0abfe
JPC
285 implantation = request.GET.get('implantation')
286 region = request.GET.get('region')
df37184c
JPC
287
288 custom_filter = {}
289 if implantation:
290 custom_filter['dossier__poste__implantation'] = implantation
291 if region:
292 custom_filter['dossier__poste__implantation__region'] = region
293
df37184c 294 c = {
e90b19fd
OL
295 'title': 'Rapport de masse salariale',
296 'form': form,
df37184c 297 'headers': [],
e90b19fd 298 'query_string': query_string,
df37184c 299 }
22b53270 300 if date_debut or date_fin:
c99116c3
JPC
301 masse = MasseSalariale(date_debut, date_fin, custom_filter,
302 request.GET.get('ne_pas_grouper', False))
515b83c3 303 if masse.rapport:
eabb27fd 304 if request.GET.get('ods'):
00ca4d9f
EMS
305 for h in (
306 h for h in masse.headers if 'background-color' in h[2]
307 ):
eabb27fd
JPC
308 del h[2]['background-color']
309 masse.ods()
310 output = StringIO.StringIO()
311 masse.doc.save(output)
312 output.seek(0)
313
00ca4d9f
EMS
314 response = HttpResponse(
315 FileWrapper(output),
316 content_type=(
317 'application/vnd.oasis.opendocument.spreadsheet'
318 )
319 )
eabb27fd
JPC
320 response['Content-Disposition'] = \
321 'attachment; filename=Masse Salariale %s.ods' % \
322 masse.annee
323 return response
324 else:
325 c['rapport'] = masse.rapport
326 c['header_keys'] = [h[0] for h in masse.headers]
327 #on enleve le background pour le header
00ca4d9f
EMS
328 for h in (
329 h for h in masse.headers if 'background-color' in h[2]
330 ):
eabb27fd
JPC
331 h[2]['background'] = 'none'
332 h = SortHeaders(request, masse.headers, order_field_type="ot",
333 not_sortable=c['header_keys'], order_field="o")
334 c['headers'] = list(h.headers())
335 for key, nom, opts in masse.headers:
336 c['headers']
337 c['total'] = masse.grand_totaux[0]
338 c['total_euro'] = masse.grand_totaux[1]
339 c['colspan'] = len(c['header_keys']) - 1
340 get_filtre.append(('ods', True))
341 query_string = urllib.urlencode(get_filtre)
342 c['url_ods'] = "%s?%s" % (
343 reverse('rhr_masse_salariale'), query_string)
22b53270 344
890b80e7 345 return render(request, 'rh/rapports/masse_salariale.html', c)
98d6eb6c
JPC
346
347
348@login_required
349@drh_or_admin_required
e2c0b1ac
PP
350def rapports_remuneration(request):
351
352 lookup_params = dict(request.GET.items())
9202c5db
JPC
353 if 'ot' in lookup_params:
354 del lookup_params['ot']
355 if 'o' in lookup_params:
356 del lookup_params['o']
e2c0b1ac
PP
357
358 for key, value in lookup_params.items():
359 if not isinstance(key, str):
360 # 'key' will be used as a keyword argument later, so Python
361 # requires it to be a string.
362 del lookup_params[key]
363 lookup_params[smart_str(key)] = value
364
365 employes = rh.Employe.objects.all()
9202c5db 366 if 'o' in request.GET:
00ca4d9f
EMS
367 employes = employes.order_by(
368 ('-' if request.GET.get('ot') == "desc" else '') + request.GET['o']
369 )
9202c5db 370
e2c0b1ac
PP
371 employes = employes.filter(**lookup_params)
372
373 output = []
9202c5db
JPC
374 headers = [
375 ("id", u"# de l'employé"),
376 ("nom", u"Nom"),
377 ("prenom", u"Prénom"),
378 ("", u"Salaire"),
379 ("", u"RAS"),
380 ("", u"Indemnités"),
381 ("", u"Accessoire"),
382 ("", u"Charges patronales"),
383 ("", u"Total"),
384 ]
385 h = SortHeaders(request, headers, order_field_type="ot", order_field="o")
e2c0b1ac
PP
386
387 for employe in employes:
388 line = {}
389 output.append(line)
390
da6c523f 391 dossiers = employe.rh_dossiers.all()
e2c0b1ac
PP
392
393 remun = {}
394 remun_sum_euro = 0
395
396 for dossier in dossiers:
00ca4d9f
EMS
397 this_remun, this_remun_sum, this_remun_sum_euro = \
398 calc_remun(dossier)
e2c0b1ac
PP
399
400 for item in this_remun:
401 if item not in remun:
402 remun[item] = this_remun[item]
403 else:
404 remun[item][0] += this_remun[item][0]
405 remun[item][1] += this_remun[item][1]
406
407 remun_sum_euro += this_remun_sum_euro
408
409 line['remun_sum_euro'] = remun_sum_euro
410
411 for r in remun:
412 if r == u'Indemnité':
e2c0b1ac
PP
413 line['Indemnite'] = remun[r][1]
414 else:
415 line[r] = remun[r][1]
416
417 line['id'] = employe.id
418 line['nom'] = employe.nom
419 line['prenom'] = employe.prenom
420
e2c0b1ac
PP
421 c = {
422 'title': 'Rapport de remuneration',
423 'employes': output,
9202c5db 424 'headers': list(h.headers()),
e2c0b1ac
PP
425 }
426
890b80e7 427 return render(request, 'rh/rapports/remuneration.html', c)
00ca4d9f 428
3ebc0952 429
783e077a
JPC
430@login_required
431@drh_or_admin_required
c8b22fd1
JPC
432def rapports_employe_sans_contrat(request):
433
434 lookup_params = dict(request.GET.items())
435 if 'ot' in lookup_params:
436 del lookup_params['ot']
437 if 'o' in lookup_params:
438 del lookup_params['o']
439
440 for key, value in lookup_params.items():
441 if not isinstance(key, str):
442 # 'key' will be used as a keyword argument later, so Python
443 # requires it to be a string.
444 del lookup_params[key]
445 lookup_params[smart_str(key)] = value
446
447 employes_query = rh.Employe.objects
448 if 'o' in request.GET:
00ca4d9f
EMS
449 employes_query = employes_query.order_by(
450 ('-' if request.GET.get('ot') == "desc" else '') + request.GET['o']
451 )
c8b22fd1 452
7c5645f0 453 employes = {}
22343fe7 454
00ca4d9f
EMS
455 dossiers_en_cours = rh.Dossier.objects.filter(
456 Q(date_fin=None) | Q(date_fin__gt=date.today())
457 )
458 tous_contrats_echus = rh.Contrat.objects.filter(
459 date_fin__lt=date.today(), dossier__in=dossiers_en_cours
460 )
00c9d7c2
OL
461 contrats = tous_contrats_echus.filter(**lookup_params).all()
462 for c in contrats:
463 if c.dossier.employe.id not in employes.keys():
00ca4d9f
EMS
464 employes[c.dossier.employe.id] = {
465 'employe': c.dossier.employe,
466 'dossiers': []
467 }
468 employes[c.dossier.employe.id]['dossiers'] += [c.dossier]
c8b22fd1
JPC
469
470 headers = [
1d2d4dc2
JPC
471 ("id", u"# de l'employé"),
472 ("nom", u"Nom"),
473 ("prenom", u"Prénom"),
80518280 474 ("dossier", u"Dossiers"),
c8b22fd1 475 ]
00ca4d9f
EMS
476 h = SortHeaders(
477 request, headers, order_field_type="ot", order_field="o",
478 not_sortable=('dossier',)
479 )
c8b22fd1
JPC
480
481 c = {
482 'title': u'Rapport des employés sans contrat',
483 'employes': employes,
484 'count': len(employes),
485 'headers': list(h.headers()),
486 }
487
890b80e7 488 return render(request, 'rh/rapports/employes_sans_contrat.html', c)
00ca4d9f 489
c8b22fd1
JPC
490
491@login_required
492@drh_or_admin_required
857b5c24
JPC
493def rapports_postes_modelisation(request):
494 c = {}
495 data = []
3c1ba807 496
7bf28694
EMS
497 for categorie in rh.CategorieEmploi.objects.all():
498 types = rh.TypePoste.objects.filter(categorie_emploi=categorie)
3c1ba807
JPC
499 data_types = []
500 for t in types.all():
501 postes = rh.Poste.objects.filter(type_poste=t)
502 data_types.append({
503 'num_postes': postes.count(),
504 'postes': postes.all(),
7bf28694 505 'type': categorie,
857b5c24 506 })
3c1ba807 507
857b5c24 508 data.append({
7bf28694 509 'categorie': categorie,
3c1ba807 510 'nb_types': types.count(),
00ca4d9f
EMS
511 'types': data_types
512 })
857b5c24
JPC
513
514 c['data'] = data
515
890b80e7 516 return render(request, 'rh/rapports/postes_modelisation.html', c)
857b5c24
JPC
517
518
519@login_required
520@drh_or_admin_required
783e077a
JPC
521def rapports_postes_implantation(request):
522 c = {}
523 data = []
524 for r in ref.Region.objects.all():
525 implantations = []
526 for i in ref.Implantation.objects.filter(region=r):
527 implantations.append({
528 'implantation': i,
529 'postes': rh.Poste.objects.filter(implantation=i),
530 'num_postes': rh.Poste.objects.filter(implantation=i).count(),
531 })
532 data.append({
533 'region': r,
534 'implantations': implantations
535 })
536
537 c['data'] = data
538
890b80e7 539 return render(request, 'rh/rapports/postes_implantation.html', c)
783e077a
JPC
540
541
542@login_required
543@drh_or_admin_required
544def rapports_postes_service(request):
545 c = {}
9988aafb
JPC
546 data = []
547 for s in rh.Service.objects.all():
548 postes = rh.Poste.objects.filter(service=s).all()
549 num_postes = rh.Poste.objects.filter(service=s).count()
22343fe7 550 data.append({'service': s, 'num_postes': num_postes, 'postes': postes})
783e077a 551
9988aafb 552 c['data'] = data
890b80e7 553 return render(request, 'rh/rapports/postes_service.html', c)
783e077a
JPC
554
555
32373f2e
OL
556def region_protected(model):
557 def wrapper(func):
558 def wrapped(request, id):
32373f2e
OL
559 if request.user.is_superuser:
560 return func(request, id)
561 user_groups = request.user.groups.all()
562 if grp_drh in user_groups:
563 return func(request, id)
564 if grp_correspondants_rh in user_groups:
565 employe = get_employe_from_user(request.user)
00ca4d9f
EMS
566 q = Q(**{
567 model.prefix_implantation: employe.implantation.region
568 })
32373f2e 569 qs = model.objects.filter(q)
718abc4d 570 if int(id) in [o.id for o in qs]:
32373f2e
OL
571 return func(request, id)
572 return redirect_interdiction(request)
573 return wrapped
574 return wrapper
575
576
577@region_protected(rh.Dossier)
3ebc0952 578def dossier_apercu(request, dossier_id):
baf9b78c 579 d = get_object_or_404(rh.Dossier, pk=dossier_id)
3ebc0952 580 c = {
838bc59d 581 'title': u"Dossier %s" % (d, ),
00ca4d9f
EMS
582 'is_popup': request.GET.get('_popup', False),
583 'dossier': d,
584 'pieces': rh.DossierPiece.objects.filter(dossier__exact=d),
585 'contrats': rh.Contrat.objects.filter(dossier__exact=d),
586 'commentaires': rh.DossierCommentaire.objects.filter(dossier=d).all(),
abf91905 587 'media_url': settings.PRIVE_MEDIA_URL,
3ebc0952 588 }
890b80e7 589 return render(request, 'admin/rh/dossier/apercu.html', c)
00ca4d9f 590
4a1f2ece 591
fbe60a9d
OL
592@region_protected(rh.Poste)
593def poste_apercu(request, poste_id):
c8cb18e3 594 p = get_object_or_404(rh.Poste, pk=poste_id)
fbe60a9d 595 c = {
c8cb18e3 596 'title': u"Poste %s" % (p, ),
00ca4d9f
EMS
597 'is_popup': request.GET.get('_popup', False),
598 'poste': p,
599 'financements': (
600 rh.PosteFinancement.objects.filter(poste=poste_id).all()
601 ),
602 'pieces': rh.PostePiece.objects.filter(poste=poste_id).all(),
603 'dossiers': (
604 rh.Dossier.objects.filter(poste=poste_id)
605 .order_by("-date_debut").all()
606 ),
607 'comparaisons': (
608 rh.PosteComparaison.objects.filter(poste=poste_id).all()
609 ),
610 'commentaires': (
611 rh.PosteCommentaire.objects.filter(poste=poste_id).all()
612 ),
abf91905 613 'media_url': settings.PRIVE_MEDIA_URL,
fbe60a9d 614 }
890b80e7 615 return render(request, 'admin/rh/poste/apercu.html', c)
00ca4d9f 616
fbe60a9d 617
4a1f2ece
OL
618def employe_apercu(request, employe_id):
619 employe = get_object_or_404(rh.Employe, pk=employe_id)
fbe60a9d 620 user_groups = request.user.groups.all()
fbe60a9d
OL
621 dossiers = None
622
623 if request.user.is_superuser or \
624 grp_drh in user_groups:
625 q = Q(employe=employe)
626 if grp_correspondants_rh in user_groups:
00ca4d9f
EMS
627 regions = [
628 d.poste.implantation.region for d in employe.rh_dossiers.all()
629 ]
fbe60a9d 630 q = Q(employe=employe) & Q(implantation__region__in=regions)
22343fe7
OL
631
632 dossiers = rh.Dossier.objects.filter(q).order_by('-date_debut')
633
4a1f2ece 634 c = {
ce740bb5 635 'title': u"Employe %s" % (employe, ),
00ca4d9f
EMS
636 'is_popup': request.GET.get('_popup', False),
637 'employe': employe,
638 'dossiers': dossiers,
abf91905 639 'media_url': settings.PRIVE_MEDIA_URL,
4a1f2ece 640 }
890b80e7 641 return render(request, 'admin/rh/employe/apercu.html', c)
00ca4d9f 642
150d83ec 643
77bd83d1
JPC
644@login_required
645@drh_or_admin_required
56264a85 646def organigrammes_employe(request, id, level="all"):
08faf06e
JPC
647
648 poste = get_object_or_404(rh.Poste, pk=id)
00ca4d9f
EMS
649 dossiers_by_poste = dict(
650 (d.poste_id, d)
651 for d in rh.Dossier.objects.select_related('employe', 'poste').all()
652 )
5c0f1778
JPC
653 postes_by_id = dict((p.id, p) for p in rh.Poste.objects.all())
654
655 e = dossiers_by_poste[poste.id].employe
656 name = u"Organigramme de [%s] %s %s" % (e.id, e.nom.upper(), e.prenom)
58014aec 657 graph = pgv.AGraph()
5c0f1778 658
f187a10f 659 if rh.Poste.objects.filter(responsable=poste).count() > 0:
58014aec
JPC
660 postes_handle = [poste]
661 while postes_handle:
00ca4d9f
EMS
662 postes_handle = rh.Poste.objects.select_related('implantation') \
663 .filter(
664 Q(date_fin__gt=date.today()) | Q(date_fin=None),
665 Q(date_debut__lt=date.today()) | Q(date_debut=None),
666 responsable__in=postes_handle
667 ).exclude(supprime=True).exclude(responsable=None).all()
32373f2e 668
58014aec
JPC
669 for p in postes_handle:
670 if p.responsable_id != p.id:
00ca4d9f
EMS
671 graph.add_edge(
672 dossiers_by_poste[p.responsable_id].poste_id, p.id
673 )
13ad5ad5 674
f187a10f 675 else:
5c0f1778
JPC
676 graph.add_node(poste.id)
677
4afec2e1
JPC
678 if level != "all":
679 postes_niveau = [poste.id]
680 for niveau in range(int(level)):
00ca4d9f
EMS
681 postes_niveau = [
682 p.id for p in
683 rh.Poste.objects.filter(responsable__in=postes_niveau).all()
684 ]
4afec2e1
JPC
685
686 while postes_niveau:
00ca4d9f
EMS
687 postes_niveau = [
688 p.id for p in
689 rh.Poste.objects.filter(responsable__in=postes_niveau).all()
690 ]
4afec2e1
JPC
691 if postes_niveau:
692 for p in postes_niveau:
693 if graph.has_node(p):
4cc81304 694 graph.delete_node(p)
4afec2e1 695
58014aec 696 a = graph
4afec2e1 697 a.name = name.encode('ascii', 'xmlcharrefreplace')
5c0f1778
JPC
698
699 poste_remontant = poste
700 while poste_remontant.responsable_id:
701 a.add_edge(poste_remontant.responsable_id, poste_remontant.id)
702 poste_remontant = poste_remontant.responsable
703
704 rh_graph.bind_poste_to_graph(a, postes_by_id)
705 #a.graph_attr['normalize'] = True
706 #a.graph_attr['level'] = 2
707 a.layout(prog='dot')
708
709 svg = a.draw(format='svg')
710
711 c = {
712 'svg': svg
713 }
a251ac8d
JPC
714
715 if 'forcer' in request.GET:
890b80e7 716 response = HttpResponse(svg, content_type='image/svg+xml')
00ca4d9f
EMS
717 response['Content-Disposition'] = \
718 'attachment; filename=organigramme.svg'
a251ac8d
JPC
719 return response
720
890b80e7
DB
721 return render(request, 'rh/organigrammes/employe.html', c,
722 content_type="image/svg+xml"
00ca4d9f 723 )
5c0f1778
JPC
724
725
726@login_required
727@drh_or_admin_required
728def organigrammes_service(request, id):
729
730 service = get_object_or_404(rh.Service, pk=id)
82af5c19
JPC
731 svg = rh_graph.organigramme_postes_cluster( \
732 cluster_filter={"service": service}, \
733 titre=u"Organigramme du service %s" % service.nom,
734 cluster_titre=service.nom)
5c0f1778 735
82af5c19
JPC
736 c = {
737 'svg': svg
738 }
5c0f1778 739
890b80e7
DB
740 return render(request, 'rh/organigrammes/vide.html', c,
741 content_type="image/svg+xml"
00ca4d9f
EMS
742 )
743
5c0f1778 744
82af5c19
JPC
745@login_required
746@drh_or_admin_required
747def organigrammes_implantation(request, id):
5c0f1778 748
82af5c19
JPC
749 implantation = get_object_or_404(ref.Implantation, pk=id)
750 svg = rh_graph.organigramme_postes_cluster( \
751 cluster_filter={"implantation": implantation}, \
752 titre=u"Organigramme de l'implantation %s" % implantation.nom,
753 cluster_titre=implantation.nom)
5c0f1778
JPC
754
755 c = {
756 'svg': svg
757 }
758
890b80e7
DB
759 return render(request, 'rh/organigrammes/vide.html', c,
760 content_type="image/svg+xml"
00ca4d9f
EMS
761 )
762
5c0f1778 763
9da4c195
JPC
764@login_required
765@drh_or_admin_required
766def organigrammes_region(request, id):
767
768 region = get_object_or_404(ref.Region, pk=id)
769 svg = rh_graph.organigramme_postes_cluster( \
770 cluster_filter={"implantation__region": region}, \
771 titre=u"Organigramme du bureau de %s" % region.nom,
772 cluster_titre=region.nom)
773
774 c = {
775 'svg': svg
776 }
777
890b80e7
DB
778 return render(request,
779 'rh/organigrammes/vide.html', c,
780 content_type="image/svg+xml"
00ca4d9f 781 )