region_protected 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
acbc95a1 22from project.decorators import redirect_interdiction
018c8eaf 23from project.decorators import drh_or_admin_required
ae99002a
DB
24from project.decorators import region_protected
25from project.utils import get_employe_from_user
7d0c2206 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
890b80e7 30from project.rh.lib import calc_remun
4ba84959 31from project.rh.masse_salariale import MasseSalariale
890b80e7 32from project.rh.templatetags.rapports import SortHeaders
9817fed5 33
9817fed5 34# pas de reference a DAE devrait etre refactorisé
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 554
32373f2e 555@region_protected(rh.Dossier)
3ebc0952 556def dossier_apercu(request, dossier_id):
baf9b78c 557 d = get_object_or_404(rh.Dossier, pk=dossier_id)
3ebc0952 558 c = {
838bc59d 559 'title': u"Dossier %s" % (d, ),
00ca4d9f
EMS
560 'is_popup': request.GET.get('_popup', False),
561 'dossier': d,
562 'pieces': rh.DossierPiece.objects.filter(dossier__exact=d),
563 'contrats': rh.Contrat.objects.filter(dossier__exact=d),
564 'commentaires': rh.DossierCommentaire.objects.filter(dossier=d).all(),
abf91905 565 'media_url': settings.PRIVE_MEDIA_URL,
3ebc0952 566 }
890b80e7 567 return render(request, 'admin/rh/dossier/apercu.html', c)
00ca4d9f 568
4a1f2ece 569
fbe60a9d
OL
570@region_protected(rh.Poste)
571def poste_apercu(request, poste_id):
c8cb18e3 572 p = get_object_or_404(rh.Poste, pk=poste_id)
fbe60a9d 573 c = {
c8cb18e3 574 'title': u"Poste %s" % (p, ),
00ca4d9f
EMS
575 'is_popup': request.GET.get('_popup', False),
576 'poste': p,
577 'financements': (
578 rh.PosteFinancement.objects.filter(poste=poste_id).all()
579 ),
580 'pieces': rh.PostePiece.objects.filter(poste=poste_id).all(),
581 'dossiers': (
582 rh.Dossier.objects.filter(poste=poste_id)
583 .order_by("-date_debut").all()
584 ),
585 'comparaisons': (
586 rh.PosteComparaison.objects.filter(poste=poste_id).all()
587 ),
588 'commentaires': (
589 rh.PosteCommentaire.objects.filter(poste=poste_id).all()
590 ),
abf91905 591 'media_url': settings.PRIVE_MEDIA_URL,
fbe60a9d 592 }
890b80e7 593 return render(request, 'admin/rh/poste/apercu.html', c)
00ca4d9f 594
fbe60a9d 595
4a1f2ece
OL
596def employe_apercu(request, employe_id):
597 employe = get_object_or_404(rh.Employe, pk=employe_id)
fbe60a9d 598 user_groups = request.user.groups.all()
fbe60a9d
OL
599 dossiers = None
600
601 if request.user.is_superuser or \
602 grp_drh in user_groups:
603 q = Q(employe=employe)
604 if grp_correspondants_rh in user_groups:
00ca4d9f
EMS
605 regions = [
606 d.poste.implantation.region for d in employe.rh_dossiers.all()
607 ]
fbe60a9d 608 q = Q(employe=employe) & Q(implantation__region__in=regions)
22343fe7
OL
609
610 dossiers = rh.Dossier.objects.filter(q).order_by('-date_debut')
611
4a1f2ece 612 c = {
ce740bb5 613 'title': u"Employe %s" % (employe, ),
00ca4d9f
EMS
614 'is_popup': request.GET.get('_popup', False),
615 'employe': employe,
616 'dossiers': dossiers,
abf91905 617 'media_url': settings.PRIVE_MEDIA_URL,
4a1f2ece 618 }
890b80e7 619 return render(request, 'admin/rh/employe/apercu.html', c)
00ca4d9f 620
150d83ec 621
77bd83d1
JPC
622@login_required
623@drh_or_admin_required
56264a85 624def organigrammes_employe(request, id, level="all"):
08faf06e
JPC
625
626 poste = get_object_or_404(rh.Poste, pk=id)
00ca4d9f
EMS
627 dossiers_by_poste = dict(
628 (d.poste_id, d)
629 for d in rh.Dossier.objects.select_related('employe', 'poste').all()
630 )
5c0f1778
JPC
631 postes_by_id = dict((p.id, p) for p in rh.Poste.objects.all())
632
633 e = dossiers_by_poste[poste.id].employe
634 name = u"Organigramme de [%s] %s %s" % (e.id, e.nom.upper(), e.prenom)
58014aec 635 graph = pgv.AGraph()
5c0f1778 636
f187a10f 637 if rh.Poste.objects.filter(responsable=poste).count() > 0:
58014aec
JPC
638 postes_handle = [poste]
639 while postes_handle:
00ca4d9f
EMS
640 postes_handle = rh.Poste.objects.select_related('implantation') \
641 .filter(
642 Q(date_fin__gt=date.today()) | Q(date_fin=None),
643 Q(date_debut__lt=date.today()) | Q(date_debut=None),
644 responsable__in=postes_handle
645 ).exclude(supprime=True).exclude(responsable=None).all()
32373f2e 646
58014aec
JPC
647 for p in postes_handle:
648 if p.responsable_id != p.id:
00ca4d9f
EMS
649 graph.add_edge(
650 dossiers_by_poste[p.responsable_id].poste_id, p.id
651 )
13ad5ad5 652
f187a10f 653 else:
5c0f1778
JPC
654 graph.add_node(poste.id)
655
4afec2e1
JPC
656 if level != "all":
657 postes_niveau = [poste.id]
658 for niveau in range(int(level)):
00ca4d9f
EMS
659 postes_niveau = [
660 p.id for p in
661 rh.Poste.objects.filter(responsable__in=postes_niveau).all()
662 ]
4afec2e1
JPC
663
664 while postes_niveau:
00ca4d9f
EMS
665 postes_niveau = [
666 p.id for p in
667 rh.Poste.objects.filter(responsable__in=postes_niveau).all()
668 ]
4afec2e1
JPC
669 if postes_niveau:
670 for p in postes_niveau:
671 if graph.has_node(p):
4cc81304 672 graph.delete_node(p)
4afec2e1 673
58014aec 674 a = graph
4afec2e1 675 a.name = name.encode('ascii', 'xmlcharrefreplace')
5c0f1778
JPC
676
677 poste_remontant = poste
678 while poste_remontant.responsable_id:
679 a.add_edge(poste_remontant.responsable_id, poste_remontant.id)
680 poste_remontant = poste_remontant.responsable
681
682 rh_graph.bind_poste_to_graph(a, postes_by_id)
683 #a.graph_attr['normalize'] = True
684 #a.graph_attr['level'] = 2
685 a.layout(prog='dot')
686
687 svg = a.draw(format='svg')
688
689 c = {
690 'svg': svg
691 }
a251ac8d
JPC
692
693 if 'forcer' in request.GET:
890b80e7 694 response = HttpResponse(svg, content_type='image/svg+xml')
00ca4d9f
EMS
695 response['Content-Disposition'] = \
696 'attachment; filename=organigramme.svg'
a251ac8d
JPC
697 return response
698
890b80e7
DB
699 return render(request, 'rh/organigrammes/employe.html', c,
700 content_type="image/svg+xml"
00ca4d9f 701 )
5c0f1778
JPC
702
703
704@login_required
705@drh_or_admin_required
706def organigrammes_service(request, id):
707
708 service = get_object_or_404(rh.Service, pk=id)
82af5c19
JPC
709 svg = rh_graph.organigramme_postes_cluster( \
710 cluster_filter={"service": service}, \
711 titre=u"Organigramme du service %s" % service.nom,
712 cluster_titre=service.nom)
5c0f1778 713
82af5c19
JPC
714 c = {
715 'svg': svg
716 }
5c0f1778 717
890b80e7
DB
718 return render(request, 'rh/organigrammes/vide.html', c,
719 content_type="image/svg+xml"
00ca4d9f
EMS
720 )
721
5c0f1778 722
82af5c19
JPC
723@login_required
724@drh_or_admin_required
725def organigrammes_implantation(request, id):
5c0f1778 726
82af5c19
JPC
727 implantation = get_object_or_404(ref.Implantation, pk=id)
728 svg = rh_graph.organigramme_postes_cluster( \
729 cluster_filter={"implantation": implantation}, \
730 titre=u"Organigramme de l'implantation %s" % implantation.nom,
731 cluster_titre=implantation.nom)
5c0f1778
JPC
732
733 c = {
734 'svg': svg
735 }
736
890b80e7
DB
737 return render(request, 'rh/organigrammes/vide.html', c,
738 content_type="image/svg+xml"
00ca4d9f
EMS
739 )
740
5c0f1778 741
9da4c195
JPC
742@login_required
743@drh_or_admin_required
744def organigrammes_region(request, id):
745
746 region = get_object_or_404(ref.Region, pk=id)
747 svg = rh_graph.organigramme_postes_cluster( \
748 cluster_filter={"implantation__region": region}, \
749 titre=u"Organigramme du bureau de %s" % region.nom,
750 cluster_titre=region.nom)
751
752 c = {
753 'svg': svg
754 }
755
890b80e7
DB
756 return render(request,
757 'rh/organigrammes/vide.html', c,
758 content_type="image/svg+xml"
00ca4d9f 759 )