Correção arredondamento dasalíquotas e valor taxa
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:
46
app/main.py
46
app/main.py
@@ -31,6 +31,7 @@ from fastapi import Query
|
||||
from sqlalchemy import select as sqla_select
|
||||
from app.models import AliquotaUF
|
||||
import pandas as pd
|
||||
from fastapi.responses import Response
|
||||
|
||||
|
||||
app = FastAPI()
|
||||
@@ -499,31 +500,54 @@ async def export_excel(
|
||||
cols = [c for c in df.columns if c != "Arquivo PDF"] + ["Arquivo PDF"]
|
||||
df = df[cols]
|
||||
|
||||
# garante que colunas percentuais estejam numéricas (se existirem)
|
||||
for col in ["ICMS (%)", "ICMS (%) (UF/Ref)", "Dif. ICMS (pp)", "PIS (%)", "COFINS (%)"]:
|
||||
# converte colunas numéricas (percentuais, R$, etc.)
|
||||
percent_cols = ["ICMS (%)", "ICMS (%) (UF/Ref)", "Dif. ICMS (pp)", "PIS (%)", "COFINS (%)"]
|
||||
money_cols = ["Valor Total", "ICMS (R$)", "PIS (R$)", "COFINS (R$)",
|
||||
"Base ICMS (R$)", "Base PIS (R$)", "Base COFINS (R$)"]
|
||||
other_dec6 = ["Tarifa", "Consumo (kWh)"]
|
||||
|
||||
from decimal import Decimal
|
||||
for col in percent_cols + money_cols + other_dec6:
|
||||
if col in df.columns:
|
||||
df[col] = df[col].map(lambda x: float(x) if isinstance(x, Decimal) else x)
|
||||
df[col] = pd.to_numeric(df[col], errors="coerce")
|
||||
|
||||
# --- gera o XLSX ---
|
||||
with pd.ExcelWriter(output, engine="xlsxwriter") as writer:
|
||||
df.to_excel(writer, index=False, sheet_name="Relatório")
|
||||
|
||||
# aplica formatação 6 casas decimais
|
||||
wb = writer.book
|
||||
ws = writer.sheets["Relatório"]
|
||||
fmt_4dec = wb.add_format({"num_format": "0.000000"})
|
||||
|
||||
for col in ["ICMS (%)", "ICMS (%) (UF/Ref)", "Dif. ICMS (pp)", "PIS (%)", "COFINS (%)", "Consumo (kWh)"]:
|
||||
fmt_dec6 = wb.add_format({"num_format": "0.000000"})
|
||||
fmt_money6 = wb.add_format({"num_format": "#,##0.000000"})
|
||||
fmt_money2 = wb.add_format({"num_format": "#,##0.00"})
|
||||
|
||||
for col in percent_cols:
|
||||
if col in df.columns:
|
||||
i = df.columns.get_loc(col)
|
||||
# largura automática básica + formato; ajuste a largura se quiser (ex.: 12)
|
||||
ws.set_column(i, i, None, fmt_4dec)
|
||||
ws.set_column(i, i, 14, fmt_dec6)
|
||||
|
||||
for col in money_cols:
|
||||
if col in df.columns:
|
||||
i = df.columns.get_loc(col)
|
||||
ws.set_column(i, i, 14, fmt_money6) # ou fmt_money2 se quiser 2 casas
|
||||
|
||||
for col in other_dec6:
|
||||
if col in df.columns:
|
||||
i = df.columns.get_loc(col)
|
||||
ws.set_column(i, i, 14, fmt_dec6)
|
||||
|
||||
# IMPORTANTE: só aqui, FORA do with
|
||||
output.seek(0)
|
||||
data = output.getvalue()
|
||||
|
||||
return StreamingResponse(
|
||||
output,
|
||||
return Response(
|
||||
content=data,
|
||||
media_type="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
|
||||
headers={"Content-Disposition": f'attachment; filename="{filename}"'}
|
||||
headers={
|
||||
"Content-Disposition": f'attachment; filename="{filename}"',
|
||||
"Content-Length": str(len(data)),
|
||||
},
|
||||
)
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user