Masse salariale WIP et bugfix
[auf_rh_dae.git] / project / rh / masse_salariale.py
index 2812b70..9db560b 100644 (file)
@@ -34,7 +34,7 @@ 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
         """
@@ -53,7 +53,11 @@ 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) \
+
+        if custom_filter:
+            remunerations = remunerations.filter(**custom_filter)
+
+        remunerations = remunerations.exclude(supprime=True) \
                 .select_related(
                         "dossier", "dossier_employe", "dossier_poste", "type"
                         )
@@ -127,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 = {
@@ -212,95 +221,102 @@ class MasseSalariale():
 
             masse_salariale = (salaire_base + total_indemnites + \
                             sum(primes.values()) + charges)
-
-            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_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),
-                    '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 * \
+                            )),
+                    ('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)
 
@@ -362,7 +378,13 @@ class MasseSalariale():
 
     def csv(self):
         self.csv_handle = StringIO.StringIO()
-        csv_writer = csv.writer(self.csv_handle)
-        csv_writer.writerow(self.rapport[0].keys())
-        csv_writer.writerow([row.values() for row in self.rapport])
-
+        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]
+            )