Masse salariale WIP et bugfix
[auf_rh_dae.git] / project / rh / masse_salariale.py
index 9e1ba89..9db560b 100644 (file)
@@ -1,11 +1,12 @@
 # -*- encoding: utf-8 -*-
 import time
 import datetime
+import csv
+import StringIO
 
 from django.db.models import Q
 
 from datamaster_modeles import models as ref
-
 import rh.models as rh
 
 
@@ -26,12 +27,14 @@ TYPE_PRIME_INTERIM = (19,)
 TYPE_PRIME_INSTALLATION = (13,)
 TYPE_PRIME_DEMENAG = (15,)
 TYPE_PRIME_AVION = (14,)
-TYPE_NATURE_PAIEMENT = u"Accessoire" 
+TYPE_NATURE_PAIEMENT = u"Accessoire"
+TYPE_NATURE_CHARGES = u"Charges"
+
 
 class MasseSalariale():
     """ Rapport de la masse salariale. """
 
-    def __init__(self, date_debut, date_fin):
+    def __init__(self, date_debut, date_fin, custom_filter=None):
         """ date_debut: date de début pour les données temporelles
         date_fin: idem
         """
@@ -50,8 +53,14 @@ class MasseSalariale():
         q_range_d = self.build_qs("dossier__date_", date_debut, date_fin)
         remunerations = rh.Remuneration.objects.filter(q_range) \
                 .filter(q_range_d) \
-                .exclude(supprime=True) \
-                .select_related("dossier", "dossier_employe", "dossier_poste", "type")
+
+        if custom_filter:
+            remunerations = remunerations.filter(**custom_filter)
+
+        remunerations = remunerations.exclude(supprime=True) \
+                .select_related(
+                        "dossier", "dossier_employe", "dossier_poste", "type"
+                        )
 
         employes = {}
         for r in remunerations:
@@ -122,9 +131,14 @@ class MasseSalariale():
             bstg_remun = None
             if bstg_dossier:
                 for r in bstg_dossier.rh_remunerations.all():
-                    if r.type in TYPE_REMUN_BSTG:
+                    if r.type.id in TYPE_REMUN_BSTG:
                         bstg_remun = r
 
+            if bstg_remun:
+                bstg_remun_euro = bstg_remun
+                self.convertir(bstg_remun_euro)
+                print bstg_remun.devise, bstg_remun_euro.devise
+
             salaire_complement = 0.0
             salaire_base = 0.0
             indemnites = {
@@ -143,6 +157,7 @@ class MasseSalariale():
                     'avion': 0.0,
                     'autre': 0.0,
                 }
+            charges = 0.0
             for r in remuns:
                 montant = float(r.montant)
 
@@ -187,6 +202,9 @@ class MasseSalariale():
                         r.type.nature_remuneration == TYPE_NATURE_PAIEMENT:
                     primes['autre'] += montant
 
+                if r.type.nature_remuneration == TYPE_NATURE_CHARGES:
+                    charges += montant
+
             total_indemnites = sum(indemnites.values())
 
             #Calcul du nombre de jours pour ce dossier.
@@ -201,85 +219,104 @@ class MasseSalariale():
             else:
                 date_delta = rapport_date_delta
 
