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