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