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