OCR
This commit is contained in:
63
static/js/receipt_analysis.js
Normal file
63
static/js/receipt_analysis.js
Normal file
@@ -0,0 +1,63 @@
|
||||
document.addEventListener("DOMContentLoaded", () => {
|
||||
const analyzeBtn = document.getElementById("analyzeBtn");
|
||||
if (analyzeBtn) {
|
||||
analyzeBtn.addEventListener("click", () => analyzeReceipts(LIST_ID));
|
||||
}
|
||||
});
|
||||
|
||||
async function analyzeReceipts(listId) {
|
||||
const resultsDiv = document.getElementById("analysisResults");
|
||||
resultsDiv.innerHTML = `<div class="text-info">⏳ Trwa analiza paragonów...</div>`;
|
||||
|
||||
const start = performance.now(); // ⏱ START
|
||||
|
||||
try {
|
||||
const res = await fetch(`/lists/${listId}/analyze`, { method: "POST" });
|
||||
const data = await res.json();
|
||||
|
||||
const duration = ((performance.now() - start) / 1000).toFixed(2); // ⏱ STOP
|
||||
|
||||
let html = `<p><b>📊 Łącznie wykryto:</b> ${data.total.toFixed(2)} PLN</p>`;
|
||||
html += `<p class="text-secondary"><small>⏱ Czas analizy OCR: ${duration} sek.</small></p>`;
|
||||
|
||||
data.results.forEach((r, i) => {
|
||||
html += `
|
||||
<div class="mb-2">
|
||||
<span class="text-light">${r.filename}</span>:
|
||||
<input type="number" id="amount-${i}" value="${r.amount}" step="0.01" class="form-control d-inline-block bg-dark text-white border-light rounded" style="width: 120px;">
|
||||
<button onclick="emitExpense(${i})" class="btn btn-sm btn-outline-success ms-2">➕ Dodaj</button>
|
||||
</div>`;
|
||||
});
|
||||
|
||||
if (data.results.length > 1) {
|
||||
html += `<button onclick="emitAllExpenses(${data.results.length})" class="btn btn-success mt-3">➕ Dodaj wszystkie</button>`;
|
||||
}
|
||||
|
||||
resultsDiv.innerHTML = html;
|
||||
window._ocr_results = data.results;
|
||||
|
||||
} catch (err) {
|
||||
resultsDiv.innerHTML = `<div class="text-danger">❌ Wystąpił błąd podczas analizy.</div>`;
|
||||
console.error(err);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
function emitExpense(i) {
|
||||
const r = window._ocr_results[i];
|
||||
const val = parseFloat(document.getElementById(`amount-${i}`).value);
|
||||
if (!isNaN(val) && val > 0) {
|
||||
socket.emit('add_expense', {
|
||||
list_id: LIST_ID,
|
||||
amount: val
|
||||
|
||||
});
|
||||
document.getElementById(`amount-${i}`).disabled = true;
|
||||
}
|
||||
}
|
||||
|
||||
function emitAllExpenses(n) {
|
||||
for (let i = 0; i < n; i++) {
|
||||
emitExpense(i);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user