contrat 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
44 # homes
45 @login_required
46 def profil(request):
47 """Profil personnel de l'employé - éditable"""
48 rc = RequestContext(request)
49 c = {}
50
51 employe = rc['this_employe']
52
53 c['user'] = request.user
54 c['employe'] = employe
55 return render_to_response('rh/profil.html', c, rc)
56
57 # employes
58 @login_required
59 def employes_liste(request):
60 """Liste des employés."""
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')
68 c = {
69 'user':request.user,
70 'employes':employes,
71 }
72 return render_to_response('rh/employes_liste.html', c, RequestContext(request))
73
74 @login_required
75 def 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))
86
87
88 # Rapports
89
90 @login_required
91 @drh_or_admin_required
92 def rapports_poste(request):
93
94 lookup_params = dict(request.GET.items())
95
96 comble = 'all'
97
98 for key, value in lookup_params.items():
99 if key == 'o' or key == 'ot':
100 del lookup_params[key]
101 continue
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
108 if key == 'comble':
109 comble = value
110 del lookup_params[key]
111
112 #postes = rh.Poste.actifs.select_related('implantation', 'dossier').all()
113 postes = rh.Poste.actifs.select_related('implantation') \
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 })
123
124 postes = postes.filter(**lookup_params)
125 if 'o' in request.GET:
126 postes = postes.order_by("%s%s" % ('-' if 'ot' in request.GET and request.GET['ot'] == "desc" else '', request.GET['o']))
127
128 headers = [
129 ("id", u"# du poste"),
130 ("nom", u"Nom du poste"),
131 ("implantation__id", u"Implantation"),
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")
137 c = {
138 'title': 'Rapport des postes',
139 'postes': postes,
140 'count': len(postes),
141 'headers': list(h.headers()),
142 }
143
144 return render_to_response('rh/rapports/postes.html', c, RequestContext(request))
145
146 @login_required
147 @drh_or_admin_required
148 def rapports_contrat(request):
149
150 lookup_params = dict(request.GET.items())
151 if 'ot' in lookup_params:
152 del lookup_params['ot']
153 if 'o' in lookup_params:
154 del lookup_params['o']
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
163 contrats = rh.Contrat.objects.select_related('dossier', 'dossier__poste', 'dossier__poste__implantation', 'type_contrat', 'dossier__employe')
164 contrats = contrats.filter(**lookup_params)
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']))
167
168 employes = set([c.dossier.employe_id for c in contrats])
169
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
181 c = {
182 'title': 'Rapport des contrats',
183 'contrats': contrats,
184 'count': len(contrats),
185 'count_employe' : len(employes),
186 'headers': list(h.headers()),
187 }
188
189 return render_to_response('rh/rapports/contrats.html', c, RequestContext(request))
190
191
192 @login_required
193 @drh_or_admin_required
194 def rapports_remuneration(request):
195
196 lookup_params = dict(request.GET.items())
197 if 'ot' in lookup_params:
198 del lookup_params['ot']
199 if 'o' in lookup_params:
200 del lookup_params['o']
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()
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
213 employes = employes.filter(**lookup_params)
214
215 output = []
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")
228
229 for employe in employes:
230 line = {}
231 output.append(line)
232
233 dossiers = employe.rh_dossiers.all()
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é':
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,
266 'headers': list(h.headers()),
267 }
268
269 return render_to_response('rh/rapports/remuneration.html', c, RequestContext(request))
270
271 def region_protected(model):
272 def wrapper(func):
273 def wrapped(request, id):
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)
283 if int(id) in [o.id for o in qs]:
284 return func(request, id)
285 return redirect_interdiction(request)
286 return wrapped
287 return wrapper
288
289
290 @region_protected(rh.Dossier)
291 def 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))
298
299 @region_protected(rh.Poste)
300 def 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
308 def employe_apercu(request, employe_id):
309 from dae.workflow import grp_drh, grp_correspondants_rh
310 employe = get_object_or_404(rh.Employe, pk=employe_id)
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:
320 regions = [d.poste.implantation.region for d in employe.rh_dossiers.all()]
321 q = Q(employe=employe) & Q(implantation__region__in=regions)
322
323 dossiers = rh.Dossier.objects.filter(employe=employe).order_by('-date_debut')
324
325 c = {
326 'is_popup' : request.GET.get('_popup', False),
327 'employe' : employe,
328 'dossiers' : dossiers,
329 }
330 return render_to_response('admin/rh/employe/apercu.html', c, RequestContext(request))
331
332