82 lines
2.4 KiB
PHP
82 lines
2.4 KiB
PHP
<?php
|
|
header('Content-Type: application/json');
|
|
require_once __DIR__ . '/../db/config.php';
|
|
require_once __DIR__ . '/../includes/functions.php';
|
|
require_once __DIR__ . '/../includes/PrinterService.php';
|
|
|
|
$pdo = db();
|
|
$input = json_decode(file_get_contents('php://input'), true);
|
|
|
|
$action = $input['action'] ?? 'print';
|
|
|
|
if ($action === 'test') {
|
|
$ip = $input['ip'] ?? null;
|
|
if (!$ip) {
|
|
echo json_encode(['success' => false, 'error' => 'IP is required']);
|
|
exit;
|
|
}
|
|
|
|
// Send a simple test line
|
|
$testData = "\x1b" . "@"; // Initialize printer
|
|
$testData .= "Printer Test Successful\n";
|
|
$testData .= "IP: $ip\n";
|
|
$testData .= "Date: " . date('Y-m-d H:i:s') . "\n";
|
|
$testData .= "\n\n\n\n\n";
|
|
$testData .= "\x1b" . "m"; // Cut
|
|
|
|
$result = PrinterService::sendToNetworkPrinter($ip, $testData);
|
|
echo json_encode($result);
|
|
exit;
|
|
}
|
|
|
|
$order_id = $input['order_id'] ?? null;
|
|
$type = $input['type'] ?? 'cashier'; // 'cashier' or 'kitchen'
|
|
|
|
if (!$order_id) {
|
|
echo json_encode(['success' => false, 'error' => 'Order ID is required']);
|
|
exit;
|
|
}
|
|
|
|
try {
|
|
// Fetch order details with outlet info
|
|
$stmt = $pdo->prepare(
|
|
"SELECT o.*, out.cashier_printer_ip, out.kitchen_printer_ip "
|
|
. "FROM orders o "
|
|
. "JOIN outlets out ON o.outlet_id = out.id "
|
|
. "WHERE o.id = ?"
|
|
);
|
|
$stmt->execute([$order_id]);
|
|
$order = $stmt->fetch(PDO::FETCH_ASSOC);
|
|
|
|
if (!$order) {
|
|
echo json_encode(['success' => false, 'error' => 'Order not found']);
|
|
exit;
|
|
}
|
|
|
|
// Fetch items
|
|
$stmt = $pdo->prepare("SELECT * FROM order_items WHERE order_id = ?");
|
|
$stmt->execute([$order_id]);
|
|
$items = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
|
|
|
// Fetch company settings
|
|
$company = get_company_settings();
|
|
|
|
// Determine target IP
|
|
$printer_ip = ($type === 'kitchen') ? $order['kitchen_printer_ip'] : $order['cashier_printer_ip'];
|
|
|
|
if (empty($printer_ip)) {
|
|
echo json_encode(['success' => false, 'error' => "No $type printer IP configured for this outlet"]);
|
|
exit;
|
|
}
|
|
|
|
// Generate ESC/POS data
|
|
$data = PrinterService::formatReceipt($order, $items, $company);
|
|
|
|
// Send to printer
|
|
$result = PrinterService::sendToNetworkPrinter($printer_ip, $data);
|
|
|
|
echo json_encode($result);
|
|
|
|
} catch (Exception $e) {
|
|
echo json_encode(['success' => false, 'error' => $e->getMessage()]);
|
|
} |