diff --git a/app/models.py b/app/models.py index f98c3ba..adb09e8 100755 --- a/app/models.py +++ b/app/models.py @@ -4,15 +4,21 @@ from sqlalchemy.dialects.postgresql import UUID import uuid from datetime import datetime from app.database import Base +from sqlalchemy import Boolean + class ParametrosFormula(Base): - __tablename__ = 'parametros_formula' - __table_args__ = {'schema': 'faturas', 'extend_existing': True} + __tablename__ = "parametros_formula" + __table_args__ = {"schema": "faturas"} - id = Column(Integer, primary_key=True, autoincrement=True) - nome = Column(String) + id = Column(Integer, primary_key=True, index=True) + tipo = Column(String(20)) formula = Column(Text) - + ativo = Column(Boolean) + aliquota_icms = Column(Float) + incluir_icms = Column(Integer) + incluir_pis = Column(Integer) + incluir_cofins = Column(Integer) class Fatura(Base): __tablename__ = "faturas" diff --git a/app/routes/parametros.py b/app/parametros.py old mode 100755 new mode 100644 similarity index 75% rename from app/routes/parametros.py rename to app/parametros.py index c25bb9f..5e9420d --- a/app/routes/parametros.py +++ b/app/parametros.py @@ -1,83 +1,99 @@ -# parametros.py -from fastapi import APIRouter, HTTPException, Depends -from sqlalchemy.orm import Session -from sqlalchemy.ext.asyncio import AsyncSession -from database import get_session -from models import AliquotaUF, ParametrosFormula, SelicMensal -from typing import List -from pydantic import BaseModel -import datetime - -router = APIRouter() - -# === Schemas === -class AliquotaUFSchema(BaseModel): - uf: str - exercicio: int - aliquota: float - - class Config: - orm_mode = True - -class ParametrosFormulaSchema(BaseModel): - nome: str - formula: str - campos: str - - class Config: - orm_mode = True - -class SelicMensalSchema(BaseModel): - mes: str # 'YYYY-MM' - fator: float - - class Config: - orm_mode = True - -# === Rotas === - -@router.get("/parametros/aliquotas", response_model=List[AliquotaUFSchema]) -def listar_aliquotas(db: AsyncSession = Depends(get_session)): - return db.query(AliquotaUF).all() - -@router.post("/parametros/aliquotas") -def adicionar_aliquota(aliq: AliquotaUFSchema, db: AsyncSession = Depends(get_session)): - existente = db.query(AliquotaUF).filter_by(uf=aliq.uf, exercicio=aliq.exercicio).first() - if existente: - existente.aliquota = aliq.aliquota - else: - novo = AliquotaUF(**aliq.dict()) - db.add(novo) - db.commit() - return {"status": "ok"} - -@router.get("/parametros/formulas", response_model=List[ParametrosFormulaSchema]) -def listar_formulas(db: AsyncSession = Depends(get_session)): - return db.query(ParametrosFormula).all() - -@router.post("/parametros/formulas") -def salvar_formula(form: ParametrosFormulaSchema, db: AsyncSession = Depends(get_session)): - existente = db.query(ParametrosFormula).filter_by(nome=form.nome).first() - if existente: - existente.formula = form.formula - existente.campos = form.campos - else: - novo = ParametrosFormula(**form.dict()) - db.add(novo) - db.commit() - return {"status": "ok"} - -@router.get("/parametros/selic", response_model=List[SelicMensalSchema]) -def listar_selic(db: AsyncSession = Depends(get_session)): - return db.query(SelicMensal).order_by(SelicMensal.mes.desc()).all() - -@router.post("/parametros/selic") -def salvar_selic(selic: SelicMensalSchema, db: AsyncSession = Depends(get_session)): - existente = db.query(SelicMensal).filter_by(mes=selic.mes).first() - if existente: - existente.fator = selic.fator - else: - novo = SelicMensal(**selic.dict()) - db.add(novo) - db.commit() - return {"status": "ok"} +# parametros.py +from fastapi import APIRouter, Request, HTTPException, Depends +from sqlalchemy.orm import Session +from sqlalchemy.ext.asyncio import AsyncSession +from app.database import get_session +from app.models import AliquotaUF, ParametrosFormula, SelicMensal +from typing import List +from pydantic import BaseModel +import datetime +from fastapi.templating import Jinja2Templates +from sqlalchemy.future import select +from app.database import AsyncSessionLocal + +router = APIRouter() + +# === Schemas === +class AliquotaUFSchema(BaseModel): + uf: str + exercicio: int + aliquota: float + + class Config: + orm_mode = True + +class ParametrosFormulaSchema(BaseModel): + nome: str + formula: str + campos: str + + class Config: + orm_mode = True + +class SelicMensalSchema(BaseModel): + mes: str # 'YYYY-MM' + fator: float + + class Config: + orm_mode = True + +# === Rotas === + +router = APIRouter() +templates = Jinja2Templates(directory="app/templates") + +@router.get("/parametros") +async def parametros_page(request: Request): + async with AsyncSessionLocal() as session: + result = await session.execute(select(ParametrosFormula).where(ParametrosFormula.ativo == True)) + parametros = result.scalars().first() + return templates.TemplateResponse("parametros.html", { + "request": request, + "parametros": parametros or {} + }) + +@router.get("/parametros/aliquotas", response_model=List[AliquotaUFSchema]) +def listar_aliquotas(db: AsyncSession = Depends(get_session)): + return db.query(AliquotaUF).all() + +@router.post("/parametros/aliquotas") +def adicionar_aliquota(aliq: AliquotaUFSchema, db: AsyncSession = Depends(get_session)): + existente = db.query(AliquotaUF).filter_by(uf=aliq.uf, exercicio=aliq.exercicio).first() + if existente: + existente.aliquota = aliq.aliquota + else: + novo = AliquotaUF(**aliq.dict()) + db.add(novo) + db.commit() + return {"status": "ok"} + +@router.get("/parametros/formulas", response_model=List[ParametrosFormulaSchema]) +def listar_formulas(db: AsyncSession = Depends(get_session)): + return db.query(ParametrosFormula).all() + +@router.post("/parametros/formulas") +def salvar_formula(form: ParametrosFormulaSchema, db: AsyncSession = Depends(get_session)): + existente = db.query(ParametrosFormula).filter_by(nome=form.nome).first() + if existente: + existente.formula = form.formula + existente.campos = form.campos + else: + novo = ParametrosFormula(**form.dict()) + db.add(novo) + db.commit() + return {"status": "ok"} + +@router.get("/parametros/selic", response_model=List[SelicMensalSchema]) +def listar_selic(db: AsyncSession = Depends(get_session)): + return db.query(SelicMensal).order_by(SelicMensal.mes.desc()).all() + +@router.post("/parametros/selic") +def salvar_selic(selic: SelicMensalSchema, db: AsyncSession = Depends(get_session)): + existente = db.query(SelicMensal).filter_by(mes=selic.mes).first() + if existente: + existente.fator = selic.fator + else: + novo = SelicMensal(**selic.dict()) + db.add(novo) + db.commit() + return {"status": "ok"} diff --git a/app/templates/parametros.html b/app/templates/parametros.html index b4854d8..4215bf7 100755 --- a/app/templates/parametros.html +++ b/app/templates/parametros.html @@ -1,32 +1,206 @@ {% extends "index.html" %} {% block title %}Parâmetros de Cálculo{% endblock %} {% block content %} -

⚙️ Parâmetros

-
-
-

+

+ ⚙️ Parâmetros de Cálculo +

-
-

+
+ + +
-
-

+ +
+ +
+
+ + +
+
+ + +
+
-
-

+
+ +
+ + +
+ + Ex: (pis_base - (pis_base - icms_valor)) * pis_aliq +
+ +
+
-
-

+
+ + + + +
- - + + -{% if mensagem %} -
- {{ mensagem }} +

📋 Fórmulas Salvas

+
+ {% for param in lista_parametros %} +
+

{{ param.tipo }}

+ {{ param.formula }} + +
+ {% else %}

Nenhuma fórmula cadastrada.

{% endfor %}
-{% endif %} +
+ + + + + + + + + {% endblock %}