afab3db82ec7b8342e70e68a87e87d94185f6260
[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, is_user_dans_region
11 from project.rh import models as rh
12
13
14 class AjaxSelect(object):
15
16 class Media:
17 css = {
18 'all': (
19 'jquery-autocomplete/jquery.autocomplete.css',
20 'css/select.css',
21 )
22 }
23 js = (
24 'js/jquery-1.5.1.min.js',
25 'jquery-autocomplete/jquery.autocomplete.js',
26 )
27
28
29 class FormDate(object):
30
31 def clean_date_fin(self):
32 date_fin = self.cleaned_data['date_fin']
33 if date_fin is None:
34 return date_fin
35 date_debut = self.cleaned_data['date_debut']
36 if date_fin < date_debut:
37 raise forms.ValidationError(
38 u"La date de fin est antérieure à la date de début"
39 )
40 return date_fin
41
42
43 class DossierForm(forms.ModelForm, FormDate):
44
45 class Model:
46 model = rh.Dossier
47
48
49 class ContratForm(forms.ModelForm, FormDate):
50
51 class Model:
52 model = rh.Contrat
53
54
55 class AyantDroitForm(forms.ModelForm, AjaxSelect):
56
57 # ne fonctionne pas dans un inline
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:
64 model = rh.AyantDroit
65
66
67 class EmployeAdminForm(forms.ModelForm, AjaxSelect):
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 )
74
75 class Meta:
76 model = rh.Employe
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
83 class ResponsableInlineForm(forms.ModelForm):
84 employes_actifs = rh.Employe.objects.actifs().distinct()
85 employe = forms.ModelChoiceField(queryset=employes_actifs)
86
87 class Meta:
88 model = rh.ResponsableImplantation
89
90
91 class MasseSalarialeForm(forms.Form):
92 zone_administrative = forms.ModelChoiceField(
93 label=u'Région', queryset=ref.ZoneAdministrative.objects.all(),
94 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 is_user_dans_region(user):
115 employe = get_employe_from_user(user)
116 self.fields['zone_administrative'].queryset = \
117 ref.ZoneAdministrative.objects.filter(
118 code=employe.implantation.zone_administrative.code
119 )
120 self.fields['implantation'].queryset = \
121 ref.Implantation.objects.filter(
122 zone_administrative=(
123 employe.implantation.zone_administrative
124 )
125 )