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