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