Fix for odette
[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
49c2f4ad 14class ClassementHistoriqueForm(forms.ModelForm):
49c2f4ad
BS
15
16 class Meta:
17 model = rh.RHDossierClassementRecord
18
19
105dd778
OL
20class AjaxSelect(object):
21
22 class Media:
23 css = {
00119a5d
EMS
24 'all': (
25 'jquery-autocomplete/jquery.autocomplete.css',
26 'css/select.css',
27 )
105dd778 28 }
00119a5d
EMS
29 js = (
30 'js/jquery-1.5.1.min.js',
31 'jquery-autocomplete/jquery.autocomplete.js',
32 )
84cbb4c5
OL
33
34
35class FormDate(object):
36
37 def clean_date_fin(self):
38 date_fin = self.cleaned_data['date_fin']
39 if date_fin is None:
00119a5d 40 return date_fin
84cbb4c5
OL
41 date_debut = self.cleaned_data['date_debut']
42 if date_fin < date_debut:
00119a5d
EMS
43 raise forms.ValidationError(
44 u"La date de fin est antérieure à la date de début"
45 )
84cbb4c5
OL
46 return date_fin
47
00119a5d 48
105dd778 49class DossierForm(forms.ModelForm, FormDate):
00119a5d 50
84cbb4c5 51 class Model:
fc62be5d 52 model = rh.Dossier
84cbb4c5 53
6bee05ff 54
84cbb4c5
OL
55class ContratForm(forms.ModelForm, FormDate):
56
57 class Model:
fc62be5d 58 model = rh.Contrat
cf022e27 59
00119a5d 60
105dd778
OL
61class AyantDroitForm(forms.ModelForm, AjaxSelect):
62
ed4fbe26 63 # ne fonctionne pas dans un inline
cf022e27
OL
64
65 def __init__(self, *args, **kwargs):
66 super(AyantDroitForm, self).__init__(*args, **kwargs)
67 self.fields['date_naissance'].widget = forms.widgets.DateInput()
68
69 class Meta:
fc62be5d 70 model = rh.AyantDroit
cf022e27
OL
71
72
105dd778 73class EmployeAdminForm(forms.ModelForm, AjaxSelect):
00119a5d
EMS
74 nationalite = AutoCompleteSelectField(
75 'pays', help_text="Taper le nom ou le code du pays", required=False
76 )
77 pays = AutoCompleteSelectField(
78 'pays', help_text="Taper le nom ou le code du pays", required=False
79 )
105dd778
OL
80
81 class Meta:
fc62be5d 82 model = rh.Employe
105dd778
OL
83
84 def __init__(self, *args, **kwargs):
85 super(EmployeAdminForm, self).__init__(*args, **kwargs)
86 self.fields['date_naissance'].widget = forms.widgets.DateInput()
87
88
6fb68b2f 89class ResponsableInlineForm(forms.ModelForm):
6b4200cd 90 employes_actifs = rh.Employe.objects.actifs().distinct()
6fb68b2f
DB
91 employe = forms.ModelChoiceField(queryset=employes_actifs)
92
93 class Meta:
fc62be5d
EMS
94 model = rh.ResponsableImplantation
95
96
97class MasseSalarialeForm(forms.Form):
b0cf30b8
EMS
98 zone_administrative = forms.ModelChoiceField(
99 label=u'Région', queryset=ref.ZoneAdministrative.objects.all(),
100 required=False
fc62be5d
EMS
101 )
102 implantation = forms.ModelChoiceField(
103 label=u'Implantation', queryset=ref.Implantation.objects.all(),
104 required=False
105 )
106
107 def __init__(self, user, *args, **kwargs):
108 super(MasseSalarialeForm, self).__init__(*args, **kwargs)
109 min_date = rh.Remuneration.objects \
110 .aggregate(Min('date_debut'))['date_debut__min']
111 years = range(
112 date.today().year,
113 min_date.year if min_date else 1997,
114 -1
115 )
116 self.fields['annee'] = forms.TypedChoiceField(
117 label='Année', choices=zip(years, years), coerce=int,
118 required=False
119 )
f1d5cb6c 120 if is_user_dans_region(user):
fc62be5d 121 employe = get_employe_from_user(user)
b0cf30b8
EMS
122 self.fields['zone_administrative'].queryset = \
123 ref.ZoneAdministrative.objects.filter(
124 code=employe.implantation.zone_administrative.code
125 )
fc62be5d
EMS
126 self.fields['implantation'].queryset = \
127 ref.Implantation.objects.filter(
b0cf30b8
EMS
128 zone_administrative=(
129 employe.implantation.zone_administrative
130 )
fc62be5d 131 )