2025-07-28 13:29:45 -03:00
|
|
|
{% extends "index.html" %}
|
|
|
|
|
{% block title %}Upload de Faturas{% endblock %}
|
|
|
|
|
{% block content %}
|
|
|
|
|
|
|
|
|
|
<h1 style="font-size: 1.5rem; margin-bottom: 1rem;">📤 Upload de Faturas</h1>
|
|
|
|
|
|
|
|
|
|
<div class="upload-box" id="upload-box">
|
|
|
|
|
<h3>Arraste faturas em PDF aqui ou clique para selecionar</h3>
|
|
|
|
|
<p style="color: gray; font-size: 0.9rem;">Apenas PDFs textuais (não escaneados)</p>
|
|
|
|
|
<br />
|
|
|
|
|
<button class="btn btn-primary" onclick="document.getElementById('file-input').click()">Selecionar Arquivos</button>
|
|
|
|
|
<input type="file" id="file-input" accept=".pdf" multiple onchange="handleFiles(this.files)" style="display:none;" />
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
<div class="buttons">
|
|
|
|
|
<button class="btn btn-primary" onclick="processar(this)">Processar Faturas</button>
|
|
|
|
|
<button class="btn btn-danger" onclick="limpar()">Limpar Tudo</button>
|
|
|
|
|
<button class="btn btn-success" onclick="baixarPlanilha()">📅 Abrir Planilha</button>
|
|
|
|
|
<button class="btn btn-success" onclick="gerarRelatorio()">📊 Gerar Relatório</button>
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
<table>
|
|
|
|
|
<thead>
|
|
|
|
|
<tr>
|
|
|
|
|
<th>Arquivo</th>
|
|
|
|
|
<th>Status</th>
|
|
|
|
|
<th>Mensagem</th>
|
2025-07-28 22:31:31 -03:00
|
|
|
<th>Tempo</th>
|
2025-07-28 13:29:45 -03:00
|
|
|
</tr>
|
|
|
|
|
</thead>
|
|
|
|
|
<tbody id="file-table">
|
|
|
|
|
<tr><td colspan="3" style="text-align: center; color: #aaa;">Nenhum arquivo selecionado.</td></tr>
|
|
|
|
|
</tbody>
|
|
|
|
|
</table>
|
|
|
|
|
|
|
|
|
|
<script>
|
|
|
|
|
let arquivos = [];
|
|
|
|
|
let statusInterval = null;
|
|
|
|
|
const fileTable = document.getElementById('file-table');
|
|
|
|
|
|
|
|
|
|
function handleFiles(files) {
|
|
|
|
|
arquivos = [...arquivos, ...files];
|
|
|
|
|
renderTable();
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-28 22:31:31 -03:00
|
|
|
function renderTable(statusList = []) {
|
|
|
|
|
const rows = statusList.length ? statusList : arquivos.map(file => ({
|
|
|
|
|
nome: file.name,
|
|
|
|
|
status: 'Aguardando',
|
|
|
|
|
mensagem: ''
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
fileTable.innerHTML = rows.length
|
|
|
|
|
? rows.map(file => {
|
|
|
|
|
const status = file.status || 'Aguardando';
|
|
|
|
|
const statusClass = status === 'Concluído' ? 'status-ok'
|
|
|
|
|
: status === 'Erro' ? 'status-error'
|
|
|
|
|
: status === 'Aguardando' ? 'status-warn'
|
|
|
|
|
: 'status-processing';
|
|
|
|
|
return `
|
|
|
|
|
<tr>
|
|
|
|
|
<td>${file.nome}</td>
|
|
|
|
|
<td class="${statusClass}">${status}</td>
|
|
|
|
|
<td>${file.mensagem || '---'}</td>
|
|
|
|
|
<td>${file.tempo || '---'}</td>
|
|
|
|
|
</tr>
|
|
|
|
|
`;
|
|
|
|
|
}).join('')
|
|
|
|
|
: '<tr><td colspan="3" style="text-align:center; color: #aaa;">Nenhum arquivo selecionado.</td></tr>';
|
|
|
|
|
}
|
2025-07-28 13:29:45 -03:00
|
|
|
|
|
|
|
|
async function processar(btn) {
|
|
|
|
|
if (arquivos.length === 0) return alert("Nenhum arquivo selecionado.");
|
|
|
|
|
btn.disabled = true;
|
|
|
|
|
btn.innerText = "⏳ Processando...";
|
|
|
|
|
|
|
|
|
|
const formData = new FormData();
|
|
|
|
|
arquivos.forEach(file => formData.append("files", file));
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
await fetch("/upload-files", { method: "POST", body: formData });
|
2025-07-28 22:31:31 -03:00
|
|
|
|
|
|
|
|
const response = await fetch("/process-queue", { method: "POST" });
|
|
|
|
|
if (!response.ok) {
|
|
|
|
|
const msg = await response.text();
|
|
|
|
|
throw new Error(`Erro no processamento: ${msg}`);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
arquivos = []; // <- só limpa se o processamento for bem-sucedido
|
2025-07-28 13:29:45 -03:00
|
|
|
statusInterval = setInterval(updateStatus, 1000);
|
|
|
|
|
} catch (err) {
|
2025-07-28 22:31:31 -03:00
|
|
|
alert("Erro ao processar faturas:\n" + err.message);
|
2025-07-28 13:29:45 -03:00
|
|
|
} finally {
|
|
|
|
|
setTimeout(() => {
|
|
|
|
|
btn.innerText = "Processar Faturas";
|
|
|
|
|
btn.disabled = false;
|
|
|
|
|
}, 1500);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-28 22:31:31 -03:00
|
|
|
|
2025-07-28 13:29:45 -03:00
|
|
|
async function updateStatus() {
|
|
|
|
|
const res = await fetch("/get-status");
|
|
|
|
|
const data = await res.json();
|
|
|
|
|
renderTable(data.files);
|
|
|
|
|
|
|
|
|
|
if (!data.is_processing && statusInterval) {
|
|
|
|
|
clearInterval(statusInterval);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function limpar() {
|
|
|
|
|
fetch("/clear-all", { method: "POST" });
|
|
|
|
|
arquivos = [];
|
2025-07-28 22:31:31 -03:00
|
|
|
document.getElementById("file-input").value = null;
|
2025-07-28 13:29:45 -03:00
|
|
|
renderTable();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function baixarPlanilha() {
|
|
|
|
|
window.open('/export-excel', '_blank');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function gerarRelatorio() {
|
|
|
|
|
window.open('/generate-report', '_blank');
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-28 22:31:31 -03:00
|
|
|
const dropZone = document.body;
|
2025-07-28 13:29:45 -03:00
|
|
|
dropZone.addEventListener('dragover', (e) => {
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
dropZone.classList.add('dragover');
|
|
|
|
|
});
|
|
|
|
|
dropZone.addEventListener('dragleave', () => {
|
|
|
|
|
dropZone.classList.remove('dragover');
|
|
|
|
|
});
|
|
|
|
|
dropZone.addEventListener('drop', (e) => {
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
dropZone.classList.remove('dragover');
|
|
|
|
|
handleFiles(e.dataTransfer.files);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
window.addEventListener('DOMContentLoaded', updateStatus);
|
2025-07-28 22:31:31 -03:00
|
|
|
|
|
|
|
|
// Adiciona destaque visual à caixa ao arrastar arquivos na tela toda
|
|
|
|
|
window.addEventListener('dragover', (e) => {
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
document.getElementById('upload-box').classList.add('dragover');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
window.addEventListener('dragleave', () => {
|
|
|
|
|
document.getElementById('upload-box').classList.remove('dragover');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
window.addEventListener('drop', (e) => {
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
document.getElementById('upload-box').classList.remove('dragover');
|
|
|
|
|
handleFiles(e.dataTransfer.files);
|
|
|
|
|
});
|
|
|
|
|
|
2025-07-28 13:29:45 -03:00
|
|
|
</script>
|
|
|
|
|
|
|
|
|
|
<style>
|
|
|
|
|
.upload-box {
|
|
|
|
|
background: #fff;
|
|
|
|
|
border: 2px dashed #ccc;
|
|
|
|
|
padding: 2rem;
|
|
|
|
|
text-align: center;
|
|
|
|
|
border-radius: 10px;
|
|
|
|
|
margin-bottom: 2rem;
|
|
|
|
|
}
|
|
|
|
|
.upload-box.dragover {
|
|
|
|
|
background-color: #eef2ff;
|
|
|
|
|
border-color: #4361ee;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.buttons {
|
|
|
|
|
display: flex;
|
|
|
|
|
gap: 1rem;
|
|
|
|
|
justify-content: center;
|
|
|
|
|
flex-wrap: wrap;
|
|
|
|
|
margin-bottom: 2rem;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.btn {
|
|
|
|
|
padding: 0.75rem 1.5rem;
|
|
|
|
|
border: none;
|
|
|
|
|
border-radius: 8px;
|
|
|
|
|
font-weight: 600;
|
|
|
|
|
cursor: pointer;
|
|
|
|
|
transition: all 0.2s ease-in-out;
|
|
|
|
|
}
|
|
|
|
|
.btn:hover { opacity: 0.9; }
|
|
|
|
|
.btn-primary { background-color: #4361ee; color: white; }
|
|
|
|
|
.btn-success { background-color: #198754; color: white; }
|
|
|
|
|
.btn-danger { background-color: #dc3545; color: white; }
|
|
|
|
|
|
|
|
|
|
table {
|
|
|
|
|
width: 100%;
|
|
|
|
|
border-collapse: collapse;
|
|
|
|
|
background: #fff;
|
|
|
|
|
border-radius: 10px;
|
|
|
|
|
overflow: hidden;
|
|
|
|
|
box-shadow: 0 0 10px rgba(0,0,0,0.05);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
th, td {
|
|
|
|
|
padding: 1rem;
|
|
|
|
|
text-align: left;
|
|
|
|
|
border-bottom: 1px solid #eee;
|
|
|
|
|
}
|
|
|
|
|
th {
|
|
|
|
|
background-color: #f2f2f2;
|
|
|
|
|
font-size: 0.85rem;
|
|
|
|
|
color: #555;
|
|
|
|
|
text-transform: uppercase;
|
|
|
|
|
letter-spacing: 1px;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.status-ok { color: #198754; }
|
|
|
|
|
.status-error { color: #dc3545; }
|
|
|
|
|
.status-warn { color: #ffc107; }
|
2025-07-28 22:31:31 -03:00
|
|
|
|
|
|
|
|
.status-processing {
|
|
|
|
|
color: #0d6efd; /* azul */
|
|
|
|
|
font-weight: bold;
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-28 13:29:45 -03:00
|
|
|
</style>
|
|
|
|
|
|
|
|
|
|
{% endblock %}
|