#3774
[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 ############################################
25f2c148
OL
55 # TRAITEMENT FINANCE
56 ############################################
57 if groups.FINANCE in groupes:
58 liste = self.get_query_set().all()
59
60 ############################################
3f5cbabe
OL
61 # TRAITEMENT HAUTE DIRECTION
62 ############################################
3383b2d1 63 if groups.HAUTE_DIRECTION in groupes:
3f5cbabe
OL
64 liste = self.get_query_set().all()
65
66 ############################################
67 # TRAITEMENT DRH
68 ############################################
3383b2d1 69 if groups.DRH_NIVEAU_1 in groupes or groups.DRH_NIVEAU_2 in groupes:
3f5cbabe
OL
70 liste = self.get_query_set().all()
71
9817fed5
OL
72 if user.is_superuser:
73 liste = self.get_query_set().all()
74
3f5cbabe
OL
75 return liste
76
77
c3550a05 78class ActifsQuerySet(QuerySet):
5e8b78d9
OL
79 debut_field = 'date_debut'
80 fin_field = 'date_fin'
c3550a05 81
a6ed66f9
OL
82 def _actifs(self, debut_field, fin_field, date_min=None, date_max=None,
83 annee=None):
5e8b78d9
OL
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 & (
a6ed66f9
OL
94 Q(**{fin_field + '__gte': date_min}) |
95 Q(**{fin_field: None}))
5e8b78d9
OL
96 if date_max:
97 q = q & (
a6ed66f9
OL
98 Q(**{debut_field + '__lte': date_max}) |
99 Q(**{debut_field: None}))
5e8b78d9 100
a6ed66f9 101 q_inconnus = self._inconnus(debut_field, fin_field)
5e8b78d9
OL
102 return q & ~q_inconnus
103
a6ed66f9
OL
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
5e8b78d9 115 def actifs(self, date_min=None, date_max=None, annee=None):
c3550a05 116 qs = self
5e8b78d9
OL
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):
a6ed66f9 121 return self._inconnus(self.debut_field, self.fin_field)
5e8b78d9
OL
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()
c3550a05
OL
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:
5e8b78d9 149 q = q & Q(**{self.debut_field + '__gt': date_min})
c3550a05 150 if date_max:
5e8b78d9
OL
151 q = q & Q(**{self.debut_field + '__gt': date_max})
152 return q
c3550a05 153
5e8b78d9
OL
154 def futurs(self, date_min=None, date_max=None, annee=None):
155 return self.get_q_futurs(date_min, date_max, annee)
c3550a05
OL
156
157
158class PosteQuerySet(ActifsQuerySet):
159 pass
160
161
45066657 162class PosteManager(SecurityManager):
3f5cbabe 163 prefixe_service = "service"
b0cf30b8 164 prefixe_implantation = "implantation__zone_administrative"
3f5cbabe 165
c3550a05
OL
166 def get_query_set(self):
167 return PosteQuerySet(self.model).select_related('type_poste')
168
3f5cbabe 169 def ma_region_ou_service(self, user):
f614ca5c 170 return super(PosteManager, self).ma_region_ou_service(user)
3f5cbabe 171
c3d9c01c
OL
172 def actifs(self, *args, **kwargs):
173 return self.get_query_set().actifs(*args, **kwargs)
c3550a05
OL
174
175class DossierQuerySet(ActifsQuerySet):
176 pass
3f5cbabe
OL
177
178
45066657 179class DossierManager(SecurityManager):
3f5cbabe 180 prefixe_service = "poste__service"
b0cf30b8 181 prefixe_implantation = "poste__implantation__zone_administrative"
3f5cbabe
OL
182
183 def get_query_set(self):
c3550a05
OL
184 return DossierQuerySet(self.model) \
185 .select_related('poste', 'employe')
3f5cbabe 186
c3d9c01c
OL
187 def actifs(self, *args, **kwargs):
188 return self.get_query_set().actifs(*args, **kwargs)
189
a20fa2a4
OL
190 def sans_contrats_ou_echus(self, *args, **kwargs):
191 app = self.model._meta.app_label
192 sql = """
fceea089
OL
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}
a20fa2a4
OL
199 ids = [d.id for d in self.raw(sql)]
200 return self.get_query_set().filter(id__in=ids).filter(*args, **kwargs)
3f5cbabe 201
fceea089 202
c3550a05
OL
203class RemunerationQuerySet(ActifsQuerySet):
204
205 def actifs(self, *args, **kwargs):
a6ed66f9
OL
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)
c3550a05
OL
210
211
212class 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
221class EmployeQuerySet(ActifsQuerySet):
5e8b78d9
OL
222 debut_field = 'rh_dossiers__date_debut'
223 fin_field = 'rh_dossiers__date_fin'
c3550a05 224
6fb68b2f 225
c3550a05 226class EmployeManager(models.Manager):
45066657 227
6fb68b2f 228 def get_query_set(self):
f80b83c6 229 return EmployeQuerySet(self.model)
6fb68b2f
DB
230
231 def actifs(self, *args, **kwargs):
232 return self.get_query_set().actifs(*args, **kwargs)
233
45066657 234
5e8b78d9
OL
235class ContratQuerySet(ActifsQuerySet):
236 pass
237
238class ContratManager(models.Manager):
239
240 def get_query_set(self):
241 return ContratQuerySet(self.model)\
242 .select_related('dossier', 'dossier__poste')
243
244
3f5cbabe
OL
245class PosteComparaisonManager(SecurityManager):
246 use_for_related_fields = True
b0cf30b8 247 prefixe_implantation = "implantation__zone_administrative"
3f5cbabe 248
4bdadf8b 249
3f5cbabe
OL
250class DossierComparaisonManager(SecurityManager):
251 use_for_related_fields = True
b0cf30b8 252 prefixe_implantation = "implantation__zone_administrative"
4bdadf8b
OL
253
254
c3550a05 255class DeviseManager(models.Manager):
7ba822a6 256 pass
4bdadf8b 257
c3550a05
OL
258
259class ServiceManager(models.Manager):
7ba822a6
OL
260 pass
261
c3550a05 262
45066657 263class TypeRemunerationManager(models.Manager):
7ba822a6 264 pass