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