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