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