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