[#2374] No. employé partout
[auf_rh_dae.git] / project / rh / lib.py
1 # -*- encoding: utf-8 -*-
2
3 from collections import defaultdict
4 import datetime
5
6 from django.db import models
7 from django.contrib import admin
8 from django.conf import settings
9 from django.db.models import Q
10 from auf.django.metadata.admin import AUFMetadataAdminMixin, AUFMetadataInlineAdminMixin, AUF_METADATA_READONLY_FIELDS
11 from project.rh import models as rh
12 from forms import DossierForm, ContratForm
13 from dae.utils import get_employe_from_user
14
15
16 class ProtectRegionMixin(object):
17
18 def queryset(self, request):
19 qs = super(ProtectRegionMixin, self).queryset(request)
20
21 if request.user.is_superuser:
22 return qs
23
24 employe = get_employe_from_user(request.user)
25
26 q = Q(**{self.model.prefix_implantation: employe.implantation.region})
27 qs = qs.filter(q).distinct()
28 return qs
29
30 def has_change_permission(self, request, obj=None):
31 if request.user.is_superuser:
32 return True
33
34 if obj:
35 employe = get_employe_from_user(request.user)
36 if employe.implantation.region in obj.get_regions():
37 return True
38 else:
39 return False
40
41 return True
42
43
44 # Inlines
45
46 class ReadOnlyInlineMixin(object):
47 def get_readonly_fields(self, request, obj=None):
48 return [f.name for f in self.model._meta.fields if f.name not in AUF_METADATA_READONLY_FIELDS]
49
50
51 class AyantDroitInline(AUFMetadataInlineAdminMixin, admin.StackedInline):
52 model = models.Model # à remplacer dans admin.py
53 extra = 0
54
55 fieldsets = (
56 (None, {
57 'fields': (('nom', 'prenom'), ('nom_affichage', 'genre'), 'nationalite', 'date_naissance', 'lien_parente', )
58 }),
59 )
60
61
62 class AyantDroitCommentaireInline(AUFMetadataInlineAdminMixin, admin.TabularInline):
63 readonly_fields = ('owner', )
64 model = models.Model # à remplacer dans admin.py
65 extra = 1
66
67
68 class ContratInline(AUFMetadataInlineAdminMixin, admin.TabularInline):
69 form = ContratForm
70 model = models.Model # à remplacer dans admin.py
71 extra = 1
72
73
74 class DossierROInline(ReadOnlyInlineMixin, admin.TabularInline):
75 exclude = AUF_METADATA_READONLY_FIELDS
76 model = models.Model # à remplacer dans admin.py
77 extra = 0
78
79
80 class DossierCommentaireInline(AUFMetadataInlineAdminMixin, admin.TabularInline):
81 readonly_fields = ('owner', )
82 model = models.Model # à remplacer dans admin.py
83 extra = 1
84
85
86 class DossierPieceInline(admin.TabularInline):
87 model = models.Model # à remplacer dans admin.py
88 extra = 4
89
90
91 class EmployeInline(admin.TabularInline):
92 model = models.Model # à remplacer dans admin.py
93
94 class EmployeCommentaireInline(AUFMetadataInlineAdminMixin, admin.TabularInline):
95 readonly_fields = ('owner', )
96 model = models.Model # à remplacer dans admin.py
97 extra = 1
98
99
100 class EmployePieceInline(admin.TabularInline):
101 model = models.Model # à remplacer dans admin.py
102 extra = 4
103
104
105 class EvenementInline(AUFMetadataInlineAdminMixin, admin.TabularInline):
106 model = models.Model # à remplacer dans admin.py
107 extra = 1
108
109
110 class EvenementRemunerationInline(AUFMetadataInlineAdminMixin, admin.TabularInline):
111 model = models.Model # à remplacer dans admin.py
112 extra = 1
113
114
115 class PosteCommentaireInline(AUFMetadataInlineAdminMixin, admin.TabularInline):
116 readonly_fields = ('owner', )
117 model = models.Model # à remplacer dans admin.py
118 extra = 1
119
120
121 class PosteFinancementInline(admin.TabularInline):
122 model = models.Model # à remplacer dans admin.py
123
124
125 class PostePieceInline(admin.TabularInline):
126 model = models.Model # à remplacer dans admin.py
127
128
129 class RemunerationInline(AUFMetadataInlineAdminMixin, admin.TabularInline):
130 model = models.Model # à remplacer dans admin.py
131 extra = 1
132
133
134 class RemunerationROInline(ReadOnlyInlineMixin, RemunerationInline):
135 pass
136
137
138 class TypePosteInline(AUFMetadataInlineAdminMixin, admin.TabularInline):
139 model = models.Model # à remplacer dans admin.py
140
141
142 # Admins
143
144 class AyantDroitAdmin(AUFMetadataAdminMixin, ProtectRegionMixin, admin.ModelAdmin):
145 """
146 L'ajout d'un nouvel ayantdroit se fait dans l'admin de l'employé.
147 """
148 alphabet_filter = 'nom'
149 search_fields = ('nom', 'prenom', 'employe__nom', 'employe__prenom', )
150 list_display = ('_employe', 'lien_parente', '_ayantdroit', )
151 inlines = (AyantDroitCommentaireInline,)
152 readonly_fields = AUFMetadataAdminMixin.readonly_fields + ('employe',)
153 fieldsets = AUFMetadataAdminMixin.fieldsets + (
154 ("Lien avec l'employé", {
155 'fields': (('employe', 'lien_parente'), )
156 }),
157
158 ('Identification', {
159 'fields': (('nom', 'prenom'), ('nom_affichage', 'genre'), 'nationalite', 'date_naissance', )
160 }),
161 )
162
163 def save_formset(self, request, form, formset, change):
164 instances = formset.save(commit=False)
165 for instance in instances:
166 if instance.__class__ == rh.AyantDroitCommentaire:
167 instance.owner = request.user
168 instance.save()
169
170 def _ayantdroit(self, obj):
171 return unicode(obj)
172 _ayantdroit.short_description = u'Ayant droit'
173
174 def _employe(self, obj):
175 return unicode(obj.employe)
176 _employe.short_description = u'Employé'
177
178 def has_add_permission(self, request):
179 return False
180
181 class AyantDroitCommentaireAdmin(admin.ModelAdmin):
182 pass
183
184
185 class ClassementAdmin(AUFMetadataAdminMixin, admin.ModelAdmin):
186 fieldsets = AUFMetadataAdminMixin.fieldsets + (
187 (None, {
188 'fields': ('type', 'echelon', 'degre', 'coefficient', )
189 }),
190 )
191
192
193 class CommentaireAdmin(admin.ModelAdmin):
194 pass
195
196
197 #class ContratAdmin(AUFMetadataAdminMixin, admin.ModelAdmin):
198 # form = ContratForm
199 # alphabet_filter = 'dossier__employe__nom'
200 # search_fields = ('dossier__employe__nom', 'dossier__employe__prenom', 'dossier__poste__nom', 'dossier__poste__nom_feminin', )
201 # list_display = ('id', '_employe', '_poste', 'date_debut', 'date_fin', '_implantation', )
202 # fieldsets = AUFMetadataAdminMixin.fieldsets + (
203 # (None, {
204 # 'fields': ('dossier', 'type_contrat', 'date_debut', 'date_fin', )
205 # }),
206 # )
207 #
208 # def lookup_allowed(self, key, value):
209 # if key in ('dossier__employe__nom__istartswith', ):
210 # return True
211 #
212 # def _employe(self, obj):
213 # return unicode(obj.dossier.employe)
214 # _employe.short_description = "Employé"
215 #
216 # def _poste(self, obj):
217 # return obj.dossier.poste.nom
218 # _poste.short_description = "Poste"
219 #
220 # def _implantation(self, obj):
221 # return obj.dossier.poste.implantation
222 # _poste.short_description = "Implantation"
223
224 class DeviseAdmin(AUFMetadataAdminMixin, admin.ModelAdmin):
225 fieldsets = AUFMetadataAdminMixin.fieldsets + (
226 (None, {
227 'fields': ('code', 'nom', ),
228 }),
229 )
230
231
232 class DossierAdmin(AUFMetadataAdminMixin, ProtectRegionMixin, admin.ModelAdmin,):
233 form = DossierForm
234 alphabet_filter = 'employe__nom'
235 search_fields = ('employe__nom', 'employe__prenom', 'poste__nom', 'poste__nom_feminin')
236 list_display = ('_employe', '_poste', 'date_debut', 'date_fin', 'date_modification', '_actif')
237 inlines = (DossierPieceInline, ContratInline,
238 RemunerationInline,
239 #EvenementInline,
240 DossierCommentaireInline,
241 )
242 fieldsets = AUFMetadataAdminMixin.fieldsets + (
243 ('Identification', {
244 'fields': ('employe', 'poste', 'statut', 'organisme_bstg',)
245 }),
246 ('Recrutement', {
247 'fields': (('remplacement', 'statut_residence'), )
248 }),
249 ('Rémunération', {
250 'fields': ('classement', ('regime_travail', 'regime_travail_nb_heure_semaine'),)
251 }),
252 ('Occupation du Poste par cet Employe', {
253 'fields': (('date_debut', 'date_fin'), )
254 }),
255 )
256
257 def lookup_allowed(self, key, value):
258 if key in ('employe__nom__istartswith', 'actif__exact', ):
259 return True
260
261 def _actif(self, dossier):
262 if dossier.employe.actif:
263 html = """<img alt="True" src="%simg/admin/icon-yes.gif">"""
264 else:
265 html = """<img alt="False" src="%simg/admin/icon-no.gif">"""
266 return html % settings.ADMIN_MEDIA_PREFIX
267 _actif.allow_tags = u'Employé actif'
268 _actif.short_description = u'Employé actif'
269 _actif.admin_order_field = 'employe__actif'
270
271 def _poste(self, dossier):
272 return unicode(dossier.poste.nom)
273 _poste.short_description = u'Poste'
274 _poste.admin_order_field = 'poste__nom'
275
276 def _employe(self, dossier):
277 return unicode(dossier.employe)
278 _employe.short_description = u'Employé'
279 _employe.admin_order_field = 'employe__nom'
280
281 def save_formset(self, request, form, formset, change):
282 instances = formset.save(commit=False)
283 for instance in instances:
284 if instance.__class__ == rh.DossierCommentaire:
285 instance.owner = request.user
286 instance.save()
287
288 def render_change_form(self, request, context, *args, **kwargs):
289 obj = kwargs['obj']
290
291 thisyear = datetime.date.today().year
292 thisyearfilter = Q(date_debut__year=thisyear) | Q(date_fin__year=thisyear)
293
294 remunnow = obj.rh_remuneration_remunerations.filter(thisyearfilter)
295
296 remun_sum = 0
297 remun_sum_euro = 0
298 sums = defaultdict(int)
299 sums_euro = defaultdict(int)
300 for r in remunnow:
301 nature = r.type.nature_remuneration
302 sums[nature] += r.montant
303 sums_euro[nature] += r.montant_euro()
304 remun_sum += r.montant
305 remun_sum_euro += r.montant_euro()
306
307 remun = {}
308 sums = dict(sums)
309 for n, s in sums.iteritems():
310 remun[n] = [sums[n], sums_euro[n]]
311
312 extra = {
313 'remun': remun,
314 'remun_sum': remun_sum,
315 'remun_sum_euro': remun_sum_euro,
316 }
317
318 context.update(extra)
319
320 return super(DossierAdmin, self).render_change_form(request, context, *args, **kwargs)
321
322
323 class DossierPieceAdmin(admin.ModelAdmin):
324 pass
325
326
327 class DossierCommentaireAdmin(admin.ModelAdmin):
328 pass
329
330
331 class EmployeAdmin(AUFMetadataAdminMixin, ProtectRegionMixin, admin.ModelAdmin):
332 alphabet_filter = 'nom'
333 search_fields = ('id', 'nom', 'prenom', 'nom_affichage', 'actif', )
334 list_filter = ('actif', )
335 ordering = ('nom', )
336 actions = ('desactiver', )
337 list_display = ('id', 'nom', 'prenom', 'actif', )
338 list_display_links = ('id', 'nom',)
339 inlines = (AyantDroitInline,
340 DossierROInline,
341 EmployePieceInline,
342 EmployeCommentaireInline)
343 fieldsets = AUFMetadataAdminMixin.fieldsets + (
344 ('Identification', {
345 'fields': (('nom', 'prenom'), ('nom_affichage', 'genre'), 'nationalite', 'date_naissance', )
346 }),
347 ('Informations personnelles', {
348 'fields': ('situation_famille', 'date_entree', )
349 }),
350 ('Coordonnées', {
351 'fields': (('tel_domicile', 'tel_cellulaire'), ('adresse', 'ville'), ('code_postal', 'province'), 'pays', )
352 }),
353 )
354
355 def save_formset(self, request, form, formset, change):
356 instances = formset.save(commit=False)
357 for instance in instances:
358 if instance.__class__ == rh.EmployeCommentaire:
359 instance.owner = request.user
360 instance.save()
361
362 class EmployeCommentaireAdmin(admin.ModelAdmin):
363 pass
364
365
366 class EmployePieceAdmin(admin.ModelAdmin):
367 pass
368
369
370 class EvenementAdmin(admin.ModelAdmin):
371 inlines = (EvenementRemunerationInline,)
372
373
374 class EvenementRemunerationAdmin(admin.ModelAdmin):
375 pass
376
377
378 class FamilleEmploiAdmin(AUFMetadataAdminMixin, admin.ModelAdmin):
379 inlines = (TypePosteInline,)
380 fieldsets = AUFMetadataAdminMixin.fieldsets + (
381 (None, {
382 'fields': ('nom', )
383 }),
384 )
385
386
387 class OrganismeBstgAdmin(AUFMetadataAdminMixin, ProtectRegionMixin, admin.ModelAdmin):
388 search_fields = ('nom', )
389 list_display = ('nom', 'type', 'pays', )
390 inlines = (DossierROInline,)
391 fieldsets = AUFMetadataAdminMixin.fieldsets + (
392 (None, {
393 'fields': ('nom', 'type', 'pays', )
394 }),
395 )
396
397
398 class PosteAdmin(AUFMetadataAdminMixin, ProtectRegionMixin, admin.ModelAdmin):
399 alphabet_filter = 'nom'
400 search_fields = ('nom', 'implantation__code', 'implantation__nom', 'implantation__region__code', 'implantation__region__nom', )
401 list_display = ('nom', 'implantation', 'service', 'type_poste', 'date_debut', 'date_fin', )
402 fieldsets = AUFMetadataAdminMixin.fieldsets + (
403 (None, {
404 'fields': (('nom', 'nom_feminin'), 'implantation', 'type_poste',
405 'service', 'responsable')
406 }),
407 ('Contrat', {
408 'fields': (('regime_travail', 'regime_travail_nb_heure_semaine'), )
409 }),
410 ('Recrutement', {
411 'fields': (('local', 'expatrie', 'mise_a_disposition', 'appel'),)
412 }),
413 ('Rémunération', {
414 'fields': (('classement_min', 'classement_max'),
415 ('valeur_point_min', 'valeur_point_max'),
416 ('devise_min', 'devise_max'),
417 ('salaire_min', 'salaire_max'),
418 ('indemn_min', 'indemn_max'),
419 ('autre_min', 'autre_max'))
420 }),
421 ('Comparatifs de rémunération', {
422 'fields': ('devise_comparaison',
423 ('comp_locale_min', 'comp_locale_max'),
424 ('comp_universite_min', 'comp_universite_max'),
425 ('comp_fonctionpub_min', 'comp_fonctionpub_max'),
426 ('comp_ong_min', 'comp_ong_max'),
427 ('comp_autre_min', 'comp_autre_max'))
428 }),
429 ('Justification', {
430 'fields': ('justification',)
431 }),
432 ('Autres Metadata', {
433 'fields': ('date_validation', ('date_debut', 'date_fin'))
434 }),
435 )
436
437 inlines = (PosteFinancementInline,
438 PostePieceInline,
439 DossierROInline,
440 PosteCommentaireInline, )
441
442 def save_formset(self, request, form, formset, change):
443 instances = formset.save(commit=False)
444 for instance in instances:
445 if instance.__class__ == rh.PosteCommentaire:
446 instance.owner = request.user
447 instance.save()
448 formset.save_m2m()
449
450
451 class PosteCommentaireAdmin(admin.ModelAdmin):
452 pass
453
454
455 class PosteFinancementAdmin(admin.ModelAdmin):
456 pass
457
458
459 class PostePieceAdmin(admin.ModelAdmin):
460 pass
461
462
463 class RemunerationAdmin(admin.ModelAdmin):
464 pass
465
466
467 class ResponsableImplantationAdmin(AUFMetadataAdminMixin, admin.ModelAdmin):
468 fieldsets = AUFMetadataAdminMixin.fieldsets + (
469 (None, {
470 'fields': ('employe', 'implantation', ),
471 }),
472 )
473
474
475 class ServiceAdmin(AUFMetadataAdminMixin, admin.ModelAdmin):
476 list_display = ('nom', 'actif', )
477 fieldsets = AUFMetadataAdminMixin.fieldsets + (
478 (None, {
479 'fields': ('nom', ),
480 }),
481 )
482
483 class StatutAdmin(AUFMetadataAdminMixin, admin.ModelAdmin):
484 fieldsets = AUFMetadataAdminMixin.fieldsets + (
485 (None, {
486 'fields': ('code', 'nom', ),
487 }),
488 )
489
490 class TauxChangeAdmin(admin.ModelAdmin):
491 list_display = ('taux', 'devise', 'annee', )
492 list_filter = ('devise', )
493 fieldsets = AUFMetadataAdminMixin.fieldsets + (
494 (None, {
495 'fields': ('taux', 'devise', 'annee', ),
496 }),
497 )
498
499 class TypeContratAdmin(admin.ModelAdmin):
500 inlines = (ContratInline,)
501 fieldsets = AUFMetadataAdminMixin.fieldsets + (
502 (None, {
503 'fields': ('nom', 'nom_long', ),
504 }),
505 )
506
507
508 class TypePosteAdmin(AUFMetadataAdminMixin, admin.ModelAdmin):
509 search_fields = ('nom', 'nom_feminin', )
510 list_display = ('nom', 'famille_emploi', )
511 list_filter = ('famille_emploi', )
512 fieldsets = AUFMetadataAdminMixin.fieldsets + (
513 (None, {
514 'fields': ('nom', 'nom_feminin', 'is_responsable', 'famille_emploi', )
515 }),
516 )
517
518
519 class TypeRemunerationAdmin(AUFMetadataAdminMixin, admin.ModelAdmin):
520 list_display = ('nom', 'type_paiement', 'nature_remuneration', )
521 #inlines = (RemunerationROInline,) utilité?
522 fieldsets = AUFMetadataAdminMixin.fieldsets + (
523 (None, {
524 'fields': ('nom', 'type_paiement', 'nature_remuneration', )
525 }),
526 )
527
528
529 class TypeRevalorisationAdmin(AUFMetadataAdminMixin, admin.ModelAdmin):
530 #inlines = (RemunerationROInline,) utilité?
531 fieldsets = AUFMetadataAdminMixin.fieldsets + (
532 (None, {
533 'fields': ('nom', )
534 }),
535 )
536
537
538 class ValeurPointAdmin(AUFMetadataAdminMixin, admin.ModelAdmin):
539 list_display = ('_devise_code', '_devise_nom', 'annee', 'valeur', )
540 fieldsets = AUFMetadataAdminMixin.fieldsets + (
541 (None, {
542 'fields': ('valeur', 'devise', 'implantation', 'annee', )
543 }),
544 )
545
546 def _devise_code(self, obj):
547 return obj.devise.code
548 _devise_code.short_description = "Code de la devise"
549
550 def _devise_nom(self, obj):
551 return obj.devise.nom
552 _devise_nom.short_description = "Nom de la devise"