Ajout du nombre de postes filtrés dans rapport
[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 postes_count = rh.Poste.actifs.select_related('implantation').all().filter(**lookup_params).count()
106
107 poste_ids = set([p.pk for p in postes])
108
109 dossiers = rh.Dossier.objects.select_related('poste', 'employe').filter(poste__in=poste_ids)
110
111 employes = dict((d.poste.id, d.employe) for d in dossiers)
112
113 out = []
114
115 for p in postes:
116 employe = employes.get(p.id)
117
118 if comble == 'c' and not employe:
119 continue
120
121 if comble == 'n' and employe:
122 continue
123
124 line = {}
125 out.append(line)
126
127 line['id'] = p.id
128 line['nom'] = p.nom
129 line['implantation'] = p.implantation.nom
130 if employe:
131 line['employe_id'] = employe.id
132 line['employe_nom'] = employe.nom
133 line['employe_prenom'] = employe.prenom
134
135 c = {
136 'title': 'Rapport des postes',
137 'postes': out,
138 'count': postes_count,
139 }
140
141 return render_to_response('rh/rapports/postes.html', c, RequestContext(request))
142
143 @login_required
144 @drh_or_admin_required
145 def rapports_contrat(request):
146
147 lookup_params = dict(request.GET.items())
148
149 for key, value in lookup_params.items():
150 if not isinstance(key, str):
151 # 'key' will be used as a keyword argument later, so Python
152 # requires it to be a string.
153 del lookup_params[key]
154 lookup_params[smart_str(key)] = value
155
156 contrats = rh.Contrat.objects.select_related('dossier', 'dossier__poste', 'dossier__poste__implantation', 'type_contrat', 'dossier__employe').all()
157 contrats = contrats.filter(**lookup_params)
158
159 c = {
160 'title': 'Rapport des contrats',
161 'contrats': contrats,
162 }
163
164 return render_to_response('rh/rapports/contrats.html', c, RequestContext(request))
165
166
167 @login_required
168 @drh_or_admin_required
169 def rapports_remuneration(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 employes = rh.Employe.objects.all()
181 employes = employes.filter(**lookup_params)
182
183 output = []
184
185 for employe in employes:
186 line = {}
187 output.append(line)
188
189 dossiers = employe.rh_dossiers.all()
190
191 remun = {}
192 remun_sum_euro = 0
193
194 for dossier in dossiers:
195 this_remun, this_remun_sum, this_remun_sum_euro = calc_remun(dossier)
196
197 for item in this_remun:
198 if item not in remun:
199 remun[item] = this_remun[item]
200 else:
201 remun[item][0] += this_remun[item][0]
202 remun[item][1] += this_remun[item][1]
203
204 remun_sum_euro += this_remun_sum_euro
205
206 line['remun_sum_euro'] = remun_sum_euro
207
208 for r in remun:
209 if r == u'Indemnité':
210 line['Indemnite'] = remun[r][1]
211 else:
212 line[r] = remun[r][1]
213
214 line['id'] = employe.id
215 line['nom'] = employe.nom
216 line['prenom'] = employe.prenom
217
218
219 c = {
220 'title': 'Rapport de remuneration',
221 'employes': output,
222 }
223
224 return render_to_response('rh/rapports/remuneration.html', c, RequestContext(request))
225
226 def region_protected(model):
227 def wrapper(func):
228 def wrapped(request, id):
229 if request.user.is_superuser:
230 return func(request, id)
231 user_groups = request.user.groups.all()
232 if grp_drh in user_groups:
233 return func(request, id)
234 if grp_correspondants_rh in user_groups:
235 employe = get_employe_from_user(request.user)
236 q = Q(**{model.prefix_implantation: employe.implantation.region})
237 qs = model.objects.filter(q)
238 if int(id) in [o.id for o in qs]:
239 return func(request, id)
240 return redirect_interdiction(request)
241 return wrapped
242 return wrapper
243
244
245 @region_protected(rh.Dossier)
246 def dossier_apercu(request, dossier_id):
247 c = {
248 'is_popup' : request.GET.get('_popup', False),
249 'dossier' : get_object_or_404(rh.Dossier, pk=dossier_id)
250
251 }
252 return render_to_response('admin/rh/dossier/apercu.html', c, RequestContext(request))
253
254 @region_protected(rh.Poste)
255 def poste_apercu(request, poste_id):
256 c = {
257 'is_popup' : request.GET.get('_popup', False),
258 'poste' : get_object_or_404(rh.Poste, pk=poste_id)
259
260 }
261 return render_to_response('admin/rh/poste/apercu.html', c, RequestContext(request))
262
263 def employe_apercu(request, employe_id):
264 from dae.workflow import grp_drh, grp_correspondants_rh
265 employe = get_object_or_404(rh.Employe, pk=employe_id)
266 user_groups = request.user.groups.all()
267
268 dernier_dossier = None
269 dossiers = None
270
271 if request.user.is_superuser or \
272 grp_drh in user_groups:
273 q = Q(employe=employe)
274 if grp_correspondants_rh in user_groups:
275 regions = [d.poste.implantation.region for d in employe.rh_dossiers.all()]
276 q = Q(employe=employe) & Q(implantation__region__in=regions)
277
278 dossiers = rh.Dossier.objects.filter(employe=employe).order_by('-date_debut')
279
280 c = {
281 'is_popup' : request.GET.get('_popup', False),
282 'employe' : employe,
283 'dossiers' : dossiers,
284 }
285 return render_to_response('admin/rh/employe/apercu.html', c, RequestContext(request))
286
287