[#3068] [#3069] [#3133] Réécriture du rapport de masse salariale
[auf_rh_dae.git] / project / rh / forms.py
1 # -*- encoding: utf-8 -*-
2
3 from datetime import date
4
5 from ajax_select.fields import AutoCompleteSelectField
6 from auf.django.references import models as ref
7 from django import forms
8 from django.db.models import Min
9
10 from project.groups import get_employe_from_user
11 from project.permissions import user_gere_obj_de_sa_region
12 from project.rh import models as rh
13
14
15 class AjaxSelect(object):
16
17 class Media:
18 css = {
19 'all': (
20 'jquery-autocomplete/jquery.autocomplete.css',
21 'css/select.css',
22 )
23 }
24 js = (
25 'js/jquery-1.5.1.min.js',
26 'jquery-autocomplete/jquery.autocomplete.js',
27 )
28
29
30 class FormDate(object):
31
32 def clean_date_fin(self):
33 date_fin = self.cleaned_data['date_fin']
34 if date_fin is None:
35 return date_fin
36 date_debut = self.cleaned_data['date_debut']
37 if date_fin < date_debut:
38 raise forms.ValidationError(
39 u"La date de fin est antérieure à la date de début"
40 )
41 return date_fin
42
43
44 class DossierForm(forms.ModelForm, FormDate):
45
46 class Model:
47 model = rh.Dossier
48
49
50 class ContratForm(forms.ModelForm, FormDate):
51
52 class Model:
53 model = rh.Contrat
54
55
56 class AyantDroitForm(forms.ModelForm, AjaxSelect):
57
58 # ne fonctionne pas dans un inline
59
60 def __init__(self, *args, **kwargs):
61 super(AyantDroitForm, self).__init__(*args, **kwargs)
62 self.fields['date_naissance'].widget = forms.widgets.DateInput()
63
64 class Meta:
65 model = rh.AyantDroit
66
67
68 class EmployeAdminForm(forms.ModelForm, AjaxSelect):
69 nationalite = AutoCompleteSelectField(
70 'pays', help_text="Taper le nom ou le code du pays", required=False
71 )
72 pays = AutoCompleteSelectField(
73 'pays', help_text="Taper le nom ou le code du pays", required=False
74 )
75
76 class Meta:
77 model = rh.Employe
78
79 def __init__(self, *args, **kwargs):
80 super(EmployeAdminForm, self).__init__(*args, **kwargs)
81 self.fields['date_naissance'].widget = forms.widgets.DateInput()
82
83
84 class ResponsableInlineForm(forms.ModelForm):
85 employes_actifs = rh.Employe.objects.actifs()
86 employe = forms.ModelChoiceField(queryset=employes_actifs)
87
88 class Meta:
89 model = rh.ResponsableImplantation
90
91
92 class MasseSalarialeForm(forms.Form):
93 region = forms.ModelChoiceField(
94 label=u'Région', queryset=ref.Region.objects.all(), required=False
95 )
96 implantation = forms.ModelChoiceField(
97 label=u'Implantation', queryset=ref.Implantation.objects.all(),
98 required=False
99 )
100
101 def __init__(self, user, *args, **kwargs):
102 super(MasseSalarialeForm, self).__init__(*args, **kwargs)
103 min_date = rh.Remuneration.objects \
104 .aggregate(Min('date_debut'))['date_debut__min']
105 years = range(
106 date.today().year,
107 min_date.year if min_date else 1997,
108 -1
109 )
110 self.fields['annee'] = forms.TypedChoiceField(
111 label='Année', choices=zip(years, years), coerce=int,
112 required=False
113 )
114 if user_gere_obj_de_sa_region(user):
115 employe = get_employe_from_user(user)
116 self.fields['region'].queryset = ref.Region.objects.filter(
117 id=employe.implantation.region.id
118 )
119 self.fields['implantation'].queryset = \
120 ref.Implantation.objects.filter(
121 region=employe.implantation.region
122 )