-            item_rapport = {
-                    'bureau': dossier.poste.implantation.region.code,
-                    'pays': pays,
-                    'implantation': dossier.poste.implantation.nom_court,
-                    'type_implantation': dossier.poste.implantation.type,
-                    #'imputation': None,
-                    'valeur_point':
+            masse_salariale = (salaire_base + total_indemnites + \
+                            sum(primes.values()) + charges)
+            masse_salariale_euro = rh.Remuneration(montant=masse_salariale, 
+                        devise=remuns[0].devise)
+            self.convertir(masse_salariale_euro)
+
+            item_rapport = (
+                    ('bureau', dossier.poste.implantation.region.code),
+                    ('pays', unicode(pays)),
+                    ('implantation', dossier.poste.implantation.nom_court),
+                    ('type_implantation', dossier.poste.implantation.type),
+                    #'imputation', None),
+                    ('valeur_point',
                         #todo valeur du point si pas présent
                         valeurs_point_par_imp.get(
                             dossier.poste.implantation_id
-                        ),
-                    'numero_employe': dossier.employe_id,
-                    'nom': dossier.employe.nom.upper(),
-                    'prenom': dossier.employe.prenom,
-                    'type_de_poste': dossier.poste.type_poste.nom,
-                    'intitule_de_poste': dossier.poste.nom,
-                    'niveau': dossier.classement,
-                    'point': "%s" %
+                        ) or ""),
+                    ('numero_employe', dossier.employe_id),
+                    ('nom', dossier.employe.nom.upper()),
+                    ('prenom', dossier.employe.prenom),
+                    ('type_de_poste', dossier.poste.type_poste.nom),
+                    ('intitule_de_poste', dossier.poste.nom),
+                    ('niveau', unicode(dossier.classement)),
+                    ('point', "%s" %
                             dossier.classement.coefficient \
                                     if dossier.classement and
                                         dossier.classement.coefficient
-                                    else "",
-                    'regime_de_travail': "%s %%" % int(regime * 100),
-                    'local_expatrie': statut,
-                    'statut': dossier.statut.code,
-                    'date_fin_contrat': dossier.date_fin,
-                    'date_debut': d_date_debut,
-                    'date_fin': d_date_fin,
-                    'nb_jours': date_delta.days,
-                    'devise': remuns[0].devise,
-                    'salaire_bstg_annuel': bstg_remun.montant \
-                            if bstg_remun else None,
-                    'salaire_bstg_total':
-                                self.convertir(bstg_remun) \
-                                * regime * date_delta.days \
-                            if bstg_remun else None,
-                    'organisme_bstg': dossier.organisme_bstg,
-                    'salaire_base_brut': \
+                                    else ""),
+                    ('regime_de_travail', "%s %%" % int(regime * 100)),
+                    ('local_expatrie', statut),
+                    ('statut', dossier.statut.code),
+                    ('date_fin_contrat', dossier.date_fin or ""),
+                    ('date_debut', d_date_debut or ""),
+                    ('date_fin', d_date_fin or ""),
+                    ('nb_jours', date_delta.days),
+                    ('devise', remuns[0].devise),
+                    ('salaire_bstg_annuel', bstg_remun.montant \
+                            if bstg_remun else ""),
+                    ('salaire_bstg_total', bstg_remun_euro.montant \
+                            if bstg_remun else ""),
+                    ('organisme_bstg', dossier.organisme_bstg or ""),
+                    ('salaire_base_brut', \
                             salaire_base * regime * (
                                 date_delta.days / rapport_date_delta.days
-                            ),
-                    'salaire_complementaire': \
+                            )),
+                    ('salaire_complementaire', \
                             salaire_complement * regime * (
                                 date_delta.days / rapport_date_delta.days
-                            ),
-                    #'salaire_total': None
-                    'indemnite_fonctions': indemnites['fonc_resp'] * regime * \
-                            (date_delta.days / rapport_date_delta.days),
-                    'indemnite_expat': indemnites['expat'] * regime * \
-                            (date_delta.days / rapport_date_delta.days),
-                    'indemnite_logement': indemnites['logement'] * regime * \
-                            (date_delta.days / rapport_date_delta.days),
-                    'indemnites_transp': indemnites['transp'] * regime * \
-                            (date_delta.days / rapport_date_delta.days),
-                    'indemnites_13e': indemnites['13e'] * regime * \
-                            (date_delta.days / rapport_date_delta.days),
-                    'indemnites_autre': indemnites['autre_recurr'] * regime * \
-                            (date_delta.days / rapport_date_delta.days),
-                    'indemnites_total': total_indemnites * regime * \
-                            (date_delta.days / rapport_date_delta.days),
-                    'total_brut': (
+                            )),
+                    #'salaire_total', None
+                    ('indemnite_fonctions', indemnites['fonc_resp'] * \
+                            regime * \
+                            (date_delta.days / rapport_date_delta.days)),
+                    ('indemnite_expat', indemnites['expat'] * regime * \
+                            (date_delta.days / rapport_date_delta.days)),
+                    ('indemnite_logement', indemnites['logement'] * \
+                            regime * \
+                            (date_delta.days / rapport_date_delta.days)),
+                    ('indemnites_transp', indemnites['transp'] * regime * \
+                            (date_delta.days / rapport_date_delta.days)),
+                    ('indemnites_13e', indemnites['13e'] * regime * \
+                            (date_delta.days / rapport_date_delta.days)),
+                    ('indemnites_autre', indemnites['autre_recurr'] * \
+                            regime * \
+                            (date_delta.days / rapport_date_delta.days)),
+                    ('indemnites_total', total_indemnites * regime * \
+                            (date_delta.days / rapport_date_delta.days)),
+                    ('total_brut', (
                                 total_indemnites + salaire_base +
                                 salaire_complement
                             ) * regime * (
                                 date_delta.days / rapport_date_delta.days
-                            ),
-                    'prime_interim': primes['interim'] * regime * \
-                            (date_delta.days / rapport_date_delta.days),
-                    'prime_installation': primes['installation'] * regime * \
-                            (date_delta.days / rapport_date_delta.days),
-                    'prime_demenagement': primes['demenagement'] * regime * \
-                            (date_delta.days / rapport_date_delta.days),
-                    'prime_avion': primes['avion'] * regime * \
-                            (date_delta.days / rapport_date_delta.days),
-                    'prime_autre': primes['autre'] * regime * \
-                            (date_delta.days / rapport_date_delta.days),
-                    'prime_total': sum(primes.values()) * regime * \
-                            (date_delta.days / rapport_date_delta.days),
-                }
+                            )),
+                    ('prime_interim', primes['interim'] * regime * \
+                            (date_delta.days / rapport_date_delta.days)),
+                    ('prime_installation', primes['installation'] * regime * \
+                            (date_delta.days / rapport_date_delta.days)),
+                    ('prime_demenagement', primes['demenagement'] * regime * \
+                            (date_delta.days / rapport_date_delta.days)),
+                    ('prime_avion', primes['avion'] * regime * \
+                            (date_delta.days / rapport_date_delta.days)),
+                    ('prime_autre', primes['autre'] * regime * \
+                            (date_delta.days / rapport_date_delta.days)),
+                    ('prime_total', sum(primes.values()) * regime * \
+                            (date_delta.days / rapport_date_delta.days)),
+                    ('charges_patronales', charges),
+                    ('charges_patronales_%s' % self.annee, charges * regime * \
+                            (date_delta.days / rapport_date_delta.days)),
+                    ('masse_salariale', masse_salariale),
+                    ('masse_salariale_%s' % self.annee, masse_salariale * \
+                            regime * (
+                                    date_delta.days / rapport_date_delta.days
+                             )),
+                     ('masse_salariale__euro_%s' % self.annee, \
+                            masse_salariale_euro.montant * regime * (
+                                    date_delta.days / rapport_date_delta.days
+                            )),
+            )
 
             self.rapport.append(item_rapport)
 
@@ -338,3 +375,16 @@ class MasseSalariale():
             )[0]
             self.taux_change[devise.code] = t
         return self.taux_change[devise.code]
+
+    def csv(self):
+        self.csv_handle = StringIO.StringIO()
+        csv_writer = csv.writer(self.csv_handle, delimiter=",",
+                doublequote=False, escapechar="\\", quoting=csv.QUOTE_ALL,
+        )
+        header = [v[0] for v in self.rapport[0]]
+        csv_writer.writerow(header)
+        for row in self.rapport:
+            values = [v[1] for v in row]
+            csv_writer.writerow(
+                    [unicode(r).encode('utf-8') for r in values]
+            )