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