Browse Source

amélioration transparence

Élie Bouttier 7 years ago
parent
commit
47f3af0145
2 changed files with 29 additions and 19 deletions
  1. 26 19
      banking/management/commands/transparence.py
  2. 3 0
      services/models.py

+ 26 - 19
banking/management/commands/transparence.py

@@ -5,6 +5,7 @@ from services.models import Service, ServiceType, IPResource, Route
 from banking.models import RecurringPayment, PaymentUpdate
 
 from operator import add
+from djadhere.utils import get_active_filter
 
 
 class Command(BaseCommand):
@@ -22,33 +23,39 @@ class Command(BaseCommand):
 
     def handle_services(self):
         ttnn = Adhesion.objects.get(id=100)
-        total = [0, 0, 0, 0, 0, 0]
+        total = [0, 0, 0, 0, 0, 0, 0]
         lines = []
         for service_type in ServiceType.objects.all():
-            services = service_type.services.filter(active=True)
-            ntotal = services.count()
-            services = services.exclude(adhesion=ttnn)
-            nadh = services.count()
-            ninf = ntotal - nadh
-            payments = map(lambda service: service.contribution.get_last_validated_update(), services)
-            payments = list(filter(lambda payment: payment is not None and payment.payment_method != PaymentUpdate.STOP, payments))
-            ptotal = len(payments)
-            payments = list(filter(lambda payment: payment.payment_method != PaymentUpdate.FREE, payments))
-            ppay = len(payments)
-            pfree = ptotal - ppay
-            pmiss = nadh - ptotal
-            income = sum(map(lambda payment: float(payment.amount) / payment.period, payments))
-            total = list(map(add, total, [nadh, ninf, ppay, pfree, pmiss, income]))
-            lines += [(str(service_type), nadh, ninf, ppay, pfree, pmiss, income)]
-        self.stdout.write("%-16s%12s%12s%12s%12s%12s%12s" % ('Service', 'infra', 'adhérents', 'gratuit', 'manquant', 'euros', 'pourcent'))
+            ntotal = ninf = nadh = npay = nfree = nmiss = income = 0
+            for service in service_type.services.all():
+                if not service.is_active():
+                    continue
+                ntotal += 1
+                if service.adhesion == ttnn:
+                    ninf += 1
+                    continue
+                nadh += 1
+                contrib = service.contribution.get_last_validated_update()
+                if not contrib or contrib.payment_method == PaymentUpdate.STOP:
+                    nmiss += 1
+                elif contrib.payment_method == PaymentUpdate.FREE:
+                    nfree += 1
+                else:
+                    npay += 1
+                    income += float(contrib.amount) / contrib.period
+            assert(ntotal == ninf + nadh)
+            assert(nadh == npay + nfree + nmiss)
+            total = list(map(add, total, [ntotal, ninf, nadh, npay, nfree, nmiss, income]))
+            lines += [(str(service_type), ntotal, ninf, nadh, npay, nfree, nmiss, income)]
+        self.stdout.write("%-16s%12s%12s%12s%12s%12s%12s%12s" % ('Service', 'total', 'infra', 'adhérents', 'gratuit', 'manquant', 'euros', 'pourcent'))
         lines += [('TOTAL',) + tuple(total)]
         total_income = total[5]
-        for service_type, nadh, ninf, ppay, pfree, pmiss, income in lines:
+        for service_type, ntotal, ninf, nadh, npay, nfree, nmiss, income in lines:
             if total_income:
                 percent = income / total_income * 100
             else:
                 percent = 0
-            self.stdout.write("%-16s%12d%12d%12d%12d%12.2f%12.1f" % (service_type, ninf, nadh, pfree, pmiss, income, percent))
+            self.stdout.write("%-16s%12d%12d%12d%12d%12d%12.2f%12.1f" % (service_type, ntotal, ninf, nadh, nfree, nmiss, income, percent))
 
     def handle_adhesions(self):
         adhesions = Adhesion.objects.select_related('membership').exclude(active=False)

+ 3 - 0
services/models.py

@@ -137,6 +137,9 @@ class Service(models.Model):
         if self.label != '' and Service.objects.exclude(pk=self.pk).filter(service_type=self.service_type, label=self.label):
             raise ValidationError("Un service du même type existe déjà avec ce label.")
 
+    def is_active(self):
+        return self.allocations.filter(get_active_filter()).exists()
+
     def get_absolute_url(self):
         return reverse('admin:%s_%s_change' % (self._meta.app_label, self._meta.model_name), args=(self.pk,))