[#3165] Remplacé les filtres et protection par région pour des filtres et protections...
[auf_rh_dae.git] / project / rh / managers.py
CommitLineData
3f5cbabe
OL
1# -*- encoding: utf-8 -*-
2
6fb68b2f 3from datetime import date
75f0e87b 4
3f5cbabe 5from django.db import models
fceea089 6from django.db.models import Q
c3550a05 7from django.db.models.query import QuerySet
75f0e87b 8
3383b2d1 9from project import groups
3f5cbabe
OL
10
11
12class SecurityManager(models.Manager):
13
14 prefixe_service = None
15 prefixe_implantation = None
16
17 def ma_region_ou_service(self, user):
18 """
19 Filtrage des postes en fonction du user connecté (region / service)
45066657
EMS
20 On s'intéresse aussi au groupe auquel appartient le user car
21 certains groupes peuvent tout voir.
3f5cbabe 22 """
3383b2d1
OL
23 employe = groups.get_employe_from_user(user)
24 groupes = [g.name for g in user.groups.all()]
3f5cbabe
OL
25
26 ############################################
27 # TRAITEMENT NORMAL
28 ############################################
29 # REGION
b0cf30b8
EMS
30 q = Q(**{
31 self.prefixe_implantation:
32 employe.implantation.zone_administrative
33 })
3f5cbabe
OL
34
35 # SERVICE
45066657 36 if self.prefixe_service \
3383b2d1 37 and groups.SERVICE_UTILISATEURS in groupes:
45066657 38 q = q | Q(**{self.prefixe_service: employe.service})
3f5cbabe
OL
39
40 liste = self.get_query_set().filter(q)
41
42 ############################################
43 # TRAITEMENT ACCIOR
44 ############################################
3383b2d1 45 if groups.ACCIOR in groupes:
3f5cbabe
OL
46 liste = self.get_query_set().all()
47
48 ############################################
49 # TRAITEMENT ABF
50 ############################################
3383b2d1 51 if groups.ABF in groupes:
3f5cbabe
OL
52 liste = self.get_query_set().all()
53
54 ############################################
55 # TRAITEMENT HAUTE DIRECTION
56 ############################################
3383b2d1 57 if groups.HAUTE_DIRECTION in groupes:
3f5cbabe
OL
58 liste = self.get_query_set().all()
59
60 ############################################
61 # TRAITEMENT DRH
62 ############################################
3383b2d1 63 if groups.DRH_NIVEAU_1 in groupes or groups.DRH_NIVEAU_2 in groupes:
3f5cbabe
OL
64 liste = self.get_query_set().all()
65
9817fed5
OL
66 if user.is_superuser:
67 liste = self.get_query_set().all()
68
3f5cbabe
OL
69 return liste
70
71
c3550a05
OL
72class ActifsQuerySet(QuerySet):
73
74 def _actifs(self, debut_field, fin_field, date_min=None, date_max=None,
75 annee=None):
76 qs = self
77 if annee:
78 janvier = date(annee, 1, 1)
79 decembre = date(annee, 12, 31)
80 date_min = max(janvier, date_min) if date_min else janvier
81 date_max = min(decembre, date_max) if date_max else decembre
82 if not date_min and not date_max:
83 date_min = date_max = date.today()
84 if date_min:
85 qs = qs.filter(
86 Q(**{fin_field + '__gte': date_min}) |
fceea089 87 Q(**{fin_field: None}))
c3550a05
OL
88 if date_max:
89 qs = qs.filter(
90 Q(**{debut_field + '__lte': date_max}) |
fceea089 91 Q(**{debut_field: None}))
c3550a05
OL
92 return qs
93
94 def actifs(self, *args, **kwargs):
95 return self._actifs('date_debut', 'date_fin', *args, **kwargs)
96
97
98class PosteQuerySet(ActifsQuerySet):
99 pass
100
101
45066657 102class PosteManager(SecurityManager):
3f5cbabe 103 prefixe_service = "service"
b0cf30b8 104 prefixe_implantation = "implantation__zone_administrative"
3f5cbabe 105
c3550a05
OL
106 def get_query_set(self):
107 return PosteQuerySet(self.model).select_related('type_poste')
108
109 def actifs(self, *args, **kwargs):
110 return self.get_query_set().actifs(*args, **kwargs)
f614ca5c 111
3f5cbabe 112 def ma_region_ou_service(self, user):
f614ca5c 113 return super(PosteManager, self).ma_region_ou_service(user)
3f5cbabe 114
c3550a05
OL
115
116class DossierQuerySet(ActifsQuerySet):
117 pass
3f5cbabe
OL
118
119
45066657 120class DossierManager(SecurityManager):
3f5cbabe 121 prefixe_service = "poste__service"
b0cf30b8 122 prefixe_implantation = "poste__implantation__zone_administrative"
3f5cbabe
OL
123
124 def get_query_set(self):
c3550a05
OL
125 return DossierQuerySet(self.model) \
126 .select_related('poste', 'employe')
3f5cbabe 127
c3550a05
OL
128 def actifs(self, *args, **kwargs):
129 return self.get_query_set().actifs(*args, **kwargs)
3f5cbabe 130
a20fa2a4
OL
131 def sans_contrats_ou_echus(self, *args, **kwargs):
132 app = self.model._meta.app_label
133 sql = """
fceea089
OL
134 SELECT D.id FROM %(app)s_dossier AS D
135 LEFT JOIN %(app)s_contrat AS C ON D.id = C.dossier AND C.date_debut =
136 (SELECT MAX(date_debut) FROM %(app)s_contrat WHERE dossier=D.id)
137 WHERE (D.date_fin > '%(today)s' OR D.date_fin IS NULL)
138 AND (C.id IS NULL OR C.date_fin < '%(today)s')
139 """ % {'today': date.today(), 'app': app}
a20fa2a4
OL
140 ids = [d.id for d in self.raw(sql)]
141 return self.get_query_set().filter(id__in=ids).filter(*args, **kwargs)
3f5cbabe 142
fceea089 143
c3550a05
OL
144class RemunerationQuerySet(ActifsQuerySet):
145
146 def actifs(self, *args, **kwargs):
147 return self \
148 ._actifs('date_debut', 'date_fin', *args, **kwargs) \
149 ._actifs(
fceea089
OL
150 'dossier__date_debut', 'dossier__date_fin',
151 *args, **kwargs)
c3550a05
OL
152
153
154class RemunerationManager(models.Manager):
155
156 def get_query_set(self):
157 return RemunerationQuerySet(self.model)
158
159 def actifs(self, *args, **kwargs):
160 return self.get_query_set().actifs(*args, **kwargs)
161
162
163class EmployeQuerySet(ActifsQuerySet):
45066657 164
6fb68b2f 165 def actifs(self, date_min=None, date_max=None, annee=None):
c3550a05
OL
166 return self \
167 ._actifs('rh_dossiers__date_debut', 'rh_dossiers__date_fin') \
168 .distinct()
169
6fb68b2f 170
c3550a05 171class EmployeManager(models.Manager):
45066657 172
6fb68b2f 173 def get_query_set(self):
f80b83c6 174 return EmployeQuerySet(self.model)
6fb68b2f
DB
175
176 def actifs(self, *args, **kwargs):
177 return self.get_query_set().actifs(*args, **kwargs)
178
45066657 179
3f5cbabe
OL
180class PosteComparaisonManager(SecurityManager):
181 use_for_related_fields = True
b0cf30b8 182 prefixe_implantation = "implantation__zone_administrative"
3f5cbabe 183
4bdadf8b 184
3f5cbabe
OL
185class DossierComparaisonManager(SecurityManager):
186 use_for_related_fields = True
b0cf30b8 187 prefixe_implantation = "implantation__zone_administrative"
4bdadf8b
OL
188
189
c3550a05 190class DeviseManager(models.Manager):
7ba822a6 191 pass
4bdadf8b 192
c3550a05
OL
193
194class ServiceManager(models.Manager):
7ba822a6
OL
195 pass
196
c3550a05 197
45066657 198class TypeRemunerationManager(models.Manager):
7ba822a6 199 pass