transparence.py 2.9 KB

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