[#3165] Remplacé les filtres et protection par région pour des filtres et protections...
[auf_rh_dae.git] / project / rh / views.py
CommitLineData
e9bbd6ba 1# -*- encoding: utf-8 -*-
22343fe7 2
fc62be5d 3from collections import defaultdict
c9122f1d 4from datetime import date
fc62be5d 5from decimal import Decimal
9b0a8554 6
9f55f18a 7import pygraphviz as pgv
edbc9e37 8from reversion.models import Revision
f80b83c6 9
d104b0ae 10from auf.django.references import models as ref
4ba84959 11from django.conf import settings
e90b19fd 12from django.contrib.auth.decorators import login_required
dcd1b959 13from django.core.urlresolvers import reverse
fbe60a9d 14from django.db.models import Q
4ba84959 15from django.http import HttpResponse
edbc9e37 16from django.shortcuts import render, get_object_or_404, redirect
8e26a99b 17
018c8eaf 18from project.decorators import drh_or_admin_required
ae99002a 19from project.decorators import region_protected
afd3be54 20from project.groups import get_employe_from_user
c3550a05
OL
21from project import groups
22from project.decorators import in_one_of_group, in_drh_or_admin
e9bbd6ba 23
fc62be5d 24from project.rh import ods
4ba84959 25from project.rh import graph as rh_graph
fc62be5d 26from project.rh import models as rh
890b80e7 27from project.rh.change_list import RechercheTemporelle
fc62be5d 28from project.rh.forms import MasseSalarialeForm
d104b0ae 29from project.rh.lib import get_lookup_params
890b80e7 30from project.rh.templatetags.rapports import SortHeaders
edbc9e37 31from project.rh.historique import get_active_revisions, TodoForm
9817fed5 32
fc62be5d
EMS
33TWOPLACES = Decimal('0.01')
34
22343fe7 35
c9122f1d 36@login_required
d9a82745 37@drh_or_admin_required
c9122f1d 38def profil(request):
39 """Profil personnel de l'employé - éditable"""
d104b0ae 40 employe = get_employe_from_user(request.user)
7d0c2206 41 c = {
8b3f6ff5
DB
42 'user': request.user,
43 'employe': employe,
7d0c2206 44 }
890b80e7 45 return render(request, 'rh/profil.html', c)
22343fe7
OL
46
47
3411ac33 48@login_required
d9a82745 49@drh_or_admin_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
d9a82745 65@drh_or_admin_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 }
890b80e7 76 return render(request, 'rh/employe.html', c)
63e17dff
PP
77
78
08faf06e 79@login_required
3383b2d1
OL
80@in_one_of_group((groups.CORRESPONDANT_RH,
81 groups.ADMINISTRATEURS,
82 groups.DIRECTEUR_DE_BUREAU,
83 groups.DRH_NIVEAU_1,
84 groups.DRH_NIVEAU_2))
f2d65e83 85def rapports_contrat(request):
dd18f6e3 86 # statut default = actif?
dcd1b959 87 if 'HTTP_REFERER' in request.META.keys():
58348377
EMS
88 referer = request.META['HTTP_REFERER']
89 referer = "/".join(referer.split('/')[3:])
90 referer = "/%s" % referer.split('?')[0]
91 if referer != reverse('rhr_contrats'):
92 params = request.GET.copy()
93 params.update({'statut': 'Actif'})
94 request.GET = params
f2d65e83 95
00ca4d9f
EMS
96 contrats = rh.Contrat.objects.select_related(
97 'dossier', 'dossier__poste', 'dossier__poste__implantation',
98 'type_contrat', 'dossier__employe'
99 )
22343fe7 100
dd18f6e3 101 # filters et recherche temporelle
6bee05ff 102 cl = RechercheTemporelle(dict(request.GET.items()), rh.Contrat)
dd18f6e3 103 lookup_params = get_lookup_params(request)
f0f6b03e 104 lookup_params = cl.purge_params(lookup_params)
dcd1b959
OL
105 q_temporel = cl.get_q_temporel(contrats)
106 q = Q(**lookup_params) & q_temporel
45066657 107 contrats = contrats.filter(q)
3383b2d1
OL
108 user_groups = [g.name for g in request.user.groups.all()]
109 if groups.CORRESPONDANT_RH in user_groups or\
110 groups.ADMINISTRATEURS in user_groups or\
111 groups.DIRECTEUR_DE_BUREAU in user_groups:
f96ec25c 112 employe = get_employe_from_user(request.user)
b0cf30b8
EMS
113 q = q & Q(
114 dossier__poste__implantation__zone_administrative=(
115 employe.implantation.zone_administrative
116 )
117 )
f96ec25c 118
c3550a05 119 contrats = contrats.filter(q)
f0f6b03e 120
dd18f6e3 121 # tri
65e02621 122 if 'o' in request.GET:
00ca4d9f
EMS
123 contrats = contrats.order_by(
124 ('-' if request.GET.get('ot') == "desc" else '') + request.GET['o']
125 )
f2d65e83 126
dd18f6e3 127 # affichage
8ee135b7 128 employes = set([c.dossier.employe_id for c in contrats])
65e02621 129 headers = [
dd18f6e3 130 ("dossier__employe__id", u"#"),
d50a5e5d 131 ("dossier__employe__nom", u"Employé"),
dd18f6e3
DB
132 ("dossier", u"Dossier : Poste"),
133 ("type_contrat__nom", u"Contrat"),
134 ("date_debut", u"Début contrat"),
135 ("date_fin", u"Fin contrat"),
136 ("dossier__statut_residence", u"Statut"),
b0cf30b8 137 ("dossier__poste__implantation__zone_administrative", u"Zone administrative"),
d50a5e5d 138 ("dossier__poste__implantation", u"Implantation"),
65e02621 139 ]
dd18f6e3
DB
140 h = SortHeaders(
141 request, headers, order_field_type="ot", order_field="o",
142 not_sortable=('dossier',)
143 )
65e02621 144
f2d65e83 145 c = {
00ca4d9f 146 'cl': cl,
f2d65e83
PP
147 'title': 'Rapport des contrats',
148 'contrats': contrats,
00ca4d9f 149 'count_employe': len(employes),
65e02621 150 'headers': list(h.headers()),
f2d65e83 151 }
890b80e7 152 return render(request, 'rh/rapports/contrats.html', c)
e2c0b1ac
PP
153
154
02c1b3dc 155@login_required
3383b2d1
OL
156@in_one_of_group((groups.CORRESPONDANT_RH,
157 groups.ADMINISTRATEURS,
158 groups.DIRECTEUR_DE_BUREAU,
159 groups.DRH_NIVEAU_1,
160 groups.DRH_NIVEAU_2))
6b7cf654 161def rapports_employes_sans_contrat(request):
5db1c5a3
DB
162 """
163 Employé sans contrat = a un Dossier qui n'a pas de Contrat associé
164 Employé avec contrat échu = a un Dossier avec un Contrat se terminant hier
165 au plus tard... (aujourd'hui = ok, pas date de fin = illimité donc ok)
166 """
1a7b9e36 167 lookup_params = get_lookup_params(request)
c8b22fd1 168
82c5e37d 169 # régionalisation
3383b2d1 170 user_groups = [g.name for g in request.user.groups.all()]
3383b2d1
OL
171 if groups.CORRESPONDANT_RH in user_groups or\
172 groups.ADMINISTRATEURS in user_groups or\
173 groups.DIRECTEUR_DE_BUREAU in user_groups:
219ae355 174 employe = get_employe_from_user(request.user)
b0cf30b8
EMS
175 lookup_params['poste__implantation__zone_administrative'] = \
176 employe.implantation.zone_administrative
219ae355 177
a20fa2a4
OL
178
179 dossiers = rh.Dossier.objects.sans_contrats_ou_echus(**lookup_params)
a20fa2a4
OL
180 dossiers_ids = [d.id for d in dossiers]
181 employes = rh.Employe.objects.filter(id__in=dossiers_ids).distinct().count()
a623d1b2 182
5db1c5a3 183 # tri
c8b22fd1 184 if 'o' in request.GET:
5db1c5a3 185 dossiers = dossiers.order_by(
00ca4d9f
EMS
186 ('-' if request.GET.get('ot') == "desc" else '') + request.GET['o']
187 )
c8b22fd1 188
6b7cf654 189 # affichage
c8b22fd1 190 headers = [
5db1c5a3
DB
191 ("employe__id", u"#"),
192 ("employe__nom", u"Employé"),
1a7b9e36 193 ("dossier", u"Dossier : Poste"),
5db1c5a3
DB
194 ("rh_contrats__type_contrat", u"Contrat"),
195 ("rh_contrats__date_debut", u"Début contrat"),
196 ("rh_contrats__date_fin", u"Fin contrat"),
197 ("statut_residence", u"Statut"),
b0cf30b8 198 ("poste__implantation__zone_administrative__code", u"Zone administrative"),
5db1c5a3 199 ("poste__implantation__nom", u"Implantation"),
c8b22fd1 200 ]
00ca4d9f
EMS
201 h = SortHeaders(
202 request, headers, order_field_type="ot", order_field="o",
5db1c5a3 203 not_sortable=('dossier',)
00ca4d9f 204 )
c8b22fd1
JPC
205
206 c = {
5db1c5a3 207 'title': u'Rapport des employés sans contrat ou contrat échu',
c8b22fd1 208 'employes': employes,
5db1c5a3 209 'dossiers': dossiers,
c8b22fd1
JPC
210 'headers': list(h.headers()),
211 }
212
890b80e7 213 return render(request, 'rh/rapports/employes_sans_contrat.html', c)
00ca4d9f 214
c8b22fd1
JPC
215
216@login_required
3383b2d1
OL
217@in_one_of_group((groups.CORRESPONDANT_RH,
218 groups.ADMINISTRATEURS,
219 groups.DIRECTEUR_DE_BUREAU,
220 groups.DRH_NIVEAU_1,
221 groups.DRH_NIVEAU_2))
98d6eb6c 222def rapports_masse_salariale(request):
fc62be5d
EMS
223 form = MasseSalarialeForm(request.user, request.GET)
224 if 'annee' in request.GET and form.is_valid():
b0cf30b8 225 zone_administrative = form.cleaned_data['zone_administrative']
fc62be5d
EMS
226 implantation = form.cleaned_data['implantation']
227 annee = form.cleaned_data['annee']
228 debut_annee = date(annee, 1, 1)
229 fin_annee = date(annee, 12, 31)
230 jours_annee = (fin_annee - debut_annee).days + 1
231 today = date.today()
232
233 # Récupérer les dossiers actifs
234 dossiers = rh.Dossier.objects \
235 .actifs(annee=annee) \
236 .select_related(
237 'poste', 'poste__implantation',
b0cf30b8 238 'poste__implantation__zone_administrative',
fc62be5d
EMS
239 'poste__implantation__adresse_physique_pays',
240 'employe', 'poste__type_poste', 'classement',
241 'statut', 'organisme_bstg'
242 ) \
243 .extra(
fc62be5d 244 select={
471fd30f
EMS
245 'valeur_point': (
246 'SELECT valeur FROM rh_valeurpoint '
247 'WHERE annee = %d '
248 'AND implantation = ref_implantation.id' % annee
249 ),
250 'valeur_point_devise': (
251 'SELECT d.code '
252 'FROM rh_valeurpoint vp '
253 'INNER JOIN rh_devise d ON d.id = vp.devise '
254 'WHERE annee = %d '
255 'AND implantation = ref_implantation.id' % annee
256 )
fc62be5d 257 }
48c0abfe 258 )
b0cf30b8
EMS
259 if zone_administrative:
260 dossiers = dossiers.filter(poste__implantation__zone_administrative=zone_administrative)
fc62be5d
EMS
261 if implantation:
262 dossiers = dossiers.filter(poste__implantation=implantation)
263
264 # Récupérer les rémunérations actives
265 remuns = rh.Remuneration.objects \
266 .actifs(annee=annee) \
267 .select_related('devise', 'type') \
268 .extra(
269 tables=['rh_tauxchange'],
270 where=[
271 'rh_tauxchange.annee = %s',
272 'rh_tauxchange.devise = rh_devise.id'
273 ],
274 params=[annee],
275 select={
276 'taux_change': 'rh_tauxchange.taux'
277 }
278 )
b0cf30b8
EMS
279 if zone_administrative:
280 remuns = remuns.filter(dossier__poste__implantation__zone_administrative=zone_administrative)
471fd30f
EMS
281 if implantation:
282 remuns = remuns.filter(dossier__poste__implantation=implantation)
fc62be5d 283 remuns_par_dossier = defaultdict(list)
471fd30f 284 types_remun = set()
fc62be5d 285 for remun in remuns:
471fd30f 286 types_remun.add(remun.type_id)
fc62be5d
EMS
287 remuns_par_dossier[remun.dossier_id].append(remun)
288
289 # Récupérer les types de rémunération par nature
290 types_remun_par_nature = defaultdict(list)
291 for type in rh.TypeRemuneration.objects.all():
471fd30f
EMS
292 if type.id in types_remun:
293 types_remun_par_nature[type.nature_remuneration].append(type)
fc62be5d
EMS
294 titres_traitements = [
295 t.nom for t in types_remun_par_nature[u'Traitement']
296 ]
297 titres_indemnites = [
298 t.nom for t in types_remun_par_nature[u'Indemnité']
299 ]
300 titres_primes = [
301 t.nom for t in types_remun_par_nature[u'Accessoire']
302 ]
303 titres_charges = [
304 t.nom for t in types_remun_par_nature[u'Charges']
305 ]
df37184c 306
fc62be5d
EMS
307 # Boucler sur les dossiers et préprarer les lignes du rapport
308 lignes = []
309 masse_salariale_totale = 0
310 for dossier in dossiers:
311 debut_cette_annee = \
312 max(dossier.date_debut or debut_annee, debut_annee)
313 fin_cette_annee = \
314 min(dossier.date_fin or fin_annee, fin_annee)
315 jours = (fin_cette_annee - debut_cette_annee).days + 1
316
317 remuns = remuns_par_dossier[dossier.id]
318 devises = set(remun.devise.code for remun in remuns)
319 if len(devises) == 1:
320 devise = remuns[0].devise.code
321 montant_remun = lambda r: r.montant
322 taux_change = Decimal(str(remuns[0].taux_change))
323 else:
324 devise = 'EUR'
325 montant_remun = lambda r: (
326 r.montant * Decimal(str(r.taux_change))
48c0abfe 327 )
fc62be5d
EMS
328 taux_change = Decimal(1)
329
330 remuns_par_type = defaultdict(lambda: 0)
331 for remun in remuns:
ff9bc43d
EMS
332 if remun.type.nature_remuneration == u'Accessoire':
333 remuns_par_type[remun.type_id] += montant_remun(remun)
334 else:
335 remuns_par_type[remun.type_id] += (
336 montant_remun(remun) * ((
337 min(remun.date_fin or fin_annee, fin_annee) -
338 max(remun.date_debut or debut_annee, debut_annee)
a81bde86
EMS
339 ).days + 1) / jours_annee *
340 dossier.regime_travail / 100
ff9bc43d 341 ).quantize(TWOPLACES)
fc62be5d
EMS
342 traitements = [
343 remuns_par_type[type.id]
344 for type in types_remun_par_nature[u'Traitement']
345 ]
346 indemnites = [
347 remuns_par_type[type.id]
348 for type in types_remun_par_nature[u'Indemnité']
349 ]
350 primes = [
351 remuns_par_type[type.id]
352 for type in types_remun_par_nature[u'Accessoire']
353 ]
354 charges = [
355 remuns_par_type[type.id]
356 for type in types_remun_par_nature[u'Charges']
357 ]
358 masse_salariale = sum(remuns_par_type.values())
359 masse_salariale_eur = (
360 masse_salariale * taux_change
361 ).quantize(TWOPLACES)
362 masse_salariale_totale += masse_salariale_eur
363
364 if dossier.valeur_point and dossier.classement \
365 and dossier.classement.coefficient and dossier.regime_travail:
366 salaire_theorique = (Decimal(str(
367 dossier.valeur_point * dossier.classement.coefficient
368 )) * dossier.regime_travail / 100).quantize(TWOPLACES)
369 else:
370 salaire_theorique = None
371
372 lignes.append({
373 'dossier': dossier,
374 'poste': dossier.poste,
375 'date_debut': (
376 dossier.date_debut
377 if dossier.date_debut and dossier.date_debut.year == annee
378 else None
379 ),
380 'date_fin': (
381 dossier.date_fin
382 if dossier.date_fin and dossier.date_fin.year == annee
383 else None
384 ),
385 'jours': jours,
386 'devise': devise,
387 'valeur_point': dossier.valeur_point,
388 'valeur_point_devise': dossier.valeur_point_devise,
389 'regime_travail': dossier.regime_travail,
390 'local_expatrie': {
391 'local': 'L', 'expat': 'E'
392 }.get(dossier.statut_residence),
393 'salaire_bstg': remuns_par_type[2],
394 'salaire_bstg_eur': (
395 (remuns_par_type[2] * taux_change).quantize(TWOPLACES)
396 ),
397 'salaire_theorique': salaire_theorique,
398 'traitements': traitements,
399 'total_traitements': sum(traitements),
400 'indemnites': indemnites,
401 'total_indemnites': sum(indemnites),
402 'primes': primes,
403 'total_primes': sum(primes),
404 'charges': charges,
405 'total_charges': sum(charges),
406 'masse_salariale': masse_salariale,
407 'masse_salariale_eur': masse_salariale_eur,
408 })
00ca4d9f 409
fc62be5d
EMS
410 # Récupérer les postes actifs pour déterminer le nombre de jours
411 # vacants.
50e2340a
EMS
412 postes = rh.Poste.objects.actifs(annee=annee) \
413 .select_related(
414 'devise_max', 'valeur_point_max', 'classement_max'
415 ) \
416 .extra(
417 tables=['rh_tauxchange'],
418 where=[
419 'rh_tauxchange.annee = %s',
420 'rh_tauxchange.devise = rh_devise.id'
421 ],
422 params=[annee],
423 select={
424 'taux_change': 'rh_tauxchange.taux'
425 }
426 )
b0cf30b8
EMS
427 if zone_administrative:
428 postes = postes.filter(implantation__zone_administrative=zone_administrative)
50e2340a
EMS
429 if implantation:
430 postes = postes.filter(implantation=implantation)
431 postes = list(postes)
fc62be5d
EMS
432 postes_par_id = dict((poste.id, poste) for poste in postes)
433 jours_vacants_date = dict(
434 (poste.id, max(today, poste.date_debut or today))
435 for poste in postes
436 )
437 jours_vacants = defaultdict(lambda: 0)
438 for dossier in rh.Dossier.objects.actifs(annee=annee) \
439 .order_by('date_debut'):
440 if dossier.poste_id not in jours_vacants_date:
441 continue
442 derniere_date = jours_vacants_date[dossier.poste_id]
443 if dossier.date_debut is not None:
444 jours_vacants[dossier.poste_id] += max((
445 dossier.date_debut - derniere_date
446 ).days - 1, 0)
447 jours_vacants_date[dossier.poste_id] = max(
448 min(dossier.date_fin or fin_annee, fin_annee),
449 derniere_date
48c0abfe 450 )
24b5b57f
EMS
451 for poste_id, derniere_date in jours_vacants_date.iteritems():
452 jours_vacants[poste_id] += max((
453 min(postes_par_id[poste_id].date_fin or fin_annee, fin_annee) -
454 derniere_date
455 ).days, 0)
48c0abfe 456
fc62be5d
EMS
457 # Ajouter les lignes des postes vacants au rapport
458 for poste_id, jours in jours_vacants.iteritems():
459 if jours == 0:
460 continue
461 poste = postes_par_id[poste_id]
462 if poste.valeur_point_max and poste.classement_max \
463 and poste.classement_max.coefficient and poste.regime_travail:
464 salaire_theorique = (Decimal(str(
465 poste.valeur_point_max.valeur *
466 poste.classement_max.coefficient
467 )) * poste.regime_travail / 100).quantize(TWOPLACES)
468 else:
469 salaire_theorique = None
df37184c 470
fc62be5d
EMS
471 local_expatrie = '/'.join(
472 (['L'] if poste.local else []) +
473 (['E'] if poste.expatrie else [])
474 )
df37184c 475
fc62be5d
EMS
476 salaire = (
477 poste.salaire_max * jours / jours_annee *
478 poste.regime_travail / 100
479 ).quantize(TWOPLACES)
480 indemnites = (
481 poste.indemn_max * jours / jours_annee *
482 poste.regime_travail / 100
483 ).quantize(TWOPLACES)
484 charges = (
485 poste.autre_max * jours / jours_annee *
486 poste.regime_travail / 100
487 ).quantize(TWOPLACES)
488 masse_salariale = salaire + indemnites + charges
489 masse_salariale_eur = (
490 masse_salariale * Decimal(str(poste.taux_change))
491 ).quantize(TWOPLACES)
492 masse_salariale_totale += masse_salariale_eur
493
494 lignes.append({
495 'poste': poste,
496 'regime_travail': poste.regime_travail,
497 'local_expatrie': local_expatrie,
498 'jours': jours,
499 'devise': poste.devise_max and poste.devise_max.code,
500 'valeur_point': (
501 poste.valeur_point_max and poste.valeur_point_max.valeur
502 ),
503 'valeur_point_devise': (
504 poste.valeur_point_max and \
505 poste.valeur_point_max.devise.code
506 ),
507 'salaire_theorique': salaire_theorique,
24b5b57f
EMS
508 'traitements': [0] * len(titres_traitements),
509 'total_traitements': salaire,
510 'indemnites': [0] * len(titres_indemnites),
fc62be5d 511 'total_indemnites': indemnites,
24b5b57f
EMS
512 'primes': [0] * len(titres_primes),
513 'total_primes': 0,
514 'charges': [0] * len(titres_charges),
fc62be5d
EMS
515 'total_charges': charges,
516 'masse_salariale': masse_salariale,
517 'masse_salariale_eur': masse_salariale_eur
518 })
519 if 'ods' in request.GET:
520 doc = ods.masse_salariale(
521 lignes=lignes,
522 annee=annee,
523 titres_traitements=titres_traitements,
524 titres_indemnites=titres_indemnites,
525 titres_primes=titres_primes,
526 titres_charges=titres_charges,
527 masse_salariale_totale=masse_salariale_totale
528 )
529 response = HttpResponse(
530 mimetype='vnd.oasis.opendocument.spreadsheet'
531 )
532 response['Content-Disposition'] = 'filename=masse-salariale.ods'
533 doc.write(response)
534 return response
535 else:
536 return render(request, 'rh/rapports/masse_salariale.html', {
537 'form': form,
538 'titres_traitements': titres_traitements,
539 'titres_indemnites': titres_indemnites,
540 'titres_primes': titres_primes,
541 'titres_charges': titres_charges,
542 'masse_salariale_totale': masse_salariale_totale,
543 'lignes': lignes,
544 'annee': annee
545 })
546 return render(request, 'rh/rapports/masse_salariale.html', {
547 'form': form
548 })
98d6eb6c
JPC
549
550
551@login_required
552@drh_or_admin_required
857b5c24
JPC
553def rapports_postes_modelisation(request):
554 c = {}
555 data = []
3c1ba807 556
7bf28694
EMS
557 for categorie in rh.CategorieEmploi.objects.all():
558 types = rh.TypePoste.objects.filter(categorie_emploi=categorie)
3c1ba807
JPC
559 data_types = []
560 for t in types.all():
561 postes = rh.Poste.objects.filter(type_poste=t)
562 data_types.append({
563 'num_postes': postes.count(),
564 'postes': postes.all(),
7bf28694 565 'type': categorie,
857b5c24 566 })
3c1ba807 567
857b5c24 568 data.append({
7bf28694 569 'categorie': categorie,
3c1ba807 570 'nb_types': types.count(),
00ca4d9f
EMS
571 'types': data_types
572 })
857b5c24
JPC
573
574 c['data'] = data
575
890b80e7 576 return render(request, 'rh/rapports/postes_modelisation.html', c)
857b5c24
JPC
577
578
579@login_required
580@drh_or_admin_required
783e077a
JPC
581def rapports_postes_implantation(request):
582 c = {}
583 data = []
584 for r in ref.Region.objects.all():
585 implantations = []
586 for i in ref.Implantation.objects.filter(region=r):
587 implantations.append({
588 'implantation': i,
589 'postes': rh.Poste.objects.filter(implantation=i),
590 'num_postes': rh.Poste.objects.filter(implantation=i).count(),
591 })
592 data.append({
593 'region': r,
594 'implantations': implantations
595 })
596
597 c['data'] = data
598
890b80e7 599 return render(request, 'rh/rapports/postes_implantation.html', c)
783e077a
JPC
600
601
602@login_required
603@drh_or_admin_required
604def rapports_postes_service(request):
605 c = {}
9988aafb
JPC
606 data = []
607 for s in rh.Service.objects.all():
608 postes = rh.Poste.objects.filter(service=s).all()
609 num_postes = rh.Poste.objects.filter(service=s).count()
22343fe7 610 data.append({'service': s, 'num_postes': num_postes, 'postes': postes})
783e077a 611
9988aafb 612 c['data'] = data
890b80e7 613 return render(request, 'rh/rapports/postes_service.html', c)
32373f2e 614
d104b0ae 615
32373f2e 616@region_protected(rh.Dossier)
3ebc0952 617def dossier_apercu(request, dossier_id):
baf9b78c 618 d = get_object_or_404(rh.Dossier, pk=dossier_id)
3ebc0952 619 c = {
838bc59d 620 'title': u"Dossier %s" % (d, ),
00ca4d9f
EMS
621 'is_popup': request.GET.get('_popup', False),
622 'dossier': d,
623 'pieces': rh.DossierPiece.objects.filter(dossier__exact=d),
624 'contrats': rh.Contrat.objects.filter(dossier__exact=d),
625 'commentaires': rh.DossierCommentaire.objects.filter(dossier=d).all(),
abf91905 626 'media_url': settings.PRIVE_MEDIA_URL,
3ebc0952 627 }
890b80e7 628 return render(request, 'admin/rh/dossier/apercu.html', c)
00ca4d9f 629
4a1f2ece 630
fbe60a9d
OL
631@region_protected(rh.Poste)
632def poste_apercu(request, poste_id):
c8cb18e3 633 p = get_object_or_404(rh.Poste, pk=poste_id)
fbe60a9d 634 c = {
c8cb18e3 635 'title': u"Poste %s" % (p, ),
00ca4d9f
EMS
636 'is_popup': request.GET.get('_popup', False),
637 'poste': p,
638 'financements': (
639 rh.PosteFinancement.objects.filter(poste=poste_id).all()
640 ),
641 'pieces': rh.PostePiece.objects.filter(poste=poste_id).all(),
642 'dossiers': (
643 rh.Dossier.objects.filter(poste=poste_id)
644 .order_by("-date_debut").all()
645 ),
646 'comparaisons': (
647 rh.PosteComparaison.objects.filter(poste=poste_id).all()
648 ),
649 'commentaires': (
650 rh.PosteCommentaire.objects.filter(poste=poste_id).all()
651 ),
abf91905 652 'media_url': settings.PRIVE_MEDIA_URL,
fbe60a9d 653 }
890b80e7 654 return render(request, 'admin/rh/poste/apercu.html', c)
00ca4d9f 655
3383b2d1
OL
656@login_required
657@in_one_of_group((groups.CORRESPONDANT_RH,
658 groups.ADMINISTRATEURS,
659 groups.DIRECTEUR_DE_BUREAU,
660 groups.DRH_NIVEAU_1,
661 groups.DRH_NIVEAU_2))
4a1f2ece
OL
662def employe_apercu(request, employe_id):
663 employe = get_object_or_404(rh.Employe, pk=employe_id)
3383b2d1 664 user_groups = [g.name for g in request.user.groups.all()]
fbe60a9d
OL
665 dossiers = None
666
cd4c1b65 667 if in_drh_or_admin(request.user):
fbe60a9d 668 q = Q(employe=employe)
22343fe7 669
3383b2d1
OL
670 if groups.CORRESPONDANT_RH in user_groups or\
671 groups.ADMINISTRATEURS in user_groups or\
672 groups.DIRECTEUR_DE_BUREAU in user_groups:
bcc0c52d 673 employe_connecte = get_employe_from_user(request.user)
b0cf30b8
EMS
674 q = Q(employe=employe) & Q(
675 poste__implantation__zone_administrative=(
676 employe_connecte.implantation.zone_administrative
677 )
678 )
679
22343fe7
OL
680 dossiers = rh.Dossier.objects.filter(q).order_by('-date_debut')
681
4a1f2ece 682 c = {
ce740bb5 683 'title': u"Employe %s" % (employe, ),
00ca4d9f
EMS
684 'is_popup': request.GET.get('_popup', False),
685 'employe': employe,
686 'dossiers': dossiers,
abf91905 687 'media_url': settings.PRIVE_MEDIA_URL,
4a1f2ece 688 }
890b80e7 689 return render(request, 'admin/rh/employe/apercu.html', c)
00ca4d9f 690
150d83ec 691
77bd83d1 692@login_required
3383b2d1
OL
693@in_one_of_group((groups.CORRESPONDANT_RH,
694 groups.ADMINISTRATEURS,
695 groups.DIRECTEUR_DE_BUREAU,
696 groups.DRH_NIVEAU_1,
697 groups.DRH_NIVEAU_2))
56264a85 698def organigrammes_employe(request, id, level="all"):
08faf06e 699 poste = get_object_or_404(rh.Poste, pk=id)
00ca4d9f
EMS
700 dossiers_by_poste = dict(
701 (d.poste_id, d)
702 for d in rh.Dossier.objects.select_related('employe', 'poste').all()
703 )
5c0f1778
JPC
704 postes_by_id = dict((p.id, p) for p in rh.Poste.objects.all())
705
706 e = dossiers_by_poste[poste.id].employe
707 name = u"Organigramme de [%s] %s %s" % (e.id, e.nom.upper(), e.prenom)
58014aec 708 graph = pgv.AGraph()
5c0f1778 709
f187a10f 710 if rh.Poste.objects.filter(responsable=poste).count() > 0:
58014aec
JPC
711 postes_handle = [poste]
712 while postes_handle:
00ca4d9f
EMS
713 postes_handle = rh.Poste.objects.select_related('implantation') \
714 .filter(
715 Q(date_fin__gt=date.today()) | Q(date_fin=None),
716 Q(date_debut__lt=date.today()) | Q(date_debut=None),
717 responsable__in=postes_handle
c3550a05 718 ).exclude(responsable=None).all()
32373f2e 719
58014aec
JPC
720 for p in postes_handle:
721 if p.responsable_id != p.id:
00ca4d9f
EMS
722 graph.add_edge(
723 dossiers_by_poste[p.responsable_id].poste_id, p.id
724 )
13ad5ad5 725
f187a10f 726 else:
5c0f1778
JPC
727 graph.add_node(poste.id)
728
4afec2e1
JPC
729 if level != "all":
730 postes_niveau = [poste.id]
731 for niveau in range(int(level)):
00ca4d9f
EMS
732 postes_niveau = [
733 p.id for p in
734 rh.Poste.objects.filter(responsable__in=postes_niveau).all()
735 ]
4afec2e1
JPC
736
737 while postes_niveau:
00ca4d9f
EMS
738 postes_niveau = [
739 p.id for p in
740 rh.Poste.objects.filter(responsable__in=postes_niveau).all()
741 ]
4afec2e1
JPC
742 if postes_niveau:
743 for p in postes_niveau:
744 if graph.has_node(p):
4cc81304 745 graph.delete_node(p)
4afec2e1 746
58014aec 747 a = graph
4afec2e1 748 a.name = name.encode('ascii', 'xmlcharrefreplace')
5c0f1778
JPC
749
750 poste_remontant = poste
751 while poste_remontant.responsable_id:
752 a.add_edge(poste_remontant.responsable_id, poste_remontant.id)
753 poste_remontant = poste_remontant.responsable
754
7000b7b3 755 rh_graph.bind_poste_to_graph(request.user, a, postes_by_id)
5c0f1778
JPC
756 #a.graph_attr['normalize'] = True
757 #a.graph_attr['level'] = 2
758 a.layout(prog='dot')
759
760 svg = a.draw(format='svg')
761
762 c = {
763 'svg': svg
764 }
a251ac8d
JPC
765
766 if 'forcer' in request.GET:
890b80e7 767 response = HttpResponse(svg, content_type='image/svg+xml')
00ca4d9f
EMS
768 response['Content-Disposition'] = \
769 'attachment; filename=organigramme.svg'
a251ac8d
JPC
770 return response
771
890b80e7
DB
772 return render(request, 'rh/organigrammes/employe.html', c,
773 content_type="image/svg+xml"
00ca4d9f 774 )
5c0f1778
JPC
775
776
777@login_required
3383b2d1
OL
778@in_one_of_group((groups.CORRESPONDANT_RH,
779 groups.ADMINISTRATEURS,
780 groups.DIRECTEUR_DE_BUREAU,
781 groups.DRH_NIVEAU_1,
782 groups.DRH_NIVEAU_2))
5c0f1778 783def organigrammes_service(request, id):
5c0f1778 784 service = get_object_or_404(rh.Service, pk=id)
82af5c19 785 svg = rh_graph.organigramme_postes_cluster( \
7000b7b3 786 request.user, \
82af5c19
JPC
787 cluster_filter={"service": service}, \
788 titre=u"Organigramme du service %s" % service.nom,
789 cluster_titre=service.nom)
5c0f1778 790
d104b0ae 791 return render(request, 'rh/organigrammes/vide.html', {
82af5c19 792 'svg': svg
d104b0ae 793 }, content_type="image/svg+xml")
00ca4d9f 794
5c0f1778 795
82af5c19 796@login_required
3383b2d1
OL
797@in_one_of_group((groups.CORRESPONDANT_RH,
798 groups.ADMINISTRATEURS,
799 groups.DIRECTEUR_DE_BUREAU,
800 groups.DRH_NIVEAU_1,
801 groups.DRH_NIVEAU_2))
82af5c19 802def organigrammes_implantation(request, id):
82af5c19
JPC
803 implantation = get_object_or_404(ref.Implantation, pk=id)
804 svg = rh_graph.organigramme_postes_cluster( \
7000b7b3 805 request.user, \
82af5c19
JPC
806 cluster_filter={"implantation": implantation}, \
807 titre=u"Organigramme de l'implantation %s" % implantation.nom,
808 cluster_titre=implantation.nom)
d104b0ae 809 return render(request, 'rh/organigrammes/vide.html', {
5c0f1778 810 'svg': svg
d104b0ae 811 }, content_type="image/svg+xml")
00ca4d9f 812
5c0f1778 813
9da4c195 814@login_required
3383b2d1
OL
815@in_one_of_group((groups.CORRESPONDANT_RH,
816 groups.ADMINISTRATEURS,
817 groups.DIRECTEUR_DE_BUREAU,
818 groups.DRH_NIVEAU_1,
819 groups.DRH_NIVEAU_2))
9da4c195 820def organigrammes_region(request, id):
9da4c195
JPC
821 region = get_object_or_404(ref.Region, pk=id)
822 svg = rh_graph.organigramme_postes_cluster( \
7000b7b3 823 request.user, \
9da4c195
JPC
824 cluster_filter={"implantation__region": region}, \
825 titre=u"Organigramme du bureau de %s" % region.nom,
826 cluster_titre=region.nom)
827
d104b0ae 828 return render(request, 'rh/organigrammes/vide.html', {
9da4c195 829 'svg': svg
d104b0ae 830 }, content_type="image/svg+xml")
8e26a99b
OL
831
832
833@login_required
834@drh_or_admin_required
835def historique_des_modifications(request,):
836
837 from django.core.paginator import Paginator
838
edbc9e37 839 revisions = get_active_revisions()
45c66f19 840 paginator = Paginator(revisions, 50)
8e26a99b
OL
841 page = request.GET.get('page')
842 try:
843 page_revisions = paginator.page(page)
844 except:
845 page_revisions = paginator.page(1)
846
edbc9e37
OL
847 results = page_revisions.object_list
848 if request.POST:
849 form = TodoForm(request.POST, revisions=results)
850 if form.is_valid():
851 for id in form.cleaned_data['items']:
852 revision = Revision.objects.get(id=id)
853 rh.ModificationTraite(revision=revision).save()
854 return redirect(reverse('rhr_historique_des_modifications'))
855 else:
8e26a99b 856
edbc9e37
OL
857 form = TodoForm(revisions=results)
858
8e26a99b 859 c = {
edbc9e37
OL
860 'headers': ('Date', 'Auteur', 'Type', 'Objet', 'Historique',
861 'Commentaire', 'Traité'),
8e26a99b
OL
862 'revisions': results,
863 'page': page_revisions,
edbc9e37 864 'form': form,
8e26a99b
OL
865 }
866
867 return render(request, 'rh/rapports/historique_des_modifications.html', c)