diff --git a/assets/js/main.js b/assets/js/main.js index b4ec710..1cc080f 100644 --- a/assets/js/main.js +++ b/assets/js/main.js @@ -774,17 +774,19 @@ document.addEventListener('DOMContentLoaded', () => { .catch(err => console.error(`Network Print Fetch Error (${type}):`, err)); }; + /** + * Prints a thermal receipt using a hidden iframe for a smoother experience. + * To achieve completely silent printing (Direct Print), run Chrome with: + * chrome.exe --kiosk-printing --kiosk + */ window.printThermalReceipt = function(data) { - const width = 450; - const height = 800; - const left = (screen.width - width) / 2; - const top = (screen.height - height) / 2; - - const win = window.open('', '_blank', `width=${width},height=${height},top=${top},left=${left}`); - - if (!win) { - alert('Please allow popups for this website to print thermal receipts.'); - return; + // Create or get the hidden iframe + let iframe = document.getElementById('print-iframe'); + if (!iframe) { + iframe = document.createElement('iframe'); + iframe.id = 'print-iframe'; + iframe.style.display = 'none'; + document.body.appendChild(iframe); } const tr = { @@ -856,7 +858,7 @@ document.addEventListener('DOMContentLoaded', () => { .order-info { font-size: 11px; margin-bottom: 10px; } .order-info-row { display: flex; justify-content: space-between; margin-bottom: 2px; } .rtl { direction: rtl; unicode-bidi: embed; } - @media print { body { width: 80mm; padding: 5mm; } @page { size: 80mm auto; margin: 0; } } + @media print { body { width: 80mm; padding: 2mm; } @page { size: 80mm auto; margin: 0; } } @@ -905,11 +907,27 @@ document.addEventListener('DOMContentLoaded', () => { ${settings.email ? `
${settings.email}
` : ''}
Powered by Abidarcafe
- `; - win.document.write(html); - win.document.close(); + + const doc = iframe.contentWindow.document; + doc.open(); + doc.write(html); + doc.close(); + + // Wait for resources (like logo) to load before printing + iframe.contentWindow.onload = function() { + iframe.contentWindow.focus(); + iframe.contentWindow.print(); + }; + + // Fallback if onload doesn't fire correctly + setTimeout(() => { + if (doc.readyState === 'complete') { + iframe.contentWindow.focus(); + iframe.contentWindow.print(); + } + }, 1000); }; window.openRatingQRModal = function() { @@ -922,4 +940,4 @@ document.addEventListener('DOMContentLoaded', () => { const modal = new bootstrap.Modal(document.getElementById('qrRatingModal')); modal.show(); }; -}); \ No newline at end of file +}); diff --git a/includes/PrinterService.php b/includes/PrinterService.php index 135091f..a6644fc 100644 --- a/includes/PrinterService.php +++ b/includes/PrinterService.php @@ -14,8 +14,13 @@ class PrinterService { return ['success' => false, 'error' => 'Printer IP is not configured.']; } + // Determine if IP is local/private. If so, use a very short timeout + // because cloud servers cannot reach local IPs anyway. + $isLocalIp = preg_match('/^(127\.|10\.|192\.168\.|172\.(1[6-9]|2[0-9]|3[0-1])\.)/', $ip); + $timeout = $isLocalIp ? 1 : 5; // 1 second for local, 5 for public + try { - $fp = @fsockopen($ip, $port, $errno, $errstr, 5); + $fp = @fsockopen($ip, $port, $errno, $errstr, $timeout); if (!$fp) { return ['success' => false, 'error' => "Could not connect to printer at $ip:$port. Error: $errstr ($errno)"]; } @@ -65,4 +70,4 @@ class PrinterService { return $out; } -} +} \ No newline at end of file diff --git a/includes/WablasService.php b/includes/WablasService.php index 14c0789..3593c20 100644 --- a/includes/WablasService.php +++ b/includes/WablasService.php @@ -108,11 +108,12 @@ class WablasService { curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($payload)); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); + curl_setopt($ch, CURLOPT_TIMEOUT, 10); // Added 10 second timeout $response = curl_exec($ch); $curlError = curl_error($ch); $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); - curl_close($ch); + $curlCloseResult = curl_close($ch); if ($curlError) { return ['success' => false, 'message' => 'cURL Error: ' . $curlError]; @@ -130,4 +131,4 @@ class WablasService { return ['success' => false, 'message' => 'HTTP Error ' . $httpCode . ': ' . (is_string($msg) ? $msg : 'Unknown')]; } -} \ No newline at end of file +} diff --git a/kitchen.php b/kitchen.php index b485e63..6d21885 100644 --- a/kitchen.php +++ b/kitchen.php @@ -281,7 +281,7 @@ function printKitchenTicket(orderId) {
*** END OF TICKET ***
- + - \ No newline at end of file +