php Copy code
<?php
// public/orders/view_orders.php
// Enable error reporting for debugging (disable in production)
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
// Include authentication and database connection
require_once __DIR__ . '/../../includes/auth.php';
require_once __DIR__ . '/../../includes/db_connect.php';
// Helper function to get product price
function get_product_price($conn, $product_id) {
$price_sql = "SELECT price FROM products WHERE id = ? LIMIT 1";
$price_stmt = $conn->prepare($price_sql);
$price_stmt->bind_param("i", $product_id);
$price_stmt->execute();
$price_result = $price_stmt->get_result();
if ($price_result->num_rows == 1) {
$price = $price_result->fetch_assoc()['price'];
} else {
$price = "0.00";
}
$price_stmt->close();
return $price;
}
// Handle status update
if (isset($_GET['action']) && $_GET['action'] === 'update_status') {
$order_id = isset($_GET['order_id']) ? intval($_GET['order_id']) : 0;
$new_status = isset($_GET['new_status']) ? trim($_GET['new_status']) : '';
// Validate new_status (Removed 'In Progress')
$valid_statuses = ['Pending', 'Completed', 'Delivered'];
if ($order_id > 0 && in_array($new_status, $valid_statuses)) {
// Optional: If setting to 'Delivered', ensure delivery_datetime is set
if ($new_status === 'Delivered') {
$check_sql = "SELECT delivery_datetime FROM orders WHERE id = ?";
$check_stmt = $conn->prepare($check_sql);
$check_stmt->bind_param("i", $order_id);
$check_stmt->execute();
$check_result = $check_stmt->get_result();
if ($check_result->num_rows == 1) {
$order = $check_result->fetch_assoc();
if (empty($order['delivery_datetime'])) {
header("Location: view_orders.php?error=3"); // Delivery datetime not set
exit();
}
}
$check_stmt->close();
}
// Update status using prepared statement
$update_sql = "UPDATE orders SET status = ? WHERE id = ?";
$stmt = $conn->prepare($update_sql);
if ($stmt) {
$stmt->bind_param("si", $new_status, $order_id);
if ($stmt->execute()) {
$stmt->close();
header("Location: view_orders.php?success=1");
exit();
} else {
$stmt->close();
header("Location: view_orders.php?error=1");
exit();
}
} else {
header("Location: view_orders.php?error=2");
exit();
}
} else {
header("Location: view_orders.php?error=2");
exit();
}
}
// Handle Date Filter
$filter = isset($_GET['filter']) ? $_GET['filter'] : 'all';
$where_clause = "";
$current_date = date('Y-m-d');
switch ($filter) {
case 'today':
$where_clause = "WHERE DATE(orders.order_date) = '$current_date'";
break;
case 'yesterday':
$yesterday = date('Y-m-d', strtotime('-1 day'));
$where_clause = "WHERE DATE(orders.order_date) = '$yesterday'";
break;
case 'week':
$start_of_week = date('Y-m-d', strtotime('monday this week'));
$end_of_week = date('Y-m-d', strtotime('sunday this week'));
$where_clause = "WHERE DATE(orders.order_date) BETWEEN '$start_of_week' AND '$end_of_week'";
break;
case 'month':
$start_of_month = date('Y-m-01');
$end_of_month = date('Y-m-t');
$where_clause = "WHERE DATE(orders.order_date) BETWEEN '$start_of_month' AND '$end_of_month'";
break;
case 'all':
default:
$where_clause = "";
$filter = 'all';
break;
}
// Fetch orders with customer and product details based on filter
$orders_sql = "
SELECT
orders.id AS order_id,
orders.status,
orders.order_date,
orders.delivery_datetime,
customers.name AS customer_name,
customers.contact,
customers.delivery_address,
orders.product_id AS primary_product_id,
orders.quantity AS primary_quantity,
orders.packaging_preferences AS primary_packaging_preferences,
products.name AS primary_product_name,
products.size AS primary_product_size
FROM
orders
JOIN
customers ON orders.customer_id = customers.id
JOIN
products ON orders.product_id = products.id
$where_clause
ORDER BY
orders.order_date DESC
";
$orders_result = $conn->query($orders_sql);
// Fetch order items for all orders
$order_items_sql = "
SELECT
order_items.order_id,
order_items.product_id,
order_items.quantity,
order_items.packaging_preferences,
products.name AS product_name,
products.size AS product_size
FROM
order_items
JOIN
products ON order_items.product_id = products.id
";
$order_items_result = $conn->query($order_items_sql);
// Organize order items by order_id
$order_items = [];
if ($order_items_result && $order_items_result->num_rows > 0) {
while ($item = $order_items_result->fetch_assoc()) {
$order_items[$item['order_id']][] = $item;
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>View Orders - Kaak Alia Management System</title>
<!-- Google Fonts -->
<link href="https://fonts.googleapis.com/css2?family=Cairo:wght@400;600;700&display=swap" rel="stylesheet">
<!-- Bootstrap CSS -->
<link rel="stylesheet" href="/kaak_alia_management_system/assets/bootstrap/css/bootstrap.min.css">
<!-- Bootstrap Icons -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.10.5/font/bootstrap-icons.css">
<!-- Embedded Custom Styles -->
<style>
/* ---------------------------------------------------
1. CSS Variables for Consistent Design
---------------------------------------------------- */
:root {
--dark-gray: #333333; /* Header and Footer Background */
--soft-brown: #FFD166; /* Add New Order Button */
--blue: #118AB2; /* View Orders Button */
--green: #06D6A0; /* Manage Products Button */
--orange: #F4A261; /* Manage Users Button */
--red-pink: #EF476F; /* Generate Reports Button */
--main-text: #333333;
--secondary-text: #666666;
--white: #FFFFFF;
--light-gray: #F9F9F9;
--divider-color: #FFD166;
--transition-speed: 0.3s;
--hover-shadow: rgba(0, 0, 0, 0.2);
}
/* ---------------------------------------------------
2. Global Styles
---------------------------------------------------- */
body {
font-family: 'Cairo', sans-serif;
background: linear-gradient(to bottom, #f5f7fa, #c3cfe2);
margin: 0;
padding: 30px;
box-sizing: border-box;
}
/* ---------------------------------------------------
3. Header Styles
---------------------------------------------------- */
.header {
padding: 20px 30px;
background-color: var(--dark-gray);
border-radius: 8px;
text-align: center;
margin-bottom: 30px;
color: var(--white);
font-size: 24px;
font-weight: bold;
}
/* ---------------------------------------------------
4. Success and Error Alerts
---------------------------------------------------- */
.alert-success-custom {
background-color: var(--green);
color: var(--white);
border-radius: 8px;
padding: 15px 20px;
margin-bottom: 20px;
text-align: center;
width: 70%;
margin: 20px auto;
}
.alert-error-custom {
background-color: var(--red-pink);
color: var(--white);
border-radius: 8px;
padding: 15px 20px;
margin-bottom: 20px;
text-align: center;
width: 70%;
margin: 20px auto;
}
/* ---------------------------------------------------
5. Recent Orders Table
---------------------------------------------------- */
.recent-orders-section {
background-color: #ffffff;
border-radius: 8px;
padding: 20px;
box-shadow: 0 4px 6px rgba(0,0,0,0.1);
}
.recent-orders-section h4 {
font-size: 16px;
font-weight: bold;
color: var(--main-text);
margin-bottom: 15px;
}
.filter-section {
margin-bottom: 20px;
text-align: right;
}
.filter-section .btn-group .btn {
margin-left: 5px;
}
.recent-orders-section table {
width: 100%;
border-collapse: collapse;
}
.recent-orders-section th,
.recent-orders-section td {
padding: 12px;
text-align: left;
font-size: 16px;
vertical-align: middle;
}
.recent-orders-section th {
background-color: var(--dark-gray);
color: var(--white);
font-weight: bold;
}
.recent-orders-section tr:nth-child(even) {
background-color: var(--light-gray);
}
.recent-orders-section tr:hover {
background-color: #e6f7ff;
}
.recent-orders-section td {
font-size: 14px;
color: var(--main-text);
}
/* Badge Styles */
.badge-status-pending {
background-color: #FFD166;
color: var(--white);
padding: 5px 10px;
border-radius: 12px;
font-size: 12px;
font-weight: bold;
}
.badge-status-completed {
background-color: #06D6A0;
color: var(--white);
padding: 5px 10px;
border-radius: 12px;
font-size: 12px;
font-weight: bold;
}
.badge-status-delivered {
background-color: #EF476F;
color: var(--white);
padding: 5px 10px;
border-radius: 12px;
font-size: 12px;
font-weight: bold;
}
/* ---------------------------------------------------
6. Actions Column Styles
---------------------------------------------------- */
.action-buttons .btn {
margin-right: 5px;
}
/* ---------------------------------------------------
7. Footer Styles
---------------------------------------------------- */
.footer {
background-color: var(--dark-gray);
color: var(--white);
padding: 15px;
text-align: center;
border-radius: 8px;
font-size: 14px;
line-height: 20px;
margin-top: 40px;
}
/* ---------------------------------------------------
8. Responsive Design Enhancements
---------------------------------------------------- */
@media (max-width: 992px) {
.recent-orders-section table,
.recent-orders-section thead,
.recent-orders-section tbody,
.recent-orders-section th,
.recent-orders-section td,
.recent-orders-section tr) {
display: block;
}
.recent-orders-section tr {
margin-bottom: 15px;
}
.recent-orders-section td {
text-align: right;
padding-left: 50%;
position: relative;
}
.recent-orders-section td::before {
content: attr(data-label);
position: absolute;
left: 10px;
width: 45%;
padding-right: 10px;
white-space: nowrap;
text-align: left;
font-weight: bold;
}
.filter-section {
text-align: center;
}
}
</style>
</head>
<body>
<!-- Header -->
<div class="header">
View Orders
</div>
<!-- Display Success or Error Messages -->
<?php if (isset($_GET['success']) && $_GET['success'] == 1): ?>
<div class="alert-success-custom">Order status updated successfully!</div>
<?php endif; ?>
<?php if (isset($_GET['error'])): ?>
<?php if ($_GET['error'] == 1): ?>
<div class="alert-error-custom">Failed to update order status.</div>
<?php elseif ($_GET['error'] == 2): ?>
<div class="alert-error-custom">Invalid status selected.</div>
<?php elseif ($_GET['error'] == 3): ?>
<div class="alert-error-custom">Cannot set status to Delivered without a delivery date and time.</div>
<?php endif; ?>
<?php endif; ?>
<!-- Container -->
<div class="recent-orders-section">
<h4>All Orders</h4>
<!-- Filter Section -->
<div class="filter-section">
<div class="btn-group" role="group" aria-label="Date Filters">
<a href="view_orders.php?filter=all" class="btn btn-<?php echo $filter === 'all' ? 'primary' : 'outline-primary'; ?>">All</a>
<a href="view_orders.php?filter=today" class="btn btn-<?php echo $filter === 'today' ? 'primary' : 'outline-primary'; ?>">Today</a>
<a href="view_orders.php?filter=yesterday" class="btn btn-<?php echo $filter === 'yesterday' ? 'primary' : 'outline-primary'; ?>">Yesterday</a>
<a href="view_orders.php?filter=week" class="btn btn-<?php echo $filter === 'week' ? 'primary' : 'outline-primary'; ?>">This Week</a>
<a href="view_orders.php?filter=month" class="btn btn-<?php echo $filter === 'month' ? 'primary' : 'outline-primary'; ?>">This Month</a>
</div>
</div>
<table>
<thead>
<tr>
<th>ID</th>
<th>Customer</th>
<th>Contact</th>
<th>Delivery Address</th>
<th>Primary Product</th>
<th>Additional Products</th>
<th>Status</th>
<th>Order Date</th>
<th>Delivery DateTime</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<?php if ($orders_result && $orders_result->num_rows > 0): ?>
<?php while($order = $orders_result->fetch_assoc()): ?>
<tr>
<td data-label="ID"><?= htmlspecialchars($order['order_id']) ?></td>
<td data-label="Customer"><?= htmlspecialchars($order['customer_name']) ?></td>
<td data-label="Contact"><?= htmlspecialchars($order['contact']) ?></td>
<td data-label="Delivery Address"><?= nl2br(htmlspecialchars($order['delivery_address'])) ?></td>
<td data-label="Primary Product">
<?= htmlspecialchars($order['primary_product_name'] . " - " . $order['primary_product_size'] . " (" . number_format(get_product_price($conn, $order['primary_product_id']), 2) . " KD)") ?><br>
Quantity: <?= htmlspecialchars($order['primary_quantity']) ?><br>
Packaging: <?= htmlspecialchars($order['primary_packaging_preferences']) ?>
</td>
<td data-label="Additional Products">
<?php
if (isset($order_items[$order['order_id']])) {
foreach ($order_items[$order['order_id']] as $item) {
echo htmlspecialchars($item['product_name'] . " - " . $item['product_size'] . " (" . number_format(get_product_price($conn, $item['product_id']), 2) . " KD)") . "<br>";
echo "Qty: " . htmlspecialchars($item['quantity']) . "<br>";
echo "Packaging: " . htmlspecialchars($item['packaging_preferences']) . "<br><br>";
}
} else {
echo "None";
}
?>
</td>
<td data-label="Status">
<?php
switch ($order['status']) {
case 'Pending':
echo '<span class="badge-status-pending">Pending</span>';
break;
case 'Completed':
echo '<span class="badge-status-completed">Completed</span>';
break;
case 'Delivered':
echo '<span class="badge-status-delivered">Delivered</span>';
break;
default:
echo '<span class="badge bg-light text-dark">Unknown</span>';
}
?>
</td>
<td data-label="Order Date"><?= date("d M Y, h:i A", strtotime($order['order_date'])) ?></td>
<td data-label="Delivery DateTime"><?= $order['delivery_datetime'] ? date("d M Y, h:i A", strtotime($order['delivery_datetime'])) : 'N/A' ?></td>
<td data-label="Actions">
<div class="action-buttons">
<!-- View Details -->
<a href="view_order_customer.php?order_id=<?= htmlspecialchars($order['order_id']) ?>" class="btn btn-sm btn-outline-primary" title="View Details">
<i class="bi bi-eye-fill"></i>
</a>
<!-- Edit Order -->
<a href="edit_order.php?id=<?= htmlspecialchars($order['order_id']) ?>" class="btn btn-sm btn-outline-secondary" title="Edit Order">
<i class="bi bi-pencil-fill"></i>
</a>
<!-- Change Status Dropdown -->
<div class="btn-group">
<button type="button" class="btn btn-sm btn-outline-info dropdown-toggle" data-bs-toggle="dropdown" aria-expanded="false" title="Change Status">
<i class="bi bi-arrow-repeat"></i>
</button>
<ul class="dropdown-menu">
<li><a class="dropdown-item" href="?action=update_status&order_id=<?= htmlspecialchars($order['order_id']) ?>&new_status=Pending">Pending</a></li>
<li><a class="dropdown-item" href="?action=update_status&order_id=<?= htmlspecialchars($order['order_id']) ?>&new_status=Completed">Completed</a></li>
<li><a class="dropdown-item" href="?action=update_status&order_id=<?= htmlspecialchars($order['order_id']) ?>&new_status=Delivered">Delivered</a></li>
</ul>
</div>
<!-- Delete Order -->
<a href="delete_order.php?order_id=<?= htmlspecialchars($order['order_id']) ?>" class="btn btn-sm btn-outline-danger" title="Delete Order" onclick="return confirm('Are you sure you want to delete this order?');">
<i class="bi bi-trash-fill"></i>
</a>
</div>
</td>
</tr>
<?php endwhile; ?>
<?php else: ?>
<tr>
<td colspan="10" class="text-center">No orders found.</td>
</tr>
<?php endif; ?>
</tbody>
</table>
</div>
<!-- Footer -->
<div class="footer">
© 2024 Kaak Alia Bakery. All rights reserved.
</div>
<!-- Bootstrap JS Bundle -->
<script src="/kaak_alia_management_system/assets/bootstrap/js/bootstrap.bundle.min.js"></script>
<!-- Initialize Bootstrap Tooltip (Optional) -->
<script>
var tooltipTriggerList = [].slice.call(document.querySelectorAll('[title]'))
var tooltipList = tooltipTriggerList.map(function (tooltipTriggerEl) {
return new bootstrap.Tooltip(tooltipTriggerEl)
})
</script>
</body>
</html>
Feel free to further customize the styles and functionalities to better fit your specific requirements!