Ticket 5638
[auf_rh_dae.git] / project / dae / templatetags / dae.py
CommitLineData
0b2edb6e
OL
1# -*- encoding: utf-8 -*-
2
bbf31587 3import os
7d8f6789 4import datetime
492fb9c2 5from decimal import Decimal
bbf31587 6
5579d48d 7from django import template
7d8f6789 8from django.db.models import Q
be46739d 9# -*- coding: utf-8 -*-
75f0e87b 10
be46739d 11from project import groups
d41d8e47 12
286d0aa9 13from project.dae.workflow import ETATS_EDITABLE
b9098c33 14from project.dae.forms import (
6219cf26 15 remun_formset_factory_factory,
b9098c33
BS
16 ReadOnlyRemunFormSet,
17 PosteCompReadOnlyRemunFormSet,
18 DossierCompReadOnlyRemunFormSet,
19 )
20from project.dae import models as dae
21from project.rh import models as rh
d41d8e47 22
5579d48d
OL
23
24register = template.Library()
25
be46739d 26
5579d48d 27@register.filter
3a62d4fe 28def test_membre_drh(user):
3383b2d1
OL
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:
2c8d834d
OL
31 return True
32 else:
33 return False
3a62d4fe 34
be46739d 35
3a62d4fe 36@register.filter
5579d48d 37def peut_ajouter(user):
be46739d
OL
38 grp_ok = (groups.ADMINISTRATEURS,
39 groups.CORRESPONDANT_RH,
40 groups.DRH_NIVEAU_1,
41 groups.DRH_NIVEAU_2)
3383b2d1 42 for g in [g.name for g in user.groups.all()]:
be46739d 43 if g in grp_ok:
5579d48d
OL
44 return True
45 return False
46
67c10912
OL
47
48@register.filter
e7ee680f
OL
49def est_editable(obj, user):
50 klass = obj.__class__
3383b2d1 51 groupes_users = [g.name for g in user.groups.all()]
c511cd1f 52 if obj.etat in ETATS_EDITABLE and \
3383b2d1
OL
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):
286d0aa9
OL
56 return True
57 else:
58 return False
59
be46739d 60
0b2edb6e 61@register.filter
bbf31587
EMS
62def basename(path):
63 return os.path.basename(path)
428e3c0b 64
be46739d 65
428e3c0b
EMS
66@register.inclusion_tag('dae/sort_header.html', takes_context=True)
67def sort_header(context, field, title):
be46739d
OL
68 """
69 Génère une entête qu'on peut cliquer pour trier
70 la colonne correspondante dans une table.
71 """
428e3c0b
EMS
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
be46739d 85
428e3c0b
EMS
86@register.inclusion_tag('dae/pagination.html', takes_context=True)
87def 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()
be46739d
OL
94 return {'page': page,
95 'previous_qs': previous_qs.urlencode(),
96 'next_qs': next_qs.urlencode()
97 }
b9098c33
BS
98
99
100@register.inclusion_tag('dae/embauche-remun.html', takes_context=True)
101def remun_form(context, dossier):
102
103 return {
7d8f6789 104 'annee_remun': None,
b9098c33
BS
105 'remunForm': ReadOnlyRemunFormSet(instance=dossier)
106 }
107
108
109@register.inclusion_tag('dae/embauche-remun.html', takes_context=True)
6219cf26
BS
110def rh_remun_form(context, dossier):
111
112 return {
7d8f6789 113 'annee_remun': None,
6219cf26
BS
114 'remunForm': RHReadOnlyRemunFormSet(instance=dossier)
115 }
116
117
118@register.inclusion_tag('dae/embauche-remun.html', takes_context=True)
b9098c33
BS
119def poste_cmp_remun_form(context, poste_cmp):
120 return {
7d8f6789 121 'annee_remun': None,
b9098c33
BS
122 'remunForm': PosteCompReadOnlyRemunFormSet(instance=poste_cmp)
123 }
124
125
126@register.inclusion_tag('dae/embauche-remun.html', takes_context=True)
127def dossier_cmp_remun_form(context, dossier_cmp):
128 return {
7d8f6789 129 'annee_remun': None,
b9098c33
BS
130 'remunForm': DossierCompReadOnlyRemunFormSet(instance=dossier_cmp)
131 }
6219cf26
BS
132
133
134@register.inclusion_tag('dae/embauche-remun.html', takes_context=True)
135def 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):
7d8f6789
BS
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
6219cf26
BS
154
155 fs.get_queryset = _get_qs
156
157 return {
7d8f6789 158 'annee_remun': year,
6219cf26
BS
159 'remunForm': fs(instance=dossier)
160 }
161
162
7d8f6789
BS
163@register.simple_tag
164def remun_ajuste(remun, annee=None):
165 return '%.2f' % remun.montant_ajuste_euros(annee)
492fb9c2
BS
166
167
168@register.filter
169def 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