Files
item-code/templates/index.html
T
2026-10-13 16:00:15 +05:30

797 lines
23 KiB
HTML

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Invoice Extraction with Item Code Enrichment</title>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
min-height: 100vh;
display: flex;
justify-content: center;
align-items: center;
padding: 20px;
}
.container {
background: white;
border-radius: 10px;
box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3);
max-width: 1000px;
width: 100%;
padding: 40px;
}
.header {
text-align: center;
margin-bottom: 40px;
}
.header h1 {
color: #333;
font-size: 28px;
margin-bottom: 10px;
}
.header p {
color: #666;
font-size: 16px;
}
.upload-section {
background: #f8f9fa;
border: 2px dashed #667eea;
border-radius: 8px;
padding: 40px;
text-align: center;
cursor: pointer;
transition: all 0.3s ease;
margin-bottom: 30px;
}
.upload-section:hover {
border-color: #764ba2;
background: #f0f2ff;
}
.upload-section.dragover {
border-color: #764ba2;
background: #e8ebff;
}
.upload-icon {
font-size: 48px;
margin-bottom: 15px;
}
.upload-section p {
color: #666;
font-size: 16px;
margin-bottom: 10px;
}
.upload-section .file-input {
display: none;
}
.upload-btn {
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
color: white;
border: none;
padding: 12px 30px;
border-radius: 5px;
cursor: pointer;
font-size: 16px;
font-weight: 600;
transition: transform 0.2s;
}
.upload-btn:hover {
transform: translateY(-2px);
}
.upload-btn:active {
transform: translateY(0);
}
.button-group {
display: flex;
gap: 15px;
justify-content: center;
flex-wrap: wrap;
}
.secondary-btn {
background: #e9ecef;
color: #333;
border: none;
padding: 12px 30px;
border-radius: 5px;
cursor: pointer;
font-size: 16px;
font-weight: 600;
transition: all 0.2s;
}
.secondary-btn:hover {
background: #dee2e6;
transform: translateY(-2px);
}
.loading {
display: none;
text-align: center;
padding: 40px;
}
.spinner {
display: inline-block;
width: 40px;
height: 40px;
border: 4px solid #f3f3f3;
border-top: 4px solid #667eea;
border-radius: 50%;
animation: spin 1s linear infinite;
margin-bottom: 20px;
}
@keyframes spin {
0% { transform: rotate(0deg); }
100% { transform: rotate(360deg); }
}
.loading p {
color: #666;
font-size: 16px;
}
.results {
display: none;
margin-top: 40px;
}
.results.show {
display: block;
}
.result-header {
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 20px;
padding-bottom: 15px;
border-bottom: 2px solid #e9ecef;
}
.result-header h2 {
color: #333;
font-size: 22px;
}
.success-badge {
background: #d4edda;
color: #155724;
padding: 8px 16px;
border-radius: 20px;
font-size: 14px;
font-weight: 600;
}
.error-badge {
background: #f8d7da;
color: #721c24;
padding: 8px 16px;
border-radius: 20px;
font-size: 14px;
font-weight: 600;
}
.metadata {
background: #f8f9fa;
padding: 15px;
border-radius: 5px;
margin-bottom: 20px;
font-size: 14px;
color: #666;
}
.metadata p {
margin: 8px 0;
}
.metadata strong {
color: #333;
}
.json-display {
background: #1e1e1e;
color: #d4d4d4;
padding: 20px;
border-radius: 5px;
overflow-x: auto;
font-family: 'Courier New', monospace;
font-size: 12px;
line-height: 1.6;
max-height: 600px;
overflow-y: auto;
}
.json-display::-webkit-scrollbar {
width: 8px;
height: 8px;
}
.json-display::-webkit-scrollbar-track {
background: #2d2d2d;
}
.json-display::-webkit-scrollbar-thumb {
background: #555;
border-radius: 4px;
}
.json-display::-webkit-scrollbar-thumb:hover {
background: #777;
}
.tabs {
display: flex;
gap: 10px;
margin-bottom: 20px;
border-bottom: 2px solid #e9ecef;
}
.tab {
padding: 12px 20px;
cursor: pointer;
border-bottom: 3px solid transparent;
color: #666;
font-weight: 600;
transition: all 0.2s;
}
.tab:hover {
color: #667eea;
}
.tab.active {
color: #667eea;
border-bottom-color: #667eea;
}
.tab-content {
display: none;
}
.tab-content.active {
display: block;
}
.items-table {
width: 100%;
border-collapse: collapse;
font-size: 14px;
}
.items-table th {
background: #f8f9fa;
padding: 12px;
text-align: left;
border-bottom: 2px solid #dee2e6;
color: #333;
font-weight: 600;
}
.items-table td {
padding: 12px;
border-bottom: 1px solid #dee2e6;
color: #666;
}
.items-table tbody tr:hover {
background: #f8f9fa;
}
.item-code-tag {
display: inline-block;
padding: 4px 8px;
border-radius: 3px;
font-size: 12px;
font-weight: 600;
}
.item-code-api {
background: #d1ecf1;
color: #0c5460;
}
.item-code-matched {
background: #d4edda;
color: #155724;
}
.item-code-missing {
background: #f8d7da;
color: #721c24;
}
.action-buttons {
display: flex;
gap: 10px;
justify-content: center;
margin-top: 30px;
flex-wrap: wrap;
}
.copy-btn {
background: #6c757d;
color: white;
border: none;
padding: 10px 20px;
border-radius: 5px;
cursor: pointer;
font-size: 14px;
font-weight: 600;
transition: all 0.2s;
}
.copy-btn:hover {
background: #5a6268;
}
.download-btn {
background: #28a745;
color: white;
border: none;
padding: 10px 20px;
border-radius: 5px;
cursor: pointer;
font-size: 14px;
font-weight: 600;
transition: all 0.2s;
}
.download-btn:hover {
background: #218838;
}
.error-message {
background: #f8d7da;
border: 1px solid #f5c6cb;
color: #721c24;
padding: 15px;
border-radius: 5px;
margin-top: 20px;
display: none;
}
.error-message.show {
display: block;
}
.success-message {
background: #d4edda;
border: 1px solid #c3e6cb;
color: #155724;
padding: 15px;
border-radius: 5px;
margin-top: 20px;
display: none;
}
.success-message.show {
display: block;
}
.info-message {
background: #d1ecf1;
border: 1px solid #bee5eb;
color: #0c5460;
padding: 15px;
border-radius: 5px;
margin-bottom: 20px;
}
@media (max-width: 768px) {
.container {
padding: 20px;
}
.header h1 {
font-size: 24px;
}
.upload-section {
padding: 30px 20px;
}
.button-group {
flex-direction: column;
}
.upload-btn, .secondary-btn {
width: 100%;
}
.tabs {
flex-wrap: wrap;
}
.items-table {
font-size: 12px;
}
.items-table th, .items-table td {
padding: 8px;
}
}
.stat {
display: inline-block;
margin: 0 15px;
padding: 10px 15px;
background: #f8f9fa;
border-radius: 5px;
color: #666;
font-size: 14px;
}
.stat strong {
color: #667eea;
font-size: 18px;
}
</style>
</head>
<body>
<div class="container">
<div class="header">
<h1>📋 Invoice Extraction & Item Code Enrichment</h1>
<p>Upload invoices and automatically extract item codes from your drug database</p>
</div>
<div class="upload-section" id="uploadSection">
<div class="upload-icon">📄</div>
<p>Drag and drop your invoice PDF here, or click to select</p>
<button class="upload-btn" onclick="document.getElementById('fileInput').click()">
Choose File
</button>
<input
type="file"
id="fileInput"
class="file-input"
accept=".pdf,.png,.jpg,.jpeg"
onchange="handleFileSelect(event)"
>
<p style="margin-top: 15px; color: #999; font-size: 12px;">
Supported: PDF, PNG, JPG, JPEG • Max: 100MB
</p>
</div>
<div class="loading" id="loading">
<div class="spinner"></div>
<p>Extracting invoice data...</p>
<p style="font-size: 12px; color: #999; margin-top: 10px;">This may take a few moments</p>
</div>
<div class="error-message" id="errorMessage"></div>
<div class="success-message" id="successMessage"></div>
<div class="results" id="results">
<div class="result-header">
<h2>Extraction Results</h2>
<div id="resultBadge"></div>
</div>
<div class="metadata">
<p><strong>Upload ID:</strong> <span id="uploadId">-</span></p>
<p><strong>Timestamp:</strong> <span id="timestamp">-</span></p>
<p><strong>Filename:</strong> <span id="filename">-</span></p>
<div style="margin-top: 10px;">
<span class="stat"><strong id="totalItems">0</strong> Total Items</span>
<span class="stat"><strong id="itemsWithCode">0</strong> With Codes</span>
<span class="stat"><strong id="itemsMatched">0</strong> Matched from DB</span>
</div>
</div>
<div class="tabs">
<div class="tab active" onclick="switchTab('headers')">📋 Header Fields</div>
<div class="tab" onclick="switchTab('items')">📦 Line Items</div>
<div class="tab" onclick="switchTab('raw')">📄 Raw JSON</div>
</div>
<div id="headers" class="tab-content active">
<div id="headerFields"></div>
</div>
<div id="items" class="tab-content">
<table class="items-table">
<thead>
<tr>
<th>Description</th>
<th>Quantity</th>
<th>Unit Price</th>
<th>Item Code</th>
<th>Source</th>
</tr>
</thead>
<tbody id="itemsTable">
</tbody>
</table>
</div>
<div id="raw" class="tab-content">
<div class="json-display" id="jsonDisplay"></div>
</div>
<div class="action-buttons">
<button class="copy-btn" onclick="copyToClipboard()">📋 Copy JSON</button>
<button class="download-btn" onclick="downloadJSON()">⬇️ Download JSON</button>
<button class="secondary-btn" onclick="resetForm()">🔄 Extract Another</button>
</div>
</div>
</div>
<script>
let lastExtractedData = null;
let selectedFile = null;
// Drag and drop
const uploadSection = document.getElementById('uploadSection');
uploadSection.addEventListener('dragover', (e) => {
e.preventDefault();
uploadSection.classList.add('dragover');
});
uploadSection.addEventListener('dragleave', () => {
uploadSection.classList.remove('dragover');
});
uploadSection.addEventListener('drop', (e) => {
e.preventDefault();
uploadSection.classList.remove('dragover');
const files = e.dataTransfer.files;
if (files.length > 0) {
handleFile(files[0]);
}
});
function handleFileSelect(event) {
const files = event.target.files;
if (files.length > 0) {
handleFile(files[0]);
}
}
function handleFile(file) {
const validTypes = ['application/pdf', 'image/png', 'image/jpeg'];
if (!validTypes.includes(file.type)) {
showError('Invalid file type. Please upload PDF, PNG, or JPG');
return;
}
if (file.size > 100 * 1024 * 1024) {
showError('File size exceeds 100MB limit');
return;
}
selectedFile = file;
uploadInvoice();
}
async function uploadInvoice() {
if (!selectedFile) return;
const formData = new FormData();
formData.append('file', selectedFile);
document.getElementById('loading').style.display = 'block';
document.getElementById('results').classList.remove('show');
hideMessages();
try {
const response = await fetch('/api/upload', {
method: 'POST',
body: formData
});
const result = await response.json();
if (response.ok && result.success) {
lastExtractedData = result.data;
displayResults(result.data);
showSuccess('Invoice extracted and enriched successfully!');
} else {
showError(result.error || 'Failed to extract invoice');
}
} catch (error) {
console.error('Error:', error);
showError('Network error. Please try again.');
} finally {
document.getElementById('loading').style.display = 'none';
}
}
function displayResults(data) {
// Update metadata
document.getElementById('uploadId').textContent = data._metadata?.upload_id || '-';
document.getElementById('timestamp').textContent = new Date(data._metadata?.upload_timestamp).toLocaleString();
document.getElementById('filename').textContent = data._metadata?.original_filename || '-';
// Calculate statistics
const items = data.items || [];
const itemsWithCode = items.filter(i => i.item_code).length;
const itemsMatched = items.filter(i => i.item_code_source === 'drug_list_match').length;
document.getElementById('totalItems').textContent = items.length;
document.getElementById('itemsWithCode').textContent = itemsWithCode;
document.getElementById('itemsMatched').textContent = itemsMatched;
// Set badge
const badge = document.getElementById('resultBadge');
if (items.length > 0 && itemsWithCode === items.length) {
badge.innerHTML = '<span class="success-badge">✓ All items have codes</span>';
} else if (itemsWithCode > 0) {
badge.innerHTML = `<span class="success-badge">✓ ${itemsWithCode}/${items.length} items have codes</span>`;
} else {
badge.innerHTML = '<span class="error-badge">⚠ Some items missing codes</span>';
}
// Display header fields
displayHeaderFields(data);
// Display items table
displayItemsTable(items);
// Display raw JSON
document.getElementById('jsonDisplay').textContent = JSON.stringify(data, null, 2);
// Show results section
document.getElementById('results').classList.add('show');
}
function displayHeaderFields(data) {
const headerContainer = document.getElementById('headerFields');
const headerFields = [
'invoice_id', 'date', 'seller_name', 'seller_gstin',
'buyer_name', 'buyer_gstin', 'total_quantity',
'total_taxable_amount', 'total_gst_amount', 'invoice_amount'
];
let html = '<table class="items-table" style="margin-bottom: 20px;"><tbody>';
headerFields.forEach(field => {
if (field in data) {
const value = data[field];
const displayValue = value !== null ? String(value) : 'N/A';
html += `
<tr>
<td style="font-weight: 600; width: 30%;">${field}</td>
<td>${displayValue}</td>
</tr>
`;
}
});
html += '</tbody></table>';
headerContainer.innerHTML = html;
}
function displayItemsTable(items) {
const tbody = document.getElementById('itemsTable');
tbody.innerHTML = '';
items.forEach((item, index) => {
const code = item.item_code || '-';
const source = item.item_code_source || 'unknown';
let sourceTag = 'item-code-missing';
let sourceText = 'Not Found';
if (source === 'api_extracted') {
sourceTag = 'item-code-api';
sourceText = 'API Extracted';
} else if (source === 'drug_list_match') {
sourceTag = 'item-code-matched';
sourceText = 'Matched from DB';
}
const row = document.createElement('tr');
row.innerHTML = `
<td>${item.description || '-'}</td>
<td>${item.quantity || '-'}</td>
<td>${item.unit_price || '-'}</td>
<td><strong>${code}</strong></td>
<td><span class="item-code-tag ${sourceTag}">${sourceText}</span></td>
`;
tbody.appendChild(row);
});
}
function switchTab(tabName) {
// Hide all tabs
document.querySelectorAll('.tab-content').forEach(tab => {
tab.classList.remove('active');
});
document.querySelectorAll('.tab').forEach(tab => {
tab.classList.remove('active');
});
// Show selected tab
document.getElementById(tabName).classList.add('active');
event.target.classList.add('active');
}
function copyToClipboard() {
const json = JSON.stringify(lastExtractedData, null, 2);
navigator.clipboard.writeText(json).then(() => {
showSuccess('JSON copied to clipboard!');
}).catch(err => {
showError('Failed to copy to clipboard');
});
}
function downloadJSON() {
const json = JSON.stringify(lastExtractedData, null, 2);
const blob = new Blob([json], { type: 'application/json' });
const url = window.URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = `invoice_${lastExtractedData._metadata?.upload_id || 'export'}.json`;
a.click();
window.URL.revokeObjectURL(url);
}
function resetForm() {
selectedFile = null;
lastExtractedData = null;
document.getElementById('fileInput').value = '';
document.getElementById('results').classList.remove('show');
hideMessages();
document.getElementById('uploadSection').style.display = 'block';
}
function showError(message) {
const errorDiv = document.getElementById('errorMessage');
errorDiv.textContent = '❌ ' + message;
errorDiv.classList.add('show');
}
function showSuccess(message) {
const successDiv = document.getElementById('successMessage');
successDiv.textContent = '✓ ' + message;
successDiv.classList.add('show');
setTimeout(() => {
successDiv.classList.remove('show');
}, 5000);
}
function hideMessages() {
document.getElementById('errorMessage').classList.remove('show');
document.getElementById('successMessage').classList.remove('show');
}
// Load config on page load
window.addEventListener('load', async () => {
try {
const response = await fetch('/api/config');
const config = await response.json();
console.log('✅ Application loaded', config);
} catch (error) {
console.error('Error loading config:', error);
}
});
</script>
</body>
</html>