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