nice filter
[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', 'implantation__code', 'implantation__nom', 'implantation__region__code', 'implantation__region__nom', )
494 list_display = ('nom', 'implantation', 'service', 'type_poste', 'date_debut', 'date_fin', )
495 fieldsets = AUFMetadataAdminMixin.fieldsets + (
496 (None, {
497 'fields': (('nom', 'nom_feminin'), 'implantation', 'type_poste',
498 'service', 'responsable')
499 }),
500 ('Contrat', {
501 'fields': (('regime_travail', 'regime_travail_nb_heure_semaine'), )
502 }),
503 ('Recrutement', {
504 'fields': (('local', 'expatrie', 'mise_a_disposition', 'appel'),)
505 }),
506 ('Rémunération', {
507 'fields': (('classement_min', 'classement_max'),
508 ('valeur_point_min', 'valeur_point_max'),
509 ('devise_min', 'devise_max'),
510 ('salaire_min', 'salaire_max'),
511 ('indemn_min', 'indemn_max'),
512 ('autre_min', 'autre_max'))
513 }),
514 ('Comparatifs de rémunération', {
515 'fields': ('devise_comparaison',
516 ('comp_locale_min', 'comp_locale_max'),
517 ('comp_universite_min', 'comp_universite_max'),
518 ('comp_fonctionpub_min', 'comp_fonctionpub_max'),
519 ('comp_ong_min', 'comp_ong_max'),
520 ('comp_autre_min', 'comp_autre_max'))
521 }),
522 ('Justification', {
523 'fields': ('justification',)
524 }),
525 ('Autres Metadata', {
526 'fields': ('date_validation', ('date_debut', 'date_fin'))
527 }),
528 )
529
530 inlines = (PosteFinancementInline,
531 PostePieceInline,
532 DossierROInline,
533 PosteCommentaireInline, )
534
535 def save_formset(self, request, form, formset, change):
536 instances = formset.save(commit=False)
537 for instance in instances:
538 if instance.__class__ == rh.PosteCommentaire:
539 instance.owner = request.user
540 instance.save()
541 formset.save_m2m()
542
543
544 class PosteCommentaireAdmin(admin.ModelAdmin):
545 pass
546
547
548 class PosteFinancementAdmin(admin.ModelAdmin):
549 pass
550
551
552 class PostePieceAdmin(admin.ModelAdmin):
553 pass
554
555
556 class RemunerationAdmin(admin.ModelAdmin):
557 pass
558
559
560 class ResponsableImplantationAdmin(AUFMetadataAdminMixin, admin.ModelAdmin):
561 fieldsets = AUFMetadataAdminMixin.fieldsets + (
562 (None, {
563 'fields': ('employe', 'implantation', ),
564 }),
565 )
566
567
568 class ServiceAdmin(AUFMetadataAdminMixin, admin.ModelAdmin):
569 list_display = ('nom', 'actif', )
570 fieldsets = AUFMetadataAdminMixin.fieldsets + (
571 (None, {
572 'fields': ('nom', ),
573 }),
574 )
575
576 class StatutAdmin(AUFMetadataAdminMixin, admin.ModelAdmin):
577 fieldsets = AUFMetadataAdminMixin.fieldsets + (
578 (None, {
579 'fields': ('code', 'nom', ),
580 }),
581 )
582
583 class TauxChangeAdmin(admin.ModelAdmin):
584 list_display = ('taux', 'devise', 'annee', )
585 list_filter = ('devise', )
586 fieldsets = AUFMetadataAdminMixin.fieldsets + (
587 (None, {
588 'fields': ('taux', 'devise', 'annee', ),
589 }),
590 )
591
592 class TypeContratAdmin(admin.ModelAdmin):
593 inlines = (ContratInline,)
594 fieldsets = AUFMetadataAdminMixin.fieldsets + (
595 (None, {
596 'fields': ('nom', 'nom_long', ),
597 }),
598 )
599
600
601 class TypePosteAdmin(AUFMetadataAdminMixin, admin.ModelAdmin):
602 search_fields = ('nom', 'nom_feminin', )
603 list_display = ('nom', 'famille_emploi', )
604 list_filter = ('famille_emploi', )
605 fieldsets = AUFMetadataAdminMixin.fieldsets + (
606 (None, {
607 'fields': ('nom', 'nom_feminin', 'is_responsable', 'famille_emploi', )
608 }),
609 )
610
611
612 class TypeRemunerationAdmin(AUFMetadataAdminMixin, admin.ModelAdmin):
613 list_display = ('nom', 'type_paiement', 'nature_remuneration', )
614 #inlines = (RemunerationROInline,) utilité?
615 fieldsets = AUFMetadataAdminMixin.fieldsets + (
616 (None, {
617 'fields': ('nom', 'type_paiement', 'nature_remuneration', )
618 }),
619 )
620
621
622 class TypeRevalorisationAdmin(AUFMetadataAdminMixin, admin.ModelAdmin):
623 #inlines = (RemunerationROInline,) utilité?
624 fieldsets = AUFMetadataAdminMixin.fieldsets + (
625 (None, {
626 'fields': ('nom', )
627 }),
628 )
629
630
631 class ValeurPointAdmin(AUFMetadataAdminMixin, admin.ModelAdmin):
632 list_display = ('_devise_code', '_devise_nom', 'annee', 'valeur', )
633 fieldsets = AUFMetadataAdminMixin.fieldsets + (
634 (None, {
635 'fields': ('valeur', 'devise', 'implantation', 'annee', )
636 }),
637 )
638
639 def _devise_code(self, obj):
640 return obj.devise.code
641 _devise_code.short_description = "Code de la devise"
642
643 def _devise_nom(self, obj):
644 return obj.devise.nom
645 _devise_nom.short_description = "Nom de la devise"
646
647
648 def calc_remun(dossier):
649 thisyear = datetime.date.today().year
650 thisyearfilter = Q(date_debut__year=thisyear) | Q(date_fin__year=thisyear)
651
652 remunnow = dossier.rh_remuneration_remunerations.filter(thisyearfilter)
653
654 remun_sum = 0
655 remun_sum_euro = 0
656 sums = defaultdict(int)
657 sums_euro = defaultdict(int)
658 for r in remunnow:
659 nature = r.type.nature_remuneration
660 sums[nature] += r.montant
661 sums_euro[nature] += r.montant_euro()
662 remun_sum += r.montant
663 remun_sum_euro += r.montant_euro()
664
665 remun = {}
666 sums = dict(sums)
667 for n, s in sums.iteritems():
668 remun[n] = [sums[n], sums_euro[n]]
669
670 return remun, remun_sum, remun_sum_euro