- Filtre Jinja2 day_type_fr (WORK→Travail, TT→Télétravail, etc.) - Appliqué dans entry_list.html et dashboard.html - Passage de vehicles au template dashboard pour afficher vehicle.name - Mise à jour du test test_entry_list en conséquence - Ajout du plan docs/plans/2026-03-11-bareme-kilometrique.md
66 lines
2.1 KiB
Python
66 lines
2.1 KiB
Python
from flask import Blueprint, render_template
|
|
from datetime import date, timedelta
|
|
import sqlalchemy as sa
|
|
from app import db
|
|
from app.models import WorkEntry
|
|
from app.business.time_calc import minutes_to_str, work_minutes_reference
|
|
from app.business.travel_calc import compute_km_for_entry, compute_co2_grams
|
|
from app.business.leave_calc import compute_leave_used, get_or_create_balance
|
|
from app.config_loader import get_vehicles, get_journeys
|
|
|
|
bp = Blueprint("dashboard", __name__)
|
|
|
|
|
|
@bp.route("/")
|
|
def index():
|
|
today = date.today()
|
|
year = today.year
|
|
|
|
monday = today - timedelta(days=today.weekday())
|
|
sunday = monday + timedelta(days=6)
|
|
week_entries = db.session.scalars(
|
|
sa.select(WorkEntry).where(WorkEntry.date.between(monday, sunday))
|
|
).all()
|
|
|
|
week_actual = sum(e.total_minutes() for e in week_entries)
|
|
week_ref = sum(work_minutes_reference(e.day_type) for e in week_entries)
|
|
week_balance = week_actual - week_ref
|
|
|
|
month_start = today.replace(day=1)
|
|
month_entries = db.session.scalars(
|
|
sa.select(WorkEntry).where(WorkEntry.date.between(month_start, today))
|
|
).all()
|
|
|
|
vehicles = get_vehicles()
|
|
journeys = get_journeys()
|
|
|
|
month_km = {}
|
|
month_co2 = 0.0
|
|
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
|
|
month_co2 += compute_co2_grams(km, vehicles)
|
|
|
|
balance = get_or_create_balance(year)
|
|
used = compute_leave_used(year)
|
|
|
|
today_entry = db.session.scalar(
|
|
sa.select(WorkEntry).where(WorkEntry.date == today)
|
|
)
|
|
|
|
return render_template(
|
|
"dashboard.html",
|
|
today=today,
|
|
today_entry=today_entry,
|
|
journeys=journeys,
|
|
vehicles=vehicles,
|
|
week_actual_str=minutes_to_str(week_actual),
|
|
week_balance=week_balance,
|
|
week_balance_str=minutes_to_str(abs(week_balance)),
|
|
month_km=month_km,
|
|
month_co2_kg=round(month_co2 / 1000, 2),
|
|
balance=balance,
|
|
used=used,
|
|
)
|