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