Refactor pour somme remun
[auf_rh_dae.git] / project / rh / lib.py
index 416cf8b..279b0d2 100644 (file)
@@ -300,28 +300,13 @@ class DossierAdmin(AUFMetadataAdminMixin, ProtectRegionMixin, admin.ModelAdmin,)
             instance.save()
 
     def render_change_form(self, request, context, *args, **kwargs):
-        obj = kwargs['obj']
+        obj = kwargs.get('obj', None)
 
-        thisyear = datetime.date.today().year
-        thisyearfilter = Q(date_debut__year=thisyear) | Q(date_fin__year=thisyear)
-
-        remunnow = obj.rh_remuneration_remunerations.filter(thisyearfilter)
+        if not obj:
+            return super(DossierAdmin, self).render_change_form(request, context, *args, **kwargs)
 
-        remun_sum = 0
-        remun_sum_euro = 0
-        sums = defaultdict(int)
-        sums_euro = defaultdict(int)
-        for r in remunnow:
-            nature = r.type.nature_remuneration
-            sums[nature] += r.montant
-            sums_euro[nature] += r.montant_euro()
-            remun_sum += r.montant
-            remun_sum_euro += r.montant_euro()
 
-        remun = {}
-        sums = dict(sums)
-        for n, s in sums.iteritems():
-            remun[n] = [sums[n], sums_euro[n]]
+        remun, remun_sum, remun_sum_euro = calc_remun(obj)
 
         extra = {
             'remun': remun,
@@ -568,3 +553,28 @@ class ValeurPointAdmin(AUFMetadataAdminMixin, admin.ModelAdmin):
     def _devise_nom(self, obj):
         return obj.devise.nom
     _devise_nom.short_description = "Nom de la devise"
+
+
+def calc_remun(dossier):
+        thisyear = datetime.date.today().year
+        thisyearfilter = Q(date_debut__year=thisyear) | Q(date_fin__year=thisyear)
+
+        remunnow = dossier.rh_remuneration_remunerations.filter(thisyearfilter)
+
+        remun_sum = 0
+        remun_sum_euro = 0
+        sums = defaultdict(int)
+        sums_euro = defaultdict(int)
+        for r in remunnow:
+            nature = r.type.nature_remuneration
+            sums[nature] += r.montant
+            sums_euro[nature] += r.montant_euro()
+            remun_sum += r.montant
+            remun_sum_euro += r.montant_euro()
+
+        remun = {}
+        sums = dict(sums)
+        for n, s in sums.iteritems():
+            remun[n] = [sums[n], sums_euro[n]]
+
+        return remun, remun_sum, remun_sum_euro