cleanup and make rh/dae compliant
[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
71 def _actifs(self, debut_field, fin_field, date_min=None, date_max=None,
72 annee=None):
73 qs = self
74 if annee:
75 janvier = date(annee, 1, 1)
76 decembre = date(annee, 12, 31)
77 date_min = max(janvier, date_min) if date_min else janvier
78 date_max = min(decembre, date_max) if date_max else decembre
79 if not date_min and not date_max:
80 date_min = date_max = date.today()
81 if date_min:
82 qs = qs.filter(
83 Q(**{fin_field + '__gte': date_min}) |
84 Q(**{fin_field: None}))
85 if date_max:
86 qs = qs.filter(
87 Q(**{debut_field + '__lte': date_max}) |
88 Q(**{debut_field: None}))
89 return qs
90
91 def actifs(self, *args, **kwargs):
92 return self._actifs('date_debut', 'date_fin', *args, **kwargs)
93
94
95 class PosteQuerySet(ActifsQuerySet):
96 pass
97
98
99 class PosteManager(SecurityManager):
100 prefixe_service = "service"
101 prefixe_implantation = "implantation__region"
102
103 def get_query_set(self):
104 return PosteQuerySet(self.model).select_related('type_poste')
105
106 def actifs(self, *args, **kwargs):
107 return self.get_query_set().actifs(*args, **kwargs)
108
109 def ma_region_ou_service(self, user):
110 return super(PosteManager, self).ma_region_ou_service(user)
111
112
113 class DossierQuerySet(ActifsQuerySet):
114 pass
115
116
117 class DossierManager(SecurityManager):
118 prefixe_service = "poste__service"
119 prefixe_implantation = "poste__implantation__region"
120
121 def get_query_set(self):
122 return DossierQuerySet(self.model) \
123 .select_related('poste', 'employe')
124
125 def actifs(self, *args, **kwargs):
126 return self.get_query_set().actifs(*args, **kwargs)
127
128 def sans_contrats_ou_echus(self, *args, **kwargs):
129 app = self.model._meta.app_label
130 sql = """
131 SELECT D.id FROM %(app)s_dossier AS D
132 LEFT JOIN %(app)s_contrat AS C ON D.id = C.dossier AND C.date_debut =
133 (SELECT MAX(date_debut) FROM %(app)s_contrat WHERE dossier=D.id)
134 WHERE (D.date_fin > '%(today)s' OR D.date_fin IS NULL)
135 AND (C.id IS NULL OR C.date_fin < '%(today)s')
136 """ % {'today': date.today(), 'app': app}
137 ids = [d.id for d in self.raw(sql)]
138 return self.get_query_set().filter(id__in=ids).filter(*args, **kwargs)
139
140
141 class RemunerationQuerySet(ActifsQuerySet):
142
143 def actifs(self, *args, **kwargs):
144 return self \
145 ._actifs('date_debut', 'date_fin', *args, **kwargs) \
146 ._actifs(
147 'dossier__date_debut', 'dossier__date_fin',
148 *args, **kwargs)
149
150
151 class RemunerationManager(models.Manager):
152
153 def get_query_set(self):
154 return RemunerationQuerySet(self.model)
155
156 def actifs(self, *args, **kwargs):
157 return self.get_query_set().actifs(*args, **kwargs)
158
159
160 class EmployeQuerySet(ActifsQuerySet):
161
162 def actifs(self, date_min=None, date_max=None, annee=None):
163 return self \
164 ._actifs('rh_dossiers__date_debut', 'rh_dossiers__date_fin') \
165 .distinct()
166
167
168 class EmployeManager(models.Manager):
169
170 def get_query_set(self):
171 return EmployeQuerySet(self.model)
172
173 def actifs(self, *args, **kwargs):
174 return self.get_query_set().actifs(*args, **kwargs)
175
176
177 class PosteComparaisonManager(SecurityManager):
178 use_for_related_fields = True
179 prefixe_implantation = "implantation__region"
180
181
182 class DossierComparaisonManager(SecurityManager):
183 use_for_related_fields = True
184 prefixe_implantation = "implantation__region"
185
186
187 class DeviseManager(models.Manager):
188 pass
189
190
191 class ServiceManager(models.Manager):
192 pass
193
194
195 class TypeRemunerationManager(models.Manager):
196 pass