Criação da tela de clientes e relatórios
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:
@@ -56,7 +56,7 @@ def salvar_em_uploads(caminho_pdf_temp, nome_original, nota_fiscal):
|
||||
logger.error(f"Erro ao salvar em uploads: {e}")
|
||||
return caminho_pdf_temp
|
||||
|
||||
async def process_single_file(caminho_pdf_temp: str, nome_original: str):
|
||||
async def process_single_file(caminho_pdf_temp: str, nome_original: str, cliente_id: str | None = None):
|
||||
inicio = time.perf_counter()
|
||||
|
||||
async with AsyncSessionLocal() as session:
|
||||
@@ -89,7 +89,10 @@ async def process_single_file(caminho_pdf_temp: str, nome_original: str):
|
||||
dados['link_arquivo'] = caminho_final
|
||||
|
||||
# Salva fatura
|
||||
fatura = Fatura(**dados)
|
||||
dados['cliente_id'] = cliente_id
|
||||
if cliente_id:
|
||||
dados['cliente_id'] = cliente_id
|
||||
fatura = Fatura(**dados)
|
||||
session.add(fatura)
|
||||
|
||||
await session.commit()
|
||||
@@ -125,15 +128,36 @@ async def processar_em_lote():
|
||||
while not fila_processamento.empty():
|
||||
item = await fila_processamento.get()
|
||||
try:
|
||||
resultado = await process_single_file(item['caminho_pdf'], item['nome_original'])
|
||||
resultado = await process_single_file(
|
||||
item['caminho_pdf'],
|
||||
item['nome_original'],
|
||||
item.get('cliente_id')
|
||||
)
|
||||
# tentar tamanho/data do TEMP; se não existir mais, tenta do destino final; senão, 0/""
|
||||
temp_path = item['caminho_pdf']
|
||||
dest_path = (resultado.get("dados") or {}).get("link_arquivo", "")
|
||||
|
||||
def _safe_size(p):
|
||||
try:
|
||||
return os.path.getsize(p) // 1024
|
||||
except Exception:
|
||||
return 0
|
||||
|
||||
def _safe_mtime(p):
|
||||
try:
|
||||
return time.strftime("%d/%m/%Y", time.localtime(os.path.getmtime(p)))
|
||||
except Exception:
|
||||
return ""
|
||||
|
||||
status_arquivos[item['nome_original']] = {
|
||||
"status": resultado.get("status"),
|
||||
"mensagem": resultado.get("mensagem", ""),
|
||||
"tempo": resultado.get("tempo", "---"),
|
||||
"tamanho": os.path.getsize(item['caminho_pdf']) // 1024, # tamanho em KB
|
||||
"data": time.strftime("%d/%m/%Y", time.localtime(os.path.getmtime(item['caminho_pdf'])))
|
||||
"tamanho": _safe_size(temp_path) or _safe_size(dest_path),
|
||||
"data": _safe_mtime(temp_path) or _safe_mtime(dest_path),
|
||||
}
|
||||
|
||||
|
||||
resultados.append(status_arquivos[item['nome_original']])
|
||||
except Exception as e:
|
||||
status_arquivos[item['nome_original']] = {
|
||||
@@ -156,17 +180,21 @@ async def processar_em_lote():
|
||||
erros_txt.append(f"{nome} - {status.get('mensagem', 'Erro desconhecido')}")
|
||||
|
||||
if erros_txt:
|
||||
with open(os.path.join(UPLOADS_DIR, "erros", "erros.txt"), "w", encoding="utf-8") as f:
|
||||
erros_dir = os.path.join(UPLOADS_DIR, "erros")
|
||||
os.makedirs(erros_dir, exist_ok=True) # <- GARANTE A PASTA
|
||||
|
||||
with open(os.path.join(erros_dir, "erros.txt"), "w", encoding="utf-8") as f:
|
||||
f.write("\n".join(erros_txt))
|
||||
|
||||
# Compacta PDFs com erro
|
||||
with ZipFile(os.path.join(UPLOADS_DIR, "erros", "faturas_erro.zip"), "w") as zipf:
|
||||
with ZipFile(os.path.join(erros_dir, "faturas_erro.zip"), "w") as zipf:
|
||||
for nome in status_arquivos:
|
||||
if status_arquivos[nome]['status'] == 'Erro':
|
||||
caminho = os.path.join(UPLOADS_DIR, "temp", nome)
|
||||
if os.path.exists(caminho):
|
||||
zipf.write(caminho, arcname=nome)
|
||||
return resultados
|
||||
|
||||
return resultados
|
||||
|
||||
def limpar_arquivos_processados():
|
||||
status_arquivos.clear()
|
||||
@@ -192,6 +220,6 @@ async def garantir_selic_para_competencia(session, ano, mes):
|
||||
|
||||
if dados:
|
||||
percentual = float(dados[0]["valor"].replace(",", "."))
|
||||
novo = SelicMensal(ano=ano, mes=mes, fator=percentual)
|
||||
novo = SelicMensal(ano=ano, mes=mes, percentual=percentual)
|
||||
session.add(novo)
|
||||
await session.commit()
|
||||
Reference in New Issue
Block a user