test rapport masse salariale
[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 _actifs(self, debut_field, fin_field, date_min=None, date_max=None,
74 annee=None):
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 & (
85 Q(**{fin_field + '__gte': date_min}) |
86 Q(**{fin_field: None}))
87 if date_max:
88 q = q & (
89 Q(**{debut_field + '__lte': date_max}) |
90 Q(**{debut_field: None}))
91
92 q_inconnus = self._inconnus(debut_field, fin_field)
93 return q & ~q_inconnus
94
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
106 def actifs(self, date_min=None, date_max=None, annee=None):
107 qs = self
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):
112 return self._inconnus(self.debut_field, self.fin_field)
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()
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:
140 q = q & Q(**{self.debut_field + '__gt': date_min})
141 if date_max:
142 q = q & Q(**{self.debut_field + '__gt': date_max})
143 return q
144
145 def futurs(self, date_min=None, date_max=None, annee=None):
146 return self.get_q_futurs(date_min, date_max, annee)
147
148
149 class PosteQuerySet(ActifsQuerySet):
150 pass
151
152
153 class PosteManager(SecurityManager):
154 prefixe_service = "service"
155 prefixe_implantation = "implantation__region"
156
157 def get_query_set(self):
158 return PosteQuerySet(self.model).select_related('type_poste')
159
160 def ma_region_ou_service(self, user):
161 return super(PosteManager, self).ma_region_ou_service(user)
162
163 def actifs(self, *args, **kwargs):
164 return self.get_query_set().actifs(*args, **kwargs)
165
166 class DossierQuerySet(ActifsQuerySet):
167 pass
168
169
170 class DossierManager(SecurityManager):
171 prefixe_service = "poste__service"
172 prefixe_implantation = "poste__implantation__region"
173
174 def get_query_set(self):
175 return DossierQuerySet(self.model) \
176 .select_related('poste', 'employe')
177
178 def actifs(self, *args, **kwargs):
179 return self.get_query_set().actifs(*args, **kwargs)
180
181 def sans_contrats_ou_echus(self, *args, **kwargs):
182 app = self.model._meta.app_label
183 sql = """
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}
190 ids = [d.id for d in self.raw(sql)]
191 return self.get_query_set().filter(id__in=ids).filter(*args, **kwargs)
192
193
194 class RemunerationQuerySet(ActifsQuerySet):
195
196 def actifs(self, *args, **kwargs):
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)
201
202
203 class 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
212 class EmployeQuerySet(ActifsQuerySet):
213 debut_field = 'rh_dossiers__date_debut'
214 fin_field = 'rh_dossiers__date_fin'
215
216
217 class EmployeManager(models.Manager):
218
219 def get_query_set(self):
220 return EmployeQuerySet(self.model)
221
222 def actifs(self, *args, **kwargs):
223 return self.get_query_set().actifs(*args, **kwargs)
224
225
226 class ContratQuerySet(ActifsQuerySet):
227 pass
228
229 class ContratManager(models.Manager):
230
231 def get_query_set(self):
232 return ContratQuerySet(self.model)\
233 .select_related('dossier', 'dossier__poste')
234
235
236 class PosteComparaisonManager(SecurityManager):
237 use_for_related_fields = True
238 prefixe_implantation = "implantation__region"
239
240
241 class DossierComparaisonManager(SecurityManager):
242 use_for_related_fields = True
243 prefixe_implantation = "implantation__region"
244
245
246 class DeviseManager(models.Manager):
247 pass
248
249
250 class ServiceManager(models.Manager):
251 pass
252
253
254 class TypeRemunerationManager(models.Manager):
255 pass