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