updated to silent print

This commit is contained in:
2025-09-24 21:42:22 +03:00
parent b49a22832d
commit 198563aaba
26 changed files with 693 additions and 1520 deletions

View File

@@ -8,6 +8,17 @@
overflow: hidden;
}
/* Inserted custom CSS from user */
.card.scan-table-card table.print-module-table.scan-table thead th {
border-bottom: 2e6 !important;
background-color: #f8f9fa !important;
padding: 0.25rem 0.4rem !important;
text-align: left !important;
font-weight: 600 !important;
font-size: 10px !important;
line-height: 1.2 !important;
}
/* Enhanced table styling to match view_orders.html with higher specificity */
.card.scan-table-card table.print-module-table.scan-table {
width: 100% !important;
@@ -849,77 +860,101 @@ async function updatePrintedStatus(orderId) {
}
// PDF generation handler
// Helper to get extension ID injected by content script
function getInjectedExtensionId() {
const el = document.getElementById('chrome-extension-id');
if (el) return el.getAttribute('data-extension-id');
return null;
}
function addPDFGenerationHandler() {
const printButton = document.getElementById('print-label-btn');
if (printButton) {
printButton.addEventListener('click', async function(e) {
e.preventDefault();
// Get selected order
const selectedRow = document.querySelector('.print-module-table tbody tr.selected');
if (!selectedRow) {
alert('Please select an order first from the table below.');
return;
}
const orderId = selectedRow.dataset.orderId;
const prodOrder = selectedRow.querySelector('td:nth-child(2)').textContent.trim();
const quantity = selectedRow.querySelector('td:nth-child(5)').textContent.trim();
if (!orderId) {
alert('Error: Could not determine order ID.');
return;
}
// Show loading state
const originalText = printButton.innerHTML;
const originalColor = printButton.style.background;
printButton.innerHTML = '⏳ Processing...';
printButton.disabled = true;
if (!printButton) return;
printButton.addEventListener('click', async function(e) {
e.preventDefault();
// Get selected order
const selectedRow = document.querySelector('.print-module-table tbody tr.selected');
if (!selectedRow) {
alert('Please select an order first from the table below.');
return;
}
const orderId = selectedRow.dataset.orderId;
const prodOrder = selectedRow.querySelector('td:nth-child(2)').textContent.trim();
const quantity = selectedRow.querySelector('td:nth-child(5)').textContent.trim();
if (!orderId) {
alert('Error: Could not determine order ID.');
return;
}
// Show loading state
const originalText = printButton.innerHTML;
const originalColor = printButton.style.background;
printButton.innerHTML = '⏳ Processing...';
printButton.disabled = true;
try {
// Step 1: Generate PDF and get its URL
const pdfResponse = await fetch(`/generate_labels_pdf/${orderId}`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' }
});
if (!pdfResponse.ok) throw new Error('Failed to generate PDF');
// Try to get the PDF URL from the response (assume server returns a URL or we can construct it)
// If not, fallback to download
let pdfUrl = '';
try {
let success = false;
console.log(`🖨️ Print operation started - Service available: ${printServiceAvailable}`);
// Try Windows service first if available
if (printServiceAvailable) {
console.log('🚀 Attempting silent print via Windows service...');
try {
success = await printLabelsWithService(orderId, prodOrder, quantity);
console.log(`✅ Windows service print result: ${success}`);
} catch (serviceError) {
console.error('❌ Windows service failed:', serviceError);
console.warn('🔄 Falling back to PDF download mode');
printServiceAvailable = false; // Mark as unavailable for this session
updatePrintButtonForService(false);
}
} else {
console.log('📄 Service not available, using PDF download mode');
}
// Fallback to PDF download if service failed or unavailable
if (!success) {
console.log('📥 Generating PDF for download...');
success = await downloadPDFLabels(orderId, prodOrder, quantity);
console.log(`📄 PDF download result: ${success}`);
}
} catch (error) {
console.error('Print operation failed:', error);
alert(`❌ Print operation failed: ${error.message}\n\nPlease check:\n• Windows Print Service is running\n• Chrome extension is installed\n• Network connectivity\n• PDF generation permissions`);
} finally {
// Reset button state
printButton.innerHTML = originalText;
printButton.style.background = originalColor;
printButton.disabled = false;
// Recheck service availability for next operation
setTimeout(checkPrintServiceAvailability, 2000);
const data = await pdfResponse.json();
pdfUrl = data.pdf_url || '';
} catch {
// If not JSON, fallback to constructing the URL
pdfUrl = `/static/generated_labels/labels_${prodOrder}_${quantity}pcs.pdf`;
}
});
}
// Step 2: Prepare print job for Chrome extension
const selectedPrinter = getSelectedPrinter();
const printJob = {
pdfUrl: window.location.origin + pdfUrl,
printer: selectedPrinter,
orderId: orderId,
prodOrder: prodOrder,
quantity: quantity
};
// Step 3: Get extension ID from injected DOM
const extensionId = getInjectedExtensionId();
// Step 4: Send message to Chrome extension
if (window.chrome && window.chrome.runtime && window.chrome.runtime.sendMessage && extensionId) {
window.chrome.runtime.sendMessage(
extensionId,
{ action: 'print_pdf', ...printJob },
function(response) {
if (response && response.success) {
alert('✅ Labels sent to printer!\nOrder: ' + prodOrder + '\nQuantity: ' + quantity + '\nPrinter: ' + selectedPrinter);
updatePrintedStatus(orderId);
} else {
alert('❌ Failed to print via extension. PDF will be downloaded.');
downloadPDFLabels(orderId, prodOrder, quantity);
}
}
);
} else {
// Fallback: Download PDF
alert(' Chrome extension not detected or extension ID not injected. PDF will be downloaded.');
await downloadPDFLabels(orderId, prodOrder, quantity);
}
} catch (error) {
console.error('Print operation failed:', error);
alert('❌ Print operation failed: ' + error.message);
} finally {
printButton.innerHTML = originalText;
printButton.style.background = originalColor;
printButton.disabled = false;
}
});
}
</script>
{% endblock %}