49 lines
1.4 KiB
Python
49 lines
1.4 KiB
Python
"""Dashboard: KPIs, gastos por mês e por fornecedor, últimas notas."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from fastapi import APIRouter, Request
|
|
|
|
from .. import database as db
|
|
from ..storage import money
|
|
from ..templating import render
|
|
|
|
router = APIRouter()
|
|
|
|
_MESES = ["jan", "fev", "mar", "abr", "mai", "jun", "jul", "ago", "set", "out", "nov", "dez"]
|
|
|
|
|
|
def _mes_label(ym: str) -> str:
|
|
try:
|
|
year, month = ym.split("-")
|
|
return f"{_MESES[int(month) - 1]}/{year}"
|
|
except (ValueError, IndexError):
|
|
return ym
|
|
|
|
|
|
@router.get("/")
|
|
def dashboard(request: Request):
|
|
with db.session() as conn:
|
|
totals = db.overall_totals(conn)
|
|
months = [dict(r) for r in db.monthly_totals(conn)]
|
|
suppliers = [dict(r) for r in db.supplier_totals(conn, limit=8)]
|
|
recent = [dict(r) for r in db.list_fiscal(conn)][:10]
|
|
|
|
max_month = max((m["total"] for m in months), default=0) or 1
|
|
for m in months:
|
|
m["label"] = _mes_label(m["month"])
|
|
m["pct"] = round(m["total"] / max_month * 100, 1)
|
|
max_sup = max((s["total"] for s in suppliers), default=0) or 1
|
|
for s in suppliers:
|
|
s["pct"] = round(s["total"] / max_sup * 100, 1)
|
|
|
|
return render(
|
|
request,
|
|
"dashboard.html",
|
|
totals=totals,
|
|
months=months,
|
|
suppliers=suppliers,
|
|
recent=recent,
|
|
money=money,
|
|
)
|