prepare("SELECT o.*, c.name_en as customer_name_en, c.name_ar as customer_name_ar, c.phone as customer_phone, c.address_en as customer_address_en, c.address_ar as customer_address_ar FROM orders o LEFT JOIN customers c ON o.customer_id = c.id WHERE o.id = ?"); $stmt->execute([$order_id]); $order = $stmt->fetch(); if (!$order) { echo "Order not found"; exit; } // Handle status updates if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action'])) { if ($_POST['action'] === 'update_status') { $new_status = $_POST['status']; $stmt = db()->prepare("UPDATE orders SET status = ? WHERE id = ?"); $stmt->execute([$new_status, $order_id]); // WhatsApp Notification for Order Ready try { if ($new_status === 'ready' && get_setting('whatsapp_enabled') === '1' && !empty($order['customer_phone'])) { $template = get_setting('msg_order_ready_ar'); if (!empty($template)) { $message = str_replace( ['{customer_name}', '{order_number}'], [$order['customer_name_ar'], $order['order_number']], $template ); send_whatsapp_message($order['customer_phone'], $message); } } } catch (Exception $e) {} header("Location: order_details.php?id=$order_id"); exit; } elseif ($_POST['action'] === 'add_payment') { $amount = (float)$_POST['amount']; $method = $_POST['payment_method']; $stmt = db()->prepare("INSERT INTO payments (order_id, amount, payment_method) VALUES (?, ?, ?)"); $stmt->execute([$order_id, $amount, $method]); $stmt = db()->prepare("SELECT SUM(amount) as total_paid FROM payments WHERE order_id = ?"); $stmt->execute([$order_id]); $total_paid = $stmt->fetch()['total_paid'] ?? 0; $payment_status = $total_paid >= ($order['total_price'] - $order['loyalty_discount']) ? 'paid' : ($total_paid > 0 ? 'partially_paid' : 'unpaid'); $stmt = db()->prepare("UPDATE orders SET payment_status = ? WHERE id = ?"); $stmt->execute([$payment_status, $order_id]); // WhatsApp Notification for Payment Received try { if (get_setting('whatsapp_enabled') === '1' && !empty($order['customer_phone'])) { $remaining_now = ((float)$order['total_price'] - (float)$order['loyalty_discount']) - (float)$total_paid; $template = get_setting('msg_payment_ar'); if (!empty($template)) { $message = str_replace( ['{customer_name}', '{order_number}', '{amount}', '{remaining_balance}'], [$order['customer_name_ar'], $order['order_number'], $amount, max(0, $remaining_now)], $template ); send_whatsapp_message($order['customer_phone'], $message); } } } catch (Exception $e) {} header("Location: order_details.php?id=$order_id"); exit; } } // NOW Include header $title = 'order_details'; require_once __DIR__ . '/includes/header.php'; if ($current_role === 'limited_viewer') { header('Location: admin.php'); exit; } $stmt = db()->prepare("SELECT oi.*, i.name_en as item_en, i.name_ar as item_ar, s.name_en as service_en, s.name_ar as service_ar FROM order_items oi JOIN items i ON oi.item_id = i.id JOIN services s ON oi.service_id = s.id WHERE oi.order_id = ?"); $stmt->execute([$order_id]); $order_items = $stmt->fetchAll(); $stmt = db()->prepare("SELECT * FROM payments WHERE order_id = ?"); $stmt->execute([$order_id]); $payments = $stmt->fetchAll(); ?>
#
0): ?>
-


prepare("SELECT SUM(amount) as total_paid FROM payments WHERE order_id = ?"); $stmt->execute([$order_id]); $total_paid = $stmt->fetch()['total_paid'] ?? 0; $remaining = ((float)$order['total_price'] - (float)$order['loyalty_discount']) - (float)$total_paid; ?>
0.001): ?>
'secondary', 'processing' => 'primary', 'ready' => 'success', 'delivered' => 'dark', 'cancelled' => 'danger', ][$status] ?? 'info'; }} function getPaymentStatusColor($status) { return [ 'unpaid' => 'danger', 'partially_paid' => 'warning', 'paid' => 'success', ][$status] ?? 'info'; } require_once __DIR__ . '/includes/footer.php'; ?>