aiinfinite latest code

This commit is contained in:
2026-10-07 17:31:08 +05:30
commit e6cca0fe46
50 changed files with 28776 additions and 0 deletions
+680
View File
@@ -0,0 +1,680 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Manual Item Codes - AI Infinite</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/css/bootstrap.min.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
<style>
:root {
--primary-color: #007bff;
--background-light: #f8f9fa;
--text-dark: #333;
--white: #fff;
}
body {
font-family: Arial, sans-serif;
color: var(--text-dark);
margin: 0;
padding: 0;
line-height: 1.6;
background-color: var(--background-light);
}
.navbar {
background-color: #f7f7f7;
padding: 1rem;
display: flex;
justify-content: space-between;
align-items: center;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}
.navbar-brand {
font-size: 1.5rem;
font-weight: bold;
color: var(--primary-color);
text-decoration: none;
display: flex;
align-items: center;
}
.navbar-toggler {
padding: 0.25rem 0.5rem;
border: none;
}
.navbar-toggler-icon {
background-image: url("data:image/svg+xml,%3csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 30 30'%3e%3cpath stroke='rgba(0, 123, 255, 1)' stroke-linecap='round' stroke-miterlimit='10' stroke-width='2' d='M4 7h22M4 15h22M4 23h22'/%3e%3c/svg%3e");
}
.main-container {
display: flex;
min-height: calc(100vh - 60px);
}
.sidebar {
width: 250px;
background-color: var(--white);
border-right: 1px solid #dee2e6;
padding: 1.5rem;
position: fixed;
height: 100%;
z-index: 1000;
transform: translateX(-100%);
transition: transform 0.3s ease;
}
.sidebar.active {
transform: translateX(0);
}
.sidebar .nav-link {
display: flex;
align-items: center;
gap: 0.5rem;
color: var(--text-dark);
padding: 0.75rem 1rem;
margin: 0.3rem 0;
border-radius: 5px;
text-decoration: none;
transition: background-color 0.3s;
}
.sidebar .nav-link:hover {
background-color: var(--background-light);
}
.sidebar .nav-link.active {
background-color: #e9ecef;
font-weight: bold;
}
.container {
max-width: 1000px;
margin: 2rem auto;
padding: 0 1rem;
flex-grow: 1;
}
.card {
background-color: var(--white);
border-radius: 8px;
box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
padding: 1.5rem;
margin-top: 1.5rem;
}
.card-title {
font-size: 1.5rem;
margin-bottom: 1rem;
color: var(--text-dark);
}
.form-label {
font-weight: 500;
color: var(--text-dark);
}
.form-control {
border-radius: 5px;
border: 1px solid #ccc;
padding: 0.75rem;
width: 100%;
margin-bottom: 1rem;
}
.btn-primary {
background-color: var(--primary-color);
color: var(--white);
padding: 0.75rem 1.5rem;
border: none;
border-radius: 5px;
font-weight: bold;
transition: background-color 0.3s;
}
.btn-primary:hover {
background-color: #0056b3;
}
.btn-primary:disabled {
opacity: 0.6;
cursor: not-allowed;
}
.btn-secondary {
background-color: #6c757d;
color: var(--white);
padding: 0.6rem 1.2rem;
border: none;
border-radius: 5px;
transition: background-color 0.3s;
}
.btn-secondary:hover {
background-color: #5a6268;
}
.loading-container {
display: none;
justify-content: center;
align-items: center;
gap: 10px;
padding: 2rem 0;
}
.dot {
width: 15px;
height: 15px;
border-radius: 50%;
background-color: var(--primary-color);
animation: bounce 1.5s infinite ease-in-out;
}
.dot:nth-child(1) { animation-delay: 0s; }
.dot:nth-child(2) { animation-delay: 0.2s; }
.dot:nth-child(3) { animation-delay: 0.4s; }
@keyframes bounce {
0%, 100% { transform: translateY(0); }
50% { transform: translateY(-10px); }
}
.items-table {
width: 100%;
border-collapse: collapse;
margin-top: 1rem;
font-size: 0.9rem;
}
.items-table th, .items-table td {
padding: 10px 12px;
border-bottom: 1px solid #e9ecef;
text-align: left;
vertical-align: middle;
}
.items-table th {
background-color: var(--background-light);
font-weight: 600;
}
.code-input {
width: 100%;
min-width: 130px;
padding: 6px 10px;
border: 1px solid #ccc;
border-radius: 5px;
font-size: 0.9rem;
}
.code-input:focus {
outline: none;
border-color: var(--primary-color);
box-shadow: 0 0 0 3px rgba(0, 123, 255, 0.15);
}
.code-input.empty {
border-color: #f0ad4e;
background: #fffbea;
}
.badge-source {
display: inline-block;
padding: 4px 10px;
border-radius: 12px;
font-size: 0.75rem;
font-weight: 600;
}
.badge-invoice { background: #d1e7ff; color: #084298; }
.badge-matched { background: #d1f5e0; color: #0f5132; }
.badge-manual { background: #e2d9f3; color: #4b2a85; }
.badge-missing { background: #f8d7da; color: #842029; }
.badge-edited { background: #fff3cd; color: #856404; }
.summary-badge {
display: inline-block;
padding: 6px 14px;
border-radius: 20px;
font-size: 0.85rem;
font-weight: 600;
}
.file-chip {
display: inline-flex;
align-items: center;
gap: 10px;
margin-top: 10px;
padding: 6px 12px;
background: var(--background-light);
border-radius: 20px;
font-size: 0.9rem;
}
.file-chip button {
border: none;
background: transparent;
cursor: pointer;
color: #888;
}
.file-chip button:hover { color: #c33; }
.table-scroll { overflow-x: auto; }
.json-viewer {
background: #1e1e2e;
color: #cdd6f4;
padding: 16px;
border-radius: 8px;
margin-top: 15px;
font-family: Consolas, 'Liberation Mono', Menlo, monospace;
font-size: 0.8rem;
line-height: 1.5;
max-height: 500px;
overflow: auto;
white-space: pre;
}
.json-key { color: #89b4fa; }
.json-string { color: #a6e3a1; }
.json-number { color: #fab387; }
.json-boolean { color: #cba6f7; }
.json-null { color: #f38ba8; }
.hint { color: #888; font-size: 0.85rem; margin: 10px 0; }
.hidden { display: none !important; }
@media (max-width: 768px) {
.sidebar { width: 220px; }
.container { max-width: 95%; margin: 1rem auto; }
}
</style>
</head>
<body>
<nav class="navbar">
<div class="container-fluid">
<button class="navbar-toggler" id="sidebarToggle"><span class="navbar-toggler-icon"></span></button>
<a href="{{ url_for('index') }}" class="navbar-brand">
<a src="{{ url_for('static', filename='logos/aiinfinite.png') }}" alt="AI Infinite Logo"
style="height: 40px;">
</a>
</div>
</nav>
<div class="main-container">
<div class="sidebar">
<div class="nav flex-column">
<a class="nav-link" href="{{ url_for('home_logged_in') }}"><i class="fas fa-home"></i> Home</a>
<a class="nav-link" href="{{ url_for('dashboard') }}"><i class="fas fa-tachometer-alt"></i> Dashboard Overview</a>
<a class="nav-link" href="{{ url_for('view_history') }}"><i class="fas fa-history"></i> View Document History</a>
<a class="nav-link active" href="{{ url_for('manual_item_codes_page') }}"><i class="fas fa-edit"></i> Manual Item Codes</a>
<a class="nav-link" href="{{ url_for('logout') }}"><i class="fas fa-sign-out-alt"></i> Logout</a>
</div>
</div>
<div class="container">
<div id="pageMessage"></div>
<!-- Upload Card -->
<div class="card">
<h5 class="card-title">Upload Invoice &amp; Add Item Codes</h5>
<p class="hint" style="margin-top: -8px;">Choose a file, extract it, then add or fix item codes for any items that need one.</p>
<div class="mb-3">
<label for="manualFile" class="form-label">Invoice File (PDF or Image)</label>
<input type="file" class="form-control" id="manualFile" accept=".pdf,.png,.jpg,.jpeg">
<div class="file-chip hidden" id="fileChip">
<span id="fileChipName"></span>
<button type="button" onclick="clearFile()" title="Remove">✕</button>
</div>
</div>
<div class="mb-3 form-check">
<input type="checkbox" class="form-check-input" id="useDrugList" checked>
<label class="form-check-label" for="useDrugList">
Try to auto-fill item codes from the drug list (drug_list.csv) first
</label>
</div>
<button type="button" class="btn btn-primary" id="extractBtn" onclick="extractInvoice()" disabled>
<i class="fas fa-magic"></i> Extract Invoice
</button>
</div>
<!-- Loading -->
<div class="loading-container" id="loadingContainer">
<div class="dot"></div>
<div class="dot"></div>
<div class="dot"></div>
</div>
<!-- Results -->
<div class="card hidden" id="resultsCard">
<div class="d-flex justify-content-between align-items-center flex-wrap" style="gap: 10px;">
<h5 class="card-title" style="margin-bottom: 0;">Item Codes</h5>
<span id="summaryBadge"></span>
</div>
<p class="hint">Rows highlighted in yellow have no item code yet. Type a code, then click Save. Press Enter to move to the next row.</p>
<div class="table-scroll">
<table class="items-table">
<thead>
<tr>
<th>#</th>
<th>Description</th>
<th>Qty</th>
<th>Unit Price</th>
<th>Item Code</th>
<th>Source</th>
</tr>
</thead>
<tbody id="itemsBody"></tbody>
</table>
</div>
<div class="d-flex flex-wrap" style="gap: 10px; margin-top: 1.25rem;">
<button type="button" class="btn btn-primary" id="saveBtn" onclick="saveItemCodes()" disabled>
<i class="fas fa-save"></i> Save Item Codes
</button>
<button type="button" class="btn btn-secondary" id="jsonToggleBtn" onclick="toggleJsonView()">
<i class="fas fa-code"></i> Show JSON
</button>
<button type="button" class="btn btn-secondary" onclick="resetPage()">
<i class="fas fa-redo"></i> Upload Another
</button>
</div>
<pre class="json-viewer hidden" id="jsonViewer"></pre>
</div>
</div>
</div>
<script>
document.getElementById('sidebarToggle').addEventListener('click', function () {
document.querySelector('.sidebar').classList.toggle('active');
});
let selectedFile = null;
let extractedData = null;
let extractionId = null;
document.getElementById('manualFile').addEventListener('change', function (e) {
if (e.target.files.length > 0) {
setFile(e.target.files[0]);
}
});
function setFile(file) {
const validExt = /\.(pdf|png|jpe?g)$/i;
if (!validExt.test(file.name)) {
showMessage('Please choose a PDF, PNG or JPG file.', 'danger');
return;
}
selectedFile = file;
hideMessage();
document.getElementById('fileChipName').textContent = file.name;
document.getElementById('fileChip').classList.remove('hidden');
document.getElementById('extractBtn').disabled = false;
}
function clearFile() {
selectedFile = null;
document.getElementById('manualFile').value = '';
document.getElementById('fileChip').classList.add('hidden');
document.getElementById('extractBtn').disabled = true;
}
function showMessage(text, type) {
document.getElementById('pageMessage').innerHTML =
`<div class="alert alert-${type} alert-dismissible fade show" role="alert">
<strong>${escapeHtml(text)}</strong>
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>`;
}
function hideMessage() {
document.getElementById('pageMessage').innerHTML = '';
}
function escapeHtml(s) {
const d = document.createElement('div');
d.textContent = s;
return d.innerHTML;
}
async function extractInvoice() {
if (!selectedFile) return;
const formData = new FormData();
formData.append('file', selectedFile);
formData.append('use_drug_list', document.getElementById('useDrugList').checked ? 'true' : 'false');
document.getElementById('loadingContainer').style.display = 'flex';
document.getElementById('resultsCard').classList.add('hidden');
document.getElementById('extractBtn').disabled = true;
hideMessage();
try {
const response = await fetch('/api/manual-extract', { method: 'POST', body: formData });
const result = await response.json();
if (response.ok && result.success) {
extractedData = result.data;
extractionId = result.extraction_id || null;
renderResults();
document.getElementById('resultsCard').classList.remove('hidden');
if (!extractionId) {
showMessage('Extracted, but this could not be saved to history — item codes cannot be saved.', 'warning');
} else {
showMessage('Invoice extracted. Add or fix item codes below, then click Save.', 'success');
}
} else {
showMessage(result.error || 'Extraction failed.', 'danger');
}
} catch (err) {
showMessage('Network error. Please try again.', 'danger');
} finally {
document.getElementById('loadingContainer').style.display = 'none';
document.getElementById('extractBtn').disabled = !selectedFile;
}
}
function sourceBadge(source) {
const map = {
invoice: ['badge-invoice', 'On Invoice'],
drug_list_match: ['badge-matched', 'Matched'],
manual: ['badge-manual', 'Manual'],
not_found: ['badge-missing', 'Not Found'],
};
const [cls, label] = map[source] || map.not_found;
const span = document.createElement('span');
span.className = `badge-source ${cls}`;
span.textContent = label;
return span;
}
function renderResults() {
const items = Array.isArray(extractedData.items) ? extractedData.items : [];
const tbody = document.getElementById('itemsBody');
tbody.replaceChildren();
items.forEach((rawItem, i) => {
const item = (rawItem && typeof rawItem === 'object') ? rawItem : {};
const code = item.item_code ? String(item.item_code).trim() : '';
const tr = document.createElement('tr');
tr.dataset.source = item.item_code_source || 'not_found';
[i + 1, item.description, item.quantity, item.unit_price].forEach(val => {
const td = document.createElement('td');
td.textContent = (val === undefined || val === null || val === '') ? '-' : val;
tr.appendChild(td);
});
const codeTd = document.createElement('td');
const input = document.createElement('input');
input.type = 'text';
input.className = 'code-input' + (code ? '' : ' empty');
input.value = code;
input.placeholder = 'Enter item code';
input.maxLength = 50;
input.dataset.index = i;
input.dataset.original = code;
input.addEventListener('input', onCodeInput);
input.addEventListener('keydown', e => {
if (e.key === 'Enter') {
e.preventDefault();
const inputs = [...document.querySelectorAll('#itemsBody .code-input')];
const next = inputs[inputs.indexOf(input) + 1];
if (next) next.focus();
}
});
codeTd.appendChild(input);
tr.appendChild(codeTd);
const statusTd = document.createElement('td');
statusTd.className = 'status-cell';
statusTd.appendChild(sourceBadge(tr.dataset.source));
tr.appendChild(statusTd);
tbody.appendChild(tr);
});
updateSummary();
refreshJson();
}
function onCodeInput(e) {
const input = e.target;
const value = input.value.trim();
const tr = input.closest('tr');
const statusCell = tr.querySelector('.status-cell');
input.classList.toggle('empty', value === '');
if (value !== input.dataset.original) {
const span = document.createElement('span');
span.className = 'badge-source badge-edited';
span.textContent = 'Edited';
statusCell.replaceChildren(span);
} else {
statusCell.replaceChildren(sourceBadge(tr.dataset.source));
}
updateSummary();
refreshJson();
}
function updateSummary() {
const inputs = [...document.querySelectorAll('#itemsBody .code-input')];
const total = inputs.length;
const withCode = inputs.filter(i => i.value.trim() !== '').length;
const unsaved = inputs.filter(i => i.value.trim() !== i.dataset.original).length;
const badge = document.getElementById('summaryBadge');
if (total > 0 && withCode === total) {
badge.innerHTML = '<span class="summary-badge badge-matched">✓ All items have codes</span>';
} else {
badge.innerHTML = `<span class="summary-badge badge-missing">${total - withCode} of ${total} missing codes</span>`;
}
const saveBtn = document.getElementById('saveBtn');
saveBtn.disabled = unsaved === 0 || !extractionId;
saveBtn.innerHTML = unsaved > 0
? `<i class="fas fa-save"></i> Save Item Codes (${unsaved})`
: `<i class="fas fa-save"></i> Save Item Codes`;
}
async function saveItemCodes() {
if (!extractionId) {
showMessage('This extraction was not saved to history, so codes cannot be saved.', 'danger');
return;
}
const item_codes = [...document.querySelectorAll('#itemsBody .code-input')]
.filter(i => i.value.trim() !== i.dataset.original)
.map(i => ({ index: Number(i.dataset.index), item_code: i.value.trim() }));
if (item_codes.length === 0) return;
const saveBtn = document.getElementById('saveBtn');
saveBtn.disabled = true;
saveBtn.textContent = 'Saving...';
try {
const response = await fetch(`/api/extraction/${extractionId}/item-codes`, {
method: 'PUT',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ item_codes })
});
const result = await response.json();
if (response.ok && result.success) {
extractedData = result.data;
renderResults();
showMessage(`Saved ${result.updated} item code(s).`, 'success');
} else {
showMessage(result.error || 'Failed to save item codes.', 'danger');
}
} catch (err) {
showMessage('Network error. Please try again.', 'danger');
} finally {
updateSummary();
}
}
function highlightJson(obj) {
const json = JSON.stringify(obj, null, 2)
.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
return json.replace(
/("(?:\\u[a-fA-F0-9]{4}|\\[^u]|[^\\"])*"(\s*:)?|\b(?:true|false|null)\b|-?\d+(?:\.\d+)?(?:[eE][+-]?\d+)?)/g,
(match) => {
let cls = 'json-number';
if (match.startsWith('"')) cls = match.endsWith(':') ? 'json-key' : 'json-string';
else if (match === 'true' || match === 'false') cls = 'json-boolean';
else if (match === 'null') cls = 'json-null';
return `<span class="${cls}">${match}</span>`;
}
);
}
function refreshJson() {
const viewer = document.getElementById('jsonViewer');
if (viewer.classList.contains('hidden')) return;
viewer.innerHTML = extractedData ? highlightJson(extractedData) : '';
}
function toggleJsonView() {
const viewer = document.getElementById('jsonViewer');
const nowHidden = viewer.classList.toggle('hidden');
document.getElementById('jsonToggleBtn').innerHTML = nowHidden
? '<i class="fas fa-code"></i> Show JSON'
: '<i class="fas fa-code"></i> Hide JSON';
refreshJson();
}
function resetPage() {
const saveBtn = document.getElementById('saveBtn');
if (!saveBtn.disabled && !confirm('You have unsaved item codes. Discard them?')) {
return;
}
clearFile();
extractedData = null;
extractionId = null;
document.getElementById('resultsCard').classList.add('hidden');
document.getElementById('jsonViewer').classList.add('hidden');
document.getElementById('jsonToggleBtn').innerHTML = '<i class="fas fa-code"></i> Show JSON';
hideMessage();
}
</script>
</body>
</html>