from django.core.management.base import BaseCommand, CommandError from adhesions.models import Adhesion from services.models import Service, ServiceType, IPResource, Route from banking.models import RecurringPayment, PaymentUpdate 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_income = 0 lines = [] for service_type in ServiceType.objects.all(): services = service_type.services.filter(active=True) ntotal = services.count() services = services.exclude(adhesion=ttnn) nadh = services.count() ninf = ntotal - nadh payments = map(lambda service: service.contribution.get_last_validated_update(), services) payments = list(filter(lambda payment: payment is not None and payment.payment_method != PaymentUpdate.STOP, payments)) ptotal = len(payments) payments = list(filter(lambda payment: payment.payment_method != PaymentUpdate.FREE, payments)) ppay = len(payments) pfree = ptotal - ppay pmiss = nadh - ptotal income = sum(map(lambda payment: float(payment.amount) / payment.period, payments)) total_income += income lines += [(str(service_type), nadh, ninf, ppay, pfree, pmiss, income)] self.stdout.write("%-16s%12s%12s%12s%12s%12s%12s" % ('Service', 'infra', 'adhérents', 'gratuit', 'manquant', 'euros', 'pourcent')) for service_type, nadh, ninf, ppay, pfree, pmiss, income in lines: if total_income: percent = income / total_income * 100 else: percent = 0 self.stdout.write("%-16s%12d%12d%12d%12d%12.2f%12.1f" % (service_type, ninf, nadh, pfree, pmiss, income, percent)) def handle_adhesions(self): adhesions = Adhesion.objects.select_related('membership').exclude(active=False) nadh = adhesions.count() pmiss, pgra, ppay, income = 0, 0, 0, 0 payments = map(lambda adh: adh.membership.get_last_validated_update(), adhesions) for payment in payments: if payment is None or payment.payment_method == PaymentUpdate.STOP: pmiss += 1 elif payment.payment_method == PaymentUpdate.FREE: pgra += 1 else: income += float(payment.amount) / payment.period self.stdout.write("%12s%12s%12s%12s" % ('Adhesions', 'gratuit', 'manquante', 'euros')) self.stdout.write("%12d%12d%12d%12d" % (nadh, pgra, pmiss, income))