56 lines
2.0 KiB
Python
56 lines
2.0 KiB
Python
from __future__ import annotations
|
|
|
|
from fastapi import APIRouter, Request
|
|
|
|
from app.core.db.common import CommonSessionLocal
|
|
from app.core.security.csrf import get_or_create_csrf_token
|
|
from app.core.templating import templates
|
|
from app.modules.accounting.ai_service import provider_status, recent_decisions, usage_summary
|
|
from app.modules.accounting.ui import _require_partner, _visible_clients
|
|
from app.modules.core.rbac.deps import get_user_permissions, get_user_roles
|
|
|
|
router = APIRouter(prefix="/tools/accounting/ai", tags=["accounting-ai-ui"])
|
|
|
|
|
|
@router.get("")
|
|
def page(request: Request, client_id: int | None = None):
|
|
db = CommonSessionLocal()
|
|
try:
|
|
user, denied = _require_partner(request, db, "accounting.learning.view")
|
|
if denied:
|
|
return denied
|
|
clients, scope = _visible_clients(db, request, user)
|
|
selected = next(
|
|
(client for client in clients if client_id and int(client.id) == int(client_id)),
|
|
None,
|
|
)
|
|
summary = usage_summary(
|
|
db,
|
|
tenant_id=scope.tenant_id,
|
|
client_id=selected.id if selected else None,
|
|
)
|
|
rows = recent_decisions(
|
|
db,
|
|
tenant_id=scope.tenant_id,
|
|
client_id=selected.id if selected else None,
|
|
limit=100,
|
|
)
|
|
return templates.TemplateResponse(
|
|
"modules/accounting/templates/accounting/ai_dashboard.html",
|
|
{
|
|
"request": request,
|
|
"current_user": user,
|
|
"current_user_roles": get_user_roles(db, user.id),
|
|
"current_user_permissions": get_user_permissions(db, user.id),
|
|
"csrf_token": get_or_create_csrf_token(request),
|
|
"title": "Accounting AI",
|
|
"clients": clients,
|
|
"selected_client": selected,
|
|
"provider_status": provider_status(),
|
|
"summary": summary,
|
|
"decisions": rows,
|
|
},
|
|
)
|
|
finally:
|
|
db.close()
|