/home/awneajlw/public_html/codestechvista.com/completed-order-details.php
<?php
/**
* Completed Order Details Page - View Completed Order Details
* This page shows detailed view of a completed order with prescription info
* Features: Order details, prescription display, mark as done functionality
*/
if (session_status() == PHP_SESSION_NONE) {
session_start();
}
require_once 'config/database.php';
require_once 'includes/auth.php';
require_once 'includes/currency_helper.php';
// require_once 'vendor/autoload.php'; // Twilio SDK
// require_once 'config/twilio_config.php'; // Twilio credentials
use Twilio\Rest\Client;
// Check if user is logged in
if (!isLoggedIn()) {
header('Location: welcome.php');
exit();
}
// WhatsApp sending function
// function sendWhatsApp($to_number, $message) {
// try {
// $client = new Client(TWILIO_ACCOUNT_SID, TWILIO_AUTH_TOKEN);
// $client->messages->create(
// 'whatsapp:' . $to_number,
// [
// 'from' => TWILIO_WHATSAPP_NUMBER,
// 'body' => $message
// ]
// );
// if(WHATSAPP_DEBUG) {
// echo "WhatsApp sent to $to_number<br>";
// }
// } catch (Exception $e) {
// echo "Error sending WhatsApp: " . $e->getMessage();
// }
// }
// Get order ID from URL
$order_id = isset($_GET['id']) ? (int)$_GET['id'] : 0;
$success_message = '';
$error_message = '';
// Handle Mark as Done (optional functionality)
// if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action']) && $_POST['action'] === 'mark_done') {
// try {
// $database = new Database();
// $db = $database->getConnection();
// $user_id = $_SESSION['user_id'];
// // Fetch order first
// $query = "SELECT * FROM orders WHERE id = ? AND user_id = ?";
// $stmt = $db->prepare($query);
// $stmt->execute([$order_id, $user_id]);
// $order_data = $stmt->fetch(PDO::FETCH_ASSOC);
// // You can add additional status like 'Done' or keep as 'Completed'
// $update_query = "UPDATE orders SET status = 'Done' WHERE id = ? AND user_id = ?";
// $stmt = $db->prepare($update_query);
// $stmt->execute([$order_id, $user_id]);
// if ($stmt->rowCount() > 0) {
// $success_message = 'Order marked as done successfully!';
// // Send WhatsApp to customer
// if (!empty($order_data['whatsapp_number'])) {
// $customer_number = $order_data['whatsapp_number'];
// $message = "✅ Your order #{$order_data['id']} has been marked as Done.\nThank you for choosing OPTI SLIP.";
// sendWhatsApp($customer_number, $message);
// }
// } else {
// $error_message = 'Failed to update order status.';
// }
// } catch (Exception $e) {
// $error_message = 'Database error: ' . $e->getMessage();
// }
// }
// Email sending function
function sendOrderCompletionEmail($order_data, $user_email, $user_name, $user_currency = 'USD') {
try {
$to = $user_email;
$subject = "Order Completed - OPTI SLIP";
$message = "
<html>
<head>
<title>Order Completed</title>
</head>
<body>
<h2>Order Successfully Completed!</h2>
<p>Dear $user_name,</p>
<p>Your order has been marked as completed. Here are the details:</p>
<table border='1' cellpadding='5' cellspacing='0'>
<tr><td><strong>Order ID:</strong></td><td>{$order_data['id']}</td></tr>
<tr><td><strong>Tracking ID:</strong></td><td>{$order_data['tracking_id']}</td></tr>
<tr><td><strong>Patient Name:</strong></td><td>{$order_data['patient_name']}</td></tr>
<tr><td><strong>Total Amount:</strong></td><td>" . formatCurrency($order_data['total_amount'], $user_currency) . "</td></tr>
<tr><td><strong>Status:</strong></td><td>Completed</td></tr>
<tr><td><strong>Completion Date:</strong></td><td>" . date('d/m/Y H:i:s') . "</td></tr>
</table>
<p>Thank you for choosing OPTI SLIP!</p>
<p><strong>OPTI SLIP Team</strong></p>
</body>
</html>
";
// Email headers
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$headers .= "From: no-reply@optislip.com" . "\r\n";
// Send email
return mail($to, $subject, $message, $headers);
} catch (Exception $e) {
error_log("Email sending failed: " . $e->getMessage());
return false;
}
}
// Admin email notification function
function sendAdminNotification($order_data, $shop_name, $user_currency = 'USD') {
try {
$to = "admin@optislip.com"; // Replace with actual admin email
$subject = "New Order Completed - $shop_name";
$message = "
<html>
<head>
<title>Order Completed Notification</title>
</head>
<body>
<h2>New Order Completed!</h2>
<p>A new order has been marked as completed:</p>
<table border='1' cellpadding='5' cellspacing='0'>
<tr><td><strong>Shop Name:</strong></td><td>$shop_name</td></tr>
<tr><td><strong>Order ID:</strong></td><td>{$order_data['id']}</td></tr>
<tr><td><strong>Tracking ID:</strong></td><td>{$order_data['tracking_id']}</td></tr>
<tr><td><strong>Patient Name:</strong></td><td>{$order_data['patient_name']}</td></tr>
<tr><td><strong>Total Amount:</strong></td><td>" . formatCurrency($order_data['total_amount'], $user_currency) . "</td></tr>
<tr><td><strong>Completion Date:</strong></td><td>" . date('d/m/Y H:i:s') . "</td></tr>
</table>
</body>
</html>
";
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$headers .= "From: notifications@optislip.com" . "\r\n";
return mail($to, $subject, $message, $headers);
} catch (Exception $e) {
error_log("Admin email sending failed: " . $e->getMessage());
return false;
}
}
// if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action']) && $_POST['action'] === 'mark_done') {
// try {
// // Single database connection
// $database = new Database();
// $db = $database->getConnection();
// $user_id = $_SESSION['user_id'];
// // Pehle check karo ke order already Done to nahi
// $stmt = $db->prepare("SELECT * FROM orders WHERE id = ? AND user_id = ?");
// $stmt->execute([$order_id, $user_id]);
// $order_data = $stmt->fetch(PDO::FETCH_ASSOC);
// if (!$order_data) {
// $error_message = 'Order not found.';
// } elseif ($order_data['status'] === 'Done') {
// $error_message = 'Order is already marked as Done.';
// } else {
// // Update status
// $stmt = $db->prepare("UPDATE orders SET status = 'Done' WHERE id = ? AND user_id = ?");
// $stmt->execute([$order_id, $user_id]);
// if ($stmt->rowCount() > 0) {
// $success_message = 'Order marked as Done successfully!';
// // Send WhatsApp if number exists
// if (!empty($order_data['whatsapp_number'])) {
// $customer_number = $order_data['whatsapp_number'];
// if (substr($customer_number, 0, 1) == '0') {
// $customer_number = '+92' . substr($customer_number, 1);
// }
// $message = "✅ Your order #{$order_data['id']} has been marked as Done.\nThank you for choosing OPTI SLIP.";
// // WhatsApp send wrapped in try-catch
// try {
// sendWhatsApp($customer_number, $message);
// } catch (Exception $e) {
// // Log error instead of showing to user
// error_log("WhatsApp sending failed for Order #{$order_data['id']}: " . $e->getMessage());
// }
// }
// } else {
// $error_message = 'Failed to update order status.';
// }
// }
// } catch (Exception $e) {
// $error_message = 'Database error: ' . $e->getMessage();
// }
// }
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action']) && $_POST['action'] === 'mark_done') {
try {
$database = new Database();
$db = $database->getConnection();
$user_id = $_SESSION['user_id'];
// Get user's currency
$user_currency = getUserCurrency($db, $user_id);
// Pehle check karo ke order already Done to nahi
$stmt = $db->prepare("SELECT * FROM orders WHERE id = ? AND user_id = ?");
$stmt->execute([$order_id, $user_id]);
$order_data = $stmt->fetch(PDO::FETCH_ASSOC);
if (!$order_data) {
$error_message = 'Order not found.';
} elseif ($order_data['status'] === 'Done') {
$error_message = 'Order is already marked as Done.';
} else {
// Update status
$stmt = $db->prepare("UPDATE orders SET status = 'Done' WHERE id = ? AND user_id = ?");
$stmt->execute([$order_id, $user_id]);
if ($stmt->rowCount() > 0) {
$success_message = 'Order marked as Done successfully!';
// Send email to user
try {
// Get user details for email
$user_stmt = $db->prepare("SELECT email, name FROM users WHERE id = ?");
$user_stmt->execute([$user_id]);
$user_data = $user_stmt->fetch(PDO::FETCH_ASSOC);
if ($user_data && !empty($user_data['email'])) {
sendOrderCompletionEmail($order_data, $user_data['email'], $user_data['name'], $user_currency);
$success_message .= ' Email sent to user.';
}
} catch (Exception $e) {
error_log("User email sending failed: " . $e->getMessage());
}
// Send email to admin
try {
// Get shop name for admin notification
$shop_stmt = $db->prepare("SELECT shop_name FROM shops WHERE user_id = ?");
$shop_stmt->execute([$user_id]);
$shop_data = $shop_stmt->fetch(PDO::FETCH_ASSOC);
$shop_name = $shop_data['shop_name'] ?? 'Unknown Shop';
sendAdminNotification($order_data, $shop_name, $user_currency);
$success_message .= ' Admin notified.';
} catch (Exception $e) {
error_log("Admin email sending failed: " . $e->getMessage());
}
} else {
$error_message = 'Failed to update order status.';
}
}
} catch (Exception $e) {
$error_message = 'Database error: ' . $e->getMessage();
}
}
// Get order details
$order_data = null;
try {
$database = new Database();
$db = $database->getConnection();
$user_id = $_SESSION['user_id'];
$query = "SELECT * FROM orders WHERE id = ? AND user_id = ? AND (status = 'Completed' OR status = 'Done')";
$stmt = $db->prepare($query);
$stmt->execute([$order_id, $user_id]);
$order_data = $stmt->fetch(PDO::FETCH_ASSOC);
// Get user's currency
$user_currency = getUserCurrency($db, $user_id);
} catch (Exception $e) {
$error_message = "Error loading order: " . $e->getMessage();
}
// If no order found, redirect to completed orders
if (!$order_data) {
header('Location: completed-orders.php');
exit();
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Completed Order Details - OPTI SLIP</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css" rel="stylesheet">
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: 'Inter', sans-serif;
background: #f5f5f5;
min-height: 100vh;
padding: 20px;
}
.container {
max-width: 500px;
margin: 0 auto;
padding: 0;
}
.header-section {
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 30px;
}
.back-btn {
background: none;
border: none;
color: black;
font-size: 24px;
cursor: pointer;
padding: 10px;
transition: all 0.3s ease;
}
.back-btn:hover {
color: #169D53;
transform: translateX(-3px);
}
.logo-section {
text-align: center;
}
.logo-image {
width: 80px;
height: 80px;
object-fit: contain;
filter: brightness(0) saturate(100%);
}
.details-card {
background: white;
border-radius: 15px;
box-shadow: 0 4px 15px rgba(0,0,0,0.1);
overflow: hidden;
margin-bottom: 30px;
}
.detail-row {
display: flex;
justify-content: space-between;
align-items: center;
padding: 15px 20px;
border-bottom: 1px solid #f0f0f0;
}
.detail-row:last-child {
border-bottom: none;
}
.detail-label {
font-size: 14px;
color: #666;
font-weight: 500;
}
.detail-value {
font-size: 14px;
color: #333;
font-weight: 600;
text-align: right;
max-width: 200px;
word-wrap: break-word;
}
.special-note {
padding: 15px 20px;
border-bottom: 1px solid #f0f0f0;
}
.note-label {
font-size: 14px;
color: #666;
font-weight: 500;
margin-bottom: 8px;
}
.note-content {
font-size: 13px;
color: #333;
line-height: 1.5;
background: #f8f9fa;
padding: 10px;
border-radius: 8px;
}
.prescription-section {
padding: 15px 20px;
}
.prescription-title {
font-size: 14px;
color: #666;
font-weight: 500;
margin-bottom: 15px;
}
.eye-section {
margin-bottom: 15px;
}
.eye-title {
font-size: 13px;
color: #333;
font-weight: 600;
margin-bottom: 8px;
}
.prescription-values {
display: flex;
gap: 20px;
font-size: 12px;
color: #666;
margin-bottom: 10px;
}
.prescription-value {
display: flex;
align-items: center;
gap: 5px;
}
.add-section {
margin-top: 10px;
padding-top: 10px;
border-top: 1px solid #f0f0f0;
}
.add-value {
font-size: 12px;
color: #666;
}
.action-button {
text-align: center;
margin-top: 20px;
}
.done-btn {
background: #28a745;
color: white;
border: none;
padding: 15px 40px;
border-radius: 25px;
font-size: 16px;
font-weight: 600;
cursor: pointer;
transition: all 0.3s ease;
text-decoration: none;
display: inline-block;
}
.done-btn:hover {
background: #218838;
color: white;
transform: translateY(-2px);
}
.alert {
padding: 12px 15px;
border-radius: 8px;
margin-bottom: 20px;
font-size: 13px;
}
.alert-success {
background: #d4edda;
color: #155724;
border: 1px solid #c3e6cb;
}
.alert-danger {
background: #f8d7da;
color: #721c24;
border: 1px solid #f5c6cb;
}
/* Mobile Responsive */
@media (max-width: 768px) {
body {
padding: 10px;
}
.container {
max-width: 100%;
}
.header-section {
margin-bottom: 20px;
}
.logo-image {
width: 60px;
height: 60px;
}
.detail-row {
padding: 12px 15px;
}
.detail-value {
max-width: 150px;
font-size: 13px;
}
.detail-label {
font-size: 13px;
}
.prescription-values {
flex-direction: column;
gap: 5px;
}
.done-btn {
padding: 12px 30px;
font-size: 14px;
}
}
</style>
</head>
<body>
<div class="container">
<div class="header-section">
<button class="back-btn" onclick="window.location.href='completed-orders.php'">
<i class="fas fa-arrow-left"></i>
</button>
<div class="logo-section">
<img src="assets/images/Optislipimage.png" alt="Opti Slip Logo" class="logo-image" onerror="this.style.display='none'; this.parentElement.innerHTML='<div style=\'color: black; font-size: 16px; font-weight: bold;\'>OPTI SLIP</div>'">
</div>
<div style="width: 34px;"></div> <!-- Spacer for centering -->
</div>
<!-- Success/Error Messages -->
<?php if ($success_message): ?>
<div class="alert alert-success">
<i class="fas fa-check-circle me-2"></i>
<?php echo htmlspecialchars($success_message); ?>
</div>
<?php endif; ?>
<?php if ($error_message): ?>
<div class="alert alert-danger">
<i class="fas fa-exclamation-triangle me-2"></i>
<?php echo htmlspecialchars($error_message); ?>
</div>
<?php endif; ?>
<div class="details-card">
<div class="detail-row">
<span class="detail-label">Patient Name</span>
<span class="detail-value"><?php echo htmlspecialchars($order_data['patient_name'] ?? 'N/A'); ?></span>
</div>
<div class="detail-row">
<span class="detail-label">WhatsApp Number</span>
<span class="detail-value"><?php echo htmlspecialchars($order_data['whatsapp_number'] ?? 'N/A'); ?></span>
</div>
<div class="detail-row">
<span class="detail-label">Frame Detail</span>
<span class="detail-value"><?php echo htmlspecialchars($order_data['frame_detail'] ?? 'N/A'); ?></span>
</div>
<div class="detail-row">
<span class="detail-label">Lens Type</span>
<span class="detail-value"><?php echo htmlspecialchars($order_data['lens_type'] ?? 'N/A'); ?></span>
</div>
<div class="detail-row">
<span class="detail-label">Booking Date</span>
<span class="detail-value"><?php echo $order_data['created_at'] ? date('d/m/Y', strtotime($order_data['created_at'])) : 'N/A'; ?></span>
</div>
<div class="detail-row">
<span class="detail-label">Total Amount</span>
<span class="detail-value"><?php echo formatCurrency($order_data['total_amount'] ?? 0, $user_currency); ?></span>
</div>
<?php if (!empty($order_data['important_note'])): ?>
<div class="special-note">
<div class="note-label">Special Note</div>
<div class="note-content"><?php echo nl2br(htmlspecialchars($order_data['important_note'])); ?></div>
</div>
<?php endif; ?>
<div class="prescription-section">
<div class="prescription-title">Prescription</div>
<div class="eye-section">
<div class="eye-title">Right Eye :</div>
<div class="prescription-values">
<div class="prescription-value">
<span>SPH:</span>
<span><?php echo number_format($order_data['right_eye_sph'] ?? 0, 2); ?></span>
</div>
<div class="prescription-value">
<span>CYL:</span>
<span><?php echo number_format($order_data['right_eye_cyl'] ?? 0, 2); ?></span>
</div>
<div class="prescription-value">
<span>AXIS:</span>
<span><?php echo intval($order_data['right_eye_axis'] ?? 0); ?></span>
</div>
</div>
</div>
<div class="eye-section">
<div class="eye-title">Left Eye :</div>
<div class="prescription-values">
<div class="prescription-value">
<span>SPH:</span>
<span><?php echo number_format($order_data['left_eye_sph'] ?? 0, 2); ?></span>
</div>
<div class="prescription-value">
<span>CYL:</span>
<span><?php echo number_format($order_data['left_eye_cyl'] ?? 0, 2); ?></span>
</div>
<div class="prescription-value">
<span>AXIS:</span>
<span><?php echo intval($order_data['left_eye_axis'] ?? 0); ?></span>
</div>
</div>
</div>
<?php if (!empty($order_data['add_value'])): ?>
<div class="add-section">
<div class="add-value">Add: <?php echo htmlspecialchars($order_data['add_value']); ?></div>
</div>
<?php endif; ?>
</div>
</div>
<div class="action-button">
<form method="POST" style="display: inline;">
<input type="hidden" name="action" value="mark_done">
<button type="submit" class="done-btn" onclick="return confirm('Are you sure you want to mark this order as done?')">
Mark As Done
</button>
</form>
</div>
</div>
</body>
</html>