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