5834
[auf_rh_dae.git] / project / dae / templatetags / dae.py
1 # -*- encoding: utf-8 -*-
2
3 import os
4 import datetime
5 from decimal import Decimal
6
7 from django import template
8 from django.db.models import Q
9 # -*- coding: utf-8 -*-
10
11 from project import groups
12
13 from project.dae.workflow import ETATS_EDITABLE
14 from project.dae.forms import (
15 remun_formset_factory_factory,
16 ReadOnlyRemunFormSet,
17 PosteCompReadOnlyRemunFormSet,
18 DossierCompReadOnlyRemunFormSet,
19 RHReadOnlyRemunFormSet,
20 )
21 from project.dae import models as dae
22 from project.rh import models as rh
23
24
25 register = template.Library()
26
27
28 @register.filter
29 def test_membre_drh(user):
30 grps = [g.name for g in user.groups.all()]
31 if groups.DRH_NIVEAU_1 in grps or groups.DRH_NIVEAU_2 in grps:
32 return True
33 else:
34 return False
35
36
37 @register.filter
38 def peut_ajouter(user):
39 grp_ok = (groups.ADMINISTRATEURS,
40 groups.CORRESPONDANT_RH,
41 groups.DRH_NIVEAU_1,
42 groups.DRH_NIVEAU_2)
43 for g in [g.name for g in user.groups.all()]:
44 if g in grp_ok:
45 return True
46 return False
47
48
49 @register.filter
50 def est_editable(obj, user):
51 klass = obj.__class__
52 groupes_users = [g.name for g in user.groups.all()]
53 if obj.etat in ETATS_EDITABLE and \
54 (obj in klass.objects.mes_choses_a_faire(user).all() or \
55 groups.DRH_NIVEAU_1 in groupes_users or \
56 groups.DRH_NIVEAU_2 in groupes_users):
57 return True
58 else:
59 return False
60
61
62 @register.filter
63 def basename(path):
64 return os.path.basename(path)
65
66
67 @register.inclusion_tag('dae/sort_header.html', takes_context=True)
68 def sort_header(context, field, title):
69 """
70 Génère une entête qu'on peut cliquer pour trier
71 la colonne correspondante dans une table.
72 """
73 qs = context['request'].GET.copy()
74 current = qs.get('tri', None)
75 if current == field:
76 cls = 'header headerSortUp'
77 qs['tri'] = '-' + field
78 elif current == '-' + field:
79 cls = 'header headerSortDown'
80 qs['tri'] = field
81 else:
82 cls = 'header'
83 qs['tri'] = field
84 return {'title': title, 'qs': qs.urlencode(), 'cls': cls}
85
86
87 @register.inclusion_tag('dae/pagination.html', takes_context=True)
88 def pagination(context, page):
89 """Génère la navigation permettant de se promener de page en page."""
90 qs = context['request'].GET
91 previous_qs = qs.copy()
92 previous_qs['page'] = page.previous_page_number()
93 next_qs = qs.copy()
94 next_qs['page'] = page.next_page_number()
95 return {'page': page,
96 'previous_qs': previous_qs.urlencode(),
97 'next_qs': next_qs.urlencode()
98 }
99
100
101 @register.inclusion_tag('dae/embauche-remun.html', takes_context=True)
102 def remun_form(context, dossier):
103
104 return {
105 'annee_remun': None,
106 'remunForm': ReadOnlyRemunFormSet(instance=dossier)
107 }
108
109
110 @register.inclusion_tag('dae/embauche-remun.html', takes_context=True)
111 def rh_remun_form(context, dossier):
112
113 return {
114 'annee_remun': None,
115 'remunForm': RHReadOnlyRemunFormSet(instance=dossier)
116 }
117
118
119 @register.inclusion_tag('dae/embauche-remun.html', takes_context=True)
120 def poste_cmp_remun_form(context, poste_cmp):
121 return {
122 'annee_remun': None,
123 'remunForm': PosteCompReadOnlyRemunFormSet(instance=poste_cmp)
124 }
125
126
127 @register.inclusion_tag('dae/embauche-remun.html', takes_context=True)
128 def dossier_cmp_remun_form(context, dossier_cmp):
129 return {
130 'annee_remun': None,
131 'remunForm': DossierCompReadOnlyRemunFormSet(instance=dossier_cmp)
132 }
133
134
135 @register.inclusion_tag('dae/embauche-remun.html', takes_context=True)
136 def rh_remun_form_for_year(context, dossier, year=None):
137
138 fs = remun_formset_factory_factory(
139 read_only=True,
140 parent_model=rh.Dossier,
141 model=rh.Remuneration,
142 )
143
144 def _get_qs(inst, year=year):
145
146 if year == None:
147 year = (
148 inst.instance.date_fin.year if
149 inst.instance.date_fin else
150 datetime.date.today().year)
151
152 year_start = datetime.date(year, 1, 1)
153 year_end = datetime.date(year, 12, 31)
154 qs = inst.model.objects.filter(
155 Q(dossier=inst.instance) & (
156 Q(date_debut__lte=year_end, date_fin__gte=year_start) |
157 Q(date_debut__lte=year_end, date_fin__isnull=True) |
158 Q(date_debut__isnull=True, date_fin__gte=year_start) |
159 Q(date_debut__isnull=True, date_fin__isnull=True)
160 ))
161 return qs
162
163 fs.get_queryset = _get_qs
164
165 return {
166 'annee_remun': year,
167 'remunForm': fs(instance=dossier)
168 }
169
170
171 @register.simple_tag
172 def remun_ajuste(remun, annee=None):
173 return '%.2f' % remun.montant_ajuste_euros(annee)
174
175
176 @register.filter
177 def euros(montant, devise):
178 """
179 Template tag pour convertir en euros.
180 """
181 taux = 1
182 if devise.code != 'EUR':
183 liste_taux = devise.tauxchange_set.order_by('-annee')
184 if liste_taux.count() == 0:
185 taux = 0
186 else:
187 taux = liste_taux[0].taux
188
189 return montant * Decimal(str(taux))
190
191