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