Masse salariale: debut exoprt vers ods
authorJean-Philippe Caissy <jpcaissy@piji.ca>
Mon, 26 Mar 2012 13:31:22 +0000 (08:31 -0500)
committerJean-Philippe Caissy <jpcaissy@piji.ca>
Mon, 26 Mar 2012 13:31:22 +0000 (08:31 -0500)
buildout.cfg
project/rh/masse_salariale.py
project/rh/ods.py [new file with mode: 0644]
project/rh/views.py

index 635c775..b52b7f1 100644 (file)
@@ -43,6 +43,7 @@ eggs =
     django-qbe
     django-urldecorators
     pygraphviz
+    odfpy
 
 [versions]
 django-admin-tools = 0.4.1
@@ -62,6 +63,7 @@ django-sendfile = 0.2.1
 django-alphafilter = 0.5.3auf4
 django-urldecorators = 0.3
 pygraphviz = 0.36
+odfpy = 0.9.4
 
 [django]
 recipe = auf.recipe.django
index 0a9e596..2e5cba8 100644 (file)
@@ -8,6 +8,7 @@ import itertools
 from django.db.models import Q
 
 from datamaster_modeles import models as ref
+import rh.ods as ods
 import rh.models as rh
 
 
@@ -43,7 +44,8 @@ TYPE_NATURE_CHARGES = u"Charges"
 class MasseSalariale():
     """ Rapport de la masse salariale. """
 
-    def __init__(self, date_debut, date_fin, custom_filter=None):
+    def __init__(self, date_debut, date_fin, custom_filter=None,
+            ne_pas_grouper=False):
         """ date_debut: date de début pour les données temporelles
         date_fin: idem
         custom_filter: dictionnaire des paramètres à passer au queryset.
@@ -75,17 +77,27 @@ class MasseSalariale():
                         "dossier", "dossier_employe", "dossier_poste", "type"
                         )
 
-        employes = {}
+        contenu = {}
+
+        lineariser_dossiers = not ne_pas_grouper
+
         for r in remunerations:
-            if r.dossier.employe_id not in employes:
-                employes[r.dossier.employe_id] = {
+            if lineariser_dossiers:
+                key = r.dossier.employe_id
+            else:
+                key = r.dossier_id
+
+            if key not in contenu:
+                contenu[key] = {
                     'dossiers': set(),
                     'remunerations': []
                 }
-            employes[r.dossier.employe_id]['remunerations'].append(r)
-            employes[r.dossier.employe_id]['dossiers'].add(r.dossier)
-
-        self.employes = employes
+            if lineariser_dossiers:
+                contenu[key]['remunerations'].append(r)
+            else:
+                if r.dossier_id == key:
+                    contenu[key]['remunerations'].append(r)
+            contenu[key]['dossiers'].add(r.dossier)
 
         self.rapport = []
 
@@ -150,10 +162,13 @@ class MasseSalariale():
         grand_total = 0.0
         grand_total_euro = 0.0
 
-        for item in self.employes.values():
+        for item in contenu.values():
             dossiers = item['dossiers']
             remuns = item['remunerations']
 
+            if len(dossiers) > 1:
+                print dossiers[0].employe_id
+
             dossier = list(dossiers)[0]
             for d in dossiers:
                 if d.principal:
@@ -316,9 +331,6 @@ class MasseSalariale():
                     round(valeur_point.valeur * int(coefficient) * regime, 2) \
                     if valeur_point and coefficient and regime else "")
 
-            if dossier.employe_id == 2046:
-                import pdb; pdb.set_trace()
-
             rapport_nombre_jours = (float(date_delta.days)
                     / rapport_date_delta.days)
             item_rapport = {
@@ -401,7 +413,7 @@ class MasseSalariale():
                     item_rapport[key] = round(item_rapport[key], 2)
 
             grand_total += round(masse_salariale, 2)
-            grand_total_euro += round(masse_salariale_euro.montant * regime 
+            grand_total_euro += round(masse_salariale_euro.montant * regime
                             * (
                                     date_delta.days / rapport_date_delta.days
                             ), 2)
@@ -478,3 +490,7 @@ class MasseSalariale():
             csv_writer.writerow(
                     [unicode(r).encode('utf-8') for r in values]
             )
+
+    def ods(self):
+        self.ods = ods.OpenDocumentSpreadsheet()
+        self.ods
diff --git a/project/rh/ods.py b/project/rh/ods.py
new file mode 100644 (file)
index 0000000..442663e
--- /dev/null
@@ -0,0 +1,81 @@
+# encoding: utf-8
+
+from decimal import Decimal
+
+import odf.opendocument
+import odf.style
+import odf.table
+
+
+class Wrapper(object):
+
+    def __init__(self, *args, **kwargs):
+        self.__wrapped = self._wrapper_constructor(*args, **kwargs)
+
+    def __getattr__(self, attr):
+        return getattr(self.__wrapped, attr)
+
+
+class OpenDocumentSpreadsheet(Wrapper):
+    _wrapper_constructor = staticmethod(
+        odf.opendocument.OpenDocumentSpreadsheet
+    )
+
+    def __init__(self, *args, **kwargs):
+        super(OpenDocumentSpreadsheet, self).__init__(*args, **kwargs)
+        self._automatic_style_idx = 0
+
+    def add_table(self, **kwargs):
+        table = Table(**kwargs)
+        table._doc = self
+        self.spreadsheet.addElement(table)
+        return table
+
+    def add_automatic_style(self, **kwargs):
+        name = 'auto_style_%d' % self._automatic_style_idx
+        style = odf.style.Style(name=name, **kwargs)
+        self.automaticstyles.addElement(style)
+        self._automatic_style_idx += 1
+        return style
+
+
+class Table(Wrapper):
+    _wrapper_constructor = staticmethod(odf.table.Table)
+
+    def add_row(self, values=[], **kwargs):
+        row = TableRow(**kwargs)
+        for value in values:
+            row.add_cell(value)
+        self.addElement(row)
+        return row
+
+    def add_column(self, **kwargs):
+
+        # attributs appartenant à table-column-poperties
+        props = {}
+        for attr in ['columnwidth']:
+            if attr in kwargs:
+                props[attr] = kwargs.pop(attr)
+
+        if props:
+            style = self._doc.add_automatic_style(family='table-column')
+            style.addElement(odf.style.TableColumnProperties(**props))
+            kwargs['stylename'] = style.getAttribute('name')
+        col = odf.table.TableColumn(**kwargs)
+        self.addElement(col)
+        return col
+
+
+class TableRow(Wrapper):
+    _wrapper_constructor = staticmethod(odf.table.TableRow)
+
+    def add_cell(self, value=None, **kwargs):
+        if value:
+            if isinstance(value, basestring):
+                kwargs['stringvalue'] = value
+            elif isinstance(value, (int, float, Decimal)):
+                kwargs['valuetype'] = 'float'
+                kwargs['value'] = float(value)
+        cell = odf.table.TableCell(**kwargs)
+        self.addElement(cell)
+        return cell
index 6553732..1ba75f7 100644 (file)
@@ -214,7 +214,8 @@ def rapports_masse_salariale(request):
             'headers': [],
     }
     if date_debut or date_fin:
-        masse = MasseSalariale(date_debut, date_fin, custom_filter)
+        masse = MasseSalariale(date_debut, date_fin, custom_filter,
+                request.GET.get('ne_pas_grouper', False))
         if masse.rapport:
             c['rapport'] = masse.rapport
             headers = masse.headers