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