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