from django.core.management.base import BaseCommand, CommandError from django.db.models import Q from adhesions.models import Adhesion from services.models import Service, ServiceType, IPResource, Route from banking.models import RecurringPayment, PaymentUpdate from operator import add from djadhere.utils import get_active_filter class Command(BaseCommand): help = 'Afficher les statistiques financières' def add_arguments(self, parser): parser.add_argument('--services', action='store_true') parser.add_argument('--adhesions', action='store_true') def handle(self, *args, **options): if options['services']: self.handle_services() if options['adhesions']: self.handle_adhesions() def handle_services(self): ttnn = Adhesion.objects.get(id=100) total = [0, 0, 0, 0, 0, 0, 0] lines = [] for service_type in ServiceType.objects.all(): ntotal = ninf = nadh = npay = nfree = nmiss = income = 0 for service in service_type.services.all(): if not service.is_active(): continue ntotal += 1 if service.adhesion == ttnn: ninf += 1 continue nadh += 1 contrib = service.contribution.current if not contrib or contrib.payment_method == PaymentUpdate.STOP: nmiss += 1 elif contrib.payment_method == PaymentUpdate.FREE: nfree += 1 else: npay += 1 income += float(contrib.amount) / contrib.period assert(ntotal == ninf + nadh) assert(nadh == npay + nfree + nmiss) total = list(map(add, total, [ntotal, ninf, nadh, npay, nfree, nmiss, income])) lines += [(str(service_type), ntotal, ninf, nadh, npay, nfree, nmiss, income)] self.stdout.write("%-18s%12s%12s%12s%12s%12s%12s%12s" % ('Service', 'total', 'infra', 'adhérents', 'gratuits', 'payés', 'euros', 'pourcent')) lines += [('TOTAL',) + tuple(total)] total_income = total[6] for service_type, ntotal, ninf, nadh, npay, nfree, nmiss, income in lines: if total_income: percent = income / total_income * 100 else: percent = 0 self.stdout.write("%-18s%12d%12d%12d%12d%12d%12.2f%12.1f" % (service_type, ntotal, ninf, nadh, nfree + nmiss, npay, income, percent)) def handle_adhesions(self): adhesions = Adhesion.objects.filter(Q(active__isnull=True) | Q(active=True)) nadh = adhesions.count() pmiss, pgra, ppay, income = 0, 0, 0, 0 payments = map(lambda adh: adh.membership.current, adhesions) for payment in payments: if payment is None: pmiss += 1 elif payment.payment_method == PaymentUpdate.FREE: pgra += 1 else: assert(payment.payment_method != PaymentUpdate.STOP) ppay += 1 income += float(payment.amount) / payment.period self.stdout.write("%12s%12s%12s%12s" % ('Adhesions', 'gratuites', 'payées', 'euros')) self.stdout.write("%12d%12d%12d%12d" % (nadh, pgra + pmiss, ppay, income))