dossier PJ
[auf_rh_dae.git] / project / rh / views.py
CommitLineData
e9bbd6ba 1# -*- encoding: utf-8 -*-
c9122f1d 2from datetime import date
9b0a8554 3
fbe60a9d 4from django.db.models import Q
3411ac33 5from django.contrib.auth.decorators import login_required
fd009814 6from django.utils.encoding import smart_str
e9bbd6ba 7from django.shortcuts import redirect, render_to_response, get_object_or_404
8from django.template import RequestContext
9d53bc73 9from django.http import Http404
a184c555 10from sendfile import sendfile
e9bbd6ba 11
d04d084c 12from datamaster_modeles import models as ref
d04d084c 13from project.lib import get_employe_from_id
ae936f8d 14
d04d084c 15from rh import models as rh
e2c0b1ac 16from rh.lib import calc_remun
02c1b3dc 17from rh.decorators import drh_or_admin_required
9d53bc73 18from rh.templatetags.rapports import SortHeaders
a9faef67 19
9817fed5
OL
20# pas de reference a DAE devrait etre refactorisé
21from dae.utils import get_employe_from_user
22from dae.decorators import redirect_interdiction
23from dae.workflow import grp_drh, grp_correspondants_rh
24
a184c555 25@login_required
9817fed5 26def piece(request, filename):
a184c555 27 """Téléchargement d'une pièce jointe à un poste."""
9817fed5 28 model, id, f = filename.split('/')
4ba73558
OL
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)
9817fed5
OL
33 if model == 'employe':
34 # TODO definir peut-être un controle d'accès
35 piece = get_object_or_404(rh.EmployePiece, fichier=filename)
a184c555 36 return sendfile(request, piece.fichier.path)
9817fed5
OL
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)
0ce4c68b
OL
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)
a184c555 49
3411ac33 50# homes
c9122f1d 51@login_required
52def profil(request):
53 """Profil personnel de l'employé - éditable"""
54 rc = RequestContext(request)
55 c = {}
d04d084c 56
c9122f1d 57 employe = rc['this_employe']
65f9fac8 58
c9122f1d 59 c['user'] = request.user
60 c['employe'] = employe
c9122f1d 61 return render_to_response('rh/profil.html', c, rc)
a9faef67 62
63# employes
3411ac33 64@login_required
a9faef67 65def employes_liste(request):
66 """Liste des employés."""
5ea6b5bb 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')
a9faef67 74 c = {
75 'user':request.user,
76 'employes':employes,
77 }
78 return render_to_response('rh/employes_liste.html', c, RequestContext(request))
79
3411ac33 80@login_required
a9faef67 81def 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))
63e17dff
PP
92
93
94# Rapports
95
96@login_required
02c1b3dc 97@drh_or_admin_required
63e17dff
PP
98def rapports_poste(request):
99
fd009814
PP
100 lookup_params = dict(request.GET.items())
101
7821915f
PP
102 comble = 'all'
103
fd009814 104 for key, value in lookup_params.items():
9d53bc73
JPC
105 if key == 'o' or key == 'ot':
106 del lookup_params[key]
107 continue
fd009814
PP
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
7821915f
PP
114 if key == 'comble':
115 comble = value
116 del lookup_params[key]
117
65e02621
JPC
118 #postes = rh.Poste.actifs.select_related('implantation', 'dossier').all()
119 postes = rh.Poste.actifs.select_related('implantation') \
9202c5db
JPC
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 })
9d53bc73 129
65e02621 130 postes = postes.filter(**lookup_params)
9d53bc73 131 if 'o' in request.GET:
65e02621
JPC
132 postes = postes.order_by("%s%s" % ('-' if 'ot' in request.GET and request.GET['ot'] == "desc" else '', request.GET['o']))
133
4dade240
JPC
134 headers = [
135 ("id", u"# du poste"),
136 ("nom", u"Nom du poste"),
65e02621 137 ("implantation__id", u"Implantation"),
4dade240
JPC
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")
63e17dff
PP
143 c = {
144 'title': 'Rapport des postes',
65e02621
JPC
145 'postes': postes,
146 'count': len(postes),
147 'headers': list(h.headers()),
63e17dff
PP
148 }
149
150 return render_to_response('rh/rapports/postes.html', c, RequestContext(request))
f2d65e83 151
02c1b3dc
JPC
152@login_required
153@drh_or_admin_required
f2d65e83
PP
154def rapports_contrat(request):
155
156 lookup_params = dict(request.GET.items())
65e02621
JPC
157 if 'ot' in lookup_params:
158 del lookup_params['ot']
159 if 'o' in lookup_params:
160 del lookup_params['o']
f2d65e83
PP
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
65e02621 169 contrats = rh.Contrat.objects.select_related('dossier', 'dossier__poste', 'dossier__poste__implantation', 'type_contrat', 'dossier__employe')
f2d65e83 170 contrats = contrats.filter(**lookup_params)
65e02621
JPC
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']))
f2d65e83 173
8ee135b7
JPC
174 employes = set([c.dossier.employe_id for c in contrats])
175
65e02621
JPC
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
f2d65e83
PP
187 c = {
188 'title': 'Rapport des contrats',
189 'contrats': contrats,
8ee135b7
JPC
190 'count': len(contrats),
191 'count_employe' : len(employes),
65e02621 192 'headers': list(h.headers()),
f2d65e83
PP
193 }
194
195 return render_to_response('rh/rapports/contrats.html', c, RequestContext(request))
e2c0b1ac
PP
196
197
02c1b3dc
JPC
198@login_required
199@drh_or_admin_required
e2c0b1ac
PP
200def rapports_remuneration(request):
201
202 lookup_params = dict(request.GET.items())
9202c5db
JPC
203 if 'ot' in lookup_params:
204 del lookup_params['ot']
205 if 'o' in lookup_params:
206 del lookup_params['o']
e2c0b1ac
PP
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()
9202c5db
JPC
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
e2c0b1ac
PP
219 employes = employes.filter(**lookup_params)
220
221 output = []
9202c5db
JPC
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")
e2c0b1ac
PP
234
235 for employe in employes:
236 line = {}
237 output.append(line)
238
da6c523f 239 dossiers = employe.rh_dossiers.all()
e2c0b1ac
PP
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é':
e2c0b1ac
PP
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,
9202c5db 272 'headers': list(h.headers()),
e2c0b1ac
PP
273 }
274
275 return render_to_response('rh/rapports/remuneration.html', c, RequestContext(request))
3ebc0952 276
32373f2e
OL
277def region_protected(model):
278 def wrapper(func):
279 def wrapped(request, id):
32373f2e
OL
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)
718abc4d 289 if int(id) in [o.id for o in qs]:
32373f2e
OL
290 return func(request, id)
291 return redirect_interdiction(request)
292 return wrapped
293 return wrapper
294
295
296@region_protected(rh.Dossier)
3ebc0952
OL
297def 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))
4a1f2ece 304
fbe60a9d
OL
305@region_protected(rh.Poste)
306def 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
4a1f2ece 314def employe_apercu(request, employe_id):
fbe60a9d 315 from dae.workflow import grp_drh, grp_correspondants_rh
4a1f2ece 316 employe = get_object_or_404(rh.Employe, pk=employe_id)
fbe60a9d
OL
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:
7eeafa57 326 regions = [d.poste.implantation.region for d in employe.rh_dossiers.all()]
fbe60a9d
OL
327 q = Q(employe=employe) & Q(implantation__region__in=regions)
328
329 dossiers = rh.Dossier.objects.filter(employe=employe).order_by('-date_debut')
330
4a1f2ece
OL
331 c = {
332 'is_popup' : request.GET.get('_popup', False),
333 'employe' : employe,
fbe60a9d 334 'dossiers' : dossiers,
4a1f2ece
OL
335 }
336 return render_to_response('admin/rh/employe/apercu.html', c, RequestContext(request))
150d83ec 337
32373f2e 338