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