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