transparence.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. parser.add_argument('--methodes', action='store_true')
  14. def handle(self, *args, **options):
  15. if options['services']:
  16. self.handle_services()
  17. if options['adhesions']:
  18. self.handle_adhesions()
  19. if options['methodes']:
  20. self.handle_methodes()
  21. def handle_services(self):
  22. ttnn = Adhesion.objects.get(id=100)
  23. total = [0, 0, 0, 0, 0, 0, 0]
  24. lines = []
  25. for service_type in ServiceType.objects.exclude(name='Contribution'):
  26. ntotal = ninf = nadh = npay = nfree = nmiss = income = 0
  27. for service in service_type.services.filter(get_active_filter('allocation')).order_by('pk').distinct():
  28. ntotal += 1
  29. if service.adhesion == ttnn:
  30. ninf += 1
  31. continue
  32. nadh += 1
  33. contrib = service.contribution.get_current_payment()
  34. if not contrib or contrib.payment_method == PaymentUpdate.STOP:
  35. nmiss += 1
  36. elif contrib.payment_method == PaymentUpdate.FREE:
  37. nfree += 1
  38. else:
  39. npay += 1
  40. income += float(contrib.amount) / contrib.period
  41. assert(ntotal == ninf + nadh)
  42. assert(nadh == npay + nfree + nmiss)
  43. total = list(map(add, total, [ntotal, ninf, nadh, npay, nfree, nmiss, income]))
  44. lines += [(str(service_type), ntotal, ninf, nadh, npay, nfree, nmiss, income)]
  45. self.stdout.write("%-18s%12s%12s%14s%12s%12s%12s%12s%12s%12s" % ('Service', 'total', 'infra', 'adhérent·e·s', 'gratuits', 'manquants', 'payés', 'euros', 'moyenne', 'pourcent'))
  46. lines += [('TOTAL',) + tuple(total)]
  47. total_income = total[6]
  48. for service_type, ntotal, ninf, nadh, npay, nfree, nmiss, income in lines:
  49. if total_income:
  50. percent = income / total_income * 100
  51. else:
  52. percent = 0
  53. if npay:
  54. moy = '%12.2f' % (income / npay)
  55. else:
  56. moy = '%12s' % '-'
  57. self.stdout.write("%-18s%12d%12d%14d%12d%12d%12d%12.2f%s%12.1f" % (service_type, ntotal, ninf, nadh, nfree, nmiss, npay, income, moy, percent))
  58. def handle_adhesions(self):
  59. adhesions = Adhesion.objects.filter(Q(active__isnull=True) | Q(active=True))
  60. nadh = adhesions.count()
  61. pmiss, pgra, ppay, income = 0, 0, 0, 0
  62. payments = map(lambda adh: adh.membership.get_current_payment(), adhesions)
  63. for payment in payments:
  64. if payment is None:
  65. pmiss += 1
  66. elif payment.payment_method == PaymentUpdate.FREE:
  67. pgra += 1
  68. else:
  69. assert(payment.payment_method != PaymentUpdate.STOP)
  70. ppay += 1
  71. income += float(payment.amount) / payment.period
  72. self.stdout.write("%12s%12s%12s%12s%12s%12s" % ('Adhesions', 'gratuites', 'manquantes', 'payées', 'euros', 'moyenne'))
  73. self.stdout.write("%12d%12d%12d%12d%12.2f%12.2f" % (nadh, pgra, pmiss, ppay, income, 12 * income / ppay))
  74. def handle_methodes(self):
  75. prelevement, virement, facture = 0, 0, 0
  76. for payment in RecurringPayment.objects.all():
  77. payment = payment.get_current_payment()
  78. if not payment:
  79. continue
  80. if payment.payment_method == PaymentUpdate.DEBIT:
  81. prelevement += payment.amount / payment.period
  82. elif payment.payment_method == PaymentUpdate.TRANSFER:
  83. virement += payment.amount / payment.period
  84. elif payment.payment_method == PaymentUpdate.INVOICE:
  85. facture += payment.amount / payment.period
  86. self.stdout.write("%12s%12s%12s%12s" % ('prélèvement', 'virement', 'facture', 'total'))
  87. self.stdout.write("%12.2f%12.2f%12.2f%12.2f" % (prelevement, virement, facture, prelevement + virement + facture))