plug recherche temporelle dans rapport contrats
[auf_rh_dae.git] / project / rh / templatetags / rapports.py
CommitLineData
fd009814
PP
1# -*- coding: utf-8 -*-
2
72ac5e55
PP
3import datetime
4
fd009814
PP
5from django.utils.encoding import smart_unicode
6from django.template import Library
7from django.utils.http import urlencode
8
9from datamaster_modeles.models import Implantation, Region
23e749e1 10from rh.models import TypeContrat
fd009814
PP
11
12
13register = Library()
14
15
549830eb 16COMBLE_CHOICES = (('c', 'Comblé'), ('n', 'Vacant'))
7821915f
PP
17
18
19@register.inclusion_tag('admin/filter.html', takes_context=True)
20def filter_comble(context):
21 return {'title': 'comblé',
22 'choices': prepare_choices(COMBLE_CHOICES, 'comble', context)}
23
24
f0f6b03e 25@register.inclusion_tag('admin/filter.html', takes_context=True)
fd009814
PP
26def filter_region(context):
27 return {'title': u"région",
28 'choices': prepare_choices(Region.objects.values_list('id', 'nom'), 'implantation__region', context, remove=['pays', 'nord_sud'])}
29
30
f0f6b03e 31@register.inclusion_tag('admin/filter.html', takes_context=True)
fd009814
PP
32def filter_implantation(context):
33 return {'title': u"implantation",
34 'choices': prepare_choices(Implantation.objects.values_list('id', 'nom'), 'implantation', context)}
35
36
f0f6b03e 37@register.inclusion_tag('admin/filter.html', takes_context=True)
23e749e1
PP
38def filter_region_contrat(context):
39 return {'title': u"région",
40 'choices': prepare_choices(Region.objects.values_list('id', 'nom'), 'dossier__poste__implantation__region', context, remove=['pays', 'nord_sud'])}
41
42
f0f6b03e 43@register.inclusion_tag('admin/filter.html', takes_context=True)
23e749e1
PP
44def filter_implantation_contrat(context):
45 return {'title': u"implantation",
46 'choices': prepare_choices(Implantation.objects.values_list('id', 'nom'), 'dossier__poste__implantation', context)}
47
48
f0f6b03e 49@register.inclusion_tag('admin/filter.html', takes_context=True)
23e749e1
PP
50def filter_type_contrat(context):
51 return {'title': u"type de contrat",
52 'choices': prepare_choices(TypeContrat.objects.values_list('id', 'nom'), 'type_contrat', context)}
53
72ac5e55
PP
54@register.inclusion_tag('admin/filter_select.html', takes_context=True)
55def filter_echeance_contrat(context):
eb204143
JPC
56 today = datetime.date.today()
57 three_months = today + datetime.timedelta(days=3*30)
58 six_months = today + datetime.timedelta(days=6*30)
59 twelve_months = today + datetime.timedelta(days=12*30)
60
61 field_name = 'date_fin'
72ac5e55 62 return {'title': u"échéance",
eb204143
JPC
63 'choices': prepare_choices_date(field_name, context, links=(
64 ('Tous', {}),
7c80ddca 65 ('échus', {'%s__lt' % field_name: today.strftime('%Y-%m-%d'),}),
eb204143
JPC
66 ('moins de 3 mois', {'%s__gte' % field_name: today.strftime('%Y-%m-%d'),
67 '%s__lte' % field_name: three_months.strftime('%Y-%m-%d')}),
68 ('3 à 6 mois', {'%s__gte' % field_name: three_months.strftime('%Y-%m-%d'),
69 '%s__lte' % field_name: six_months.strftime('%Y-%m-%d')}),
70 ('6 à 12 mois', {'%s__gte' % field_name: six_months.strftime('%Y-%m-%d'),
71 '%s__lte' % field_name: twelve_months.strftime('%Y-%m-%d')}),
72 )
73 )}
74
75@register.inclusion_tag('admin/filter_select.html', takes_context=True)
76def filter_debut_contrat(context):
77 year = datetime.date.today().timetuple()[0]
78 this_year = datetime.date(year, 1, 1)
79 next_year = datetime.date(year + 1, 1, 1)
80 last_year = datetime.date(year - 1, 12,31)
81
82 field_name = 'date_debut'
83 return {'title': u"date début",
84 'choices': prepare_choices_date(field_name, context, links=(
85 ('Tous', {}),
2953bb4d 86 ('années à venir', {'%s__gte' % field_name: next_year.strftime('%Y-%m-%d')}),
eb204143
JPC
87 ('cette anneée', {'%s__gte' % field_name: this_year.strftime('%Y-%m-%d'),
88 '%s__lt' % field_name: next_year.strftime('%Y-%m-%d')}),
02d63d23 89 ('années passées', {'%s__lte' % field_name: last_year.strftime('%Y-%m-%d')}),
eb204143
JPC
90 )
91 )}
92
35beb92c
JPC
93@register.inclusion_tag('admin/filter_select.html', takes_context=True)
94def filter_a_venir(context):
95 today = datetime.date.today()
96 year, month, day = datetime.date.today().timetuple()[:3]
97 mois_prochain = datetime.date(year+((month+1)/13), (month+1)%12, 1)
98 this_month = datetime.date(year, month, 1)
99
100 field_name = 'date_debut'
101 return {'title': u"à venir",
102 'choices': prepare_choices_date(field_name, context, links=(
103 ('Tous', {}),
104 ('à venir', {'%s__gt' % field_name: today.strftime('%Y-%m-%d')}),
105 ('à venir mois prochain', {'%s__gte' % field_name: mois_prochain.strftime('%Y-%m-%d')}),
106 ('à venir ce mois', {'%s__gte' % field_name: this_month.strftime('%Y-%m-%d'),
107 '%s__lt' % field_name: mois_prochain.strftime('%Y-%m-%d')}),
108 )
109 )}
110
111
72ac5e55
PP
112
113
af5073aa
PP
114@register.inclusion_tag('admin/filter_select.html', takes_context=True)
115def filter_region_remun(context):
116 return {'title': u"région",
117 'choices': prepare_choices(Region.objects.values_list('id', 'nom'), 'dossiers__poste__implantation__region', context, remove=['pays', 'nord_sud'])}
118
119
120@register.inclusion_tag('admin/filter_select.html', takes_context=True)
121def filter_implantation_remun(context):
122 return {'title': u"implantation",
123 'choices': prepare_choices(Implantation.objects.values_list('id', 'nom'), 'dossiers__poste__implantation', context)}
124
4dade240 125@register.inclusion_tag('rh/rapports/table_header.html', takes_context=True)
9d53bc73
JPC
126def table_header(context, headers):
127 return {'headers': headers}
af5073aa 128
fd009814
PP
129def get_query_string(request, new_params=None, remove=None):
130 if new_params is None: new_params = {}
131 if remove is None: remove = []
132 p = dict(request.GET.items())
133 for r in remove:
134 for k in p.keys():
135 if k.startswith(r):
136 del p[k]
137 for k, v in new_params.items():
138 if v is None:
139 if k in p:
140 del p[k]
141 else:
142 p[k] = v
143 return '?%s' % urlencode(p)
144
145
146def prepare_choices(choices, query_param, context, remove=[]):
147 request = context['request']
148 query_val = request.GET.get(query_param)
149 result = [{'selected': query_val is None,
150 'query_string': get_query_string(request, {}, [query_param] + remove),
151 'display': 'Tout'}]
152 for k, v in choices:
153 result.append({'selected': smart_unicode(k) == query_val,
154 'query_string': get_query_string(request, {query_param: k}, remove),
155 'display': v})
156 return result
72ac5e55
PP
157
158
eb204143 159def prepare_choices_date(field_name, context, links, remove=[]):
72ac5e55
PP
160 request = context['request']
161 params = request.GET
162 field_generic = '%s__' % field_name
163 date_params = dict([(k, v) for k, v in params.items() if k.startswith(field_generic)])
164
165
eb204143 166
72ac5e55
PP
167 result = []
168 for title, param_dict in links:
169 result.append({'selected': date_params == param_dict,
170 'query_string': get_query_string(request, param_dict, [field_generic]),
171 'display': title})
172 return result
9d53bc73 173
9d53bc73 174class SortHeaders:
4dade240
JPC
175 def __init__(self, request, headers, order_field_type, order_field, default_order_type="asc", not_sortable=[]):
176 self.request = request
177 self.order_field_type = order_field_type
178 self.order_field = order_field
9d53bc73 179 self.header_defs = headers
4dade240
JPC
180 self.params = dict(request.GET)
181 if default_order_type not in ('asc', 'desc'):
182 raise AttributeError(u"L'ordre par défaut n'est pas valide")
183 self.default_order_type = default_order_type
184 self.current_order_field = self.params[self.order_field][0] if self.order_field in self.params else None
185 self.current_order_field_type = self.params[self.order_field_type][0] if self.order_field_type in self.params else None
186 self.not_sortable = not_sortable
9d53bc73
JPC
187
188 def headers(self):
4dade240
JPC
189 for h in self.header_defs:
190 params = self.params
191 if h[0] == self.current_order_field:
192 order_type = {'asc': 'desc', 'desc': 'asc'}[self.current_order_field_type]
193 class_order = "%sending" % self.current_order_field_type
194 else:
195 order_type = self.default_order_type
196 class_order = ""
197 params[self.order_field_type] = [order_type]
198 params[self.order_field] = [h[0]]
199 url = "?%s" % "&".join(['%s=%s' % (param, value[0]) for param, value in params.items()])
9d53bc73 200 yield {
4dade240
JPC
201 "class_attr": "sorted %s" % class_order if self.field_sorted(h[0]) else "",
202 "sortable": self.field_sorted(h[0]),
203 "url": url,
204 "text": h[1]
9d53bc73
JPC
205 }
206
4dade240
JPC
207 def field_sorted(self, field):
208 return True if field not in self.not_sortable else False
209
9202c5db
JPC
210@register.filter
211def split(str, splitter):
212 return str.split(splitter)