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