updated printing solutions

This commit is contained in:
2025-10-05 09:29:37 +03:00
parent f105da2012
commit 16641193de
11 changed files with 1072 additions and 22 deletions

View File

@@ -26,7 +26,7 @@
</div>
<div class="right-header">
<button id="theme-toggle" class="theme-toggle">Change to dark theme</button>
{% if request.endpoint in ['main.upload_data', 'main.print_module', 'main.label_templates', 'main.create_template'] %}
{% if request.endpoint in ['main.upload_data', 'main.upload_orders', 'main.print_module', 'main.label_templates', 'main.create_template', 'main.print_lost_labels', 'main.view_orders'] %}
<a href="{{ url_for('main.etichete') }}" class="btn go-to-main-etichete-btn">Main Page Etichete</a>
{% endif %}
<a href="{{ url_for('main.dashboard') }}" class="btn go-to-dashboard-btn">Go to Dashboard</a>

View File

@@ -1,5 +1,57 @@
{% extends "base.html" %}
{% block title %}Create Warehouse Locations{% endblock %}
{% block head %}
<style>
/* Barcode label preview */
#barcode-label-preview {
width: 4cm;
height: 8cm;
border: 2px solid #333;
background: white;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
margin: 10px auto;
position: relative;
}
#barcode-label-preview .location-text {
font-size: 12px;
font-weight: bold;
text-align: center;
margin-bottom: 10px;
}
#barcode-label-preview #location-barcode {
max-width: 90%;
max-height: 60%;
}
.print-section {
margin-top: 24px;
padding: 16px;
border: 1px solid #ddd;
border-radius: 5px;
background-color: #f9f9f9;
}
.location-row.selected {
background-color: #007bff !important;
color: white !important;
}
.location-row {
cursor: pointer;
}
.location-row:hover {
background-color: #f8f9fa;
}
</style>
{% endblock %}
{% block content %}
<div class="scan-container">
<!-- Add Warehouse Location Card -->
@@ -25,6 +77,38 @@
<a href="{{ url_for('warehouse.import_locations_csv') }}" class="btn" style="padding: 4px 12px; font-size: 0.95em;">Go to Import Page</a>
</div>
</div>
<!-- Print Barcode Section -->
<div class="print-section">
<h3 style="font-size: 1.1em; margin-bottom: 12px;">Print Location Barcode</h3>
<!-- Label Preview -->
<div id="barcode-label-preview">
<div class="location-text">
<div id="location-code-display">Select a location</div>
</div>
<canvas id="location-barcode" style="display: none;"></canvas>
<div id="barcode-placeholder" style="color: #999; font-style: italic;">No location selected</div>
</div>
<!-- Print Controls -->
<div style="text-align: center; margin-top: 15px;">
<div style="margin-bottom: 10px;">
<label for="printer-select" style="font-size: 11px; font-weight: 600;">Select Printer:</label>
<select id="printer-select" class="form-control form-control-sm" style="width: 200px; margin: 5px auto; font-size: 11px;">
<option value="">Loading printers...</option>
</select>
</div>
<button id="print-barcode-btn" class="btn btn-success" style="font-size: 12px; padding: 6px 20px;" disabled>
🖨️ Print Location Barcode
</button>
<button onclick="testQZConnection()" class="btn btn-info" style="font-size: 11px; padding: 4px 12px; margin-left: 8px;">
🔧 Test QZ Tray
</button>
<div id="print-status" style="margin-top: 8px; font-size: 11px;"></div>
</div>
</div>
<!-- Delete Location Area -->
{% if session['role'] in ['administrator', 'management'] %}
<div style="margin-top: 32px; padding: 12px; border-top: 1px solid #eee;">
@@ -45,7 +129,10 @@
<!-- Locations Table Card -->
<div class="card scan-table-card">
<h3>Warehouse Locations</h3>
<table class="scan-table">
<div id="location-selection-hint" style="margin-bottom: 10px; font-size: 11px; color: #666; text-align: center;">
Click on a row to select a location for printing
</div>
<table class="scan-table" id="locations-table">
<thead>
<tr>
<th>ID</th>
@@ -56,7 +143,7 @@
</thead>
<tbody>
{% for loc in locations %}
<tr>
<tr class="location-row" data-location-code="{{ loc[1] }}">
<td>{{ loc[0] }}</td>
<td>{{ loc[1] }}</td>
<td>{{ loc[2] }}</td>
@@ -67,4 +154,356 @@
</table>
</div>
</div>
<!-- Include required JS libraries -->
<script src="{{ url_for('static', filename='JsBarcode.all.min.js') }}"></script>
<script src="{{ url_for('static', filename='qz-tray.js') }}"></script>
<script src="{{ url_for('static', filename='html2canvas.min.js') }}"></script>
<script>
// Simplified notification system (matching print module)
function showNotification(message, type = 'info') {
const existingNotifications = document.querySelectorAll('.notification');
existingNotifications.forEach(n => n.remove());
const notification = document.createElement('div');
notification.className = `notification alert alert-${type === 'error' ? 'danger' : type === 'success' ? 'success' : type === 'warning' ? 'warning' : 'info'}`;
notification.style.cssText = `
position: fixed;
top: 20px;
right: 20px;
z-index: 9999;
max-width: 450px;
padding: 15px;
border-radius: 5px;
box-shadow: 0 4px 6px rgba(0,0,0,0.1);
`;
const formattedMessage = message.replace(/\n/g, '<br>');
notification.innerHTML = `
<div style="display: flex; align-items: flex-start; justify-content: space-between;">
<span style="flex: 1; padding-right: 10px; white-space: pre-wrap; font-family: monospace; font-size: 12px;">${formattedMessage}</span>
<button type="button" onclick="this.parentElement.parentElement.remove()" style="background: none; border: none; font-size: 20px; cursor: pointer; flex-shrink: 0;">&times;</button>
</div>
`;
document.body.appendChild(notification);
const timeout = type === 'error' ? 15000 : 5000;
setTimeout(() => {
if (notification.parentNode) {
notification.remove();
}
}, timeout);
}
let selectedLocationCode = null;
let qzTray = null;
let availablePrinters = [];
// Initialize QZ Tray connection (matching print module implementation)
async function initializeQZTray() {
try {
console.log('🔍 Checking for QZ Tray...');
// Check if QZ Tray library is loaded
if (typeof qz === 'undefined') {
console.error('❌ QZ Tray library not loaded');
const errorMsg = '❌ QZ Tray Library Not Loaded\n\n' +
'The QZ Tray JavaScript library failed to load.\n' +
'Check the browser console for errors and refresh the page.\n\n' +
'Path: /static/qz-tray.js';
document.getElementById('print-status').textContent = 'Library Error';
showNotification(errorMsg, 'error');
return false;
}
console.log('✅ QZ Tray library loaded');
console.log('🔒 Using patched qz-tray.js for pairing-key authentication...');
console.log('🔌 Attempting to connect to QZ Tray on client PC...');
// Set connection closed callback
qz.websocket.setClosedCallbacks(function() {
console.warn('⚠️ QZ Tray connection closed');
document.getElementById('print-status').textContent = 'QZ Tray Disconnected';
});
// Connect to QZ Tray running on client PC
console.log('⏳ Connecting...');
await qz.websocket.connect();
qzTray = qz;
const version = await qz.api.getVersion();
console.log('✅ QZ Tray connected successfully');
console.log('📋 QZ Tray Version:', version);
document.getElementById('print-status').textContent = 'QZ Tray Connected';
// Load printers
await loadQZTrayPrinters();
return true;
} catch (error) {
console.error('❌ QZ Tray connection failed:', error);
let errorMsg = '❌ QZ Tray Connection Failed\n\n';
let statusText = 'Connection Failed';
if (error.message && error.message.includes('certificate')) {
errorMsg += '🔒 Certificate Trust Required\n\n';
errorMsg += 'QZ Tray requires you to trust its certificate:\n';
errorMsg += '1. Open: https://localhost:8182\n';
errorMsg += '2. Accept/Trust the security certificate\n';
errorMsg += '3. Refresh this page and try again\n\n';
errorMsg += '🔍 This is normal and safe for QZ Tray';
statusText = 'Certificate Error';
} else {
errorMsg += '⚠️ Troubleshooting:\n';
errorMsg += '1. Make sure QZ Tray is running\n';
errorMsg += '2. Check firewall settings\n';
errorMsg += '3. Try restarting QZ Tray\n';
errorMsg += '4. Restart your browser\n\n';
errorMsg += 'Error: ' + error.message;
}
document.getElementById('print-status').textContent = statusText;
showNotification(errorMsg, 'error');
return false;
}
}
// Load available printers from QZ Tray (matching print module)
async function loadQZTrayPrinters() {
try {
if (!qzTray) return;
const printers = await qzTray.printers.find();
availablePrinters = printers;
const printerSelect = document.getElementById('printer-select');
printerSelect.innerHTML = '<option value="">Select a printer...</option>';
printers.forEach(printer => {
const option = document.createElement('option');
option.value = printer;
option.textContent = printer;
printerSelect.appendChild(option);
});
console.log(`📄 Found ${printers.length} printers:`, printers);
// Auto-select first thermal printer if available
const thermalPrinter = printers.find(p =>
p.toLowerCase().includes('thermal') ||
p.toLowerCase().includes('label') ||
p.toLowerCase().includes('zebra') ||
p.toLowerCase().includes('epson')
);
if (thermalPrinter) {
printerSelect.value = thermalPrinter;
console.log('🏷️ Auto-selected thermal printer:', thermalPrinter);
} else if (printers.length > 0) {
printerSelect.value = printers[0];
console.log('🖨️ Auto-selected first printer:', printers[0]);
}
showNotification(`✅ Found ${printers.length} printer(s)`, 'success');
} catch (error) {
console.error('Error loading printers:', error);
document.getElementById('printer-select').innerHTML = '<option value="">Failed to load printers</option>';
showNotification('⚠️ Failed to load printers: ' + error.message, 'warning');
}
}
// Generate barcode for location
function generateLocationBarcode(locationCode) {
const canvas = document.getElementById('location-barcode');
const placeholder = document.getElementById('barcode-placeholder');
if (locationCode) {
try {
JsBarcode(canvas, locationCode, {
format: "CODE128",
width: 2,
height: 80,
displayValue: true,
fontSize: 12,
margin: 5
});
canvas.style.display = 'block';
placeholder.style.display = 'none';
} catch (error) {
console.error('Error generating barcode:', error);
canvas.style.display = 'none';
placeholder.style.display = 'block';
placeholder.textContent = 'Barcode generation failed';
}
} else {
canvas.style.display = 'none';
placeholder.style.display = 'block';
placeholder.textContent = 'No location selected';
}
}
// Handle location row selection
document.addEventListener('DOMContentLoaded', function() {
const locationRows = document.querySelectorAll('.location-row');
const locationCodeDisplay = document.getElementById('location-code-display');
const printButton = document.getElementById('print-barcode-btn');
locationRows.forEach(row => {
row.addEventListener('click', function() {
// Remove selection from other rows
locationRows.forEach(r => r.classList.remove('selected'));
// Select this row
this.classList.add('selected');
// Get location code
selectedLocationCode = this.dataset.locationCode;
// Update display
locationCodeDisplay.textContent = selectedLocationCode;
// Generate barcode
generateLocationBarcode(selectedLocationCode);
// Enable print button
printButton.disabled = false;
showNotification(`📍 Selected location: ${selectedLocationCode}`, 'info');
});
});
// Initialize QZ Tray
initializeQZTray();
});
// Print barcode function with enhanced QZ Tray support
async function printLocationBarcode() {
if (!selectedLocationCode) {
showNotification('❌ Please select a location first', 'error');
return;
}
const selectedPrinter = document.getElementById('printer-select').value;
if (!selectedPrinter) {
showNotification('❌ Please select a printer', 'error');
return;
}
const printButton = document.getElementById('print-barcode-btn');
const printStatus = document.getElementById('print-status');
try {
printButton.disabled = true;
printStatus.textContent = 'Generating label...';
// Generate PDF for the 4x8cm label
const response = await fetch('/generate_location_label_pdf', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
location_code: selectedLocationCode
})
});
if (!response.ok) {
throw new Error('Failed to generate PDF');
}
const pdfBlob = await response.blob();
const pdfArrayBuffer = await pdfBlob.arrayBuffer();
const pdfBase64 = btoa(String.fromCharCode(...new Uint8Array(pdfArrayBuffer)));
printStatus.textContent = 'Sending to printer...';
// Configure QZ Tray for PDF printing with 4x8cm size
const config = qzTray.configs.create(selectedPrinter, {
size: { width: 4, height: 8, units: 'cm' },
margins: { top: 0, right: 0, bottom: 0, left: 0 },
orientation: 'portrait'
});
const data = [{
type: 'pdf',
format: 'base64',
data: pdfBase64
}];
await qzTray.print(config, data);
printStatus.textContent = 'Label printed successfully!';
showNotification(`✅ Location barcode printed successfully!\n📍 Location: ${selectedLocationCode}\n🖨️ Printer: ${selectedPrinter}`, 'success');
setTimeout(() => {
printStatus.textContent = '';
}, 3000);
} catch (error) {
console.error('Print error:', error);
printStatus.textContent = 'Print failed: ' + error.message;
showNotification('❌ Print failed: ' + error.message, 'error');
} finally {
printButton.disabled = false;
}
}
// Add event listener for print button
document.getElementById('print-barcode-btn').addEventListener('click', printLocationBarcode);
// Test QZ Tray connection function (for debugging)
async function testQZConnection() {
console.log('🔍 ========== QZ TRAY CONNECTION TEST ==========');
const printStatus = document.getElementById('print-status');
const originalStatus = printStatus.textContent;
printStatus.textContent = 'Testing QZ Tray...';
let testResults = [];
try {
// Test 1: Check if library is loaded
if (typeof qz === 'undefined') {
testResults.push('❌ Test 1: Library NOT loaded');
throw new Error('QZ Tray library not loaded');
}
testResults.push('✅ Test 1: Library loaded successfully');
// Test 2: Test connection
if (!qzTray) {
await initializeQZTray();
}
testResults.push('✅ Test 2: Connection successful');
// Test 3: Test printer discovery
await loadQZTrayPrinters();
testResults.push(`✅ Test 3: Found ${availablePrinters.length} printers`);
const resultMsg = 'QZ Tray Connection Test Results:\n\n' + testResults.join('\n');
showNotification(resultMsg, 'success');
printStatus.textContent = 'Test completed successfully';
} catch (error) {
testResults.push(`❌ Test failed: ${error.message}`);
const resultMsg = 'QZ Tray Connection Test Results:\n\n' + testResults.join('\n');
showNotification(resultMsg, 'error');
printStatus.textContent = 'Test failed';
}
setTimeout(() => {
printStatus.textContent = originalStatus;
}, 3000);
}
// Add test button functionality (for debugging - can be removed in production)
console.log('🔧 QZ Tray test function available: testQZConnection()');
</script>
{% endblock %}

