Commit | Line | Data |
---|---|---|
84cbb4c5 OL |
1 | # -*- encoding: utf-8 -*- |
2 | ||
fc62be5d EMS |
3 | from datetime import date |
4 | ||
105dd778 | 5 | from ajax_select.fields import AutoCompleteSelectField |
fc62be5d EMS |
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 | |
105dd778 OL |
13 | |
14 | ||
15 | class AjaxSelect(object): | |
16 | ||
17 | class Media: | |
18 | css = { | |
00119a5d EMS |
19 | 'all': ( |
20 | 'jquery-autocomplete/jquery.autocomplete.css', | |
21 | 'css/select.css', | |
22 | ) | |
105dd778 | 23 | } |
00119a5d EMS |
24 | js = ( |
25 | 'js/jquery-1.5.1.min.js', | |
26 | 'jquery-autocomplete/jquery.autocomplete.js', | |
27 | ) | |
84cbb4c5 OL |
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: | |
00119a5d | 35 | return date_fin |
84cbb4c5 OL |
36 | date_debut = self.cleaned_data['date_debut'] |
37 | if date_fin < date_debut: | |
00119a5d EMS |
38 | raise forms.ValidationError( |
39 | u"La date de fin est antérieure à la date de début" | |
40 | ) | |
84cbb4c5 OL |
41 | return date_fin |
42 | ||
00119a5d | 43 | |
105dd778 | 44 | class DossierForm(forms.ModelForm, FormDate): |
00119a5d | 45 | |
84cbb4c5 | 46 | class Model: |
fc62be5d | 47 | model = rh.Dossier |
84cbb4c5 | 48 | |
6bee05ff | 49 | |
84cbb4c5 OL |
50 | class ContratForm(forms.ModelForm, FormDate): |
51 | ||
52 | class Model: | |
fc62be5d | 53 | model = rh.Contrat |
cf022e27 | 54 | |
00119a5d | 55 | |
105dd778 OL |
56 | class AyantDroitForm(forms.ModelForm, AjaxSelect): |
57 | ||
ed4fbe26 | 58 | # ne fonctionne pas dans un inline |
cf022e27 OL |
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: | |
fc62be5d | 65 | model = rh.AyantDroit |
cf022e27 OL |
66 | |
67 | ||
105dd778 | 68 | class EmployeAdminForm(forms.ModelForm, AjaxSelect): |
00119a5d EMS |
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 | ) | |
105dd778 OL |
75 | |
76 | class Meta: | |
fc62be5d | 77 | model = rh.Employe |
105dd778 OL |
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 | ||
6fb68b2f | 84 | class ResponsableInlineForm(forms.ModelForm): |
fc62be5d | 85 | employes_actifs = rh.Employe.objects.actifs() |
6fb68b2f DB |
86 | employe = forms.ModelChoiceField(queryset=employes_actifs) |
87 | ||
88 | class Meta: | |
fc62be5d EMS |
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 | ) |