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