transparence.py 3.2 KB

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