Poste: Aperçu
[auf_rh_dae.git] / project / rh / views.py
CommitLineData
e9bbd6ba 1# -*- encoding: utf-8 -*-
c9122f1d 2from datetime import date
9b0a8554 3
fbe60a9d 4from django.db.models import Q
3411ac33 5from django.contrib.auth.decorators import login_required
fd009814 6from django.utils.encoding import smart_str
e9bbd6ba 7from django.shortcuts import redirect, render_to_response, get_object_or_404
8from django.template import RequestContext
9d53bc73 9from django.http import Http404
a184c555 10from sendfile import sendfile
e9bbd6ba 11
d04d084c 12from datamaster_modeles import models as ref
d04d084c 13from project.lib import get_employe_from_id
ae936f8d 14
d04d084c 15from rh import models as rh
e2c0b1ac 16from rh.lib import calc_remun
02c1b3dc 17from rh.decorators import drh_or_admin_required
9d53bc73 18from rh.templatetags.rapports import SortHeaders
a9faef67 19
9817fed5
OL
20# pas de reference a DAE devrait etre refactorisé
21from dae.utils import get_employe_from_user
22from dae.decorators import redirect_interdiction
23from dae.workflow import grp_drh, grp_correspondants_rh
baf9b78c 24from django.conf import settings
9817fed5 25
a184c555 26@login_required
9817fed5 27def piece(request, filename):
a184c555 28 """Téléchargement d'une pièce jointe à un poste."""
9817fed5 29 model, id, f = filename.split('/')
4ba73558
OL
30 if model == 'contrat':
31 # TODO definir peut-être un controle d'accès
32 piece = get_object_or_404(rh.Contrat, fichier=filename)
33 return sendfile(request, piece.fichier.path)
9817fed5
OL
34 if model == 'employe':
35 # TODO definir peut-être un controle d'accès
36 piece = get_object_or_404(rh.EmployePiece, fichier=filename)
a184c555 37 return sendfile(request, piece.fichier.path)
9817fed5
OL
38 if model == 'poste':
39 piece = get_object_or_404(rh.PostePiece, fichier=filename)
40 if rh.Poste.objects.ma_region_ou_service(request.user).filter(id=piece.poste_id).exists():
41 return sendfile(request, piece.fichier.path)
42 else:
43 return redirect_interdiction(request)
0ce4c68b
OL
44 if model == 'dossier':
45 piece = get_object_or_404(rh.DossierPiece, fichier=filename)
46 if rh.Dossier.objects.ma_region_ou_service(request.user).filter(id=piece.dossier_id).exists():
47 return sendfile(request, piece.fichier.path)
48 else:
49 return redirect_interdiction(request)
a184c555 50
3411ac33 51# homes
c9122f1d 52@login_required
53def profil(request):
54 """Profil personnel de l'employé - éditable"""
55 rc = RequestContext(request)
56 c = {}
d04d084c 57
c9122f1d 58 employe = rc['this_employe']
65f9fac8 59
c9122f1d 60 c['user'] = request.user
61 c['employe'] = employe
c9122f1d 62 return render_to_response('rh/profil.html', c, rc)
a9faef67 63
64# employes
3411ac33 65@login_required
a9faef67 66def employes_liste(request):
67 """Liste des employés."""
5ea6b5bb 68 today = date.today()
69 employes = rh.Employe.objects \
70 .filter(actif=True, supprime=False) \
71 .filter(dossiers__actif=True, dossiers__supprime=False) \
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
65e02621
JPC
119 #postes = rh.Poste.actifs.select_related('implantation', 'dossier').all()
120 postes = rh.Poste.actifs.select_related('implantation') \
9202c5db
JPC
121 .extra(select={'employe_id':'select group_concat(employe separator "|") from rh_dossier where poste=rh_poste.id and rh_dossier.actif = 1 and rh_dossier.date_fin is null %s' % \
122 ("%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 "") \
123 }) \
124 .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.actif = 1 and rh_dossier.date_fin is null %s' % \
125 ("%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 "") \
126 }) \
127 .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.actif = 1 and rh_dossier.date_fin is null %s' % \
128 ("%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 "") \
129 })
9d53bc73 130
65e02621 131 postes = postes.filter(**lookup_params)
9d53bc73 132 if 'o' in request.GET:
65e02621
JPC
133 postes = postes.order_by("%s%s" % ('-' if 'ot' in request.GET and request.GET['ot'] == "desc" else '', request.GET['o']))
134
4dade240
JPC
135 headers = [
136 ("id", u"# du poste"),
137 ("nom", u"Nom du poste"),
65e02621 138 ("implantation__id", u"Implantation"),
4dade240
JPC
139 ("employe_id", u"# de l'employé"),
140 ("employe_nom", u"Nom"),
141 ("employe_prenom", u"Prénom"),
142 ]
143 h = SortHeaders(request, headers, order_field_type="ot", order_field="o")
63e17dff
PP
144 c = {
145 'title': 'Rapport des postes',
65e02621
JPC
146 'postes': postes,
147 'count': len(postes),
148 'headers': list(h.headers()),
63e17dff
PP
149 }
150
151 return render_to_response('rh/rapports/postes.html', c, RequestContext(request))
f2d65e83 152
02c1b3dc
JPC
153@login_required
154@drh_or_admin_required
f2d65e83
PP
155def rapports_contrat(request):
156
157 lookup_params = dict(request.GET.items())
65e02621
JPC
158 if 'ot' in lookup_params:
159 del lookup_params['ot']
160 if 'o' in lookup_params:
161 del lookup_params['o']
f2d65e83
PP
162
163 for key, value in lookup_params.items():
164 if not isinstance(key, str):
165 # 'key' will be used as a keyword argument later, so Python
166 # requires it to be a string.
167 del lookup_params[key]
168 lookup_params[smart_str(key)] = value
169
65e02621 170 contrats = rh.Contrat.objects.select_related('dossier', 'dossier__poste', 'dossier__poste__implantation', 'type_contrat', 'dossier__employe')
f2d65e83 171 contrats = contrats.filter(**lookup_params)
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
PP
188 c = {
189 'title': 'Rapport des contrats',
190 'contrats': contrats,
8ee135b7
JPC
191 'count': len(contrats),
192 'count_employe' : len(employes),
65e02621 193 'headers': list(h.headers()),
f2d65e83
PP
194 }
195
196 return render_to_response('rh/rapports/contrats.html', c, RequestContext(request))
e2c0b1ac
PP
197
198
02c1b3dc
JPC
199@login_required
200@drh_or_admin_required
e2c0b1ac
PP
201def rapports_remuneration(request):
202
203 lookup_params = dict(request.GET.items())
9202c5db
JPC
204 if 'ot' in lookup_params:
205 del lookup_params['ot']
206 if 'o' in lookup_params:
207 del lookup_params['o']
e2c0b1ac
PP
208
209 for key, value in lookup_params.items():
210 if not isinstance(key, str):
211 # 'key' will be used as a keyword argument later, so Python
212 # requires it to be a string.
213 del lookup_params[key]
214 lookup_params[smart_str(key)] = value
215
216 employes = rh.Employe.objects.all()
9202c5db
JPC
217 if 'o' in request.GET:
218 employes = employes.order_by("%s%s" % ('-' if 'ot' in request.GET and request.GET['ot'] == "desc" else '', request.GET['o']))
219
e2c0b1ac
PP
220 employes = employes.filter(**lookup_params)
221
222 output = []
9202c5db
JPC
223 headers = [
224 ("id", u"# de l'employé"),
225 ("nom", u"Nom"),
226 ("prenom", u"Prénom"),
227 ("", u"Salaire"),
228 ("", u"RAS"),
229 ("", u"Indemnités"),
230 ("", u"Accessoire"),
231 ("", u"Charges patronales"),
232 ("", u"Total"),
233 ]
234 h = SortHeaders(request, headers, order_field_type="ot", order_field="o")
e2c0b1ac
PP
235
236 for employe in employes:
237 line = {}
238 output.append(line)
239
da6c523f 240 dossiers = employe.rh_dossiers.all()
e2c0b1ac
PP
241
242 remun = {}
243 remun_sum_euro = 0
244
245 for dossier in dossiers:
246 this_remun, this_remun_sum, this_remun_sum_euro = calc_remun(dossier)
247
248 for item in this_remun:
249 if item not in remun:
250 remun[item] = this_remun[item]
251 else:
252 remun[item][0] += this_remun[item][0]
253 remun[item][1] += this_remun[item][1]
254
255 remun_sum_euro += this_remun_sum_euro
256
257 line['remun_sum_euro'] = remun_sum_euro
258
259 for r in remun:
260 if r == u'Indemnité':
e2c0b1ac
PP
261 line['Indemnite'] = remun[r][1]
262 else:
263 line[r] = remun[r][1]
264
265 line['id'] = employe.id
266 line['nom'] = employe.nom
267 line['prenom'] = employe.prenom
268
269
270 c = {
271 'title': 'Rapport de remuneration',
272 'employes': output,
9202c5db 273 'headers': list(h.headers()),
e2c0b1ac
PP
274 }
275
276 return render_to_response('rh/rapports/remuneration.html', c, RequestContext(request))
3ebc0952 277
32373f2e
OL
278def region_protected(model):
279 def wrapper(func):
280 def wrapped(request, id):
32373f2e
OL
281 if request.user.is_superuser:
282 return func(request, id)
283 user_groups = request.user.groups.all()
284 if grp_drh in user_groups:
285 return func(request, id)
286 if grp_correspondants_rh in user_groups:
287 employe = get_employe_from_user(request.user)
288 q = Q(**{model.prefix_implantation: employe.implantation.region})
289 qs = model.objects.filter(q)
718abc4d 290 if int(id) in [o.id for o in qs]:
32373f2e
OL
291 return func(request, id)
292 return redirect_interdiction(request)
293 return wrapped
294 return wrapper
295
296
297@region_protected(rh.Dossier)
3ebc0952 298def dossier_apercu(request, dossier_id):
baf9b78c 299 d = get_object_or_404(rh.Dossier, pk=dossier_id)
3ebc0952
OL
300 c = {
301 'is_popup' : request.GET.get('_popup', False),
baf9b78c
JPC
302 'dossier' : d,
303 'pieces': rh.DossierPiece.objects.filter(dossier__exact=d),
304 'media': settings.PRIVE_MEDIA_URL
3ebc0952
OL
305 }
306 return render_to_response('admin/rh/dossier/apercu.html', c, RequestContext(request))
4a1f2ece 307
fbe60a9d
OL
308@region_protected(rh.Poste)
309def poste_apercu(request, poste_id):
310 c = {
311 'is_popup' : request.GET.get('_popup', False),
0dcfc806
JPC
312 'poste' : get_object_or_404(rh.Poste, pk=poste_id),
313 'financements' : rh.PosteFinancement.objects.filter(poste=poste_id).all(),
314 'pieces' : rh.PostePiece.objects.filter(poste=poste_id).all(),
315 'dossiers' : rh.Dossier.objects.filter(poste=poste_id).order_by("-date_debut").all(),
316 'comparaisons' : rh.PosteComparaison.objects.filter(poste=poste_id).all(),
317 'commentaires' : rh.PosteCommentaire.objects.filter(poste=poste_id).all(),
318
fbe60a9d
OL
319
320 }
321 return render_to_response('admin/rh/poste/apercu.html', c, RequestContext(request))
322
4a1f2ece 323def employe_apercu(request, employe_id):
fbe60a9d 324 from dae.workflow import grp_drh, grp_correspondants_rh
4a1f2ece 325 employe = get_object_or_404(rh.Employe, pk=employe_id)
fbe60a9d
OL
326 user_groups = request.user.groups.all()
327
328 dernier_dossier = None
329 dossiers = None
330
331 if request.user.is_superuser or \
332 grp_drh in user_groups:
333 q = Q(employe=employe)
334 if grp_correspondants_rh in user_groups:
7eeafa57 335 regions = [d.poste.implantation.region for d in employe.rh_dossiers.all()]
fbe60a9d
OL
336 q = Q(employe=employe) & Q(implantation__region__in=regions)
337
338 dossiers = rh.Dossier.objects.filter(employe=employe).order_by('-date_debut')
339
4a1f2ece
OL
340 c = {
341 'is_popup' : request.GET.get('_popup', False),
342 'employe' : employe,
fbe60a9d 343 'dossiers' : dossiers,
4a1f2ece
OL
344 }
345 return render_to_response('admin/rh/employe/apercu.html', c, RequestContext(request))
150d83ec 346
32373f2e 347