[#2372] Réorganisation champs Poste
[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 395 (None, {
4497b3cb 396 'fields': (('nom', 'nom_feminin'), 'implantation', 'type_poste',
6e7c919b
NC
397 'service', 'responsable')
398 }),
399 ('Contrat', {
4497b3cb 400 'fields': (('regime_travail', 'regime_travail_nb_heure_semaine'), )
6e7c919b
NC
401 }),
402 ('Recrutement', {
4497b3cb 403 'fields': (('local', 'expatrie', 'mise_a_disposition', 'appel'),)
6e7c919b
NC
404 }),
405 ('Rémunération', {
4497b3cb
PP
406 'fields': (('classement_min', 'classement_max'),
407 ('valeur_point_min', 'valeur_point_max'),
408 ('devise_min', 'devise_max'),
409 ('salaire_min', 'salaire_max'),
410 ('indemn_min', 'indemn_max'),
411 ('autre_min', 'autre_max'))
6e7c919b
NC
412 }),
413 ('Comparatifs de rémunération', {
4497b3cb
PP
414 'fields': ('devise_comparaison',
415 ('comp_locale_min', 'comp_locale_max'),
416 ('comp_universite_min', 'comp_universite_max'),
417 ('comp_fonctionpub_min', 'comp_fonctionpub_max'),
418 ('comp_ong_min', 'comp_ong_max'),
419 ('comp_autre_min', 'comp_autre_max'))
6e7c919b
NC
420 }),
421 ('Justification', {
422 'fields': ('justification',)
423 }),
424 ('Autres Metadata', {
4497b3cb 425 'fields': ('date_validation', ('date_debut', 'date_fin'))
6e7c919b
NC
426 }),
427 )
428
babf71ec
NC
429 inlines = (PosteFinancementInline,
430 PostePieceInline,
70561dc2
OL
431 DossierROInline,
432 PosteCommentaireInline, )
6e7c919b 433
70561dc2
OL
434 def save_formset(self, request, form, formset, change):
435 instances = formset.save(commit=False)
436 for instance in instances:
437 if instance.__class__ == rh.PosteCommentaire:
438 instance.owner = request.user
439 instance.save()
440 formset.save_m2m()
6e7c919b 441
aff1a4c6 442
6e7c919b
NC
443class PosteCommentaireAdmin(admin.ModelAdmin):
444 pass
445
6e7c919b 446
babf71ec 447class PosteFinancementAdmin(admin.ModelAdmin):
6e7c919b
NC
448 pass
449
6e7c919b 450
babf71ec 451class PostePieceAdmin(admin.ModelAdmin):
6e7c919b
NC
452 pass
453
6e7c919b 454
babf71ec 455class RemunerationAdmin(admin.ModelAdmin):
6e7c919b
NC
456 pass
457
6e7c919b 458
1eede0db
OL
459class ResponsableImplantationAdmin(AUFMetadataAdminMixin, admin.ModelAdmin):
460 fieldsets = AUFMetadataAdminMixin.fieldsets + (
461 (None, {
462 'fields': ('employe', 'implantation', ),
463 }),
464 )
465
6e7c919b 466
43f5653a
OL
467class ServiceAdmin(AUFMetadataAdminMixin, admin.ModelAdmin):
468 list_display = ('nom', 'actif', )
469 fieldsets = AUFMetadataAdminMixin.fieldsets + (
470 (None, {
471 'fields': ('nom', ),
472 }),
473 )
6e7c919b 474
43f5653a
OL
475class StatutAdmin(AUFMetadataAdminMixin, admin.ModelAdmin):
476 fieldsets = AUFMetadataAdminMixin.fieldsets + (
477 (None, {
478 'fields': ('code', 'nom', ),
479 }),
480 )
6e7c919b 481
babf71ec 482class TauxChangeAdmin(admin.ModelAdmin):
24475cf1
OL
483 list_display = ('taux', 'devise', 'annee', )
484 list_filter = ('devise', )
dcbadfad
OL
485 fieldsets = AUFMetadataAdminMixin.fieldsets + (
486 (None, {
487 'fields': ('taux', 'devise', 'annee', ),
488 }),
489 )
6e7c919b 490
6e7c919b 491class TypeContratAdmin(admin.ModelAdmin):
babf71ec 492 inlines = (ContratInline,)
6e7c919b 493
6e7c919b 494
240893cb 495class TypePosteAdmin(AUFMetadataAdminMixin, admin.ModelAdmin):
d8d7985b 496 search_fields = ('nom', 'nom_feminin', )
52e05b08
OL
497 list_display = ('nom', 'famille_emploi', )
498 list_filter = ('famille_emploi', )
240893cb
OL
499 fieldsets = AUFMetadataAdminMixin.fieldsets + (
500 (None, {
501 'fields': ('nom', 'nom_feminin', 'is_responsable', 'famille_emploi', )
502 }),
503 )
6e7c919b 504
6e7c919b 505
3f486b41 506class TypeRemunerationAdmin(AUFMetadataAdminMixin, admin.ModelAdmin):
f7efd29a 507 list_display = ('nom', 'type_paiement', 'nature_remuneration', )
3f486b41
OL
508 #inlines = (RemunerationROInline,) utilité?
509 fieldsets = AUFMetadataAdminMixin.fieldsets + (
510 (None, {
511 'fields': ('nom', 'type_paiement', 'nature_remuneration', )
512 }),
513 )
6e7c919b 514
6e7c919b 515
3f486b41
OL
516class TypeRevalorisationAdmin(AUFMetadataAdminMixin, admin.ModelAdmin):
517 #inlines = (RemunerationROInline,) utilité?
518 fieldsets = AUFMetadataAdminMixin.fieldsets + (
519 (None, {
520 'fields': ('nom', )
521 }),
522 )
6e7c919b 523
6e7c919b 524
84fc088b 525class ValeurPointAdmin(AUFMetadataAdminMixin, admin.ModelAdmin):
701f3bea 526 list_display = ('_devise_code', '_devise_nom', 'annee', 'valeur', )
84fc088b
OL
527 fieldsets = AUFMetadataAdminMixin.fieldsets + (
528 (None, {
529 'fields': ('valeur', 'devise', 'implantation', 'annee', )
530 }),
531 )
701f3bea
OL
532
533 def _devise_code(self, obj):
534 return obj.devise.code
535 _devise_code.short_description = "Code de la devise"
536
537 def _devise_nom(self, obj):
538 return obj.devise.nom
539 _devise_nom.short_description = "Nom de la devise"