deplug calc in edit
[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 list_filter = ('poste__implantation__region', 'poste__implantation', 'actif', )
263 inlines = (DossierPieceInline, ContratInline,
264 RemunerationInline,
265 #EvenementInline,
266 DossierCommentaireInline,
267 )
268 fieldsets = AUFMetadataAdminMixin.fieldsets + (
269 (None, {
270 'fields': ('employe', 'poste', 'statut', 'organisme_bstg',)
271 }),
272 ('Recrutement', {
273 'fields': ('statut_residence', 'remplacement', 'remplacement_de', )
274 }),
275 ('Rémunération', {
276 'fields': ('classement', ('regime_travail', 'regime_travail_nb_heure_semaine'),)
277 }),
278 ('Occupation du Poste par cet Employe', {
279 'fields': (('date_debut', 'date_fin'), )
280 }),
281 )
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
321 class DossierPieceAdmin(admin.ModelAdmin):
322 pass
323
324
325 class DossierCommentaireAdmin(admin.ModelAdmin):
326 pass
327
328
329 class EmployeAdminForm(forms.ModelForm):
330 class Meta:
331 model = rh.Employe
332
333 def __init__(self, *args, **kwargs):
334 super(EmployeAdminForm, self).__init__(*args, **kwargs)
335 self.fields['date_naissance'].widget = forms.widgets.DateInput()
336
337
338 class EmployeAdmin(AUFMetadataAdminMixin, ProtectRegionMixin, admin.ModelAdmin):
339 alphabet_filter = 'nom'
340 DEFAULT_ALPHABET = u'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
341 search_fields = ('id', 'nom', 'prenom', 'nom_affichage', )
342 ordering = ('nom', )
343 actions = ('desactiver', )
344 form = EmployeAdminForm
345 list_display = ('_nom', '_dossiers', 'date_modification', 'user_modification',)
346 list_filter = ('dossiers__poste__implantation__region', 'dossiers__poste__implantation', 'actif', )
347 inlines = (AyantDroitInline,
348 DossierROInline,
349 EmployePieceInline,
350 EmployeCommentaireInline)
351 fieldsets = AUFMetadataAdminMixin.fieldsets + (
352 ('Identification', {
353 'fields': (('nom', 'prenom'), ('nom_affichage', 'genre'), 'nationalite', 'date_naissance', )
354 }),
355 ('Informations personnelles', {
356 'fields': ('situation_famille', 'date_entree', )
357 }),
358 ('Coordonnées', {
359 'fields': (('tel_domicile', 'tel_cellulaire'), ('adresse', 'ville'), ('code_postal', 'province'), 'pays', )
360 }),
361 )
362
363 def _nom(self, obj):
364 view_link = reverse('employe_apercu', args=(obj.id,))
365 edit_link = reverse('admin:rh_employe_change', args=(obj.id,))
366 return u"""<a onclick="return showAddAnotherPopup(this);" href='%s'>[%s] %s %s</a>
367 <a href="%s" title="Modifier l'employé"><img src="%simg/user_edit.png" /></a>""" % \
368 (view_link, obj.id, obj.nom.upper(), obj.prenom.title(), edit_link, settings.MEDIA_URL,)
369 _nom.allow_tags = True
370 _nom.short_description = u"Employé ([code] NOM Prénom)"
371 _nom.admin_order_field = "nom"
372
373 def _dossiers(self, obj):
374 l = []
375 for d in obj.dossiers.all().order_by('-date_debut'):
376 style = ""
377 if d.date_fin is not None:
378 style = "color: grey";
379 link = u"""<li><a style="%s;" onclick="return showAddAnotherPopup(this);" href='%s'>%s : %s</a> <a href="%s" title="Modifier le dossier"><img src="%simg/page_edit.png" /></a> </li>""" % \
380 (style,
381 reverse('dossier_apercu', args=(d.id,)),
382 d.date_debut.year,
383 d.poste,
384 reverse('admin:rh_dossier_change', args=(d.id,)),
385 settings.MEDIA_URL,
386 )
387 l.append(link)
388 return "<ul>%s</ul>" % "\n".join(l)
389 _dossiers.allow_tags = True
390
391 def queryset(self, request):
392 qs = super(EmployeAdmin, self).queryset(request)
393 return qs.filter(actif=True).select_related(depth=1).order_by('nom')
394
395 def save_formset(self, request, form, formset, change):
396 instances = formset.save(commit=False)
397 for instance in instances:
398 if instance.__class__ == rh.EmployeCommentaire:
399 instance.owner = request.user
400 instance.save()
401
402
403
404 class EmployeCommentaireAdmin(admin.ModelAdmin):
405 pass
406
407
408 class EmployePieceAdmin(admin.ModelAdmin):
409 pass
410
411
412 class EvenementAdmin(admin.ModelAdmin):
413 inlines = (EvenementRemunerationInline,)
414
415
416 class EvenementRemunerationAdmin(admin.ModelAdmin):
417 pass
418
419
420 class FamilleEmploiAdmin(AUFMetadataAdminMixin, admin.ModelAdmin):
421 inlines = (TypePosteInline,)
422 fieldsets = AUFMetadataAdminMixin.fieldsets + (
423 (None, {
424 'fields': ('nom', )
425 }),
426 )
427
428
429 class OrganismeBstgAdmin(AUFMetadataAdminMixin, ProtectRegionMixin, admin.ModelAdmin):
430 search_fields = ('nom', )
431 list_display = ('nom', 'type', 'pays', )
432 inlines = (DossierROInline,)
433 fieldsets = AUFMetadataAdminMixin.fieldsets + (
434 (None, {
435 'fields': ('nom', 'type', 'pays', )
436 }),
437 )
438
439
440 class PosteAdmin(AUFMetadataAdminMixin, ProtectRegionMixin, admin.ModelAdmin):
441 alphabet_filter = 'nom'
442 search_fields = ('nom',
443 'implantation__code',
444 'implantation__nom',
445 'implantation__region__code',
446 'implantation__region__nom',
447 )
448 list_display = ('nom',
449 '_occupe_par',
450 'implantation',
451 'service',
452 'type_poste',
453 'date_debut',
454 'date_fin',
455 'date_modification',
456 'user_modification',
457 )
458 list_filter = ('service',
459 'implantation__region',
460 'implantation__adresse_physique_pays',
461 'implantation',
462 )
463 fieldsets = AUFMetadataAdminMixin.fieldsets + (
464 (None, {
465 'fields': (('nom', 'nom_feminin'), 'implantation', 'type_poste',
466 'service', 'responsable')
467 }),
468 ('Contrat', {
469 'fields': (('regime_travail', 'regime_travail_nb_heure_semaine'), )
470 }),
471 ('Recrutement', {
472 'fields': (('local', 'expatrie', 'mise_a_disposition', 'appel'),)
473 }),
474 ('Rémunération', {
475 'fields': (('classement_min', 'classement_max'),
476 ('valeur_point_min', 'valeur_point_max'),
477 ('devise_min', 'devise_max'),
478 ('salaire_min', 'salaire_max'),
479 ('indemn_min', 'indemn_max'),
480 ('autre_min', 'autre_max'))
481 }),
482 ('Comparatifs de rémunération', {
483 'fields': ('devise_comparaison',
484 ('comp_locale_min', 'comp_locale_max'),
485 ('comp_universite_min', 'comp_universite_max'),
486 ('comp_fonctionpub_min', 'comp_fonctionpub_max'),
487 ('comp_ong_min', 'comp_ong_max'),
488 ('comp_autre_min', 'comp_autre_max'))
489 }),
490 ('Justification', {
491 'fields': ('justification',)
492 }),
493 ('Autres Metadata', {
494 'fields': ('date_validation', ('date_debut', 'date_fin'))
495 }),
496 )
497
498 inlines = (PosteFinancementInline,
499 PostePieceInline,
500 DossierROInline,
501 PosteCommentaireInline, )
502
503 def _occupe_par(self, obj):
504 """Formatte la méthode Poste.occupe_par() pour l'admin"""
505 output = "VACANT"
506 employes = obj.occupe_par()
507 if employes:
508 l = []
509 for e in employes:
510 link = "<a href='%s'>%s</a>" % \
511 (reverse('admin:rh_employe_change', args=(e.id,)),
512 e)
513 l.append(link)
514 output = "\n<br />".join(l)
515 return output
516 _occupe_par.allow_tags = True
517 _occupe_par.short_description = "Occupé par"
518
519 def save_formset(self, request, form, formset, change):
520 instances = formset.save(commit=False)
521 for instance in instances:
522 if instance.__class__ == rh.PosteCommentaire:
523 instance.owner = request.user
524 instance.save()
525 formset.save_m2m()
526
527
528 class PosteCommentaireAdmin(admin.ModelAdmin):
529 pass
530
531
532 class PosteFinancementAdmin(admin.ModelAdmin):
533 pass
534
535
536 class PostePieceAdmin(admin.ModelAdmin):
537 pass
538
539
540 class RemunerationAdmin(admin.ModelAdmin):
541 pass
542
543
544 class ResponsableImplantationAdmin(AUFMetadataAdminMixin, admin.ModelAdmin):
545 fieldsets = AUFMetadataAdminMixin.fieldsets + (
546 (None, {
547 'fields': ('employe', 'implantation', ),
548 }),
549 )
550
551
552 class ServiceAdmin(AUFMetadataAdminMixin, admin.ModelAdmin):
553 list_display = ('nom', 'actif', )
554 fieldsets = AUFMetadataAdminMixin.fieldsets + (
555 (None, {
556 'fields': ('nom', ),
557 }),
558 )
559
560 class StatutAdmin(AUFMetadataAdminMixin, admin.ModelAdmin):
561 fieldsets = AUFMetadataAdminMixin.fieldsets + (
562 (None, {
563 'fields': ('code', 'nom', ),
564 }),
565 )
566
567 class TauxChangeAdmin(admin.ModelAdmin):
568 list_display = ('taux', 'devise', 'annee', )
569 list_filter = ('devise', )
570 fieldsets = AUFMetadataAdminMixin.fieldsets + (
571 (None, {
572 'fields': ('taux', 'devise', 'annee', ),
573 }),
574 )
575
576 class TypeContratAdmin(admin.ModelAdmin):
577 inlines = (ContratInline,)
578 fieldsets = AUFMetadataAdminMixin.fieldsets + (
579 (None, {
580 'fields': ('nom', 'nom_long', ),
581 }),
582 )
583
584
585 class TypePosteAdmin(AUFMetadataAdminMixin, admin.ModelAdmin):
586 search_fields = ('nom', 'nom_feminin', )
587 list_display = ('nom', 'famille_emploi', )
588 list_filter = ('famille_emploi', )
589 fieldsets = AUFMetadataAdminMixin.fieldsets + (
590 (None, {
591 'fields': ('nom', 'nom_feminin', 'is_responsable', 'famille_emploi', )
592 }),
593 )
594
595
596 class TypeRemunerationAdmin(AUFMetadataAdminMixin, admin.ModelAdmin):
597 list_display = ('nom', 'type_paiement', 'nature_remuneration', )
598 #inlines = (RemunerationROInline,) utilité?
599 fieldsets = AUFMetadataAdminMixin.fieldsets + (
600 (None, {
601 'fields': ('nom', 'type_paiement', 'nature_remuneration', )
602 }),
603 )
604
605
606 class TypeRevalorisationAdmin(AUFMetadataAdminMixin, admin.ModelAdmin):
607 #inlines = (RemunerationROInline,) utilité?
608 fieldsets = AUFMetadataAdminMixin.fieldsets + (
609 (None, {
610 'fields': ('nom', )
611 }),
612 )
613
614
615 class ValeurPointAdmin(AUFMetadataAdminMixin, admin.ModelAdmin):
616 list_display = ('_devise_code', '_devise_nom', 'annee', 'valeur', )
617 fieldsets = AUFMetadataAdminMixin.fieldsets + (
618 (None, {
619 'fields': ('valeur', 'devise', 'implantation', 'annee', )
620 }),
621 )
622
623 def _devise_code(self, obj):
624 return obj.devise.code
625 _devise_code.short_description = "Code de la devise"
626
627 def _devise_nom(self, obj):
628 return obj.devise.nom
629 _devise_nom.short_description = "Nom de la devise"
630
631
632 def calc_remun(dossier):
633 thisyear = datetime.date.today().year
634 thisyearfilter = Q(date_debut__year=thisyear) | Q(date_fin__year=thisyear)
635
636 remunnow = dossier.rh_remuneration_remunerations.filter(thisyearfilter)
637
638 remun_sum = 0
639 remun_sum_euro = 0
640 sums = defaultdict(int)
641 sums_euro = defaultdict(int)
642 for r in remunnow:
643 nature = r.type.nature_remuneration
644 sums[nature] += r.montant
645 sums_euro[nature] += r.montant_euro()
646 remun_sum += r.montant
647 remun_sum_euro += r.montant_euro()
648
649 remun = {}
650 sums = dict(sums)
651 for n, s in sums.iteritems():
652 remun[n] = [sums[n], sums_euro[n]]
653
654 return remun, remun_sum, remun_sum_euro