38682-vm/includes/PrinterService.php
2026-02-27 09:26:03 +00:00

69 lines
2.3 KiB
PHP

<?php
class PrinterService {
/**
* Send raw ESC/POS data to a network printer.
*
* @param string $ip Printer IP address
* @param string $data Raw ESC/POS data
* @param int $port Printer port (default 9100)
* @return array Success status and error message if any
*/
public static function sendToNetworkPrinter($ip, $data, $port = 9100) {
if (empty($ip)) {
return ['success' => false, 'error' => 'Printer IP is not configured.'];
}
try {
$fp = @fsockopen($ip, $port, $errno, $errstr, 5);
if (!$fp) {
return ['success' => false, 'error' => "Could not connect to printer at $ip:$port. Error: $errstr ($errno)"];
}
fwrite($fp, $data);
fclose($fp);
return ['success' => true];
} catch (Exception $e) {
return ['success' => false, 'error' => 'Exception: ' . $e->getMessage()];
}
}
/**
* Generate basic ESC/POS receipt content.
* This is a simplified version. For full features, an ESC/POS library is recommended.
*/
public static function formatReceipt($order, $items, $company) {
$esc = "\x1b";
$gs = "\x1d";
$line = str_repeat("-", 32) . "\n";
$out = "";
$out .= $esc . "!" . "\x38"; // Double height and width
$out .= $esc . "a" . "\x01"; // Center align
$out .= $company['company_name'] . "\n";
$out .= $esc . "!" . "\x00"; // Reset
$out .= $company['address'] . "\n";
$out .= $company['phone'] . "\n\n";
$out .= $esc . "a" . "\x00"; // Left align
$out .= "Order ID: #" . $order['id'] . "\n";
$out .= "Date: " . $order['created_at'] . "\n";
$out .= $line;
foreach ($items as $item) {
$name = substr($item['name'], 0, 20);
$qty = $item['quantity'] . "x";
$price = number_format($item['price'], 2);
$out .= sprintf("% -20s %3s %7s\n", $name, $qty, $price);
}
$out .= $line;
$out .= sprintf("% -20s %11s\n", "TOTAL", number_format($order['total_amount'], 2));
$out .= "\n\n\n\n";
$out .= $esc . "m"; // Cut paper (optional, depending on printer)
return $out;
}
}