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