transparence.py 3.3 KB

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