Masse salariale: affichage des dossiers sans remuneratino
authorJean-Philippe Caissy <jpcaissy@piji.ca>
Mon, 2 Apr 2012 18:54:22 +0000 (13:54 -0500)
committerJean-Philippe Caissy <jpcaissy@piji.ca>
Mon, 2 Apr 2012 18:54:22 +0000 (13:54 -0500)
project/rh/masse_salariale.py

index fd64814..4423ddd 100644 (file)
@@ -6,6 +6,7 @@ import StringIO
 import itertools
 
 from django.db.models import Q
+from django.db.models import Count
 
 from datamaster_modeles import models as ref
 import rh.ods as ods
@@ -76,12 +77,21 @@ class MasseSalariale():
 
         if custom_filter:
             remunerations = remunerations.filter(**custom_filter)
+        self.custom_filter = custom_filter
 
         remunerations = remunerations.exclude(supprime=True) \
                 .select_related(
                         "dossier", "dossier_employe", "dossier_poste", "type"
                         )
 
+        custom_filter = {}
+        for k, v in self.custom_filter.items():
+            custom_filter[k.replace('dossier__', '')] = v
+        count_dossiers_by_employe = dict((d['id'], d['count']) for d in 
+                rh.Dossier.objects.filter(q_range).values('id') \
+                .filter(**custom_filter) \
+                .annotate(count=Count('employe')))
+
         contenu = {}
 
         lineariser_dossiers = not ne_pas_grouper
@@ -223,6 +233,33 @@ class MasseSalariale():
         grand_total = 0.0
         grand_total_euro = 0.0
 
+        if not lineariser_dossiers:
+            for dossier_id, count in count_dossiers_by_employe.items():
+                if dossier_id not in contenu or \
+                    len(contenu[dossier_id]['dossiers']) != count:
+                    not_in = []
+                    if dossier_id in contenu:
+                        for d in contenu[dossier_id]['dossiers']:
+                            not_in.append(d.id)
+                    custom_filter = {}
+                    for k, v in self.custom_filter.items():
+                        custom_filter[k.replace('dossier__', '')] = v
+
+                    employe_id = rh.Dossier.objects.values('employe').filter(
+                            id=dossier_id).all()[0]['employe']
+                    dossiers = rh.Dossier.objects.filter(q_range) \
+                            .filter(employe=employe_id) \
+                            .filter(**custom_filter) \
+                            .all()
+                    for d in dossiers:
+                        if d.id not in contenu:
+                            contenu[d.id] = {
+                                    'dossiers': set((d,)),
+                                    'remunerations': []
+                            }
+                        else:
+                            contenu[d.id]['dossiers'].add(d)
+
         for item in contenu.values():
             dossiers = item['dossiers']
             remuns = item['remunerations']
@@ -254,7 +291,10 @@ class MasseSalariale():
                 pays_list[dossier.poste.implantation.adresse_physique_pays.id]
 
             #on détermine si les rémunérations sont tous dans la même devise
-            devise = remuns[0].devise
+            try:
+                devise = remuns[0].devise
+            except IndexError:
+                devise = self.devise_base
             meme_devise = True
             for r in remuns[1:]:
                 if devise != r.devise:
@@ -397,7 +437,7 @@ class MasseSalariale():
             total_indemnites = sum(indemnites.values())
 
             masse_salariale_euro = rh.Remuneration(montant=masse_salariale,
-                        devise=remuns[0].devise)
+                        devise=devise)
             self.convertir(masse_salariale_euro)
 
             if dossier.classement and dossier.classement.coefficient:
@@ -437,7 +477,7 @@ class MasseSalariale():
                     'date_debut': d_date_debut or "",
                     'date_fin': d_date_fin or "",
                     'nb_jours': dossier_date_delta.days,
-                    'devise': remuns[0].devise.code,
+                    'devise': devise.code,
                     'salaire_bstg_annuel': bstg_remun.montant \
                             if bstg_remun else "",
                     'salaire_bstg_total': bstg_remun_euro.montant \