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