from flask import Blueprint, render_template, request from datetime import date from collections import defaultdict import sqlalchemy as sa from app import db from app.models import WorkEntry from app.business.travel_calc import compute_km_for_entry, compute_co2_grams, compute_frais_reels from app.business.time_calc import count_day_types, monthly_stats, minutes_to_str from app.config_loader import get_vehicles, get_journeys, get_bareme bp = Blueprint("reports", __name__, url_prefix="/reports") MONTHS_FR = { 1: "Janvier", 2: "Février", 3: "Mars", 4: "Avril", 5: "Mai", 6: "Juin", 7: "Juillet", 8: "Août", 9: "Septembre", 10: "Octobre", 11: "Novembre", 12: "Décembre", } @bp.route("/") def index(): year = request.args.get("year", date.today().year, type=int) start = date(year, 1, 1) end = date(year, 12, 31) entries = db.session.scalars( sa.select(WorkEntry).where(WorkEntry.date.between(start, end)) ).all() vehicles = get_vehicles() journeys = get_journeys() # --- Stats annuelles (inchangées) --- total_km = {} total_co2 = 0.0 for entry in entries: km = compute_km_for_entry(entry.journey_profile_id, journeys, entry.motor_vehicle_id) for v, d in km.items(): total_km[v] = total_km.get(v, 0) + d total_co2 += compute_co2_grams(km, vehicles) frais_reels = {} for vehicle_id, km in total_km.items(): vehicle = vehicles.get(vehicle_id, {}) cv = vehicle.get("cv") if cv: tranches = get_bareme(year, cv) electric = vehicle.get("fuel") == "electric" frais_reels[vehicle_id] = round(compute_frais_reels(km, tranches, electric=electric), 2) day_type_counts = count_day_types(entries) # --- Stats mensuelles --- entries_by_month: dict[int, list] = defaultdict(list) for entry in entries: entries_by_month[entry.date.month].append(entry) monthly_data = {} for month in range(1, 13): month_entries = entries_by_month.get(month, []) month_km: dict[str, int] = {} for entry in month_entries: km = compute_km_for_entry(entry.journey_profile_id, journeys, entry.motor_vehicle_id) for v, d in km.items(): month_km[v] = month_km.get(v, 0) + d stats = monthly_stats(month_entries) monthly_data[month] = { "month_name": MONTHS_FR[month], "entry_count": len(month_entries), "km_by_vehicle": month_km, "km_total": sum(month_km.values()), "median_daily_str": minutes_to_str(stats["median_daily_min"]) if month_entries else "–", "median_weekly_str": minutes_to_str(stats["median_weekly_min"]) if month_entries else "–", } return render_template( "reports.html", year=year, total_km=total_km, total_co2_kg=round(total_co2 / 1000, 2), frais_reels=frais_reels, vehicles=vehicles, day_type_counts=day_type_counts, monthly_data=monthly_data, )