Retirer debug
[auf_rh_dae.git] / project / rh / views.py
1 # -*- encoding: utf-8 -*-
2 from datetime import date
3
4 from django.contrib.auth.decorators import login_required
5 from django.utils.encoding import smart_str
6 from django.shortcuts import redirect, render_to_response, get_object_or_404
7 from django.template import RequestContext
8
9 from datamaster_modeles import models as ref
10 from project.lib import get_employe_from_id
11
12 from rh import models as rh
13 from rh.lib import calc_remun
14
15 # homes
16 @login_required
17 def profil(request):
18 """Profil personnel de l'employé - éditable"""
19 rc = RequestContext(request)
20 c = {}
21
22 employe = rc['this_employe']
23
24 c['user'] = request.user
25 c['employe'] = employe
26 return render_to_response('rh/profil.html', c, rc)
27
28 # employes
29 @login_required
30 def employes_liste(request):
31 """Liste des employés."""
32 today = date.today()
33 employes = rh.Employe.objects \
34 .filter(actif=True, supprime=False) \
35 .filter(dossiers__actif=True, dossiers__supprime=False) \
36 .exclude(dossiers__date_debut__gt=today) \
37 .exclude(dossiers__date_fin__lt=today) \
38 .order_by('nom')
39 c = {
40 'user':request.user,
41 'employes':employes,
42 }
43 return render_to_response('rh/employes_liste.html', c, RequestContext(request))
44
45 @login_required
46 def employe(request, id):
47 """Information publique sur un employé."""
48 try:
49 employe = rh.Employe.objects.get(pk=id)
50 except:
51 employe = rh.Employe.objects.none()
52 c = {
53 'user':request.user,
54 'employe':employe,
55 }
56 return render_to_response('rh/employe.html', c, RequestContext(request))
57
58
59 # Rapports
60
61 @login_required
62 def rapports_poste(request):
63
64 lookup_params = dict(request.GET.items())
65
66 comble = 'all'
67
68 for key, value in lookup_params.items():
69 if not isinstance(key, str):
70 # 'key' will be used as a keyword argument later, so Python
71 # requires it to be a string.
72 del lookup_params[key]
73 lookup_params[smart_str(key)] = value
74
75 if key == 'comble':
76 comble = value
77 del lookup_params[key]
78
79 postes = rh.Poste.actifs.select_related('implantation').all()
80 postes = postes.filter(**lookup_params)
81
82 poste_ids = set([p.pk for p in postes])
83
84 dossiers = rh.Dossier.objects.select_related('poste', 'employe').filter(poste__in=poste_ids)
85
86 employes = dict((d.poste.id, d.employe) for d in dossiers)
87
88 out = []
89
90 for p in postes:
91 employe = employes.get(p.id)
92
93 if comble == 'c' and not employe:
94 continue
95
96 if comble == 'n' and employe:
97 continue
98
99 line = {}
100 out.append(line)
101
102 line['id'] = p.id
103 line['nom'] = p.nom
104 line['implantation'] = p.implantation.nom
105 if employe:
106 line['employe_id'] = employe.id
107 line['employe_nom'] = employe.nom
108 line['employe_prenom'] = employe.prenom
109
110 c = {
111 'title': 'Rapport des postes',
112 'postes': out,
113 }
114
115 return render_to_response('rh/rapports/postes.html', c, RequestContext(request))
116
117
118 def rapports_contrat(request):
119
120 lookup_params = dict(request.GET.items())
121
122 for key, value in lookup_params.items():
123 if not isinstance(key, str):
124 # 'key' will be used as a keyword argument later, so Python
125 # requires it to be a string.
126 del lookup_params[key]
127 lookup_params[smart_str(key)] = value
128
129 contrats = rh.Contrat.objects.select_related('dossier', 'dossier__poste', 'dossier__poste__implantation', 'type_contrat', 'dossier__employe').all()
130 contrats = contrats.filter(**lookup_params)
131
132 c = {
133 'title': 'Rapport des contrats',
134 'contrats': contrats,
135 }
136
137 return render_to_response('rh/rapports/contrats.html', c, RequestContext(request))
138
139
140 def rapports_remuneration(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 employes = rh.Employe.objects.all()
152 employes = employes.filter(**lookup_params)
153
154 output = []
155
156 for employe in employes:
157 line = {}
158 output.append(line)
159
160 dossiers = employe.dossiers.all()
161
162 remun = {}
163 remun_sum_euro = 0
164
165 for dossier in dossiers:
166 this_remun, this_remun_sum, this_remun_sum_euro = calc_remun(dossier)
167
168 for item in this_remun:
169 if item not in remun:
170 remun[item] = this_remun[item]
171 else:
172 remun[item][0] += this_remun[item][0]
173 remun[item][1] += this_remun[item][1]
174
175 remun_sum_euro += this_remun_sum_euro
176
177 line['remun_sum_euro'] = remun_sum_euro
178
179 for r in remun:
180 if r == u'Indemnité':
181 line['Indemnite'] = remun[r][1]
182 else:
183 line[r] = remun[r][1]
184
185 line['id'] = employe.id
186 line['nom'] = employe.nom
187 line['prenom'] = employe.prenom
188
189
190 c = {
191 'title': 'Rapport de remuneration',
192 'employes': output,
193 }
194
195 return render_to_response('rh/rapports/remuneration.html', c, RequestContext(request))