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