View File

@@ -125,8 +125,27 @@ table.view-orders-table.scan-table tbody tr.selected td {
background-color: #6c757d;
cursor: not-allowed;
}
/* Force barcode SVG elements to be black */
#barcode-display rect,
#vertical-barcode-display rect {
fill: #000000 !important;
stroke: #000000 !important;
}
#barcode-display path,
#vertical-barcode-display path {
fill: #000000 !important;
stroke: #000000 !important;
}
/* Ensure barcode frames have proper contrast */
#barcode-frame,
#vertical-barcode-frame {
background: #ffffff !important;
border: 1px solid #ddd;
}
</style>
<script src="https://cdn.jsdelivr.net/npm/jsbarcode@3.11.5/dist/JsBarcode.all.min.js"></script>
{% endblock %}
{% block content %}
@@ -194,7 +213,69 @@ table.view-orders-table.scan-table tbody tr.selected td {
<div id="vertical-barcode-text" style="position: absolute; bottom: -15px; font-size: 7px; font-family: 'Courier New', monospace; text-align: center; font-weight: bold; width: 100%;"></div>
</div>
</div>
<!-- Print Options removed as requested -->
<!-- Print Options (copied from print_module.html) -->
<div style="width: 100%; margin-top: 20px;">
<!-- Print Method Selection -->
<div style="margin-bottom: 15px;" role="group" aria-labelledby="print-method-label">
<div id="print-method-label" style="font-size: 12px; font-weight: 600; color: #495057; margin-bottom: 8px;">
📄 Print Method:
</div>
<div class="form-check" style="margin-bottom: 6px;">
<input class="form-check-input" type="radio" name="printMethod" id="qzTrayPrint" value="qztray" checked>
<label class="form-check-label" for="qzTrayPrint" style="font-size: 11px; line-height: 1.2;">
<strong>🖨️ Direct Print</strong> <span id="qztray-status" class="badge badge-success" style="font-size: 9px; padding: 2px 6px;">Ready</span>
</label>
</div>
<div class="form-check" id="pdf-option-container" style="display: none; margin-bottom: 6px;">
<input class="form-check-input" type="radio" name="printMethod" id="pdfGenerate" value="pdf">
<label class="form-check-label" for="pdfGenerate" style="font-size: 11px; line-height: 1.2;">
<strong>📄 PDF Export</strong> <span class="text-muted" style="font-size: 10px;">(fallback)</span>
</label>
</div>
</div>
<!-- Printer Selection for QZ Tray (Compact) -->
<div id="qztray-printer-selection" style="margin-bottom: 10px;">
<label for="qztray-printer-select" style="font-size: 11px; font-weight: 600; color: #495057; margin-bottom: 3px; display: block;">
Printer:
</label>
<select id="qztray-printer-select" class="form-control form-control-sm" style="font-size: 11px; padding: 3px 6px;">
<option value="">Loading...</option>
</select>
</div>
<!-- Labels Range Selection -->
<div style="margin-bottom: 15px;">
<label for="labels-range-input" style="font-size: 11px; font-weight: 600; color: #495057; margin-bottom: 3px; display: block;">
Select Labels Range:
</label>
<input type="text" id="labels-range-input" class="form-control form-control-sm"
placeholder="e.g., 003 or 003-007"
style="font-size: 11px; padding: 3px 6px; text-align: center;">
<div style="font-size: 9px; color: #6c757d; margin-top: 2px; text-align: center;">
Single: "005" | Range: "003-007" | Leave empty for all
</div>
</div>
<!-- Print Button -->
<div style="width: 100%; text-align: center; margin-bottom: 10px;">
<button id="print-label-btn" class="btn btn-success" style="font-size: 13px; padding: 8px 24px; border-radius: 5px; font-weight: 600;">
🖨️ Print Labels
</button>
</div>
<!-- Print Information -->
<div style="width: 100%; text-align: center; color: #6c757d; font-size: 10px; line-height: 1.3;">
<small>(e.g., CP00000711-001, 002, ...)</small>
</div>
<!-- QZ Tray Installation Info - Simplified -->
<div id="qztray-info" style="width: 100%; margin-top: 15px; padding-top: 15px; border-top: 1px solid #e9ecef;">
<div style="background: #f8f9fa; border: 1px solid #dee2e6; border-radius: 6px; padding: 10px; text-align: center;">
<div style="font-size: 10px; color: #495057; margin-bottom: 8px;">
QZ Tray is required for direct printing
</div>
<a href="https://filebrowser.moto-adv.com/filebrowser/api/public/dl/Fk0ZaiEY/QP_Tray/qz-tray-2.2.6-SNAPSHOT-x86_64.exe?token=TJ7gSu3CRcWWQuyFLoZv5I8j4diDjP47DDqWRtM0oKAx-2_orj1stfKPJsuuqKR9mE2GQNm1jlZ0BPR7lfZ3gHmu56SkY9fC5AJlC9n_80oX643ojlGc-U7XVb1SDd0w" class="btn btn-outline-secondary btn-sm" style="font-size: 10px; padding: 4px 16px;">
📥 Download QZ Tray
</a>
</div>
</div>
</div>
</div>
<!-- Orders Table Card (right, with load button and notification system) -->
<div class="card scan-table-card" style="min-height: 700px; width: calc(100% - 350px); margin: 0;">
@@ -231,11 +312,23 @@ table.view-orders-table.scan-table tbody tr.selected td {
</div>
</div>
<!-- JavaScript Libraries -->
<!-- JsBarcode library for real barcode generation -->
<script src="{{ url_for('static', filename='JsBarcode.all.min.js') }}"></script>
<!-- Add html2canvas library for capturing preview as image -->
<script src="{{ url_for('static', filename='html2canvas.min.js') }}"></script>
<!-- PATCHED QZ Tray library - works with custom server using pairing key authentication -->
<script src="{{ url_for('static', filename='qz-tray.js') }}"></script>
<script>
// Store all orders data for searching
const allOrders = {{ orders|tojson|safe }};
let selectedOrderData = null;
// QZ Tray Integration
let qzTray = null;
let availablePrinters = [];
function searchOrder() {
const searchValue = document.getElementById('search-input').value.trim().toLowerCase();
if (!searchValue) {
@@ -274,13 +367,26 @@ function fetchMatchingOrders() {
}
matchingOrders.forEach((order, idx) => {
const tr = document.createElement('tr');
// Format data_livrare as DD/MM/YYYY if possible
let dataLivrareFormatted = '-';
if (order.data_livrare) {
const d = new Date(order.data_livrare);
if (!isNaN(d)) {
const day = String(d.getDate()).padStart(2, '0');
const month = String(d.getMonth() + 1).padStart(2, '0');
const year = d.getFullYear();
dataLivrareFormatted = `${day}/${month}/${year}`;
} else {
dataLivrareFormatted = order.data_livrare;
}
}
tr.innerHTML = `
<td>${order.id}</td>
<td><strong>${order.comanda_productie}</strong></td>
<td>${order.cod_articol || '-'}</td>
<td>${order.descr_com_prod}</td>
<td style="text-align: right; font-weight: 600;">${order.cantitate}</td>
<td style="text-align: center;">${order.data_livrare || '-'}</td>
<td style="text-align: center;">${dataLivrareFormatted}</td>
<td style="text-align: center;">${order.dimensiune || '-'}</td>
<td>${order.com_achiz_client || '-'}</td>
<td style="text-align: right;">${order.nr_linie_com_client || '-'}</td>
@@ -292,12 +398,296 @@ function fetchMatchingOrders() {
<td style="font-size: 11px; color: #6c757d;">${order.created_at || '-'}</td>
<td>1</td>
`;
tr.addEventListener('click', function() {
document.querySelectorAll('.print-module-table tbody tr').forEach(row => {
row.classList.remove('selected');
const cells = row.querySelectorAll('td');
cells.forEach(cell => {
cell.style.backgroundColor = '';
cell.style.color = '';
});
});
this.classList.add('selected');
const cells = this.querySelectorAll('td');
cells.forEach(cell => {
cell.style.backgroundColor = '#007bff';
cell.style.color = 'white';
});
updatePreviewCard(order);
});
tbody.appendChild(tr);
// Update preview card with the first matching order
if (idx === 0) updatePreviewCard(order);
});
}
// QZ Tray and Print Button Logic (copied/adapted from print_module.html)
async function initializeQZTray() {
try {
if (typeof qz === 'undefined') {
document.getElementById('qztray-status').textContent = 'Library Error';
document.getElementById('qztray-status').className = 'badge badge-danger';
return false;
}
qz.websocket.setClosedCallbacks(function() {
document.getElementById('qztray-status').textContent = 'Disconnected';
document.getElementById('qztray-status').className = 'badge badge-warning';
});
await qz.websocket.connect();
qzTray = qz;
const version = await qz.api.getVersion();
document.getElementById('qztray-status').textContent = 'Ready';
document.getElementById('qztray-status').className = 'badge badge-success';
document.getElementById('qztray-printer-selection').style.display = 'block';
document.getElementById('pdf-option-container').style.display = 'none';
await loadQZTrayPrinters();
return true;
} catch (error) {
document.getElementById('qztray-status').textContent = 'Not Connected';
document.getElementById('qztray-status').className = 'badge badge-danger';
document.getElementById('qztray-printer-selection').style.display = 'none';
document.getElementById('pdf-option-container').style.display = 'block';
document.getElementById('pdfGenerate').checked = true;
document.getElementById('qzTrayPrint').disabled = true;
return false;
}
}
async function loadQZTrayPrinters() {
try {
if (!qzTray) return;
const printers = await qzTray.printers.find();
availablePrinters = printers;
const printerSelect = document.getElementById('qztray-printer-select');
printerSelect.innerHTML = '<option value="">Select a printer...</option>';
printers.forEach(printer => {
const option = document.createElement('option');
option.value = printer;
option.textContent = printer;
printerSelect.appendChild(option);
});
const thermalPrinter = printers.find(p =>
p.toLowerCase().includes('thermal') ||
p.toLowerCase().includes('label') ||
p.toLowerCase().includes('zebra') ||
p.toLowerCase().includes('epson')
);
if (thermalPrinter) {
printerSelect.value = thermalPrinter;
}
} catch (error) {}
}
// Print Button Handler
document.addEventListener('DOMContentLoaded', function() {
setTimeout(initializeQZTray, 1000);
document.getElementById('print-label-btn').addEventListener('click', async function(e) {
e.preventDefault();
const selectedRow = document.querySelector('.print-module-table tbody tr.selected');
if (!selectedRow) {
alert('Please select an order first from the table below.');
return;
}
const printMethod = document.querySelector('input[name="printMethod"]:checked').value;
if (printMethod === 'qztray') {
await handleQZTrayPrint(selectedRow);
} else {
handlePDFGeneration(selectedRow);
}
});
document.querySelectorAll('input[name="printMethod"]').forEach(radio => {
radio.addEventListener('change', updatePrintMethodUI);
});
updatePrintMethodUI();
});
function updatePrintMethodUI() {
const printMethod = document.querySelector('input[name="printMethod"]:checked').value;
const printerSelection = document.getElementById('qztray-printer-selection');
const printButton = document.getElementById('print-label-btn');
if (printMethod === 'qztray') {
printButton.textContent = '🖨️ Print Labels';
printButton.className = 'btn btn-primary';
} else {
printerSelection.style.display = 'none';
printButton.textContent = '📄 Generate PDF';
printButton.className = 'btn btn-success';
}
}
async function handleQZTrayPrint(selectedRow) {
try {
if (!qzTray) {
await initializeQZTray();
if (!qzTray) throw new Error('QZ Tray not available');
}
const selectedPrinter = document.getElementById('qztray-printer-select').value;
if (!selectedPrinter) {
alert('Please select a printer first');
return;
}
const cells = selectedRow.querySelectorAll('td');
const orderData = {
id: cells[0].textContent,
comanda_productie: cells[1].textContent.trim(),
cod_articol: cells[2].textContent.trim(),
descr_com_prod: cells[3].textContent.trim(),
cantitate: parseInt(cells[4].textContent.trim()),
data_livrare: cells[5].textContent.trim(),
dimensiune: cells[6].textContent.trim(),
com_achiz_client: cells[7].textContent.trim(),
nr_linie_com_client: cells[8].textContent.trim(),
customer_name: cells[9].textContent.trim()
};
// Parse labels range input
const labelsRangeInput = document.getElementById('labels-range-input').value.trim();
let labelNumbers = [];
if (labelsRangeInput) {
if (labelsRangeInput.includes('-')) {
// Range format: "003-007"
const rangeParts = labelsRangeInput.split('-');
if (rangeParts.length === 2) {
const start = parseInt(rangeParts[0]);
const end = parseInt(rangeParts[1]);
if (!isNaN(start) && !isNaN(end) && start > 0 && end >= start && end <= orderData.cantitate) {
for (let i = start; i <= end; i++) {
labelNumbers.push(i);
}
} else {
alert(`Invalid range. Please use format "001-${String(orderData.cantitate).padStart(3, '0')}" or single number.`);
return;
}
} else {
alert('Invalid range format. Use "003-007" format.');
return;
}
} else {
// Single number format: "005"
const singleNumber = parseInt(labelsRangeInput);
if (!isNaN(singleNumber) && singleNumber > 0 && singleNumber <= orderData.cantitate) {
labelNumbers.push(singleNumber);
} else {
alert(`Invalid label number. Please use 1-${orderData.cantitate}.`);
return;
}
}
} else {
// No range specified, print all labels (original behavior)
for (let i = 1; i <= orderData.cantitate; i++) {
labelNumbers.push(i);
}
}
// Print the specified labels
for (let i = 0; i < labelNumbers.length; i++) {
const labelNumber = labelNumbers[i];
await generatePDFAndPrint(selectedPrinter, orderData, labelNumber, orderData.cantitate);
if (i < labelNumbers.length - 1) await new Promise(resolve => setTimeout(resolve, 500));
}
// Show success message
const rangeText = labelsRangeInput ?
(labelNumbers.length === 1 ? `label ${String(labelNumbers[0]).padStart(3, '0')}` :
`labels ${String(labelNumbers[0]).padStart(3, '0')}-${String(labelNumbers[labelNumbers.length-1]).padStart(3, '0')}`) :
`all ${orderData.cantitate} labels`;
alert(`Successfully printed ${rangeText} for order ${orderData.comanda_productie}`);
} catch (error) {
alert('QZ Tray print error: ' + error.message);
}
}
async function generatePDFAndPrint(selectedPrinter, orderData, pieceNumber, totalPieces) {
try {
const pdfData = {
...orderData,
quantity: 1,
piece_number: pieceNumber,
total_pieces: totalPieces
};
const response = await fetch('/generate_label_pdf', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(pdfData)
});
if (!response.ok) throw new Error('Failed to generate PDF');
const pdfBlob = await response.blob();
const pdfBase64 = await new Promise((resolve, reject) => {
const reader = new FileReader();
reader.onloadend = () => resolve(reader.result.split(',')[1]);
reader.onerror = reject;
reader.readAsDataURL(pdfBlob);
});
const config = qz.configs.create(selectedPrinter, {
scaleContent: false,
rasterize: false,
size: { width: 80, height: 100 },
units: 'mm',
margins: { top: 0, right: 0, bottom: 0, left: 0 }
});
const data = [{ type: 'pdf', format: 'base64', data: pdfBase64 }];
await qz.print(config, data);
} catch (error) {
throw error;
}
}
function handlePDFGeneration(selectedRow) {
// Check if labels range is specified
const labelsRangeInput = document.getElementById('labels-range-input').value.trim();
if (labelsRangeInput) {
alert('PDF generation currently supports printing all labels only. Please use QZ Tray for custom label ranges, or leave the range field empty for PDF generation.');
return;
}
const orderId = selectedRow.querySelector('td').textContent;
const quantityCell = selectedRow.querySelector('td:nth-child(5)');
const quantity = quantityCell ? parseInt(quantityCell.textContent) : 1;
const prodOrderCell = selectedRow.querySelector('td:nth-child(2)');
const prodOrder = prodOrderCell ? prodOrderCell.textContent.trim() : 'N/A';
const button = document.getElementById('print-label-btn');
const originalText = button.textContent;
button.textContent = 'Generating PDF...';
button.disabled = true;
fetch(`/generate_labels_pdf/${orderId}/true`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' }
})
.then(response => {
if (!response.ok) throw new Error(`HTTP error! status: ${response.status}`);
return response.blob();
})
.then(blob => {
const url = window.URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = `labels_${prodOrder}_${quantity}pcs.pdf`;
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
const printWindow = window.open(url, '_blank');
if (printWindow) {
printWindow.focus();
setTimeout(() => {
printWindow.print();
setTimeout(() => { window.URL.revokeObjectURL(url); }, 2000);
}, 1500);
} else {
setTimeout(() => { window.URL.revokeObjectURL(url); }, 1000);
}
setTimeout(() => {}, 1000);
})
.catch(error => {
alert('Failed to generate PDF labels. Error: ' + error.message);
})
.finally(() => {
button.textContent = originalText;
button.disabled = false;
});
}
// Update the preview card with order data (as in print_module.html)
function updatePreviewCard(order) {
function set(id, value) {
@@ -325,21 +715,128 @@ function updatePreviewCard(order) {
set('customer-name-row', order.customer_name || '');
set('quantity-ordered-value', order.cantitate || '');
set('client-order-info', (order.com_achiz_client && order.nr_linie_com_client) ? `${order.com_achiz_client}-${order.nr_linie_com_client}` : '');
set('delivery-date-value', order.data_livrare || '');
// Format delivery date as DD/MM/YYYY
let deliveryDateFormatted = '';
if (order.data_livrare) {
const d = new Date(order.data_livrare);
if (!isNaN(d)) {
const day = String(d.getDate()).padStart(2, '0');
const month = String(d.getMonth() + 1).padStart(2, '0');
const year = d.getFullYear();
deliveryDateFormatted = `${day}/${month}/${year}`;
} else {
deliveryDateFormatted = order.data_livrare;
}
}
set('delivery-date-value', deliveryDateFormatted);
set('description-value', order.descr_com_prod || '');
set('size-value', order.dimensiune || '');
set('article-code-value', order.cod_articol || '');
set('prod-order-value', (order.comanda_productie && order.cantitate) ? `${order.comanda_productie}-${order.cantitate}` : '');
set('barcode-text', order.comanda_productie ? `${order.comanda_productie}/001` : '');
set('vertical-barcode-text', (order.com_achiz_client && order.nr_linie_com_client) ? `${order.com_achiz_client}/${order.nr_linie_com_client}` : '');
// Generate barcodes if JsBarcode is available
if (typeof JsBarcode !== 'undefined') {
try {
JsBarcode("#barcode-display", order.comanda_productie ? `${order.comanda_productie}/001` : ' ', { format: "CODE128", width: 2, height: 40, displayValue: false, margin: 2 });
} catch (e) {}
try {
JsBarcode("#vertical-barcode-display", (order.com_achiz_client && order.nr_linie_com_client) ? `${order.com_achiz_client}/${order.nr_linie_com_client}` : ' ', { format: "CODE128", width: 1.5, height: 35, displayValue: false, margin: 2 });
} catch (e) {}
// Generate barcodes if JsBarcode is available (with debugging like print_module.html)
const horizontalBarcodeData = order.comanda_productie ? `${order.comanda_productie}/001` : 'N/A';
const verticalBarcodeData = (order.com_achiz_client && order.nr_linie_com_client) ? `${order.com_achiz_client}/${order.nr_linie_com_client}` : '000000/00';
console.log('🔍 BARCODE DEBUG - Order data:', order);
console.log('🔍 Attempting to generate horizontal barcode:', horizontalBarcodeData);
console.log('🔍 JsBarcode available?', typeof JsBarcode !== 'undefined');
console.log('🔍 JsBarcode object:', typeof JsBarcode !== 'undefined' ? JsBarcode : 'undefined');
// Function to generate barcodes (can be called after library loads)
const generateBarcodes = () => {
if (horizontalBarcodeData !== 'N/A' && typeof JsBarcode !== 'undefined') {
try {
const barcodeElement = document.querySelector("#barcode-display");
console.log('🔍 Horizontal barcode element:', barcodeElement);
console.log('🔍 Element innerHTML before:', barcodeElement ? barcodeElement.innerHTML : 'null');
JsBarcode("#barcode-display", horizontalBarcodeData, {
format: "CODE128",
width: 2,
height: 40,
displayValue: false,
margin: 2,
lineColor: "#000000",
background: "#ffffff"
});
console.log('🔍 Element innerHTML after:', barcodeElement ? barcodeElement.innerHTML : 'null');
console.log('✅ Horizontal barcode generated successfully');
// Force black color on all barcode elements
const barcodeSvg = document.getElementById('barcode-display');
if (barcodeSvg) {
console.log('🔍 SVG elements found:', barcodeSvg.querySelectorAll('rect, path').length);
barcodeSvg.querySelectorAll('rect').forEach((r, i) => {
console.log(`🔍 Setting rect ${i} to black`);
r.setAttribute('fill', '#000000');
r.setAttribute('stroke', '#000000');
});
barcodeSvg.querySelectorAll('path').forEach((p, i) => {
console.log(`🔍 Setting path ${i} to black`);
p.setAttribute('fill', '#000000');
p.setAttribute('stroke', '#000000');
});
}
} catch (e) {
console.error('❌ Failed to generate horizontal barcode:', e);
}
} else {
console.warn('⚠️ Skipping horizontal barcode generation:',
horizontalBarcodeData === 'N/A' ? 'No data' : 'JsBarcode not loaded');
}
// Generate vertical barcode visual using JsBarcode (will be rotated by CSS)
console.log('🔍 Attempting to generate vertical barcode:', verticalBarcodeData);
if (verticalBarcodeData !== '000000/00' && typeof JsBarcode !== 'undefined') {
try {
const verticalElement = document.querySelector("#vertical-barcode-display");
console.log('🔍 Vertical barcode element:', verticalElement);
JsBarcode("#vertical-barcode-display", verticalBarcodeData, {
format: "CODE128",
width: 1.5,
height: 35,
displayValue: false,
margin: 2,
lineColor: "#000000",
background: "#ffffff"
});
console.log('✅ Vertical barcode generated successfully');
// Force black color on all vertical barcode elements
const vbarcodeSvg = document.getElementById('vertical-barcode-display');
if (vbarcodeSvg) {
vbarcodeSvg.querySelectorAll('rect').forEach(r => {
r.setAttribute('fill', '#000000');
r.setAttribute('stroke', '#000000');
});
vbarcodeSvg.querySelectorAll('path').forEach(p => {
p.setAttribute('fill', '#000000');
p.setAttribute('stroke', '#000000');
});
}
} catch (e) {
console.error('❌ Failed to generate vertical barcode:', e);
}
} else {
console.warn('⚠️ Skipping vertical barcode generation:',
verticalBarcodeData === '000000/00' ? 'Default value' : 'JsBarcode not loaded');
}
};
// Try to generate immediately
generateBarcodes();
// If JsBarcode is not loaded, wait a bit and try again (for CDN fallback)
if (typeof JsBarcode === 'undefined') {
setTimeout(() => {
console.log('🔍 Retry after 1s - JsBarcode available?', typeof JsBarcode !== 'undefined');
generateBarcodes();
}, 1000);
}
}

View File

@@ -257,9 +257,9 @@
<!-- JavaScript Libraries -->
<!-- JsBarcode library for real barcode generation -->
<script src="https://cdn.jsdelivr.net/npm/jsbarcode@3.11.5/dist/JsBarcode.all.min.js"></script>
<script src="{{ url_for('static', filename='JsBarcode.all.min.js') }}"></script>
<!-- Add html2canvas library for capturing preview as image -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/html2canvas/1.4.1/html2canvas.min.js"></script>
<script src="{{ url_for('static', filename='html2canvas.min.js') }}"></script>
<!-- PATCHED QZ Tray library - works with custom server using pairing key authentication -->
<script src="{{ url_for('static', filename='qz-tray.js') }}"></script>
<!-- Original CDN version (disabled): <script src="https://cdn.jsdelivr.net/npm/qz-tray@2.2.4/qz-tray.js"></script> -->