Ajout du nombre de contrats et d'employés au rapoprt de contrats
[auf_rh_dae.git] / project / rh / templatetags / rapports.py
1 # -*- coding: utf-8 -*-
2
3 import datetime
4
5 from django.utils.encoding import smart_unicode
6 from django.template import Library
7 from django.utils.http import urlencode
8
9 from datamaster_modeles.models import Implantation, Region
10 from rh.models import TypeContrat
11
12
13 register = Library()
14
15
16 COMBLE_CHOICES = (('c', 'Comblé'), ('n', 'Vacant'))
17
18
19 @register.inclusion_tag('admin/filter.html', takes_context=True)
20 def filter_comble(context):
21 return {'title': 'comblé',
22 'choices': prepare_choices(COMBLE_CHOICES, 'comble', context)}
23
24
25 @register.inclusion_tag('admin/filter_select.html', takes_context=True)
26 def 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
31 @register.inclusion_tag('admin/filter_select.html', takes_context=True)
32 def filter_implantation(context):
33 return {'title': u"implantation",
34 'choices': prepare_choices(Implantation.objects.values_list('id', 'nom'), 'implantation', context)}
35
36
37 @register.inclusion_tag('admin/filter_select.html', takes_context=True)
38 def 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
43 @register.inclusion_tag('admin/filter_select.html', takes_context=True)
44 def 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
49 @register.inclusion_tag('admin/filter_select.html', takes_context=True)
50 def 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
54
55 @register.inclusion_tag('admin/filter_select.html', takes_context=True)
56 def filter_echeance_contrat(context):
57 return {'title': u"échéance",
58 'choices': prepare_choices_date('date_fin', context)}
59
60
61 @register.inclusion_tag('admin/filter_select.html', takes_context=True)
62 def filter_region_remun(context):
63 return {'title': u"région",
64 'choices': prepare_choices(Region.objects.values_list('id', 'nom'), 'dossiers__poste__implantation__region', context, remove=['pays', 'nord_sud'])}
65
66
67 @register.inclusion_tag('admin/filter_select.html', takes_context=True)
68 def filter_implantation_remun(context):
69 return {'title': u"implantation",
70 'choices': prepare_choices(Implantation.objects.values_list('id', 'nom'), 'dossiers__poste__implantation', context)}
71
72
73 def get_query_string(request, new_params=None, remove=None):
74 if new_params is None: new_params = {}
75 if remove is None: remove = []
76 p = dict(request.GET.items())
77 for r in remove:
78 for k in p.keys():
79 if k.startswith(r):
80 del p[k]
81 for k, v in new_params.items():
82 if v is None:
83 if k in p:
84 del p[k]
85 else:
86 p[k] = v
87 return '?%s' % urlencode(p)
88
89
90 def prepare_choices(choices, query_param, context, remove=[]):
91 request = context['request']
92 query_val = request.GET.get(query_param)
93 result = [{'selected': query_val is None,
94 'query_string': get_query_string(request, {}, [query_param] + remove),
95 'display': 'Tout'}]
96 for k, v in choices:
97 result.append({'selected': smart_unicode(k) == query_val,
98 'query_string': get_query_string(request, {query_param: k}, remove),
99 'display': v})
100 return result
101
102
103 def prepare_choices_date(field_name, context, remove=[]):
104 request = context['request']
105 params = request.GET
106 field_generic = '%s__' % field_name
107 date_params = dict([(k, v) for k, v in params.items() if k.startswith(field_generic)])
108
109
110 today = datetime.date.today()
111 three_months = today + datetime.timedelta(days=3*30)
112 six_months = today + datetime.timedelta(days=6*30)
113 twelve_months = today + datetime.timedelta(days=12*30)
114
115 links = (
116 ('Tous', {}),
117 ('moins de 3 mois', {'%s__gte' % field_name: today.strftime('%Y-%m-%d'),
118 '%s__lte' % field_name: three_months.strftime('%Y-%m-%d')}),
119 ('3 à 6 mois', {'%s__gte' % field_name: three_months.strftime('%Y-%m-%d'),
120 '%s__lte' % field_name: six_months.strftime('%Y-%m-%d')}),
121 ('6 à 12 mois', {'%s__gte' % field_name: six_months.strftime('%Y-%m-%d'),
122 '%s__lte' % field_name: twelve_months.strftime('%Y-%m-%d')}),
123 )
124
125 result = []
126 for title, param_dict in links:
127 result.append({'selected': date_params == param_dict,
128 'query_string': get_query_string(request, param_dict, [field_generic]),
129 'display': title})
130 return result