Renommage du filtre comblé par non-vacant dans rapport postes
[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
a184c555 9from sendfile import sendfile
e9bbd6ba 10
d04d084c 11from datamaster_modeles import models as ref
d04d084c 12from project.lib import get_employe_from_id
ae936f8d 13
d04d084c 14from rh import models as rh
e2c0b1ac 15from rh.lib import calc_remun
02c1b3dc 16from rh.decorators import drh_or_admin_required
a9faef67 17
9817fed5
OL
18# pas de reference a DAE devrait etre refactorisé
19from dae.utils import get_employe_from_user
20from dae.decorators import redirect_interdiction
21from dae.workflow import grp_drh, grp_correspondants_rh
22
a184c555 23@login_required
9817fed5 24def piece(request, filename):
a184c555 25 """Téléchargement d'une pièce jointe à un poste."""
9817fed5
OL
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)
a184c555 30 return sendfile(request, piece.fichier.path)
9817fed5
OL
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)
a184c555 37
3411ac33 38# homes
c9122f1d 39@login_required
40def profil(request):
41 """Profil personnel de l'employé - éditable"""
42 rc = RequestContext(request)
43 c = {}
d04d084c 44
c9122f1d 45 employe = rc['this_employe']
65f9fac8 46
c9122f1d 47 c['user'] = request.user
48 c['employe'] = employe
c9122f1d 49 return render_to_response('rh/profil.html', c, rc)
a9faef67 50
51# employes
3411ac33 52@login_required
a9faef67 53def employes_liste(request):
54 """Liste des employés."""
5ea6b5bb 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')
a9faef67 62 c = {
63 'user':request.user,
64 'employes':employes,
65 }
66 return render_to_response('rh/employes_liste.html', c, RequestContext(request))
67
3411ac33 68@login_required
a9faef67 69def 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))
63e17dff
PP
80
81
82# Rapports
83
84@login_required
02c1b3dc 85@drh_or_admin_required
63e17dff
PP
86def rapports_poste(request):
87
fd009814
PP
88 lookup_params = dict(request.GET.items())
89
7821915f
PP
90 comble = 'all'
91
fd009814
PP
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
7821915f
PP
99 if key == 'comble':
100 comble = value
101 del lookup_params[key]
102
63e17dff 103 postes = rh.Poste.actifs.select_related('implantation').all()
fd009814
PP
104 postes = postes.filter(**lookup_params)
105
63e17dff
PP
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:
7821915f
PP
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
63e17dff
PP
123 line = {}
124 out.append(line)
125
126 line['id'] = p.id
127 line['nom'] = p.nom
128 line['implantation'] = p.implantation.nom
63e17dff
PP
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,
08fabc85 137 'count': len(out),
63e17dff
PP
138 }
139
140 return render_to_response('rh/rapports/postes.html', c, RequestContext(request))
f2d65e83 141
02c1b3dc
JPC
142@login_required
143@drh_or_admin_required
f2d65e83
PP
144def 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))
e2c0b1ac
PP
164
165
02c1b3dc
JPC
166@login_required
167@drh_or_admin_required
e2c0b1ac
PP
168def 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
da6c523f 188 dossiers = employe.rh_dossiers.all()
e2c0b1ac
PP
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é':
e2c0b1ac
PP
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))
3ebc0952 224
32373f2e
OL
225def region_protected(model):
226 def wrapper(func):
227 def wrapped(request, id):
32373f2e
OL
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)
718abc4d 237 if int(id) in [o.id for o in qs]:
32373f2e
OL
238 return func(request, id)
239 return redirect_interdiction(request)
240 return wrapped
241 return wrapper
242
243
244@region_protected(rh.Dossier)
3ebc0952
OL
245def 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))
4a1f2ece 252
fbe60a9d
OL
253@region_protected(rh.Poste)
254def 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
4a1f2ece 262def employe_apercu(request, employe_id):
fbe60a9d 263 from dae.workflow import grp_drh, grp_correspondants_rh
4a1f2ece 264 employe = get_object_or_404(rh.Employe, pk=employe_id)
fbe60a9d
OL
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:
7eeafa57 274 regions = [d.poste.implantation.region for d in employe.rh_dossiers.all()]
fbe60a9d
OL
275 q = Q(employe=employe) & Q(implantation__region__in=regions)
276
277 dossiers = rh.Dossier.objects.filter(employe=employe).order_by('-date_debut')
278
4a1f2ece
OL
279 c = {
280 'is_popup' : request.GET.get('_popup', False),
281 'employe' : employe,
fbe60a9d 282 'dossiers' : dossiers,
4a1f2ece
OL
283 }
284 return render_to_response('admin/rh/employe/apercu.html', c, RequestContext(request))
150d83ec 285
32373f2e 286