Rapports: trie dans postes et petit helper pour afficher le header du tableau
[auf_rh_dae.git] / project / rh / views.py
1 # -*- encoding: utf-8 -*-
2 from datetime import date
3
4 from django.db.models import Q
5 from django.contrib.auth.decorators import login_required
6 from django.utils.encoding import smart_str
7 from django.shortcuts import redirect, render_to_response, get_object_or_404
8 from django.template import RequestContext
9 from django.http import Http404
10 from sendfile import sendfile
11
12 from datamaster_modeles import models as ref
13 from project.lib import get_employe_from_id
14
15 from rh import models as rh
16 from rh.lib import calc_remun
17 from rh.decorators import drh_or_admin_required
18 from rh.templatetags.rapports import SortHeaders
19 from project.lib import multikeysort
20
21 # pas de reference a DAE devrait etre refactorisé
22 from dae.utils import get_employe_from_user
23 from dae.decorators import redirect_interdiction
24 from dae.workflow import grp_drh, grp_correspondants_rh
25
26 @login_required
27 def piece(request, filename):
28 """Téléchargement d'une pièce jointe à un poste."""
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)
33 return sendfile(request, piece.fichier.path)
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)
40
41 # homes
42 @login_required
43 def profil(request):
44 """Profil personnel de l'employé - éditable"""
45 rc = RequestContext(request)
46 c = {}
47
48 employe = rc['this_employe']
49
50 c['user'] = request.user
51 c['employe'] = employe
52 return render_to_response('rh/profil.html', c, rc)
53
54 # employes
55 @login_required
56 def employes_liste(request):
57 """Liste des employés."""
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')
65 c = {
66 'user':request.user,
67 'employes':employes,
68 }
69 return render_to_response('rh/employes_liste.html', c, RequestContext(request))
70
71 @login_required
72 def 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))
83
84
85 # Rapports
86
87 @login_required
88 @drh_or_admin_required
89 def rapports_poste(request):
90
91 lookup_params = dict(request.GET.items())
92
93 comble = 'all'
94
95 for key, value in lookup_params.items():
96 if key == 'o' or key == 'ot':
97 del lookup_params[key]
98 continue
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
105 if key == 'comble':
106 comble = value
107 del lookup_params[key]
108
109 postes = rh.Poste.actifs.select_related('implantation').all()
110 postes = postes.filter(**lookup_params)
111
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:
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
129 line = {}
130 out.append(line)
131
132 line['id'] = p.id
133 line['nom'] = p.nom
134 line['implantation'] = p.implantation.nom
135 if employe:
136 line['employe_id'] = employe.id
137 line['employe_nom'] = employe.nom
138 line['employe_prenom'] = employe.prenom
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, ["%s%s" % ('-' if 'ot' in request.GET and request.GET['ot'] == "desc" else '', request.GET['o'])])
147 except KeyError:
148 raise Http404
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")
158 c = {
159 'title': 'Rapport des postes',
160 'postes': out,
161 'count': len(out),
162 'headers': list(h.headers())
163 }
164
165 return render_to_response('rh/rapports/postes.html', c, RequestContext(request))
166
167 @login_required
168 @drh_or_admin_required
169 def 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
183 employes = set([c.dossier.employe_id for c in contrats])
184
185 c = {
186 'title': 'Rapport des contrats',
187 'contrats': contrats,
188 'count': len(contrats),
189 'count_employe' : len(employes),
190 }
191
192 return render_to_response('rh/rapports/contrats.html', c, RequestContext(request))
193
194
195 @login_required
196 @drh_or_admin_required
197 def 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
217 dossiers = employe.rh_dossiers.all()
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é':
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))
253
254 def region_protected(model):
255 def wrapper(func):
256 def wrapped(request, id):
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)
266 if int(id) in [o.id for o in qs]:
267 return func(request, id)
268 return redirect_interdiction(request)
269 return wrapped
270 return wrapper
271
272
273 @region_protected(rh.Dossier)
274 def 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))
281
282 @region_protected(rh.Poste)
283 def 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
291 def employe_apercu(request, employe_id):
292 from dae.workflow import grp_drh, grp_correspondants_rh
293 employe = get_object_or_404(rh.Employe, pk=employe_id)
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:
303 regions = [d.poste.implantation.region for d in employe.rh_dossiers.all()]
304 q = Q(employe=employe) & Q(implantation__region__in=regions)
305
306 dossiers = rh.Dossier.objects.filter(employe=employe).order_by('-date_debut')
307
308 c = {
309 'is_popup' : request.GET.get('_popup', False),
310 'employe' : employe,
311 'dossiers' : dossiers,
312 }
313 return render_to_response('admin/rh/employe/apercu.html', c, RequestContext(request))
314
315