From 3cfd3d34520a57c4d8a4da16e9299bd22983493c Mon Sep 17 00:00:00 2001 From: "ewerton.almeida" Date: Sat, 9 Aug 2025 16:12:34 -0300 Subject: [PATCH] =?UTF-8?q?Ajuste=20no=20dashboard:=20gr=C3=A1fico=20dentr?= =?UTF-8?q?o=20de=20card,=20removidas=20linhas=20de=20fundo,=20legendas=20?= =?UTF-8?q?com=20linha=20e=20valores=20exibidos=20nos=20pontos?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/routes/dashboard.py | 90 ----------------------------------------- 1 file changed, 90 deletions(-) delete mode 100755 app/routes/dashboard.py diff --git a/app/routes/dashboard.py b/app/routes/dashboard.py deleted file mode 100755 index 78e29ce..0000000 --- a/app/routes/dashboard.py +++ /dev/null @@ -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 - })