[#3068] [#3069] [#3133] Réécriture du rapport de masse salariale
[auf_rh_dae.git] / project / rh / managers.py
CommitLineData
3f5cbabe
OL
1# -*- encoding: utf-8 -*-
2
6fb68b2f 3from datetime import date
75f0e87b 4
3f5cbabe
OL
5from django.db import models
6from django.db.models import Q
fc62be5d 7from django.db.models.query import QuerySet
75f0e87b 8
6fb68b2f 9from auf.django.metadata.managers import NoDeleteManager, NoDeleteQuerySet
75f0e87b 10
afd3be54 11from project.groups import get_employe_from_user
fc62be5d
EMS
12from project.groups import \
13 grp_drh, grp_drh2, grp_accior, grp_abf, grp_haute_direction, \
14 grp_service_utilisateurs
3f5cbabe
OL
15
16
17class SecurityManager(models.Manager):
18
19 prefixe_service = None
20 prefixe_implantation = None
21
22 def ma_region_ou_service(self, user):
23 """
24 Filtrage des postes en fonction du user connecté (region / service)
fc62be5d
EMS
25 On s'intéresse aussi au groupe auquel appartient le user car
26 certains groupes peuvent tout voir.
3f5cbabe
OL
27 """
28 employe = get_employe_from_user(user)
29
30 ############################################
31 # TRAITEMENT NORMAL
32 ############################################
33 # REGION
fc62be5d 34 q = Q(**{self.prefixe_implantation: employe.implantation.region})
3f5cbabe
OL
35
36 # SERVICE
fc62be5d
EMS
37 if self.prefixe_service \
38 and grp_service_utilisateurs in user.groups.all():
39 q = q | Q(**{self.prefixe_service: employe.service})
3f5cbabe
OL
40
41 liste = self.get_query_set().filter(q)
42
43 ############################################
44 # TRAITEMENT ACCIOR
45 ############################################
46 if grp_accior in user.groups.all():
47 liste = self.get_query_set().all()
48
49 ############################################
50 # TRAITEMENT ABF
51 ############################################
52 if grp_abf in user.groups.all():
53 liste = self.get_query_set().all()
54
55 ############################################
56 # TRAITEMENT HAUTE DIRECTION
57 ############################################
58 if grp_haute_direction in user.groups.all():
59 liste = self.get_query_set().all()
60
61 ############################################
62 # TRAITEMENT DRH
63 ############################################
64 if grp_drh in user.groups.all() or grp_drh2 in user.groups.all():
65 liste = self.get_query_set().all()
66
9817fed5
OL
67 if user.is_superuser:
68 liste = self.get_query_set().all()
69
3f5cbabe
OL
70 return liste
71
72
fc62be5d
EMS
73class ActifsQuerySet(QuerySet):
74
75 def _actifs(self, debut_field, fin_field, date_min=None, date_max=None,
76 annee=None):
77 qs = self
78 if annee:
79 janvier = date(annee, 1, 1)
80 decembre = date(annee, 12, 31)
81 date_min = max(janvier, date_min) if date_min else janvier
82 date_max = min(decembre, date_max) if date_max else decembre
83 if not date_min and not date_max:
84 date_min = date_max = date.today()
85 if date_min:
86 qs = qs.filter(
87 Q(**{fin_field + '__gte': date_min}) |
88 Q(**{fin_field: None})
89 )
90 if date_max:
91 qs = qs.filter(
92 Q(**{debut_field + '__lte': date_max}) |
93 Q(**{debut_field: None})
94 )
95 return qs
96
97 def actifs(self, *args, **kwargs):
98 return self._actifs('date_debut', 'date_fin', *args, **kwargs)
99
100
101class PosteQuerySet(NoDeleteQuerySet, ActifsQuerySet):
102 pass
103
104
a2c3ad52 105class PosteManager(SecurityManager, NoDeleteManager):
3f5cbabe
OL
106 prefixe_service = "service"
107 prefixe_implantation = "implantation__region"
108
fc62be5d
EMS
109 def get_query_set(self):
110 return PosteQuerySet(self.model).filter(supprime=False) \
111 .select_related('type_poste')
112
113 def actifs(self, *args, **kwargs):
114 return self.get_query_set().actifs(*args, **kwargs)
f614ca5c 115
3f5cbabe 116 def ma_region_ou_service(self, user):
f614ca5c 117 return super(PosteManager, self).ma_region_ou_service(user)
3f5cbabe 118
fc62be5d
EMS
119
120class DossierQuerySet(NoDeleteQuerySet, ActifsQuerySet):
121 pass
3f5cbabe
OL
122
123
a2c3ad52 124class DossierManager(SecurityManager, NoDeleteManager):
3f5cbabe
OL
125 prefixe_service = "poste__service"
126 prefixe_implantation = "poste__implantation__region"
127
128 def get_query_set(self):
fc62be5d
EMS
129 return DossierQuerySet(self.model) \
130 .filter(supprime=False) \
131 .select_related('poste', 'employe')
3f5cbabe 132
fc62be5d
EMS
133 def actifs(self, *args, **kwargs):
134 return self.get_query_set().actifs(*args, **kwargs)
135
136
137class RemunerationQuerySet(NoDeleteQuerySet, ActifsQuerySet):
138
139 def actifs(self, *args, **kwargs):
140 return self \
141 ._actifs('date_debut', 'date_fin', *args, **kwargs) \
142 ._actifs(
143 'dossier__date_debut', 'dossier__date_fin', *args, **kwargs
144 )
145
146
147class RemunerationManager(NoDeleteManager):
148
149 def get_query_set(self):
150 return RemunerationQuerySet(self.model).filter(supprime=False)
151
152 def actifs(self, *args, **kwargs):
153 return self.get_query_set().actifs(*args, **kwargs)
3f5cbabe
OL
154
155
fc62be5d
EMS
156class EmployeQuerySet(NoDeleteQuerySet, ActifsQuerySet):
157
6fb68b2f 158 def actifs(self, date_min=None, date_max=None, annee=None):
fc62be5d
EMS
159 return self._actifs('dossiers__date_debut', 'dossiers__date_fin') \
160 .distinct()
161
6fb68b2f
DB
162
163class EmployeManager(NoDeleteManager):
164 def get_query_set(self):
165 return EmployeQuerySet(self.model) \
166 .filter(supprime=False)
167
168 def actifs(self, *args, **kwargs):
169 return self.get_query_set().actifs(*args, **kwargs)
170
fc62be5d 171
3f5cbabe
OL
172class PosteComparaisonManager(SecurityManager):
173 use_for_related_fields = True
174 prefixe_implantation = "implantation__region"
175
4bdadf8b 176
3f5cbabe
OL
177class DossierComparaisonManager(SecurityManager):
178 use_for_related_fields = True
179 prefixe_implantation = "implantation__region"
4bdadf8b
OL
180
181
182class DeviseManager(NoDeleteManager):
7ba822a6 183 pass
4bdadf8b 184
fc62be5d 185
7ba822a6
OL
186class ServiceManager(NoDeleteManager):
187 pass
188
fc62be5d 189
7ba822a6
OL
190class TypeRemunerationManager(NoDeleteManager):
191 pass