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