2b70753ba8b392c3a9d9b4c2fcc0cc9183feacb3
[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
56 class AyantDroitCommentaireInline(AUFMetadataInlineAdminMixin, admin.TabularInline):
57 readonly_fields = ('owner', )
58 model = models.Model # à remplacer dans admin.py
59 extra = 1
60
61
62 class ContratInline(AUFMetadataInlineAdminMixin, admin.TabularInline):
63 form = ContratForm
64 model = models.Model # à remplacer dans admin.py
65 extra = 1
66
67
68 class DossierROInline(ReadOnlyInlineMixin, admin.TabularInline):
69 exclude = AUF_METADATA_READONLY_FIELDS
70 model = models.Model # à remplacer dans admin.py
71
72
73 class DossierCommentaireInline(AUFMetadataInlineAdminMixin, admin.TabularInline):
74 readonly_fields = ('owner', )
75 model = models.Model # à remplacer dans admin.py
76 extra = 1
77
78
79 class DossierPieceInline(admin.TabularInline):
80 model = models.Model # à remplacer dans admin.py
81 extra = 4
82
83
84 class EmployeInline(admin.TabularInline):
85 model = models.Model # à remplacer dans admin.py
86
87 class EmployeCommentaireInline(AUFMetadataInlineAdminMixin, admin.TabularInline):
88 readonly_fields = ('owner', )
89 model = models.Model # à remplacer dans admin.py
90 extra = 1
91
92
93 class EmployePieceInline(admin.TabularInline):
94 model = models.Model # à remplacer dans admin.py
95 extra = 4
96
97
98 class EvenementInline(AUFMetadataInlineAdminMixin, admin.TabularInline):
99 model = models.Model # à remplacer dans admin.py
100 extra = 1
101
102
103 class EvenementRemunerationInline(AUFMetadataInlineAdminMixin, admin.TabularInline):
104 model = models.Model # à remplacer dans admin.py
105 extra = 1
106
107
108 class PosteCommentaireInline(AUFMetadataInlineAdminMixin, admin.TabularInline):
109 readonly_fields = ('owner', )
110 model = models.Model # à remplacer dans admin.py
111 extra = 1
112
113
114 class PosteFinancementInline(admin.TabularInline):
115 model = models.Model # à remplacer dans admin.py
116
117
118 class PostePieceInline(admin.TabularInline):
119 model = models.Model # à remplacer dans admin.py
120
121
122 class RemunerationInline(AUFMetadataInlineAdminMixin, admin.TabularInline):
123 model = models.Model # à remplacer dans admin.py
124 extra = 1
125
126
127 class RemunerationROInline(ReadOnlyInlineMixin, RemunerationInline):
128 pass
129
130
131 class TypePosteInline(AUFMetadataInlineAdminMixin, admin.TabularInline):
132 model = models.Model # à remplacer dans admin.py
133
134
135 # Admins
136
137 class AyantDroitAdmin(AUFMetadataAdminMixin, ProtectRegionMixin, admin.ModelAdmin):
138 """
139 L'ajout d'un nouvel ayantdroit se fait dans l'admin de l'employé.
140 """
141 alphabet_filter = 'nom'
142 search_fields = ('nom', 'prenom', 'employe__nom', 'employe__prenom', )
143 list_display = ('_employe', 'lien_parente', '_ayantdroit', )
144 inlines = (AyantDroitCommentaireInline,)
145 readonly_fields = AUFMetadataAdminMixin.readonly_fields + ('employe',)
146 fieldsets = AUFMetadataAdminMixin.fieldsets + (
147 ("Lien avec l'employé", {
148 'fields': ('employe', 'lien_parente', )
149 }),
150
151 ('Identification', {
152 'fields': ('nom', 'prenom', 'nom_affichage', 'nationalite', 'date_naissance', 'genre', )
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()
162
163 def _ayantdroit(self, obj):
164 return unicode(obj)
165 _ayantdroit.short_description = u'Ayant droit'
166
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
173
174 class AyantDroitCommentaireAdmin(admin.ModelAdmin):
175 pass
176
177
178 class ClassementAdmin(AUFMetadataAdminMixin, admin.ModelAdmin):
179 fieldsets = AUFMetadataAdminMixin.fieldsets + (
180 (None, {
181 'fields': ('type', 'echelon', 'degre', 'coefficient', )
182 }),
183 )
184
185
186 class CommentaireAdmin(admin.ModelAdmin):
187 pass
188
189
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"
216
217 class DeviseAdmin(AUFMetadataAdminMixin, admin.ModelAdmin):
218 fieldsets = AUFMetadataAdminMixin.fieldsets + (
219 (None, {
220 'fields': ('code', 'nom', ),
221 }),
222 )
223
224
225 class DossierAdmin(AUFMetadataAdminMixin, ProtectRegionMixin, admin.ModelAdmin,):
226 form = DossierForm
227 alphabet_filter = 'employe__nom'
228 search_fields = ('employe__nom', 'employe__prenom', 'poste__nom', 'poste__nom_feminin')
229 list_display = ('_employe', '_poste', 'date_debut', 'date_fin', 'date_modification', '_actif')
230 inlines = (DossierPieceInline, ContratInline,
231 RemunerationInline,
232 #EvenementInline,
233 DossierCommentaireInline,
234 )
235 fieldsets = AUFMetadataAdminMixin.fieldsets + (
236 ('Identification', {
237 'fields': ('employe', 'poste', 'statut', 'organisme_bstg',)
238 }),
239 ('Recrutement', {
240 'fields': (('remplacement', 'statut_residence'), )
241 }),
242 ('Rémunération', {
243 'fields': ('classement', ('regime_travail', 'regime_travail_nb_heure_semaine'),)
244 }),
245 ('Occupation du Poste par cet Employe', {
246 'fields': (('date_debut', 'date_fin'), )
247 }),
248 )
249
250 def lookup_allowed(self, key, value):
251 if key in ('employe__nom__istartswith', 'actif__exact', ):
252 return True
253
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'
263
264 def _poste(self, dossier):
265 return unicode(dossier.poste.nom)
266 _poste.short_description = u'Poste'
267 _poste.admin_order_field = 'poste__nom'
268
269 def _employe(self, dossier):
270 return unicode(dossier.employe)
271 _employe.short_description = u'Employé'
272 _employe.admin_order_field = 'employe__nom'
273
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()
280
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
316 class DossierPieceAdmin(admin.ModelAdmin):
317 pass
318
319
320 class DossierCommentaireAdmin(admin.ModelAdmin):
321 pass
322
323
324 class EmployeAdmin(AUFMetadataAdminMixin, ProtectRegionMixin, admin.ModelAdmin):
325 alphabet_filter = 'nom'
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', )
331 inlines = (AyantDroitInline,
332 DossierROInline,
333 EmployePieceInline,
334 EmployeCommentaireInline)
335 fieldsets = AUFMetadataAdminMixin.fieldsets + (
336 ('Identification', {
337 'fields': (('nom', 'prenom'), ('nom_affichage', 'genre'), 'nationalite', 'date_naissance', )
338 }),
339 ('Informations personnelles', {
340 'fields': ('situation_famille', 'date_entree', )
341 }),
342 ('Coordonnées', {
343 'fields': (('tel_domicile', 'tel_cellulaire'), ('adresse', 'ville'), ('code_postal', 'province'), 'pays', )
344 }),
345 )
346
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()
353
354 class EmployeCommentaireAdmin(admin.ModelAdmin):
355 pass
356
357
358 class EmployePieceAdmin(admin.ModelAdmin):
359 pass
360
361
362 class EvenementAdmin(admin.ModelAdmin):
363 inlines = (EvenementRemunerationInline,)
364
365
366 class EvenementRemunerationAdmin(admin.ModelAdmin):
367 pass
368
369
370 class FamilleEmploiAdmin(AUFMetadataAdminMixin, admin.ModelAdmin):
371 inlines = (TypePosteInline,)
372 fieldsets = AUFMetadataAdminMixin.fieldsets + (
373 (None, {
374 'fields': ('nom', )
375 }),
376 )
377
378
379 class OrganismeBstgAdmin(AUFMetadataAdminMixin, ProtectRegionMixin, admin.ModelAdmin):
380 search_fields = ('nom', )
381 list_display = ('nom', 'type', 'pays', )
382 inlines = (DossierROInline,)
383 fieldsets = AUFMetadataAdminMixin.fieldsets + (
384 (None, {
385 'fields': ('nom', 'type', 'pays', )
386 }),
387 )
388
389
390 class PosteAdmin(AUFMetadataAdminMixin, ProtectRegionMixin, admin.ModelAdmin):
391 alphabet_filter = 'nom'
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', )
394 fieldsets = AUFMetadataAdminMixin.fieldsets + (
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
426 inlines = (PosteFinancementInline,
427 PostePieceInline,
428 DossierROInline,
429 PosteCommentaireInline, )
430
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()
438
439
440 class PosteCommentaireAdmin(admin.ModelAdmin):
441 pass
442
443
444 class PosteFinancementAdmin(admin.ModelAdmin):
445 pass
446
447
448 class PostePieceAdmin(admin.ModelAdmin):
449 pass
450
451
452 class RemunerationAdmin(admin.ModelAdmin):
453 pass
454
455
456 class ResponsableImplantationAdmin(AUFMetadataAdminMixin, admin.ModelAdmin):
457 fieldsets = AUFMetadataAdminMixin.fieldsets + (
458 (None, {
459 'fields': ('employe', 'implantation', ),
460 }),
461 )
462
463
464 class ServiceAdmin(AUFMetadataAdminMixin, admin.ModelAdmin):
465 list_display = ('nom', 'actif', )
466 fieldsets = AUFMetadataAdminMixin.fieldsets + (
467 (None, {
468 'fields': ('nom', ),
469 }),
470 )
471
472 class StatutAdmin(AUFMetadataAdminMixin, admin.ModelAdmin):
473 fieldsets = AUFMetadataAdminMixin.fieldsets + (
474 (None, {
475 'fields': ('code', 'nom', ),
476 }),
477 )
478
479 class TauxChangeAdmin(admin.ModelAdmin):
480 list_display = ('taux', 'devise', 'annee', )
481 list_filter = ('devise', )
482 fieldsets = AUFMetadataAdminMixin.fieldsets + (
483 (None, {
484 'fields': ('taux', 'devise', 'annee', ),
485 }),
486 )
487
488 class TypeContratAdmin(admin.ModelAdmin):
489 inlines = (ContratInline,)
490
491
492 class TypePosteAdmin(AUFMetadataAdminMixin, admin.ModelAdmin):
493 search_fields = ('nom', 'nom_feminin', )
494 list_display = ('nom', 'famille_emploi', )
495 list_filter = ('famille_emploi', )
496 fieldsets = AUFMetadataAdminMixin.fieldsets + (
497 (None, {
498 'fields': ('nom', 'nom_feminin', 'is_responsable', 'famille_emploi', )
499 }),
500 )
501
502
503 class TypeRemunerationAdmin(AUFMetadataAdminMixin, admin.ModelAdmin):
504 list_display = ('nom', 'type_paiement', 'nature_remuneration', )
505 #inlines = (RemunerationROInline,) utilité?
506 fieldsets = AUFMetadataAdminMixin.fieldsets + (
507 (None, {
508 'fields': ('nom', 'type_paiement', 'nature_remuneration', )
509 }),
510 )
511
512
513 class TypeRevalorisationAdmin(AUFMetadataAdminMixin, admin.ModelAdmin):
514 #inlines = (RemunerationROInline,) utilité?
515 fieldsets = AUFMetadataAdminMixin.fieldsets + (
516 (None, {
517 'fields': ('nom', )
518 }),
519 )
520
521
522 class ValeurPointAdmin(AUFMetadataAdminMixin, admin.ModelAdmin):
523 list_display = ('_devise_code', '_devise_nom', 'annee', 'valeur', )
524 fieldsets = AUFMetadataAdminMixin.fieldsets + (
525 (None, {
526 'fields': ('valeur', 'devise', 'implantation', 'annee', )
527 }),
528 )
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"