Rapports: trie des rapports en WIP
[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:
146 out = multikeysort(out, [request.GET['o']])
147 except KeyError:
148 raise Http404
63e17dff
PP
149
150 c = {
151 'title': 'Rapport des postes',
152 'postes': out,
08fabc85 153 'count': len(out),
9d53bc73
JPC
154 'headers': list(SortHeaders(request, (
155 (u"# de l'employé", "id"),
156 (u"Nom", "nom"),
157 (u"Implantation", "implantation"),
158 (u"# de l'employé", "employe_id"),
159 (u"Nom", "employe_nom"),
160 (u"Prénom", "employe_prenom"),
161 )).headers())
63e17dff
PP
162 }
163
164 return render_to_response('rh/rapports/postes.html', c, RequestContext(request))
f2d65e83 165
02c1b3dc
JPC
166@login_required
167@drh_or_admin_required
f2d65e83
PP
168def rapports_contrat(request):
169
170 lookup_params = dict(request.GET.items())
171
172 for key, value in lookup_params.items():
173 if not isinstance(key, str):
174 # 'key' will be used as a keyword argument later, so Python
175 # requires it to be a string.
176 del lookup_params[key]
177 lookup_params[smart_str(key)] = value
178
179 contrats = rh.Contrat.objects.select_related('dossier', 'dossier__poste', 'dossier__poste__implantation', 'type_contrat', 'dossier__employe').all()
180 contrats = contrats.filter(**lookup_params)
181
8ee135b7
JPC
182 employes = set([c.dossier.employe_id for c in contrats])
183
f2d65e83
PP
184 c = {
185 'title': 'Rapport des contrats',
186 'contrats': contrats,
8ee135b7
JPC
187 'count': len(contrats),
188 'count_employe' : len(employes),
f2d65e83
PP
189 }
190
191 return render_to_response('rh/rapports/contrats.html', c, RequestContext(request))
e2c0b1ac
PP
192
193
02c1b3dc
JPC
194@login_required
195@drh_or_admin_required
e2c0b1ac
PP
196def rapports_remuneration(request):
197
198 lookup_params = dict(request.GET.items())
199
200 for key, value in lookup_params.items():
201 if not isinstance(key, str):
202 # 'key' will be used as a keyword argument later, so Python
203 # requires it to be a string.
204 del lookup_params[key]
205 lookup_params[smart_str(key)] = value
206
207 employes = rh.Employe.objects.all()
208 employes = employes.filter(**lookup_params)
209
210 output = []
211
212 for employe in employes:
213 line = {}
214 output.append(line)
215
da6c523f 216 dossiers = employe.rh_dossiers.all()
e2c0b1ac
PP
217
218 remun = {}
219 remun_sum_euro = 0
220
221 for dossier in dossiers:
222 this_remun, this_remun_sum, this_remun_sum_euro = calc_remun(dossier)
223
224 for item in this_remun:
225 if item not in remun:
226 remun[item] = this_remun[item]
227 else:
228 remun[item][0] += this_remun[item][0]
229 remun[item][1] += this_remun[item][1]
230
231 remun_sum_euro += this_remun_sum_euro
232
233 line['remun_sum_euro'] = remun_sum_euro
234
235 for r in remun:
236 if r == u'Indemnité':
e2c0b1ac
PP
237 line['Indemnite'] = remun[r][1]
238 else:
239 line[r] = remun[r][1]
240
241 line['id'] = employe.id
242 line['nom'] = employe.nom
243 line['prenom'] = employe.prenom
244
245
246 c = {
247 'title': 'Rapport de remuneration',
248 'employes': output,
249 }
250
251 return render_to_response('rh/rapports/remuneration.html', c, RequestContext(request))
3ebc0952 252
32373f2e
OL
253def region_protected(model):
254 def wrapper(func):
255 def wrapped(request, id):
32373f2e
OL
256 if request.user.is_superuser:
257 return func(request, id)
258 user_groups = request.user.groups.all()
259 if grp_drh in user_groups:
260 return func(request, id)
261 if grp_correspondants_rh in user_groups:
262 employe = get_employe_from_user(request.user)
263 q = Q(**{model.prefix_implantation: employe.implantation.region})
264 qs = model.objects.filter(q)
718abc4d 265 if int(id) in [o.id for o in qs]:
32373f2e
OL
266 return func(request, id)
267 return redirect_interdiction(request)
268 return wrapped
269 return wrapper
270
271
272@region_protected(rh.Dossier)
3ebc0952
OL
273def dossier_apercu(request, dossier_id):
274 c = {
275 'is_popup' : request.GET.get('_popup', False),
276 'dossier' : get_object_or_404(rh.Dossier, pk=dossier_id)
277
278 }
279 return render_to_response('admin/rh/dossier/apercu.html', c, RequestContext(request))
4a1f2ece 280
fbe60a9d
OL
281@region_protected(rh.Poste)
282def poste_apercu(request, poste_id):
283 c = {
284 'is_popup' : request.GET.get('_popup', False),
285 'poste' : get_object_or_404(rh.Poste, pk=poste_id)
286
287 }
288 return render_to_response('admin/rh/poste/apercu.html', c, RequestContext(request))
289
4a1f2ece 290def employe_apercu(request, employe_id):
fbe60a9d 291 from dae.workflow import grp_drh, grp_correspondants_rh
4a1f2ece 292 employe = get_object_or_404(rh.Employe, pk=employe_id)
fbe60a9d
OL
293 user_groups = request.user.groups.all()
294
295 dernier_dossier = None
296 dossiers = None
297
298 if request.user.is_superuser or \
299 grp_drh in user_groups:
300 q = Q(employe=employe)
301 if grp_correspondants_rh in user_groups:
7eeafa57 302 regions = [d.poste.implantation.region for d in employe.rh_dossiers.all()]
fbe60a9d
OL
303 q = Q(employe=employe) & Q(implantation__region__in=regions)
304
305 dossiers = rh.Dossier.objects.filter(employe=employe).order_by('-date_debut')
306
4a1f2ece
OL
307 c = {
308 'is_popup' : request.GET.get('_popup', False),
309 'employe' : employe,
fbe60a9d 310 'dossiers' : dossiers,
4a1f2ece
OL
311 }
312 return render_to_response('admin/rh/employe/apercu.html', c, RequestContext(request))
150d83ec 313
32373f2e 314