transparence.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. from django.core.management.base import BaseCommand, CommandError
  2. from adhesions.models import Adhesion
  3. from services.models import Service, ServiceType, IPResource, Route
  4. from banking.models import RecurringPayment, PaymentUpdate
  5. from operator import add
  6. class Command(BaseCommand):
  7. help = 'Afficher les statistiques financières'
  8. def add_arguments(self, parser):
  9. parser.add_argument('--services', action='store_true')
  10. parser.add_argument('--adhesions', action='store_true')
  11. def handle(self, *args, **options):
  12. if options['services']:
  13. self.handle_services()
  14. if options['adhesions']:
  15. self.handle_adhesions()
  16. def handle_services(self):
  17. ttnn = Adhesion.objects.get(id=100)
  18. total = [0, 0, 0, 0, 0, 0]
  19. lines = []
  20. for service_type in ServiceType.objects.all():
  21. services = service_type.services.filter(active=True)
  22. ntotal = services.count()
  23. services = services.exclude(adhesion=ttnn)
  24. nadh = services.count()
  25. ninf = ntotal - nadh
  26. payments = map(lambda service: service.contribution.get_last_validated_update(), services)
  27. payments = list(filter(lambda payment: payment is not None and payment.payment_method != PaymentUpdate.STOP, payments))
  28. ptotal = len(payments)
  29. payments = list(filter(lambda payment: payment.payment_method != PaymentUpdate.FREE, payments))
  30. ppay = len(payments)
  31. pfree = ptotal - ppay
  32. pmiss = nadh - ptotal
  33. income = sum(map(lambda payment: float(payment.amount) / payment.period, payments))
  34. total = list(map(add, total, [nadh, ninf, ppay, pfree, pmiss, income]))
  35. lines += [(str(service_type), nadh, ninf, ppay, pfree, pmiss, income)]
  36. self.stdout.write("%-16s%12s%12s%12s%12s%12s%12s" % ('Service', 'infra', 'adhérents', 'gratuit', 'manquant', 'euros', 'pourcent'))
  37. lines += [('TOTAL',) + tuple(total)]
  38. total_income = total[5]
  39. for service_type, nadh, ninf, ppay, pfree, pmiss, income in lines:
  40. if total_income:
  41. percent = income / total_income * 100
  42. else:
  43. percent = 0
  44. self.stdout.write("%-16s%12d%12d%12d%12d%12.2f%12.1f" % (service_type, ninf, nadh, pfree, pmiss, income, percent))
  45. def handle_adhesions(self):
  46. adhesions = Adhesion.objects.select_related('membership').exclude(active=False)
  47. nadh = adhesions.count()
  48. pmiss, pgra, ppay, income = 0, 0, 0, 0
  49. payments = map(lambda adh: adh.membership.get_last_validated_update(), adhesions)
  50. for payment in payments:
  51. if payment is None or payment.payment_method == PaymentUpdate.STOP:
  52. pmiss += 1
  53. elif payment.payment_method == PaymentUpdate.FREE:
  54. pgra += 1
  55. else:
  56. income += float(payment.amount) / payment.period
  57. self.stdout.write("%12s%12s%12s%12s" % ('Adhesions', 'gratuit', 'manquante', 'euros'))
  58. self.stdout.write("%12d%12d%12d%12d" % (nadh, pgra, pmiss, income))