poste dae -> poste rh
[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 sendfile import sendfile
10
11 from datamaster_modeles import models as ref
12 from project.lib import get_employe_from_id
13
14 from rh import models as rh
15 from rh.lib import calc_remun
16
17 @login_required
18 def poste_piece(request, filename):
19 """Téléchargement d'une pièce jointe à un poste."""
20 piece = get_object_or_404(rh.PostePiece, fichier=filename)
21 if rh.Poste.objects.ma_region_ou_service(request.user).filter(id=piece.poste_id).exists():
22 return sendfile(request, piece.fichier.path)
23 else:
24 return redirect_interdiction(request)
25
26 # homes
27 @login_required
28 def profil(request):
29 """Profil personnel de l'employé - éditable"""
30 rc = RequestContext(request)
31 c = {}
32
33 employe = rc['this_employe']
34
35 c['user'] = request.user
36 c['employe'] = employe
37 return render_to_response('rh/profil.html', c, rc)
38
39 # employes
40 @login_required
41 def employes_liste(request):
42 """Liste des employés."""
43 today = date.today()
44 employes = rh.Employe.objects \
45 .filter(actif=True, supprime=False) \
46 .filter(dossiers__actif=True, dossiers__supprime=False) \
47 .exclude(dossiers__date_debut__gt=today) \
48 .exclude(dossiers__date_fin__lt=today) \
49 .order_by('nom')
50 c = {
51 'user':request.user,
52 'employes':employes,
53 }
54 return render_to_response('rh/employes_liste.html', c, RequestContext(request))
55
56 @login_required
57 def employe(request, id):
58 """Information publique sur un employé."""
59 try:
60 employe = rh.Employe.objects.get(pk=id)
61 except:
62 employe = rh.Employe.objects.none()
63 c = {
64 'user':request.user,
65 'employe':employe,
66 }
67 return render_to_response('rh/employe.html', c, RequestContext(request))
68
69
70 # Rapports
71
72 @login_required
73 def rapports_poste(request):
74
75 lookup_params = dict(request.GET.items())
76
77 comble = 'all'
78
79 for key, value in lookup_params.items():
80 if not isinstance(key, str):
81 # 'key' will be used as a keyword argument later, so Python
82 # requires it to be a string.
83 del lookup_params[key]
84 lookup_params[smart_str(key)] = value
85
86 if key == 'comble':
87 comble = value
88 del lookup_params[key]
89
90 postes = rh.Poste.actifs.select_related('implantation').all()
91 postes = postes.filter(**lookup_params)
92
93 poste_ids = set([p.pk for p in postes])
94
95 dossiers = rh.Dossier.objects.select_related('poste', 'employe').filter(poste__in=poste_ids)
96
97 employes = dict((d.poste.id, d.employe) for d in dossiers)
98
99 out = []
100
101 for p in postes:
102 employe = employes.get(p.id)
103
104 if comble == 'c' and not employe:
105 continue
106
107 if comble == 'n' and employe:
108 continue
109
110 line = {}
111 out.append(line)
112
113 line['id'] = p.id
114 line['nom'] = p.nom
115 line['implantation'] = p.implantation.nom
116 if employe:
117 line['employe_id'] = employe.id
118 line['employe_nom'] = employe.nom
119 line['employe_prenom'] = employe.prenom
120
121 c = {
122 'title': 'Rapport des postes',
123 'postes': out,
124 }
125
126 return render_to_response('rh/rapports/postes.html', c, RequestContext(request))
127
128
129 def rapports_contrat(request):
130
131 lookup_params = dict(request.GET.items())
132
133 for key, value in lookup_params.items():
134 if not isinstance(key, str):
135 # 'key' will be used as a keyword argument later, so Python
136 # requires it to be a string.
137 del lookup_params[key]
138 lookup_params[smart_str(key)] = value
139
140 contrats = rh.Contrat.objects.select_related('dossier', 'dossier__poste', 'dossier__poste__implantation', 'type_contrat', 'dossier__employe').all()
141 contrats = contrats.filter(**lookup_params)
142
143 c = {
144 'title': 'Rapport des contrats',
145 'contrats': contrats,
146 }
147
148 return render_to_response('rh/rapports/contrats.html', c, RequestContext(request))
149
150
151 def rapports_remuneration(request):
152
153 lookup_params = dict(request.GET.items())
154
155 for key, value in lookup_params.items():
156 if not isinstance(key, str):
157 # 'key' will be used as a keyword argument later, so Python
158 # requires it to be a string.
159 del lookup_params[key]
160 lookup_params[smart_str(key)] = value
161
162 employes = rh.Employe.objects.all()
163 employes = employes.filter(**lookup_params)
164
165 output = []
166
167 for employe in employes:
168 line = {}
169 output.append(line)
170
171 dossiers = employe.dossiers.all()
172
173 remun = {}
174 remun_sum_euro = 0
175
176 for dossier in dossiers:
177 this_remun, this_remun_sum, this_remun_sum_euro = calc_remun(dossier)
178
179 for item in this_remun:
180 if item not in remun:
181 remun[item] = this_remun[item]
182 else:
183 remun[item][0] += this_remun[item][0]
184 remun[item][1] += this_remun[item][1]
185
186 remun_sum_euro += this_remun_sum_euro
187
188 line['remun_sum_euro'] = remun_sum_euro
189
190 for r in remun:
191 if r == u'Indemnité':
192 line['Indemnite'] = remun[r][1]
193 else:
194 line[r] = remun[r][1]
195
196 line['id'] = employe.id
197 line['nom'] = employe.nom
198 line['prenom'] = employe.prenom
199
200
201 c = {
202 'title': 'Rapport de remuneration',
203 'employes': output,
204 }
205
206 return render_to_response('rh/rapports/remuneration.html', c, RequestContext(request))
207
208 def region_protected(model):
209 def wrapper(func):
210 def wrapped(request, id):
211 from dae.utils import get_employe_from_user
212 from dae.decorators import redirect_interdiction
213 from dae.workflow import grp_drh, grp_correspondants_rh
214 if request.user.is_superuser:
215 return func(request, id)
216 user_groups = request.user.groups.all()
217 if grp_drh in user_groups:
218 return func(request, id)
219 if grp_correspondants_rh in user_groups:
220 employe = get_employe_from_user(request.user)
221 q = Q(**{model.prefix_implantation: employe.implantation.region})
222 qs = model.objects.filter(q)
223 if int(id) in [o.id for o in qs]:
224 return func(request, id)
225 return redirect_interdiction(request)
226 return wrapped
227 return wrapper
228
229
230 @region_protected(rh.Dossier)
231 def dossier_apercu(request, dossier_id):
232 c = {
233 'is_popup' : request.GET.get('_popup', False),
234 'dossier' : get_object_or_404(rh.Dossier, pk=dossier_id)
235
236 }
237 return render_to_response('admin/rh/dossier/apercu.html', c, RequestContext(request))
238
239 @region_protected(rh.Poste)
240 def poste_apercu(request, poste_id):
241 c = {
242 'is_popup' : request.GET.get('_popup', False),
243 'poste' : get_object_or_404(rh.Poste, pk=poste_id)
244
245 }
246 return render_to_response('admin/rh/poste/apercu.html', c, RequestContext(request))
247
248 def employe_apercu(request, employe_id):
249 from dae.workflow import grp_drh, grp_correspondants_rh
250 employe = get_object_or_404(rh.Employe, pk=employe_id)
251 user_groups = request.user.groups.all()
252
253 dernier_dossier = None
254 dossiers = None
255
256 if request.user.is_superuser or \
257 grp_drh in user_groups:
258 q = Q(employe=employe)
259 if grp_correspondants_rh in user_groups:
260 regions = [d.poste.implantation.region for d in employe.dossiers.all()]
261 q = Q(employe=employe) & Q(implantation__region__in=regions)
262
263 dossiers = rh.Dossier.objects.filter(employe=employe).order_by('-date_debut')
264
265 c = {
266 'is_popup' : request.GET.get('_popup', False),
267 'employe' : employe,
268 'dossiers' : dossiers,
269 }
270 return render_to_response('admin/rh/employe/apercu.html', c, RequestContext(request))
271
272