[#2372] Réorganisation champs TypeContrat
[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
972f50e4
PP
55 fieldsets = (
56 (None, {
57 'fields': (('nom', 'prenom'), ('nom_affichage', 'genre'), 'nationalite', 'date_naissance', 'lien_parente', )
58 }),
59 )
60
d6985a3a
OL
61
62class AyantDroitCommentaireInline(AUFMetadataInlineAdminMixin, admin.TabularInline):
cf786fb2 63 readonly_fields = ('owner', )
babf71ec 64 model = models.Model # à remplacer dans admin.py
cf786fb2 65 extra = 1
6e7c919b 66
54d04eed 67
d6985a3a 68class ContratInline(AUFMetadataInlineAdminMixin, admin.TabularInline):
84cbb4c5 69 form = ContratForm
babf71ec 70 model = models.Model # à remplacer dans admin.py
1f2979b8 71 extra = 1
babf71ec
NC
72
73
d6985a3a
OL
74class DossierROInline(ReadOnlyInlineMixin, admin.TabularInline):
75 exclude = AUF_METADATA_READONLY_FIELDS
babf71ec 76 model = models.Model # à remplacer dans admin.py
babf71ec 77
d6985a3a
OL
78
79class DossierCommentaireInline(AUFMetadataInlineAdminMixin, admin.TabularInline):
1f2979b8 80 readonly_fields = ('owner', )
babf71ec 81 model = models.Model # à remplacer dans admin.py
1f2979b8 82 extra = 1
babf71ec 83
d6985a3a 84
babf71ec
NC
85class DossierPieceInline(admin.TabularInline):
86 model = models.Model # à remplacer dans admin.py
988af3fb 87 extra = 4
babf71ec
NC
88
89
90class EmployeInline(admin.TabularInline):
91 model = models.Model # à remplacer dans admin.py
92
d6985a3a 93class EmployeCommentaireInline(AUFMetadataInlineAdminMixin, admin.TabularInline):
cf786fb2 94 readonly_fields = ('owner', )
babf71ec 95 model = models.Model # à remplacer dans admin.py
cf786fb2 96 extra = 1
babf71ec
NC
97
98
99class EmployePieceInline(admin.TabularInline):
100 model = models.Model # à remplacer dans admin.py
988af3fb 101 extra = 4
babf71ec
NC
102
103
d6985a3a 104class EvenementInline(AUFMetadataInlineAdminMixin, admin.TabularInline):
babf71ec 105 model = models.Model # à remplacer dans admin.py
1f2979b8 106 extra = 1
babf71ec
NC
107
108
d6985a3a 109class EvenementRemunerationInline(AUFMetadataInlineAdminMixin, admin.TabularInline):
babf71ec 110 model = models.Model # à remplacer dans admin.py
1f2979b8 111 extra = 1
babf71ec
NC
112
113
d6985a3a 114class PosteCommentaireInline(AUFMetadataInlineAdminMixin, admin.TabularInline):
70561dc2 115 readonly_fields = ('owner', )
babf71ec 116 model = models.Model # à remplacer dans admin.py
70561dc2 117 extra = 1
babf71ec
NC
118
119
120class PosteFinancementInline(admin.TabularInline):
121 model = models.Model # à remplacer dans admin.py
122
123
124class PostePieceInline(admin.TabularInline):
125 model = models.Model # à remplacer dans admin.py
126
127
d6985a3a 128class RemunerationInline(AUFMetadataInlineAdminMixin, admin.TabularInline):
babf71ec 129 model = models.Model # à remplacer dans admin.py
1f2979b8 130 extra = 1
babf71ec
NC
131
132
d6985a3a 133class RemunerationROInline(ReadOnlyInlineMixin, RemunerationInline):
6e7c919b
NC
134 pass
135
54d04eed 136
26dacccc 137class TypePosteInline(AUFMetadataInlineAdminMixin, admin.TabularInline):
babf71ec
NC
138 model = models.Model # à remplacer dans admin.py
139
140
141# Admins
142
aff1a4c6 143class AyantDroitAdmin(AUFMetadataAdminMixin, ProtectRegionMixin, admin.ModelAdmin):
cf786fb2
OL
144 """
145 L'ajout d'un nouvel ayantdroit se fait dans l'admin de l'employé.
146 """
387ab827 147 alphabet_filter = 'nom'
cf786fb2 148 search_fields = ('nom', 'prenom', 'employe__nom', 'employe__prenom', )
1a89b1f3 149 list_display = ('_employe', 'lien_parente', '_ayantdroit', )
babf71ec 150 inlines = (AyantDroitCommentaireInline,)
d6985a3a
OL
151 readonly_fields = AUFMetadataAdminMixin.readonly_fields + ('employe',)
152 fieldsets = AUFMetadataAdminMixin.fieldsets + (
cf786fb2 153 ("Lien avec l'employé", {
76e26dfc 154 'fields': (('employe', 'lien_parente'), )
cf786fb2
OL
155 }),
156
157 ('Identification', {
76e26dfc 158 'fields': (('nom', 'prenom'), ('nom_affichage', 'genre'), 'nationalite', 'date_naissance', )
cf786fb2
OL
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()
cf786fb2
OL
168
169 def _ayantdroit(self, obj):
170 return unicode(obj)
171 _ayantdroit.short_description = u'Ayant droit'
babf71ec 172
cf786fb2
OL
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
babf71ec
NC
179
180class AyantDroitCommentaireAdmin(admin.ModelAdmin):
6e7c919b
NC
181 pass
182
54d04eed 183
324bf312
OL
184class ClassementAdmin(AUFMetadataAdminMixin, admin.ModelAdmin):
185 fieldsets = AUFMetadataAdminMixin.fieldsets + (
186 (None, {
187 'fields': ('type', 'echelon', 'degre', 'coefficient', )
188 }),
189 )
190
6e7c919b 191
6e7c919b
NC
192class CommentaireAdmin(admin.ModelAdmin):
193 pass
194
195
84cbb4c5
OL
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"
54d04eed 222
84fc088b
OL
223class DeviseAdmin(AUFMetadataAdminMixin, admin.ModelAdmin):
224 fieldsets = AUFMetadataAdminMixin.fieldsets + (
225 (None, {
226 'fields': ('code', 'nom', ),
227 }),
228 )
6e7c919b 229
54d04eed 230
aff1a4c6 231class DossierAdmin(AUFMetadataAdminMixin, ProtectRegionMixin, admin.ModelAdmin,):
84cbb4c5 232 form = DossierForm
387ab827 233 alphabet_filter = 'employe__nom'
d8d7985b 234 search_fields = ('employe__nom', 'employe__prenom', 'poste__nom', 'poste__nom_feminin')
55ca522c 235 list_display = ('_employe', '_poste', 'date_debut', 'date_fin', 'date_modification', '_actif')
1f2979b8 236 inlines = (DossierPieceInline, ContratInline,
cf786fb2
OL
237 RemunerationInline,
238 #EvenementInline,
239 DossierCommentaireInline,
1f2979b8 240 )
d6985a3a 241 fieldsets = AUFMetadataAdminMixin.fieldsets + (
1f2979b8
OL
242 ('Identification', {
243 'fields': ('employe', 'poste', 'statut', 'organisme_bstg',)
244 }),
245 ('Recrutement', {
cb962802 246 'fields': (('remplacement', 'statut_residence'), )
1f2979b8
OL
247 }),
248 ('Rémunération', {
cb962802 249 'fields': ('classement', ('regime_travail', 'regime_travail_nb_heure_semaine'),)
1f2979b8
OL
250 }),
251 ('Occupation du Poste par cet Employe', {
cb962802 252 'fields': (('date_debut', 'date_fin'), )
1f2979b8
OL
253 }),
254 )
54d04eed 255
387ab827 256 def lookup_allowed(self, key, value):
49449367 257 if key in ('employe__nom__istartswith', 'actif__exact', ):
387ab827
OL
258 return True
259
49449367
OL
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'
387ab827 269
54d04eed
NC
270 def _poste(self, dossier):
271 return unicode(dossier.poste.nom)
272 _poste.short_description = u'Poste'
d9836879 273 _poste.admin_order_field = 'poste__nom'
54d04eed
NC
274
275 def _employe(self, dossier):
276 return unicode(dossier.employe)
277 _employe.short_description = u'Employé'
d9836879
OL
278 _employe.admin_order_field = 'employe__nom'
279
1f2979b8
OL
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()
6e7c919b 286
8d3e2fff
PP
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
6e7c919b
NC
322class DossierPieceAdmin(admin.ModelAdmin):
323 pass
324
54d04eed 325
6e7c919b
NC
326class DossierCommentaireAdmin(admin.ModelAdmin):
327 pass
328
54d04eed 329
aff1a4c6 330class EmployeAdmin(AUFMetadataAdminMixin, ProtectRegionMixin, admin.ModelAdmin):
7ffc5aa4 331 alphabet_filter = 'nom'
49449367
OL
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', )
babf71ec
NC
337 inlines = (AyantDroitInline,
338 DossierROInline,
339 EmployePieceInline,
340 EmployeCommentaireInline)
d6985a3a 341 fieldsets = AUFMetadataAdminMixin.fieldsets + (
cf786fb2 342 ('Identification', {
e35610f5 343 'fields': (('nom', 'prenom'), ('nom_affichage', 'genre'), 'nationalite', 'date_naissance', )
cf786fb2
OL
344 }),
345 ('Informations personnelles', {
346 'fields': ('situation_famille', 'date_entree', )
347 }),
348 ('Coordonnées', {
e35610f5 349 'fields': (('tel_domicile', 'tel_cellulaire'), ('adresse', 'ville'), ('code_postal', 'province'), 'pays', )
cf786fb2
OL
350 }),
351 )
babf71ec 352
cf786fb2
OL
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()
babf71ec
NC
359
360class EmployeCommentaireAdmin(admin.ModelAdmin):
6e7c919b
NC
361 pass
362
54d04eed 363
babf71ec 364class EmployePieceAdmin(admin.ModelAdmin):
6e7c919b
NC
365 pass
366
54d04eed 367
6e7c919b 368class EvenementAdmin(admin.ModelAdmin):
babf71ec 369 inlines = (EvenementRemunerationInline,)
6e7c919b 370
54d04eed 371
6e7c919b
NC
372class EvenementRemunerationAdmin(admin.ModelAdmin):
373 pass
374
54d04eed 375
26dacccc 376class FamilleEmploiAdmin(AUFMetadataAdminMixin, admin.ModelAdmin):
babf71ec 377 inlines = (TypePosteInline,)
26dacccc
OL
378 fieldsets = AUFMetadataAdminMixin.fieldsets + (
379 (None, {
380 'fields': ('nom', )
381 }),
382 )
6e7c919b 383
54d04eed 384
aff1a4c6 385class OrganismeBstgAdmin(AUFMetadataAdminMixin, ProtectRegionMixin, admin.ModelAdmin):
d8d7985b 386 search_fields = ('nom', )
1c8856ef 387 list_display = ('nom', 'type', 'pays', )
babf71ec 388 inlines = (DossierROInline,)
43f5653a
OL
389 fieldsets = AUFMetadataAdminMixin.fieldsets + (
390 (None, {
391 'fields': ('nom', 'type', 'pays', )
392 }),
393 )
6e7c919b 394
54d04eed 395
aff1a4c6 396class PosteAdmin(AUFMetadataAdminMixin, ProtectRegionMixin, admin.ModelAdmin):
387ab827 397 alphabet_filter = 'nom'
70561dc2
OL
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', )
d2cf315a 400 fieldsets = AUFMetadataAdminMixin.fieldsets + (
6e7c919b 401 (None, {
4497b3cb 402 'fields': (('nom', 'nom_feminin'), 'implantation', 'type_poste',
6e7c919b
NC
403 'service', 'responsable')
404 }),
405 ('Contrat', {
4497b3cb 406 'fields': (('regime_travail', 'regime_travail_nb_heure_semaine'), )
6e7c919b
NC
407 }),
408 ('Recrutement', {
4497b3cb 409 'fields': (('local', 'expatrie', 'mise_a_disposition', 'appel'),)
6e7c919b
NC
410 }),
411 ('Rémunération', {
4497b3cb
PP
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'))
6e7c919b
NC
418 }),
419 ('Comparatifs de rémunération', {
4497b3cb
PP
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'))
6e7c919b
NC
426 }),
427 ('Justification', {
428 'fields': ('justification',)
429 }),
430 ('Autres Metadata', {
4497b3cb 431 'fields': ('date_validation', ('date_debut', 'date_fin'))
6e7c919b
NC
432 }),
433 )
434
babf71ec
NC
435 inlines = (PosteFinancementInline,
436 PostePieceInline,
70561dc2
OL
437 DossierROInline,
438 PosteCommentaireInline, )
6e7c919b 439
70561dc2
OL
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()
6e7c919b 447
aff1a4c6 448
6e7c919b
NC
449class PosteCommentaireAdmin(admin.ModelAdmin):
450 pass
451
6e7c919b 452
babf71ec 453class PosteFinancementAdmin(admin.ModelAdmin):
6e7c919b
NC
454 pass
455
6e7c919b 456
babf71ec 457class PostePieceAdmin(admin.ModelAdmin):
6e7c919b
NC
458 pass
459
6e7c919b 460
babf71ec 461class RemunerationAdmin(admin.ModelAdmin):
6e7c919b
NC
462 pass
463
6e7c919b 464
1eede0db
OL
465class ResponsableImplantationAdmin(AUFMetadataAdminMixin, admin.ModelAdmin):
466 fieldsets = AUFMetadataAdminMixin.fieldsets + (
467 (None, {
468 'fields': ('employe', 'implantation', ),
469 }),
470 )
471
6e7c919b 472
43f5653a
OL
473class ServiceAdmin(AUFMetadataAdminMixin, admin.ModelAdmin):
474 list_display = ('nom', 'actif', )
475 fieldsets = AUFMetadataAdminMixin.fieldsets + (
476 (None, {
477 'fields': ('nom', ),
478 }),
479 )
6e7c919b 480
43f5653a
OL
481class StatutAdmin(AUFMetadataAdminMixin, admin.ModelAdmin):
482 fieldsets = AUFMetadataAdminMixin.fieldsets + (
483 (None, {
484 'fields': ('code', 'nom', ),
485 }),
486 )
6e7c919b 487
babf71ec 488class TauxChangeAdmin(admin.ModelAdmin):
24475cf1
OL
489 list_display = ('taux', 'devise', 'annee', )
490 list_filter = ('devise', )
dcbadfad
OL
491 fieldsets = AUFMetadataAdminMixin.fieldsets + (
492 (None, {
493 'fields': ('taux', 'devise', 'annee', ),
494 }),
495 )
6e7c919b 496
6e7c919b 497class TypeContratAdmin(admin.ModelAdmin):
babf71ec 498 inlines = (ContratInline,)
7376afeb
PP
499 fieldsets = AUFMetadataAdminMixin.fieldsets + (
500 (None, {
501 'fields': ('nom', 'nom_long', ),
502 }),
503 )
6e7c919b 504
6e7c919b 505
240893cb 506class TypePosteAdmin(AUFMetadataAdminMixin, admin.ModelAdmin):
d8d7985b 507 search_fields = ('nom', 'nom_feminin', )
52e05b08
OL
508 list_display = ('nom', 'famille_emploi', )
509 list_filter = ('famille_emploi', )
240893cb
OL
510 fieldsets = AUFMetadataAdminMixin.fieldsets + (
511 (None, {
512 'fields': ('nom', 'nom_feminin', 'is_responsable', 'famille_emploi', )
513 }),
514 )
6e7c919b 515
6e7c919b 516
3f486b41 517class TypeRemunerationAdmin(AUFMetadataAdminMixin, admin.ModelAdmin):
f7efd29a 518 list_display = ('nom', 'type_paiement', 'nature_remuneration', )
3f486b41
OL
519 #inlines = (RemunerationROInline,) utilité?
520 fieldsets = AUFMetadataAdminMixin.fieldsets + (
521 (None, {
522 'fields': ('nom', 'type_paiement', 'nature_remuneration', )
523 }),
524 )
6e7c919b 525
6e7c919b 526
3f486b41
OL
527class TypeRevalorisationAdmin(AUFMetadataAdminMixin, admin.ModelAdmin):
528 #inlines = (RemunerationROInline,) utilité?
529 fieldsets = AUFMetadataAdminMixin.fieldsets + (
530 (None, {
531 'fields': ('nom', )
532 }),
533 )
6e7c919b 534
6e7c919b 535
84fc088b 536class ValeurPointAdmin(AUFMetadataAdminMixin, admin.ModelAdmin):
701f3bea 537 list_display = ('_devise_code', '_devise_nom', 'annee', 'valeur', )
84fc088b
OL
538 fieldsets = AUFMetadataAdminMixin.fieldsets + (
539 (None, {
540 'fields': ('valeur', 'devise', 'implantation', 'annee', )
541 }),
542 )
701f3bea
OL
543
544 def _devise_code(self, obj):
545 return obj.devise.code
546 _devise_code.short_description = "Code de la devise"
547
548 def _devise_nom(self, obj):
549 return obj.devise.nom
550 _devise_nom.short_description = "Nom de la devise"