Ajuste no dashboard: gráfico dentro de card, removidas linhas de fundo, legendas com linha e valores exibidos nos pontos
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
@@ -1,90 +0,0 @@
|
|||||||
from fastapi import APIRouter, Request
|
|
||||||
from fastapi.templating import Jinja2Templates
|
|
||||||
from sqlalchemy import create_engine, text
|
|
||||||
import os
|
|
||||||
|
|
||||||
router = APIRouter()
|
|
||||||
templates = Jinja2Templates(directory="app/templates")
|
|
||||||
|
|
||||||
# Conexão com o banco de dados PostgreSQL
|
|
||||||
DATABASE_URL = os.getenv("DATABASE_URL", "postgresql://postgres:postgres@localhost:5432/faturas")
|
|
||||||
engine = create_engine(DATABASE_URL)
|
|
||||||
|
|
||||||
@router.get("/dashboard")
|
|
||||||
def dashboard(request: Request, cliente: str = None):
|
|
||||||
with engine.connect() as conn:
|
|
||||||
filtros = ""
|
|
||||||
if cliente:
|
|
||||||
filtros = "WHERE nome = :cliente"
|
|
||||||
|
|
||||||
# Clientes únicos
|
|
||||||
clientes_query = text("SELECT DISTINCT nome FROM faturas ORDER BY nome")
|
|
||||||
clientes = [row[0] for row in conn.execute(clientes_query)]
|
|
||||||
|
|
||||||
# Indicadores
|
|
||||||
indicadores = []
|
|
||||||
|
|
||||||
indicadores.append({
|
|
||||||
"titulo": "Faturas com erro",
|
|
||||||
"valor": conn.execute(text(f"SELECT COUNT(*) FROM faturas WHERE erro IS TRUE {f'AND nome = :cliente' if cliente else ''}"), {"cliente": cliente} if cliente else {}).scalar()
|
|
||||||
})
|
|
||||||
|
|
||||||
indicadores.append({
|
|
||||||
"titulo": "Faturas com valor total igual a R$ 0",
|
|
||||||
"valor": conn.execute(text(f"SELECT COUNT(*) FROM faturas WHERE total = 0 {f'AND nome = :cliente' if cliente else ''}"), {"cliente": cliente} if cliente else {}).scalar()
|
|
||||||
})
|
|
||||||
|
|
||||||
indicadores.append({
|
|
||||||
"titulo": "Clientes únicos",
|
|
||||||
"valor": conn.execute(text(f"SELECT COUNT(DISTINCT nome) FROM faturas {filtros}"), {"cliente": cliente} if cliente else {}).scalar()
|
|
||||||
})
|
|
||||||
|
|
||||||
indicadores.append({
|
|
||||||
"titulo": "Total de faturas",
|
|
||||||
"valor": conn.execute(text(f"SELECT COUNT(*) FROM faturas {filtros}"), {"cliente": cliente} if cliente else {}).scalar()
|
|
||||||
})
|
|
||||||
|
|
||||||
indicadores.append({
|
|
||||||
"titulo": "Faturas com campos nulos",
|
|
||||||
"valor": conn.execute(text(f"SELECT COUNT(*) FROM faturas WHERE base_pis IS NULL OR base_cofins IS NULL OR base_icms IS NULL {f'AND nome = :cliente' if cliente else ''}"), {"cliente": cliente} if cliente else {}).scalar()
|
|
||||||
})
|
|
||||||
|
|
||||||
indicadores.append({
|
|
||||||
"titulo": "Alíquotas zeradas com valores diferentes de zero",
|
|
||||||
"valor": conn.execute(text(f"SELECT COUNT(*) FROM faturas WHERE (aliq_pis = 0 AND pis > 0) OR (aliq_cofins = 0 AND cofins > 0) {f'AND nome = :cliente' if cliente else ''}"), {"cliente": cliente} if cliente else {}).scalar()
|
|
||||||
})
|
|
||||||
|
|
||||||
indicadores.append({
|
|
||||||
"titulo": "Faturas com ICMS incluso após decisão STF",
|
|
||||||
"valor": conn.execute(text(f"SELECT COUNT(*) FROM faturas WHERE data_emissao > '2017-03-15' AND base_pis = base_icms {f'AND nome = :cliente' if cliente else ''}"), {"cliente": cliente} if cliente else {}).scalar()
|
|
||||||
})
|
|
||||||
|
|
||||||
indicadores.append({
|
|
||||||
"titulo": "Valor total processado",
|
|
||||||
"valor": conn.execute(text(f"SELECT ROUND(SUM(total), 2) FROM faturas {filtros}"), {"cliente": cliente} if cliente else {}).scalar() or 0
|
|
||||||
})
|
|
||||||
|
|
||||||
# Análise do STF
|
|
||||||
def media_percentual_icms(data_inicio, data_fim):
|
|
||||||
result = conn.execute(text(f"""
|
|
||||||
SELECT
|
|
||||||
ROUND(AVG(CASE WHEN base_pis = base_icms THEN 100.0 ELSE 0.0 END), 2) AS percentual_com_icms,
|
|
||||||
ROUND(AVG(pis + cofins), 2) AS media_valor
|
|
||||||
FROM faturas
|
|
||||||
WHERE data_emissao BETWEEN :inicio AND :fim
|
|
||||||
{f'AND nome = :cliente' if cliente else ''}
|
|
||||||
"""), {"inicio": data_inicio, "fim": data_fim, "cliente": cliente} if cliente else {"inicio": data_inicio, "fim": data_fim}).mappings().first()
|
|
||||||
return result or {"percentual_com_icms": 0, "media_valor": 0}
|
|
||||||
|
|
||||||
analise_stf = {
|
|
||||||
"antes": media_percentual_icms("2000-01-01", "2017-03-15"),
|
|
||||||
"depois": media_percentual_icms("2017-03-16", "2099-12-31")
|
|
||||||
}
|
|
||||||
|
|
||||||
return templates.TemplateResponse("dashboard.html", {
|
|
||||||
"request": request,
|
|
||||||
"clientes": clientes,
|
|
||||||
"cliente_atual": cliente,
|
|
||||||
"indicadores": indicadores,
|
|
||||||
"analise_stf": analise_stf
|
|
||||||
})
|
|
||||||
Reference in New Issue
Block a user