[#3165] Remplacé les filtres et protection par région pour des filtres et protections...
[auf_rh_dae.git] / project / rh / forms.py
CommitLineData
84cbb4c5
OL
1# -*- encoding: utf-8 -*-
2
fc62be5d
EMS
3from datetime import date
4
105dd778 5from ajax_select.fields import AutoCompleteSelectField
fc62be5d
EMS
6from auf.django.references import models as ref
7from django import forms
8from django.db.models import Min
9
f1d5cb6c 10from project.groups import get_employe_from_user, is_user_dans_region
fc62be5d 11from project.rh import models as rh
105dd778
OL
12
13
14class AjaxSelect(object):
15
16 class Media:
17 css = {
00119a5d
EMS
18 'all': (
19 'jquery-autocomplete/jquery.autocomplete.css',
20 'css/select.css',
21 )
105dd778 22 }
00119a5d
EMS
23 js = (
24 'js/jquery-1.5.1.min.js',
25 'jquery-autocomplete/jquery.autocomplete.js',
26 )
84cbb4c5
OL
27
28
29class FormDate(object):
30
31 def clean_date_fin(self):
32 date_fin = self.cleaned_data['date_fin']
33 if date_fin is None:
00119a5d 34 return date_fin
84cbb4c5
OL
35 date_debut = self.cleaned_data['date_debut']
36 if date_fin < date_debut:
00119a5d
EMS
37 raise forms.ValidationError(
38 u"La date de fin est antérieure à la date de début"
39 )
84cbb4c5
OL
40 return date_fin
41
00119a5d 42
105dd778 43class DossierForm(forms.ModelForm, FormDate):
00119a5d 44
84cbb4c5 45 class Model:
fc62be5d 46 model = rh.Dossier
84cbb4c5 47
6bee05ff 48
84cbb4c5
OL
49class ContratForm(forms.ModelForm, FormDate):
50
51 class Model:
fc62be5d 52 model = rh.Contrat
cf022e27 53
00119a5d 54
105dd778
OL
55class AyantDroitForm(forms.ModelForm, AjaxSelect):
56
ed4fbe26 57 # ne fonctionne pas dans un inline
cf022e27
OL
58
59 def __init__(self, *args, **kwargs):
60 super(AyantDroitForm, self).__init__(*args, **kwargs)
61 self.fields['date_naissance'].widget = forms.widgets.DateInput()
62
63 class Meta:
fc62be5d 64 model = rh.AyantDroit
cf022e27
OL
65
66
105dd778 67class EmployeAdminForm(forms.ModelForm, AjaxSelect):
00119a5d
EMS
68 nationalite = AutoCompleteSelectField(
69 'pays', help_text="Taper le nom ou le code du pays", required=False
70 )
71 pays = AutoCompleteSelectField(
72 'pays', help_text="Taper le nom ou le code du pays", required=False
73 )
105dd778
OL
74
75 class Meta:
fc62be5d 76 model = rh.Employe
105dd778
OL
77
78 def __init__(self, *args, **kwargs):
79 super(EmployeAdminForm, self).__init__(*args, **kwargs)
80 self.fields['date_naissance'].widget = forms.widgets.DateInput()
81
82
6fb68b2f 83class ResponsableInlineForm(forms.ModelForm):
fc62be5d 84 employes_actifs = rh.Employe.objects.actifs()
6fb68b2f
DB
85 employe = forms.ModelChoiceField(queryset=employes_actifs)
86
87 class Meta:
fc62be5d
EMS
88 model = rh.ResponsableImplantation
89
90
91class MasseSalarialeForm(forms.Form):
b0cf30b8
EMS
92 zone_administrative = forms.ModelChoiceField(
93 label=u'Région', queryset=ref.ZoneAdministrative.objects.all(),
94 required=False
fc62be5d
EMS
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 )
f1d5cb6c 114 if is_user_dans_region(user):
fc62be5d 115 employe = get_employe_from_user(user)
b0cf30b8
EMS
116 self.fields['zone_administrative'].queryset = \
117 ref.ZoneAdministrative.objects.filter(
118 code=employe.implantation.zone_administrative.code
119 )
fc62be5d
EMS
120 self.fields['implantation'].queryset = \
121 ref.Implantation.objects.filter(
b0cf30b8
EMS
122 zone_administrative=(
123 employe.implantation.zone_administrative
124 )
fc62be5d 125 )