[#3165] Remplacé les filtres et protection par région pour des filtres et protections...
[auf_rh_dae.git] / project / rh / managers.py
1 # -*- encoding: utf-8 -*-
2
3 from datetime import date
4
5 from django.db import models
6 from django.db.models import Q
7 from django.db.models.query import QuerySet
8
9 from project import groups
10
11
12 class 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)
20 On s'intéresse aussi au groupe auquel appartient le user car
21 certains groupes peuvent tout voir.
22 """
23 employe = groups.get_employe_from_user(user)
24 groupes = [g.name for g in user.groups.all()]
25
26 ############################################
27 # TRAITEMENT NORMAL
28 ############################################
29 # REGION
30 q = Q(**{
31 self.prefixe_implantation:
32 employe.implantation.zone_administrative
33 })
34
35 # SERVICE
36 if self.prefixe_service \
37 and groups.SERVICE_UTILISATEURS in groupes:
38 q = q | Q(**{self.prefixe_service: employe.service})
39
40 liste = self.get_query_set().filter(q)
41
42 ############################################
43 # TRAITEMENT ACCIOR
44 ############################################
45 if groups.ACCIOR in groupes:
46 liste = self.get_query_set().all()
47
48 ############################################
49 # TRAITEMENT ABF
50 ############################################
51 if groups.ABF in groupes:
52 liste = self.get_query_set().all()
53
54 ############################################
55 # TRAITEMENT HAUTE DIRECTION
56 ############################################
57 if groups.HAUTE_DIRECTION in groupes:
58 liste = self.get_query_set().all()
59
60 ############################################
61 # TRAITEMENT DRH
62 ############################################
63 if groups.DRH_NIVEAU_1 in groupes or groups.DRH_NIVEAU_2 in groupes:
64 liste = self.get_query_set().all()
65
66 if user.is_superuser:
67 liste = self.get_query_set().all()
68
69 return liste
70
71
72 class 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}) |
87 Q(**{fin_field: None}))
88 if date_max:
89 qs = qs.filter(
90 Q(**{debut_field + '__lte': date_max}) |
91 Q(**{debut_field: None}))
92 return qs
93
94 def actifs(self, *args, **kwargs):
95 return self._actifs('date_debut', 'date_fin', *args, **kwargs)
96
97
98 class PosteQuerySet(ActifsQuerySet):
99 pass
100
101
102 class PosteManager(SecurityManager):
103 prefixe_service = "service"
104 prefixe_implantation = "implantation__zone_administrative"
105
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)
111
112 def ma_region_ou_service(self, user):
113 return super(PosteManager, self).ma_region_ou_service(user)
114
115
116 class DossierQuerySet(ActifsQuerySet):
117 pass
118
119
120 class DossierManager(SecurityManager):
121 prefixe_service = "poste__service"
122 prefixe_implantation = "poste__implantation__zone_administrative"
123
124 def get_query_set(self):
125 return DossierQuerySet(self.model) \
126 .select_related('poste', 'employe')
127
128 def actifs(self, *args, **kwargs):
129 return self.get_query_set().actifs(*args, **kwargs)
130
131 def sans_contrats_ou_echus(self, *args, **kwargs):
132 app = self.model._meta.app_label
133 sql = """
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}
140 ids = [d.id for d in self.raw(sql)]
141 return self.get_query_set().filter(id__in=ids).filter(*args, **kwargs)
142
143
144 class RemunerationQuerySet(ActifsQuerySet):
145
146 def actifs(self, *args, **kwargs):
147 return self \
148 ._actifs('date_debut', 'date_fin', *args, **kwargs) \
149 ._actifs(
150 'dossier__date_debut', 'dossier__date_fin',
151 *args, **kwargs)
152
153
154 class 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
163 class EmployeQuerySet(ActifsQuerySet):
164
165 def actifs(self, date_min=None, date_max=None, annee=None):
166 return self \
167 ._actifs('rh_dossiers__date_debut', 'rh_dossiers__date_fin') \
168 .distinct()
169
170
171 class EmployeManager(models.Manager):
172
173 def get_query_set(self):
174 return EmployeQuerySet(self.model)
175
176 def actifs(self, *args, **kwargs):
177 return self.get_query_set().actifs(*args, **kwargs)
178
179
180 class PosteComparaisonManager(SecurityManager):
181 use_for_related_fields = True
182 prefixe_implantation = "implantation__zone_administrative"
183
184
185 class DossierComparaisonManager(SecurityManager):
186 use_for_related_fields = True
187 prefixe_implantation = "implantation__zone_administrative"
188
189
190 class DeviseManager(models.Manager):
191 pass
192
193
194 class ServiceManager(models.Manager):
195 pass
196
197
198 class TypeRemunerationManager(models.Manager):
199 pass