6048
[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 year = year or (
145 dossier.date_fin.year if
146 dossier.date_fin else
147 datetime.date.today().year)
148
149 def _get_qs(inst, year=year):
150
151 year_start = datetime.date(year, 1, 1)
152 year_end = datetime.date(year, 12, 31)
153 qs = inst.model.objects.filter(
154 Q(dossier=inst.instance) & (
155 Q(date_debut__lte=year_end, date_fin__gte=year_start) |
156 Q(date_debut__lte=year_end, date_fin__isnull=True) |
157 Q(date_debut__isnull=True, date_fin__gte=year_start) |
158 Q(date_debut__isnull=True, date_fin__isnull=True)
159 ))
160 return qs
161
162 fs.get_queryset = _get_qs
163
164 return {
165 'annee_remun': year,
166 'remunForm': fs(instance=dossier)
167 }
168
169
170 @register.simple_tag
171 def remun_ajuste(remun, annee=None):
172 return '%.2f' % remun.montant_ajuste_euros(annee)
173
174
175 @register.filter
176 def euros(montant, devise):
177 """
178 Template tag pour convertir en euros.
179 """
180 taux = 1
181 if devise.code != 'EUR':
182 liste_taux = devise.tauxchange_set.order_by('-annee')
183 if liste_taux.count() == 0:
184 taux = 0
185 else:
186 taux = liste_taux[0].taux
187
188 return montant * Decimal(str(taux))
189
190