Rapport contrats: filtrage avec supprime != 1
[auf_rh_dae.git] / project / rh / views.py
CommitLineData
e9bbd6ba 1# -*- encoding: utf-8 -*-
c9122f1d 2from datetime import date
a7de62d7 3from itertools import izip
9b0a8554 4
fbe60a9d 5from django.db.models import Q
3411ac33 6from django.contrib.auth.decorators import login_required
fd009814 7from django.utils.encoding import smart_str
e9bbd6ba 8from django.shortcuts import redirect, render_to_response, get_object_or_404
9from django.template import RequestContext
9d53bc73 10from django.http import Http404
a184c555 11from sendfile import sendfile
e9bbd6ba 12
d04d084c 13from datamaster_modeles import models as ref
d04d084c 14from project.lib import get_employe_from_id
ae936f8d 15
d04d084c 16from rh import models as rh
e2c0b1ac 17from rh.lib import calc_remun
02c1b3dc 18from rh.decorators import drh_or_admin_required
9d53bc73 19from rh.templatetags.rapports import SortHeaders
a9faef67 20
9817fed5
OL
21# pas de reference a DAE devrait etre refactorisé
22from dae.utils import get_employe_from_user
23from dae.decorators import redirect_interdiction
24from dae.workflow import grp_drh, grp_correspondants_rh
baf9b78c 25from django.conf import settings
9817fed5 26
a184c555 27@login_required
9817fed5 28def piece(request, filename):
a184c555 29 """Téléchargement d'une pièce jointe à un poste."""
9817fed5 30 model, id, f = filename.split('/')
4ba73558
OL
31 if model == 'contrat':
32 # TODO definir peut-être un controle d'accès
33 piece = get_object_or_404(rh.Contrat, fichier=filename)
34 return sendfile(request, piece.fichier.path)
9817fed5
OL
35 if model == 'employe':
36 # TODO definir peut-être un controle d'accès
37 piece = get_object_or_404(rh.EmployePiece, fichier=filename)
a184c555 38 return sendfile(request, piece.fichier.path)
9817fed5
OL
39 if model == 'poste':
40 piece = get_object_or_404(rh.PostePiece, fichier=filename)
41 if rh.Poste.objects.ma_region_ou_service(request.user).filter(id=piece.poste_id).exists():
42 return sendfile(request, piece.fichier.path)
43 else:
44 return redirect_interdiction(request)
0ce4c68b
OL
45 if model == 'dossier':
46 piece = get_object_or_404(rh.DossierPiece, fichier=filename)
47 if rh.Dossier.objects.ma_region_ou_service(request.user).filter(id=piece.dossier_id).exists():
48 return sendfile(request, piece.fichier.path)
49 else:
50 return redirect_interdiction(request)
a184c555 51
3411ac33 52# homes
c9122f1d 53@login_required
54def profil(request):
55 """Profil personnel de l'employé - éditable"""
56 rc = RequestContext(request)
57 c = {}
d04d084c 58
c9122f1d 59 employe = rc['this_employe']
65f9fac8 60
c9122f1d 61 c['user'] = request.user
62 c['employe'] = employe
c9122f1d 63 return render_to_response('rh/profil.html', c, rc)
a9faef67 64
65# employes
3411ac33 66@login_required
a9faef67 67def employes_liste(request):
68 """Liste des employés."""
5ea6b5bb 69 today = date.today()
70 employes = rh.Employe.objects \
71 .filter(actif=True, supprime=False) \
72 .filter(dossiers__actif=True, dossiers__supprime=False) \
73 .exclude(dossiers__date_debut__gt=today) \
74 .exclude(dossiers__date_fin__lt=today) \
75 .order_by('nom')
a9faef67 76 c = {
77 'user':request.user,
78 'employes':employes,
79 }
80 return render_to_response('rh/employes_liste.html', c, RequestContext(request))
81
3411ac33 82@login_required
a9faef67 83def employe(request, id):
84 """Information publique sur un employé."""
85 try:
86 employe = rh.Employe.objects.get(pk=id)
87 except:
88 employe = rh.Employe.objects.none()
89 c = {
90 'user':request.user,
91 'employe':employe,
92 }
93 return render_to_response('rh/employe.html', c, RequestContext(request))
63e17dff
PP
94
95
96# Rapports
97
98@login_required
02c1b3dc 99@drh_or_admin_required
63e17dff
PP
100def rapports_poste(request):
101
fd009814
PP
102 lookup_params = dict(request.GET.items())
103
7821915f
PP
104 comble = 'all'
105
fd009814 106 for key, value in lookup_params.items():
9d53bc73
JPC
107 if key == 'o' or key == 'ot':
108 del lookup_params[key]
109 continue
fd009814
PP
110 if not isinstance(key, str):
111 # 'key' will be used as a keyword argument later, so Python
112 # requires it to be a string.
113 del lookup_params[key]
114 lookup_params[smart_str(key)] = value
115
7821915f
PP
116 if key == 'comble':
117 comble = value
118 del lookup_params[key]
119
65e02621
JPC
120 #postes = rh.Poste.actifs.select_related('implantation', 'dossier').all()
121 postes = rh.Poste.actifs.select_related('implantation') \
9202c5db
JPC
122 .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' % \
123 ("%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 "") \
124 }) \
125 .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' % \
126 ("%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 "") \
127 }) \
128 .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' % \
129 ("%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 "") \
130 })
9d53bc73 131
65e02621 132 postes = postes.filter(**lookup_params)
9d53bc73 133 if 'o' in request.GET:
65e02621
JPC
134 postes = postes.order_by("%s%s" % ('-' if 'ot' in request.GET and request.GET['ot'] == "desc" else '', request.GET['o']))
135
a7de62d7
JPC
136 out = []
137 for p in postes:
138 out.append({
139 'id': p.id,
140 'nom': p.nom,
141 'implantation': p.implantation,
142 'employes': [] if not p.employe_id else [{'id': id, 'nom': nom, 'prenom': prenom} for id, nom, prenom in izip(p.employe_id.split('|'), p.employe_nom.split('|'), p.employe_prenom.split('|'))] })
143
144
4dade240
JPC
145 headers = [
146 ("id", u"# du poste"),
147 ("nom", u"Nom du poste"),
65e02621 148 ("implantation__id", u"Implantation"),
4dade240 149 ("employe_nom", u"Nom"),
4dade240
JPC
150 ]
151 h = SortHeaders(request, headers, order_field_type="ot", order_field="o")
63e17dff
PP
152 c = {
153 'title': 'Rapport des postes',
a7de62d7
JPC
154 'postes': out,
155 'count': len(out),
65e02621 156 'headers': list(h.headers()),
63e17dff
PP
157 }
158
159 return render_to_response('rh/rapports/postes.html', c, RequestContext(request))
f2d65e83 160
02c1b3dc
JPC
161@login_required
162@drh_or_admin_required
f2d65e83
PP
163def rapports_contrat(request):
164
165 lookup_params = dict(request.GET.items())
65e02621
JPC
166 if 'ot' in lookup_params:
167 del lookup_params['ot']
168 if 'o' in lookup_params:
169 del lookup_params['o']
f2d65e83
PP
170
171 for key, value in lookup_params.items():
172 if not isinstance(key, str):
173 # 'key' will be used as a keyword argument later, so Python
174 # requires it to be a string.
175 del lookup_params[key]
176 lookup_params[smart_str(key)] = value
177
65e02621 178 contrats = rh.Contrat.objects.select_related('dossier', 'dossier__poste', 'dossier__poste__implantation', 'type_contrat', 'dossier__employe')
821b9889 179 contrats = contrats.filter(**lookup_params).exclude(dossier__employe__supprime=1)
65e02621
JPC
180 if 'o' in request.GET:
181 contrats = contrats.order_by("%s%s" % ('-' if 'ot' in request.GET and request.GET['ot'] == "desc" else '', request.GET['o']))
f2d65e83 182
8ee135b7
JPC
183 employes = set([c.dossier.employe_id for c in contrats])
184
65e02621
JPC
185 headers = [
186 ("dossier__employe__id", u"# de l'employé"),
187 ("dossier__employe__nom", u"Nom"),
188 ("dossier__employe__prenom", u"Prénom"),
189 ("type_contrat__nom", u"Poste"),
190 ("type_contrat__nom", u"Type de contrat"),
191 ("date_debut", u"Date début"),
192 ("date_fin", u"Date fin"),
193 ]
194 h = SortHeaders(request, headers, order_field_type="ot", order_field="o")
195
f2d65e83
PP
196 c = {
197 'title': 'Rapport des contrats',
198 'contrats': contrats,
8ee135b7
JPC
199 'count': len(contrats),
200 'count_employe' : len(employes),
65e02621 201 'headers': list(h.headers()),
f2d65e83
PP
202 }
203
204 return render_to_response('rh/rapports/contrats.html', c, RequestContext(request))
e2c0b1ac
PP
205
206
02c1b3dc
JPC
207@login_required
208@drh_or_admin_required
e2c0b1ac
PP
209def rapports_remuneration(request):
210
211 lookup_params = dict(request.GET.items())
9202c5db
JPC
212 if 'ot' in lookup_params:
213 del lookup_params['ot']
214 if 'o' in lookup_params:
215 del lookup_params['o']
e2c0b1ac
PP
216
217 for key, value in lookup_params.items():
218 if not isinstance(key, str):
219 # 'key' will be used as a keyword argument later, so Python
220 # requires it to be a string.
221 del lookup_params[key]
222 lookup_params[smart_str(key)] = value
223
224 employes = rh.Employe.objects.all()
9202c5db
JPC
225 if 'o' in request.GET:
226 employes = employes.order_by("%s%s" % ('-' if 'ot' in request.GET and request.GET['ot'] == "desc" else '', request.GET['o']))
227
e2c0b1ac
PP
228 employes = employes.filter(**lookup_params)
229
230 output = []
9202c5db
JPC
231 headers = [
232 ("id", u"# de l'employé"),
233 ("nom", u"Nom"),
234 ("prenom", u"Prénom"),
235 ("", u"Salaire"),
236 ("", u"RAS"),
237 ("", u"Indemnités"),
238 ("", u"Accessoire"),
239 ("", u"Charges patronales"),
240 ("", u"Total"),
241 ]
242 h = SortHeaders(request, headers, order_field_type="ot", order_field="o")
e2c0b1ac
PP
243
244 for employe in employes:
245 line = {}
246 output.append(line)
247
da6c523f 248 dossiers = employe.rh_dossiers.all()
e2c0b1ac
PP
249
250 remun = {}
251 remun_sum_euro = 0
252
253 for dossier in dossiers:
254 this_remun, this_remun_sum, this_remun_sum_euro = calc_remun(dossier)
255
256 for item in this_remun:
257 if item not in remun:
258 remun[item] = this_remun[item]
259 else:
260 remun[item][0] += this_remun[item][0]
261 remun[item][1] += this_remun[item][1]
262
263 remun_sum_euro += this_remun_sum_euro
264
265 line['remun_sum_euro'] = remun_sum_euro
266
267 for r in remun:
268 if r == u'Indemnité':
e2c0b1ac
PP
269 line['Indemnite'] = remun[r][1]
270 else:
271 line[r] = remun[r][1]
272
273 line['id'] = employe.id
274 line['nom'] = employe.nom
275 line['prenom'] = employe.prenom
276
277
278 c = {
279 'title': 'Rapport de remuneration',
280 'employes': output,
9202c5db 281 'headers': list(h.headers()),
e2c0b1ac
PP
282 }
283
284 return render_to_response('rh/rapports/remuneration.html', c, RequestContext(request))
3ebc0952 285
32373f2e
OL
286def region_protected(model):
287 def wrapper(func):
288 def wrapped(request, id):
32373f2e
OL
289 if request.user.is_superuser:
290 return func(request, id)
291 user_groups = request.user.groups.all()
292 if grp_drh in user_groups:
293 return func(request, id)
294 if grp_correspondants_rh in user_groups:
295 employe = get_employe_from_user(request.user)
296 q = Q(**{model.prefix_implantation: employe.implantation.region})
297 qs = model.objects.filter(q)
718abc4d 298 if int(id) in [o.id for o in qs]:
32373f2e
OL
299 return func(request, id)
300 return redirect_interdiction(request)
301 return wrapped
302 return wrapper
303
304
305@region_protected(rh.Dossier)
3ebc0952 306def dossier_apercu(request, dossier_id):
baf9b78c 307 d = get_object_or_404(rh.Dossier, pk=dossier_id)
3ebc0952
OL
308 c = {
309 'is_popup' : request.GET.get('_popup', False),
baf9b78c 310 'dossier' : d,
520ab455
JPC
311 'pieces' : rh.DossierPiece.objects.filter(dossier__exact=d),
312 'contrats' : rh.Contrat.objects.filter(dossier__exact=d),
2a36598b 313 'commentaires' : rh.DossierCommentaire.objects.filter(dossier__exact=d).all(),
abf91905 314 'media_url': settings.PRIVE_MEDIA_URL,
3ebc0952
OL
315 }
316 return render_to_response('admin/rh/dossier/apercu.html', c, RequestContext(request))
4a1f2ece 317
fbe60a9d
OL
318@region_protected(rh.Poste)
319def poste_apercu(request, poste_id):
320 c = {
321 'is_popup' : request.GET.get('_popup', False),
0dcfc806
JPC
322 'poste' : get_object_or_404(rh.Poste, pk=poste_id),
323 'financements' : rh.PosteFinancement.objects.filter(poste=poste_id).all(),
324 'pieces' : rh.PostePiece.objects.filter(poste=poste_id).all(),
325 'dossiers' : rh.Dossier.objects.filter(poste=poste_id).order_by("-date_debut").all(),
326 'comparaisons' : rh.PosteComparaison.objects.filter(poste=poste_id).all(),
327 'commentaires' : rh.PosteCommentaire.objects.filter(poste=poste_id).all(),
abf91905 328 'media_url': settings.PRIVE_MEDIA_URL,
fbe60a9d
OL
329 }
330 return render_to_response('admin/rh/poste/apercu.html', c, RequestContext(request))
331
4a1f2ece 332def employe_apercu(request, employe_id):
fbe60a9d 333 from dae.workflow import grp_drh, grp_correspondants_rh
4a1f2ece 334 employe = get_object_or_404(rh.Employe, pk=employe_id)
fbe60a9d
OL
335 user_groups = request.user.groups.all()
336
337 dernier_dossier = None
338 dossiers = None
339
340 if request.user.is_superuser or \
341 grp_drh in user_groups:
342 q = Q(employe=employe)
343 if grp_correspondants_rh in user_groups:
7eeafa57 344 regions = [d.poste.implantation.region for d in employe.rh_dossiers.all()]
fbe60a9d
OL
345 q = Q(employe=employe) & Q(implantation__region__in=regions)
346
347 dossiers = rh.Dossier.objects.filter(employe=employe).order_by('-date_debut')
348
4a1f2ece
OL
349 c = {
350 'is_popup' : request.GET.get('_popup', False),
351 'employe' : employe,
fbe60a9d 352 'dossiers' : dossiers,
abf91905 353 'media_url': settings.PRIVE_MEDIA_URL,
4a1f2ece
OL
354 }
355 return render_to_response('admin/rh/employe/apercu.html', c, RequestContext(request))
150d83ec 356
32373f2e 357