Fix for dossier principal
[auf_rh_dae.git] / project / rh / models.py
index df61f6c..e58945a 100644 (file)
@@ -591,14 +591,24 @@ class Employe(models.Model):
         q = search.get_q_temporel(self.rh_dossiers)
         return self.rh_dossiers.filter(q)
 
-    def dossier_principal(self):
+    def dossier_principal(self, year=None):
         """
         Retourne le dossier principal (ou le plus ancien si il y en a
         plusieurs)
         """
+        if not year:
+            year = date.today().year
+        year_start = date(year, 1, 1)
+        year_end = date(year, 12, 31)
+            
         try:
-            dossier = self.rh_dossiers \
-                    .filter(principal=True).order_by('date_debut')[0]
+            dossier = self.rh_dossiers.filter(
+                (Q(date_debut__lte=year_end, date_fin=None) |
+                 Q(date_debut=None, date_fin__gte=year_start) |
+                 Q(date_debut=None, date_fin__gte=year_start) |
+                 Q(date_debut__lte=year_end, date_fin__gte=year_start) |
+                 Q(date_debut=None, date_fin=None)) &
+                Q(principal=True)).order_by('date_debut')[0]
         except IndexError, Dossier.DoesNotExist:
             dossier = None
         return dossier