remove complement nom
[auf_rh_dae.git] / project / rh / views.py
CommitLineData
e9bbd6ba 1# -*- encoding: utf-8 -*-
c9122f1d 2from datetime import date
a7de62d7 3from itertools import izip
fa1624f6 4import networkx as nx
9b0a8554 5
fbe60a9d 6from django.db.models import Q
3411ac33 7from django.contrib.auth.decorators import login_required
fd009814 8from django.utils.encoding import smart_str
e9bbd6ba 9from django.shortcuts import redirect, render_to_response, get_object_or_404
10from django.template import RequestContext
9d53bc73 11from django.http import Http404
e9bbd6ba 12
d04d084c 13from datamaster_modeles import models as ref
d04d084c 14from project.lib import get_employe_from_id
ae936f8d 15
d04d084c 16from rh import models as rh
e2c0b1ac 17from rh.lib import calc_remun
02c1b3dc 18from rh.decorators import drh_or_admin_required
9d53bc73 19from rh.templatetags.rapports import SortHeaders
f0f6b03e 20from rh.change_list import RechercheTemporelle
a9faef67 21
9817fed5
OL
22# pas de reference a DAE devrait etre refactorisé
23from dae.utils import get_employe_from_user
24from dae.decorators import redirect_interdiction
25from dae.workflow import grp_drh, grp_correspondants_rh
baf9b78c 26from django.conf import settings
9817fed5 27
3411ac33 28# homes
c9122f1d 29@login_required
30def profil(request):
31 """Profil personnel de l'employé - éditable"""
32 rc = RequestContext(request)
33 c = {}
d04d084c 34
c9122f1d 35 employe = rc['this_employe']
65f9fac8 36
c9122f1d 37 c['user'] = request.user
38 c['employe'] = employe
c9122f1d 39 return render_to_response('rh/profil.html', c, rc)
a9faef67 40
41# employes
3411ac33 42@login_required
a9faef67 43def employes_liste(request):
44 """Liste des employés."""
5ea6b5bb 45 today = date.today()
46 employes = rh.Employe.objects \
5ea6b5bb 47 .exclude(dossiers__date_debut__gt=today) \
48 .exclude(dossiers__date_fin__lt=today) \
49 .order_by('nom')
a9faef67 50 c = {
51 'user':request.user,
52 'employes':employes,
53 }
54 return render_to_response('rh/employes_liste.html', c, RequestContext(request))
55
3411ac33 56@login_required
a9faef67 57def employe(request, id):
58 """Information publique sur un employé."""
59 try:
60 employe = rh.Employe.objects.get(pk=id)
61 except:
62 employe = rh.Employe.objects.none()
63 c = {
64 'user':request.user,
65 'employe':employe,
66 }
67 return render_to_response('rh/employe.html', c, RequestContext(request))
63e17dff
PP
68
69
70# Rapports
71
72@login_required
02c1b3dc 73@drh_or_admin_required
48487f09
JPC
74def rapports_postes_hierarchie(request):
75
76 c = {}
ce18dd0a 77 graph = nx.DiGraph()
fa1624f6
JPC
78 postes = rh.Poste.objects.all()
79 postes_id = dict((p.id, p) for p in postes)
ce18dd0a 80 graph.add_node(p for p in postes)
48487f09 81
fa1624f6
JPC
82 for p in postes:
83 if p.responsable_id != p.id:
ce18dd0a 84 graph.add_edge(postes_id[p.responsable_id], p)
48487f09 85
fa1624f6 86 c['postes'] = nx.to_dict_of_lists(graph)
48487f09
JPC
87
88 return render_to_response('rh/rapports/postes_hierarchie.html', c, RequestContext(request))
89
90@login_required
91@drh_or_admin_required
63e17dff
PP
92def rapports_poste(request):
93
fd009814
PP
94 lookup_params = dict(request.GET.items())
95
7821915f
PP
96 comble = 'all'
97
fd009814 98 for key, value in lookup_params.items():
9d53bc73
JPC
99 if key == 'o' or key == 'ot':
100 del lookup_params[key]
101 continue
fd009814
PP
102 if not isinstance(key, str):
103 # 'key' will be used as a keyword argument later, so Python
104 # requires it to be a string.
105 del lookup_params[key]
106 lookup_params[smart_str(key)] = value
107
7821915f
PP
108 if key == 'comble':
109 comble = value
110 del lookup_params[key]
111
ed3cbb32
OL
112 postes = rh.Poste.objects.select_related('implantation') \
113 .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
114 ("%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 "") \
115 }) \
ed3cbb32 116 .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
117 ("%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 "") \
118 }) \
ed3cbb32 119 .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
120 ("%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 "") \
121 })
9d53bc73 122
65e02621 123 postes = postes.filter(**lookup_params)
9d53bc73 124 if 'o' in request.GET:
65e02621
JPC
125 postes = postes.order_by("%s%s" % ('-' if 'ot' in request.GET and request.GET['ot'] == "desc" else '', request.GET['o']))
126
a7de62d7
JPC
127 out = []
128 for p in postes:
129 out.append({
130 'id': p.id,
131 'nom': p.nom,
132 'implantation': p.implantation,
133 '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('|'))] })
134
135
4dade240
JPC
136 headers = [
137 ("id", u"# du poste"),
138 ("nom", u"Nom du poste"),
65e02621 139 ("implantation__id", u"Implantation"),
4dade240 140 ("employe_nom", u"Nom"),
4dade240
JPC
141 ]
142 h = SortHeaders(request, headers, order_field_type="ot", order_field="o")
63e17dff
PP
143 c = {
144 'title': 'Rapport des postes',
a7de62d7
JPC
145 'postes': out,
146 'count': len(out),
65e02621 147 'headers': list(h.headers()),
63e17dff
PP
148 }
149
150 return render_to_response('rh/rapports/postes.html', c, RequestContext(request))
f2d65e83 151
02c1b3dc
JPC
152@login_required
153@drh_or_admin_required
f2d65e83
PP
154def rapports_contrat(request):
155
156 lookup_params = dict(request.GET.items())
65e02621
JPC
157 if 'ot' in lookup_params:
158 del lookup_params['ot']
159 if 'o' in lookup_params:
160 del lookup_params['o']
f2d65e83
PP
161
162 for key, value in lookup_params.items():
163 if not isinstance(key, str):
164 # 'key' will be used as a keyword argument later, so Python
165 # requires it to be a string.
166 del lookup_params[key]
167 lookup_params[smart_str(key)] = value
168
65e02621 169 contrats = rh.Contrat.objects.select_related('dossier', 'dossier__poste', 'dossier__poste__implantation', 'type_contrat', 'dossier__employe')
f0f6b03e
OL
170
171 cl = RechercheTemporelle(request, rh.Contrat)
172 lookup_params = cl.purge_params(lookup_params)
821b9889 173 contrats = contrats.filter(**lookup_params).exclude(dossier__employe__supprime=1)
f0f6b03e
OL
174 contrats = cl.filter_temporel(contrats)
175
65e02621
JPC
176 if 'o' in request.GET:
177 contrats = contrats.order_by("%s%s" % ('-' if 'ot' in request.GET and request.GET['ot'] == "desc" else '', request.GET['o']))
f2d65e83 178
8ee135b7
JPC
179 employes = set([c.dossier.employe_id for c in contrats])
180
65e02621
JPC
181 headers = [
182 ("dossier__employe__id", u"# de l'employé"),
183 ("dossier__employe__nom", u"Nom"),
184 ("dossier__employe__prenom", u"Prénom"),
185 ("type_contrat__nom", u"Poste"),
186 ("type_contrat__nom", u"Type de contrat"),
187 ("date_debut", u"Date début"),
188 ("date_fin", u"Date fin"),
189 ]
190 h = SortHeaders(request, headers, order_field_type="ot", order_field="o")
191
f2d65e83 192 c = {
f0f6b03e 193 'cl' : cl,
f2d65e83
PP
194 'title': 'Rapport des contrats',
195 'contrats': contrats,
8ee135b7
JPC
196 'count': len(contrats),
197 'count_employe' : len(employes),
65e02621 198 'headers': list(h.headers()),
f2d65e83
PP
199 }
200
201 return render_to_response('rh/rapports/contrats.html', c, RequestContext(request))
e2c0b1ac
PP
202
203
02c1b3dc
JPC
204@login_required
205@drh_or_admin_required
e2c0b1ac
PP
206def rapports_remuneration(request):
207
208 lookup_params = dict(request.GET.items())
9202c5db
JPC
209 if 'ot' in lookup_params:
210 del lookup_params['ot']
211 if 'o' in lookup_params:
212 del lookup_params['o']
e2c0b1ac
PP
213
214 for key, value in lookup_params.items():
215 if not isinstance(key, str):
216 # 'key' will be used as a keyword argument later, so Python
217 # requires it to be a string.
218 del lookup_params[key]
219 lookup_params[smart_str(key)] = value
220
221 employes = rh.Employe.objects.all()
9202c5db
JPC
222 if 'o' in request.GET:
223 employes = employes.order_by("%s%s" % ('-' if 'ot' in request.GET and request.GET['ot'] == "desc" else '', request.GET['o']))
224
e2c0b1ac
PP
225 employes = employes.filter(**lookup_params)
226
227 output = []
9202c5db
JPC
228 headers = [
229 ("id", u"# de l'employé"),
230 ("nom", u"Nom"),
231 ("prenom", u"Prénom"),
232 ("", u"Salaire"),
233 ("", u"RAS"),
234 ("", u"Indemnités"),
235 ("", u"Accessoire"),
236 ("", u"Charges patronales"),
237 ("", u"Total"),
238 ]
239 h = SortHeaders(request, headers, order_field_type="ot", order_field="o")
e2c0b1ac
PP
240
241 for employe in employes:
242 line = {}
243 output.append(line)
244
da6c523f 245 dossiers = employe.rh_dossiers.all()
e2c0b1ac
PP
246
247 remun = {}
248 remun_sum_euro = 0
249
250 for dossier in dossiers:
251 this_remun, this_remun_sum, this_remun_sum_euro = calc_remun(dossier)
252
253 for item in this_remun:
254 if item not in remun:
255 remun[item] = this_remun[item]
256 else:
257 remun[item][0] += this_remun[item][0]
258 remun[item][1] += this_remun[item][1]
259
260 remun_sum_euro += this_remun_sum_euro
261
262 line['remun_sum_euro'] = remun_sum_euro
263
264 for r in remun:
265 if r == u'Indemnité':
e2c0b1ac
PP
266 line['Indemnite'] = remun[r][1]
267 else:
268 line[r] = remun[r][1]
269
270 line['id'] = employe.id
271 line['nom'] = employe.nom
272 line['prenom'] = employe.prenom
273
274
275 c = {
276 'title': 'Rapport de remuneration',
277 'employes': output,
9202c5db 278 'headers': list(h.headers()),
e2c0b1ac
PP
279 }
280
281 return render_to_response('rh/rapports/remuneration.html', c, RequestContext(request))
3ebc0952 282
783e077a
JPC
283@login_required
284@drh_or_admin_required
c8b22fd1
JPC
285def rapports_employe_sans_contrat(request):
286
287 lookup_params = dict(request.GET.items())
288 if 'ot' in lookup_params:
289 del lookup_params['ot']
290 if 'o' in lookup_params:
291 del lookup_params['o']
292
293 for key, value in lookup_params.items():
294 if not isinstance(key, str):
295 # 'key' will be used as a keyword argument later, so Python
296 # requires it to be a string.
297 del lookup_params[key]
298 lookup_params[smart_str(key)] = value
299
300 employes_query = rh.Employe.objects
301 if 'o' in request.GET:
302 employes_query = employes_query.order_by("%s%s" % ('-' if 'ot' in request.GET and request.GET['ot'] == "desc" else '', request.GET['o']))
303
7c5645f0 304 employes = {}
c8b22fd1 305 for e in employes_query.all():
1d2d4dc2
JPC
306 dossiers = rh.Dossier.objects.filter(employe=e). \
307 filter(Q(date_fin=None) | Q(date_fin__gt=date.today())). \
308 filter(**lookup_params).all()
c8b22fd1
JPC
309 for d in dossiers:
310 contrats = rh.Contrat.objects.filter(Q(dossier=d) & Q(date_fin__lt=date.today())).all()
311 if contrats:
7c5645f0
JPC
312 if e.id not in employes:
313 employes[e.id] = {'employe': e, 'dossiers': set([d])}
314 else:
315 employes[e.id]['dossiers'].add(d)
c8b22fd1
JPC
316
317 headers = [
1d2d4dc2
JPC
318 ("id", u"# de l'employé"),
319 ("nom", u"Nom"),
320 ("prenom", u"Prénom"),
80518280 321 ("dossier", u"Dossiers"),
c8b22fd1 322 ]
80518280 323 h = SortHeaders(request, headers, order_field_type="ot", order_field="o", not_sortable=('dossier',))
c8b22fd1
JPC
324
325 c = {
326 'title': u'Rapport des employés sans contrat',
327 'employes': employes,
328 'count': len(employes),
329 'headers': list(h.headers()),
330 }
331
332 return render_to_response('rh/rapports/employes_sans_contrat.html', c, RequestContext(request))
333
334@login_required
335@drh_or_admin_required
857b5c24
JPC
336def rapports_postes_modelisation(request):
337 c = {}
338 data = []
3c1ba807
JPC
339
340 for f in rh.FamilleEmploi.objects.all():
341 types = rh.TypePoste.objects.filter(famille_emploi=f)
342 data_types = []
343 for t in types.all():
344 postes = rh.Poste.objects.filter(type_poste=t)
345 data_types.append({
346 'num_postes': postes.count(),
347 'postes': postes.all(),
348 'type': f,
857b5c24 349 })
3c1ba807 350
857b5c24 351 data.append({
3c1ba807
JPC
352 'famille': f,
353 'nb_types': types.count(),
354 'types' : data_types
857b5c24
JPC
355 })
356
357 c['data'] = data
358
359
360 return render_to_response('rh/rapports/postes_modelisation.html', c, RequestContext(request))
361
362
363@login_required
364@drh_or_admin_required
783e077a
JPC
365def rapports_postes_implantation(request):
366 c = {}
367 data = []
368 for r in ref.Region.objects.all():
369 implantations = []
370 for i in ref.Implantation.objects.filter(region=r):
371 implantations.append({
372 'implantation': i,
373 'postes': rh.Poste.objects.filter(implantation=i),
374 'num_postes': rh.Poste.objects.filter(implantation=i).count(),
375 })
376 data.append({
377 'region': r,
378 'implantations': implantations
379 })
380
381 c['data'] = data
382
383
384 return render_to_response('rh/rapports/postes_implantation.html', c, RequestContext(request))
385
386
387@login_required
388@drh_or_admin_required
389def rapports_postes_service(request):
390 c = {}
9988aafb
JPC
391 data = []
392 for s in rh.Service.objects.all():
393 postes = rh.Poste.objects.filter(service=s).all()
394 num_postes = rh.Poste.objects.filter(service=s).count()
395 data.append({'service': s, 'num_postes': num_postes, 'postes': postes})
783e077a 396
9988aafb
JPC
397 c['data'] = data
398 return render_to_response('rh/rapports/postes_service.html', c, RequestContext(request))
783e077a
JPC
399
400
32373f2e
OL
401def region_protected(model):
402 def wrapper(func):
403 def wrapped(request, id):
32373f2e
OL
404 if request.user.is_superuser:
405 return func(request, id)
406 user_groups = request.user.groups.all()
407 if grp_drh in user_groups:
408 return func(request, id)
409 if grp_correspondants_rh in user_groups:
410 employe = get_employe_from_user(request.user)
411 q = Q(**{model.prefix_implantation: employe.implantation.region})
412 qs = model.objects.filter(q)
718abc4d 413 if int(id) in [o.id for o in qs]:
32373f2e
OL
414 return func(request, id)
415 return redirect_interdiction(request)
416 return wrapped
417 return wrapper
418
419
420@region_protected(rh.Dossier)
3ebc0952 421def dossier_apercu(request, dossier_id):
baf9b78c 422 d = get_object_or_404(rh.Dossier, pk=dossier_id)
3ebc0952
OL
423 c = {
424 'is_popup' : request.GET.get('_popup', False),
baf9b78c 425 'dossier' : d,
520ab455
JPC
426 'pieces' : rh.DossierPiece.objects.filter(dossier__exact=d),
427 'contrats' : rh.Contrat.objects.filter(dossier__exact=d),
2a36598b 428 'commentaires' : rh.DossierCommentaire.objects.filter(dossier__exact=d).all(),
abf91905 429 'media_url': settings.PRIVE_MEDIA_URL,
3ebc0952
OL
430 }
431 return render_to_response('admin/rh/dossier/apercu.html', c, RequestContext(request))
4a1f2ece 432
fbe60a9d
OL
433@region_protected(rh.Poste)
434def poste_apercu(request, poste_id):
435 c = {
436 'is_popup' : request.GET.get('_popup', False),
0dcfc806
JPC
437 'poste' : get_object_or_404(rh.Poste, pk=poste_id),
438 'financements' : rh.PosteFinancement.objects.filter(poste=poste_id).all(),
439 'pieces' : rh.PostePiece.objects.filter(poste=poste_id).all(),
440 'dossiers' : rh.Dossier.objects.filter(poste=poste_id).order_by("-date_debut").all(),
441 'comparaisons' : rh.PosteComparaison.objects.filter(poste=poste_id).all(),
442 'commentaires' : rh.PosteCommentaire.objects.filter(poste=poste_id).all(),
abf91905 443 'media_url': settings.PRIVE_MEDIA_URL,
fbe60a9d
OL
444 }
445 return render_to_response('admin/rh/poste/apercu.html', c, RequestContext(request))
446
4a1f2ece 447def employe_apercu(request, employe_id):
fbe60a9d 448 from dae.workflow import grp_drh, grp_correspondants_rh
4a1f2ece 449 employe = get_object_or_404(rh.Employe, pk=employe_id)
fbe60a9d
OL
450 user_groups = request.user.groups.all()
451
452 dernier_dossier = None
453 dossiers = None
454
455 if request.user.is_superuser or \
456 grp_drh in user_groups:
457 q = Q(employe=employe)
458 if grp_correspondants_rh in user_groups:
7eeafa57 459 regions = [d.poste.implantation.region for d in employe.rh_dossiers.all()]
fbe60a9d
OL
460 q = Q(employe=employe) & Q(implantation__region__in=regions)
461
462 dossiers = rh.Dossier.objects.filter(employe=employe).order_by('-date_debut')
463
4a1f2ece
OL
464 c = {
465 'is_popup' : request.GET.get('_popup', False),
466 'employe' : employe,
fbe60a9d 467 'dossiers' : dossiers,
abf91905 468 'media_url': settings.PRIVE_MEDIA_URL,
4a1f2ece
OL
469 }
470 return render_to_response('admin/rh/employe/apercu.html', c, RequestContext(request))
150d83ec 471
32373f2e 472