from decimal import Decimal from django.core.management import BaseCommand from django.utils import timezone from core.models import Subject from core.prediction import predict_all class Command(BaseCommand): def add_arguments(self, parser): parser.add_argument( "--start", default="0", help="the current balance to use as a starting point for prediction", ) def handle(self, *args, **options): start_balance = Decimal(options["start"]) prediction_list = predict_all(Subject.objects.order_by("name")) today = timezone.now().date() future_transactions = [] for subject, transactions, info in prediction_list: print(f">>> {subject}") if info: rec_days, rec_months, day_of_month = \ info["recurring_days"], info["recurring_months"], info["day_of_month"] if rec_months and day_of_month: print(f"~~~ predicted transaction on day-of-month {day_of_month} every {rec_months} month(s)") elif rec_months: print(f"~~~ predicted transaction every {rec_months} month(s)") elif rec_days: print(f"~~~ predicted transaction every {rec_days} day(s)") else: print("~~~ no prediction possible") for tr in transactions: print(f" {tr.booking_date:%d.%m.%Y} | {tr.amount} € | {'stored' if tr.pk else 'predicted'}") if tr.booking_date > today: future_transactions.append(tr) print() current_balance = start_balance print(f"starting calculation with amount {current_balance} €") for tr in sorted(future_transactions, key=lambda t: t.booking_date): current_balance += tr.amount print( f"{tr.booking_date:%d.%m.%Y} | " f"{tr.subject.name:<18} | " f"{tr.amount:>8} € ==> " f"{current_balance:>8} €" )