c1a459a0d373974540464a736d371a12d6b9dd9d
[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 debut_field = 'date_debut'
74 fin_field = 'date_fin'
75
76 def _actifs(self, debut_field, fin_field, date_min=None, date_max=None,
77 annee=None):
78 q = Q()
79 if annee:
80 janvier = date(annee, 1, 1)
81 decembre = date(annee, 12, 31)
82 date_min = max(janvier, date_min) if date_min else janvier
83 date_max = min(decembre, date_max) if date_max else decembre
84 if not date_min and not date_max:
85 date_min = date_max = date.today()
86 if date_min:
87 q = q & (
88 Q(**{fin_field + '__gte': date_min}) |
89 Q(**{fin_field: None}))
90 if date_max:
91 q = q & (
92 Q(**{debut_field + '__lte': date_max}) |
93 Q(**{debut_field: None}))
94
95 q_inconnus = self._inconnus(debut_field, fin_field)
96 return q & ~q_inconnus
97
98 def _inconnus(self, debut_field, fin_field):
99 q = Q(**{
100 debut_field + '__isnull': True,
101 fin_field + '__isnull': True,
102 })
103 return q
104
105 def get_q_actifs(self, date_min=None, date_max=None, annee=None):
106 return self._actifs(self.debut_field, self.fin_field,
107 date_min, date_max, annee)
108
109 def actifs(self, date_min=None, date_max=None, annee=None):
110 qs = self
111 q = self.get_q_actifs(date_min, date_max, annee)
112 return qs.filter(q)
113
114 def get_q_inconnus(self, date_min=None, date_max=None, annee=None):
115 return self._inconnus(self.debut_field, self.fin_field)
116
117 def inconnus(self):
118 qs = self
119 q = self.get_q_inconnus()
120 return qs.filter(q)
121
122 def get_q_inactifs(self, date_min=None, date_max=None, annee=None):
123 actifs_ids = [e['id'] for e in self.actifs(date_min, date_max,
124 annee).values('id')]
125 q_non_actifs = ~Q(id__in=actifs_ids)
126 q_inconnus = self.get_q_inconnus()
127 q_futurs = self.get_q_futurs(date_min, date_max, annee)
128 return q_non_actifs & ~q_inconnus & ~q_futurs
129
130 def inactifs(self, date_min=None, date_max=None, annee=None):
131 return self.get_q_inactifs(date_min, date_max, annee)
132
133 def get_q_futurs(self, date_min=None, date_max=None, annee=None):
134 q = Q()
135 if annee:
136 janvier = date(annee, 1, 1)
137 decembre = date(annee, 12, 31)
138 date_min = max(janvier, date_min) if date_min else janvier
139 date_max = min(decembre, date_max) if date_max else decembre
140 if not date_min and not date_max:
141 date_min = date_max = date.today()
142 if date_min:
143 q = q & Q(**{self.debut_field + '__gt': date_min})
144 if date_max:
145 q = q & Q(**{self.debut_field + '__gt': date_max})
146 return q
147
148 def futurs(self, date_min=None, date_max=None, annee=None):
149 return self.get_q_futurs(date_min, date_max, annee)
150
151
152 class PosteQuerySet(ActifsQuerySet):
153 pass
154
155
156 class PosteManager(SecurityManager):
157 prefixe_service = "service"
158 prefixe_implantation = "implantation__zone_administrative"
159
160 def get_query_set(self):
161 return PosteQuerySet(self.model).select_related('type_poste')
162
163 def ma_region_ou_service(self, user):
164 return super(PosteManager, self).ma_region_ou_service(user)
165
166 def actifs(self, *args, **kwargs):
167 return self.get_query_set().actifs(*args, **kwargs)
168
169 class DossierQuerySet(ActifsQuerySet):
170 pass
171
172
173 class DossierManager(SecurityManager):
174 prefixe_service = "poste__service"
175 prefixe_implantation = "poste__implantation__zone_administrative"
176
177 def get_query_set(self):
178 return DossierQuerySet(self.model) \
179 .select_related('poste', 'employe')
180
181 def actifs(self, *args, **kwargs):
182 return self.get_query_set().actifs(*args, **kwargs)
183
184 def sans_contrats_ou_echus(self, *args, **kwargs):
185 app = self.model._meta.app_label
186 sql = """
187 SELECT D.id FROM %(app)s_dossier AS D
188 LEFT JOIN %(app)s_contrat AS C ON D.id = C.dossier AND C.date_debut =
189 (SELECT MAX(date_debut) FROM %(app)s_contrat WHERE dossier=D.id)
190 WHERE (D.date_fin > '%(today)s' OR D.date_fin IS NULL)
191 AND (C.id IS NULL OR C.date_fin < '%(today)s')
192 """ % {'today': date.today(), 'app': app}
193 ids = [d.id for d in self.raw(sql)]
194 return self.get_query_set().filter(id__in=ids).filter(*args, **kwargs)
195
196
197 class RemunerationQuerySet(ActifsQuerySet):
198
199 def actifs(self, *args, **kwargs):
200 remun_q = self._actifs('date_debut', 'date_fin', *args, **kwargs)
201 dossier_q = self._actifs('dossier__date_debut', 'dossier__date_fin',
202 *args, **kwargs)
203 return self.filter(remun_q & dossier_q)
204
205
206 class RemunerationManager(models.Manager):
207
208 def get_query_set(self):
209 return RemunerationQuerySet(self.model)
210
211 def actifs(self, *args, **kwargs):
212 return self.get_query_set().actifs(*args, **kwargs)
213
214
215 class EmployeQuerySet(ActifsQuerySet):
216 debut_field = 'rh_dossiers__date_debut'
217 fin_field = 'rh_dossiers__date_fin'
218
219
220 class EmployeManager(models.Manager):
221
222 def get_query_set(self):
223 return EmployeQuerySet(self.model)
224
225 def actifs(self, *args, **kwargs):
226 return self.get_query_set().actifs(*args, **kwargs)
227
228
229 class ContratQuerySet(ActifsQuerySet):
230 pass
231
232 class ContratManager(models.Manager):
233
234 def get_query_set(self):
235 return ContratQuerySet(self.model)\
236 .select_related('dossier', 'dossier__poste')
237
238
239 class PosteComparaisonManager(SecurityManager):
240 use_for_related_fields = True
241 prefixe_implantation = "implantation__zone_administrative"
242
243
244 class DossierComparaisonManager(SecurityManager):
245 use_for_related_fields = True
246 prefixe_implantation = "implantation__zone_administrative"
247
248
249 class DeviseManager(models.Manager):
250 pass
251
252
253 class ServiceManager(models.Manager):
254 pass
255
256
257 class TypeRemunerationManager(models.Manager):
258 pass