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(); ?>
| = __('item') ?> | = __('service') ?> | = __('quantity') ?> | = __('price') ?> | = __('vat') ?> | = __('subtotal') ?> |
|---|---|---|---|---|---|
| = $lang === 'ar' ? ($item['item_ar'] ?: $item['item_en']) : $item['item_en'] ?> | = $lang === 'ar' ? ($item['service_ar'] ?: $item['service_en']) : $item['service_en'] ?> | = $item['quantity'] ?> | = format_amount($item['unit_price']) ?> | = format_amount($item['vat_amount'] * $item['quantity']) ?> | = format_amount($item['subtotal'] + ($item['vat_amount'] * $item['quantity'])) ?> |
| = __('subtotal') ?> | = format_amount($subtotal_sum) ?> | ||||
| = __('vat_total') ?? 'VAT Total' ?> | = format_amount($order['vat_total']) ?> | ||||
| = __('loyalty_discount') ?? 'Loyalty Discount' ?> | -= format_amount($order['loyalty_discount']) ?> | ||||
| = __('total') ?> | = format_amount($order['total_price'] - $order['loyalty_discount']) ?> | ||||
= $lang === 'ar' ? ($order['customer_name_ar'] ?: $order['customer_name_en']) : $order['customer_name_en'] ?>
= $order['customer_phone'] ?>
= $lang === 'ar' ? ($order['customer_address_ar'] ?: $order['customer_address_en']) : $order['customer_address_en'] ?>
= date('d M Y', strtotime($order['created_at'])) ?>
= date('h:i A', strtotime($order['created_at'])) ?>
| = __('date') ?> | = __('method') ?> | = __('amount') ?> |
|---|---|---|
| = date('d M Y, h:i A', strtotime($p['created_at'])) ?> | = __($p['payment_method']) ?> | = format_amount($p['amount']) ?> |
| = __('no_payments') ?> | ||