38682-vm/api/print.php
2026-02-27 09:26:03 +00:00

61 lines
1.8 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);
$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()]);
}