Merge branch 'dev' of ssh://git.auf/auf_rh_dae into dev
[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
JPC
18from rh.templatetags.rapports import SortHeaders
19from project.lib import multikeysort
a9faef67 20
9817fed5
OL
21# pas de reference a DAE devrait etre refactorisé
22from dae.utils import get_employe_from_user
23from dae.decorators import redirect_interdiction
24from dae.workflow import grp_drh, grp_correspondants_rh
25
a184c555 26@login_required
9817fed5 27def piece(request, filename):
a184c555 28 """Téléchargement d'une pièce jointe à un poste."""
9817fed5
OL
29 model, id, f = filename.split('/')
30 if model == 'employe':
31 # TODO definir peut-être un controle d'accès
32 piece = get_object_or_404(rh.EmployePiece, fichier=filename)
a184c555 33 return sendfile(request, piece.fichier.path)
9817fed5
OL
34 if model == 'poste':
35 piece = get_object_or_404(rh.PostePiece, fichier=filename)
36 if rh.Poste.objects.ma_region_ou_service(request.user).filter(id=piece.poste_id).exists():
37 return sendfile(request, piece.fichier.path)
38 else:
39 return redirect_interdiction(request)
a184c555 40
3411ac33 41# homes
c9122f1d 42@login_required
43def profil(request):
44 """Profil personnel de l'employé - éditable"""
45 rc = RequestContext(request)
46 c = {}
d04d084c 47
c9122f1d 48 employe = rc['this_employe']
65f9fac8 49
c9122f1d 50 c['user'] = request.user
51 c['employe'] = employe
c9122f1d 52 return render_to_response('rh/profil.html', c, rc)
a9faef67 53
54# employes
3411ac33 55@login_required
a9faef67 56def employes_liste(request):
57 """Liste des employés."""
5ea6b5bb 58 today = date.today()
59 employes = rh.Employe.objects \
60 .filter(actif=True, supprime=False) \
61 .filter(dossiers__actif=True, dossiers__supprime=False) \
62 .exclude(dossiers__date_debut__gt=today) \
63 .exclude(dossiers__date_fin__lt=today) \
64 .order_by('nom')
a9faef67 65 c = {
66 'user':request.user,
67 'employes':employes,
68 }
69 return render_to_response('rh/employes_liste.html', c, RequestContext(request))
70
3411ac33 71@login_required
a9faef67 72def employe(request, id):
73 """Information publique sur un employé."""
74 try:
75 employe = rh.Employe.objects.get(pk=id)
76 except:
77 employe = rh.Employe.objects.none()
78 c = {
79 'user':request.user,
80 'employe':employe,
81 }
82 return render_to_response('rh/employe.html', c, RequestContext(request))
63e17dff
PP
83
84
85# Rapports
86
87@login_required
02c1b3dc 88@drh_or_admin_required
63e17dff
PP
89def rapports_poste(request):
90
fd009814
PP
91 lookup_params = dict(request.GET.items())
92
7821915f
PP
93 comble = 'all'
94
fd009814 95 for key, value in lookup_params.items():
9d53bc73
JPC
96 if key == 'o' or key == 'ot':
97 del lookup_params[key]
98 continue
fd009814
PP
99 if not isinstance(key, str):
100 # 'key' will be used as a keyword argument later, so Python
101 # requires it to be a string.
102 del lookup_params[key]
103 lookup_params[smart_str(key)] = value
104
7821915f
PP
105 if key == 'comble':
106 comble = value
107 del lookup_params[key]
108
63e17dff 109 postes = rh.Poste.actifs.select_related('implantation').all()
fd009814
PP
110 postes = postes.filter(**lookup_params)
111
63e17dff
PP
112 poste_ids = set([p.pk for p in postes])
113
114 dossiers = rh.Dossier.objects.select_related('poste', 'employe').filter(poste__in=poste_ids)
115
116 employes = dict((d.poste.id, d.employe) for d in dossiers)
117
118 out = []
119
120 for p in postes:
7821915f
PP
121 employe = employes.get(p.id)
122
123 if comble == 'c' and not employe:
124 continue
125
126 if comble == 'n' and employe:
127 continue
128
63e17dff
PP
129 line = {}
130 out.append(line)
131
132 line['id'] = p.id
133 line['nom'] = p.nom
134 line['implantation'] = p.implantation.nom
63e17dff
PP
135 if employe:
136 line['employe_id'] = employe.id
137 line['employe_nom'] = employe.nom
138 line['employe_prenom'] = employe.prenom
9d53bc73
JPC
139 else:
140 line['employe_id'] = None
141 line['employe_nom'] = None
142 line['employe_prenom'] = None
143
144 if 'o' in request.GET:
145 try:
4dade240 146 out = multikeysort(out, ["%s%s" % ('-' if 'ot' in request.GET and request.GET['ot'] == "desc" else '', request.GET['o'])])
9d53bc73
JPC
147 except KeyError:
148 raise Http404
4dade240
JPC
149 headers = [
150 ("id", u"# du poste"),
151 ("nom", u"Nom du poste"),
152 ("implantation", u"Implantation"),
153 ("employe_id", u"# de l'employé"),
154 ("employe_nom", u"Nom"),
155 ("employe_prenom", u"Prénom"),
156 ]
157 h = SortHeaders(request, headers, order_field_type="ot", order_field="o")
63e17dff
PP
158 c = {
159 'title': 'Rapport des postes',
160 'postes': out,
08fabc85 161 'count': len(out),
4dade240 162 'headers': list(h.headers())
63e17dff
PP
163 }
164
165 return render_to_response('rh/rapports/postes.html', c, RequestContext(request))
f2d65e83 166
02c1b3dc
JPC
167@login_required
168@drh_or_admin_required
f2d65e83
PP
169def rapports_contrat(request):
170
171 lookup_params = dict(request.GET.items())
172
173 for key, value in lookup_params.items():
174 if not isinstance(key, str):
175 # 'key' will be used as a keyword argument later, so Python
176 # requires it to be a string.
177 del lookup_params[key]
178 lookup_params[smart_str(key)] = value
179
180 contrats = rh.Contrat.objects.select_related('dossier', 'dossier__poste', 'dossier__poste__implantation', 'type_contrat', 'dossier__employe').all()
181 contrats = contrats.filter(**lookup_params)
182
8ee135b7
JPC
183 employes = set([c.dossier.employe_id for c in contrats])
184
f2d65e83
PP
185 c = {
186 'title': 'Rapport des contrats',
187 'contrats': contrats,
8ee135b7
JPC
188 'count': len(contrats),
189 'count_employe' : len(employes),
f2d65e83
PP
190 }
191
192 return render_to_response('rh/rapports/contrats.html', c, RequestContext(request))
e2c0b1ac
PP
193
194
02c1b3dc
JPC
195@login_required
196@drh_or_admin_required
e2c0b1ac
PP
197def rapports_remuneration(request):
198
199 lookup_params = dict(request.GET.items())
200
201 for key, value in lookup_params.items():
202 if not isinstance(key, str):
203 # 'key' will be used as a keyword argument later, so Python
204 # requires it to be a string.
205 del lookup_params[key]
206 lookup_params[smart_str(key)] = value
207
208 employes = rh.Employe.objects.all()
209 employes = employes.filter(**lookup_params)
210
211 output = []
212
213 for employe in employes:
214 line = {}
215 output.append(line)
216
da6c523f 217 dossiers = employe.rh_dossiers.all()
e2c0b1ac
PP
218
219 remun = {}
220 remun_sum_euro = 0
221
222 for dossier in dossiers:
223 this_remun, this_remun_sum, this_remun_sum_euro = calc_remun(dossier)
224
225 for item in this_remun:
226 if item not in remun:
227 remun[item] = this_remun[item]
228 else:
229 remun[item][0] += this_remun[item][0]
230 remun[item][1] += this_remun[item][1]
231
232 remun_sum_euro += this_remun_sum_euro
233
234 line['remun_sum_euro'] = remun_sum_euro
235
236 for r in remun:
237 if r == u'Indemnité':
e2c0b1ac
PP
238 line['Indemnite'] = remun[r][1]
239 else:
240 line[r] = remun[r][1]
241
242 line['id'] = employe.id
243 line['nom'] = employe.nom
244 line['prenom'] = employe.prenom
245
246
247 c = {
248 'title': 'Rapport de remuneration',
249 'employes': output,
250 }
251
252 return render_to_response('rh/rapports/remuneration.html', c, RequestContext(request))
3ebc0952 253
32373f2e
OL
254def region_protected(model):
255 def wrapper(func):
256 def wrapped(request, id):
32373f2e
OL
257 if request.user.is_superuser:
258 return func(request, id)
259 user_groups = request.user.groups.all()
260 if grp_drh in user_groups:
261 return func(request, id)
262 if grp_correspondants_rh in user_groups:
263 employe = get_employe_from_user(request.user)
264 q = Q(**{model.prefix_implantation: employe.implantation.region})
265 qs = model.objects.filter(q)
718abc4d 266 if int(id) in [o.id for o in qs]:
32373f2e
OL
267 return func(request, id)
268 return redirect_interdiction(request)
269 return wrapped
270 return wrapper
271
272
273@region_protected(rh.Dossier)
3ebc0952
OL
274def dossier_apercu(request, dossier_id):
275 c = {
276 'is_popup' : request.GET.get('_popup', False),
277 'dossier' : get_object_or_404(rh.Dossier, pk=dossier_id)
278
279 }
280 return render_to_response('admin/rh/dossier/apercu.html', c, RequestContext(request))
4a1f2ece 281
fbe60a9d
OL
282@region_protected(rh.Poste)
283def poste_apercu(request, poste_id):
284 c = {
285 'is_popup' : request.GET.get('_popup', False),
286 'poste' : get_object_or_404(rh.Poste, pk=poste_id)
287
288 }
289 return render_to_response('admin/rh/poste/apercu.html', c, RequestContext(request))
290
4a1f2ece 291def employe_apercu(request, employe_id):
fbe60a9d 292 from dae.workflow import grp_drh, grp_correspondants_rh
4a1f2ece 293 employe = get_object_or_404(rh.Employe, pk=employe_id)
fbe60a9d
OL
294 user_groups = request.user.groups.all()
295
296 dernier_dossier = None
297 dossiers = None
298
299 if request.user.is_superuser or \
300 grp_drh in user_groups:
301 q = Q(employe=employe)
302 if grp_correspondants_rh in user_groups:
7eeafa57 303 regions = [d.poste.implantation.region for d in employe.rh_dossiers.all()]
fbe60a9d
OL
304 q = Q(employe=employe) & Q(implantation__region__in=regions)
305
306 dossiers = rh.Dossier.objects.filter(employe=employe).order_by('-date_debut')
307
4a1f2ece
OL
308 c = {
309 'is_popup' : request.GET.get('_popup', False),
310 'employe' : employe,
fbe60a9d 311 'dossiers' : dossiers,
4a1f2ece
OL
312 }
313 return render_to_response('admin/rh/employe/apercu.html', c, RequestContext(request))
150d83ec 314
32373f2e 315