177 lines
7.2 KiB
PHP
177 lines
7.2 KiB
PHP
<?php
|
|
$page_title = 'Dashboard';
|
|
require_once __DIR__ . '/partials/header.php';
|
|
require_once __DIR__ . '/db/config.php';
|
|
|
|
$user_id = $_SESSION['user_id'];
|
|
$user_role = $_SESSION['user_role'];
|
|
|
|
$pdo = db();
|
|
|
|
// Initialize counts
|
|
$pending_orders_count = 0;
|
|
$pending_replies_count = 0;
|
|
$shipped_orders_month_count = 0;
|
|
|
|
try {
|
|
// --- Fetch Dashboard Counts ---
|
|
$base_conditions_arr = [];
|
|
$params = [];
|
|
|
|
if ($user_role === 'Sales Rep') {
|
|
$base_conditions_arr[] = "sales_rep_id = :user_id";
|
|
$params[':user_id'] = $user_id;
|
|
}
|
|
|
|
// Pending Orders Count
|
|
$pending_orders_conditions_arr = $base_conditions_arr;
|
|
$pending_orders_conditions_arr[] = "status = 'Pending'";
|
|
|
|
$pending_orders_where_clause = '';
|
|
if (!empty($pending_orders_conditions_arr)) {
|
|
$pending_orders_where_clause = " WHERE " . implode(" AND ", $pending_orders_conditions_arr);
|
|
}
|
|
$stmt = $pdo->prepare("SELECT COUNT(*) FROM orders " . $pending_orders_where_clause);
|
|
$stmt->execute($params);
|
|
$pending_orders_count = $stmt->fetchColumn();
|
|
|
|
// Pending Replies Count (Orders with status 'Query' or 'Query Replied')
|
|
$pending_replies_conditions_arr = $base_conditions_arr;
|
|
$pending_replies_conditions_arr[] = "(status = 'Query' OR status = 'Query Replied')";
|
|
|
|
$pending_replies_where_clause = '';
|
|
if (!empty($pending_replies_conditions_arr)) {
|
|
$pending_replies_where_clause = " WHERE " . implode(" AND ", $pending_replies_conditions_arr);
|
|
}
|
|
$stmt = $pdo->prepare("SELECT COUNT(*) FROM orders " . $pending_replies_where_clause);
|
|
$stmt->execute($params);
|
|
$pending_replies_count = $stmt->fetchColumn();
|
|
|
|
// Shipped Orders This Month Count
|
|
$current_month_start = date('Y-m-01 00:00:00');
|
|
$current_month_end = date('Y-m-t 23:59:59');
|
|
|
|
$shipped_conditions_arr = $base_conditions_arr;
|
|
$shipped_conditions_arr[] = "status = 'Shipped'";
|
|
$shipped_conditions_arr[] = "created_at >= :start_date";
|
|
$shipped_conditions_arr[] = "created_at <= :end_date";
|
|
|
|
$shipped_where_clause = '';
|
|
$shipped_params = $params;
|
|
$shipped_params[':start_date'] = $current_month_start;
|
|
$shipped_params[':end_date'] = $current_month_end;
|
|
|
|
if (!empty($shipped_conditions_arr)) {
|
|
$shipped_where_clause = " WHERE " . implode(" AND ", $shipped_conditions_arr);
|
|
}
|
|
|
|
$stmt = $pdo->prepare("SELECT COUNT(*) FROM orders " . $shipped_where_clause);
|
|
$stmt->execute($shipped_params);
|
|
$shipped_orders_month_count = $stmt->fetchColumn();
|
|
|
|
// --- Fetch Order List ---
|
|
$orders = [];
|
|
$sql = "SELECT o.id, o.order_number, o.order_date, o.order_text, o.status, o.sales_rep_id, u.name as sales_rep_name
|
|
FROM orders o
|
|
JOIN users u ON o.sales_rep_id = u.id";
|
|
|
|
if ($user_role === 'Sales Rep') {
|
|
$sql .= " WHERE o.sales_rep_id = :user_id";
|
|
$stmt = $pdo->prepare($sql);
|
|
$stmt->bindParam(':user_id', $user_id, PDO::PARAM_INT);
|
|
} else {
|
|
// Dispatch and Admin can see all orders
|
|
$stmt = $pdo->prepare($sql);
|
|
}
|
|
$stmt->execute();
|
|
$orders = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
|
} catch (PDOException $e) {
|
|
error_log("Database error fetching dashboard data or orders: " . $e->getMessage());
|
|
echo '<div class="alert alert-danger" role="alert">Error loading dashboard data. Please try again later.</div>';
|
|
}
|
|
|
|
?>
|
|
|
|
<div class="row mb-4">
|
|
<div class="col-md-4">
|
|
<div class="card border-0 shadow-sm text-primary-emphasis bg-primary-subtle">
|
|
<div class="card-body">
|
|
<h5 class="card-title">Pending Orders</h5>
|
|
<p class="card-text h2"><?php echo htmlspecialchars($pending_orders_count); ?></p>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<div class="col-md-4">
|
|
<div class="card border-0 shadow-sm text-warning-emphasis bg-warning-subtle">
|
|
<div class="card-body">
|
|
<h5 class="card-title">Pending Replies</h5>
|
|
<p class="card-text h2"><?php echo htmlspecialchars($pending_replies_count); ?></p>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<div class="col-md-4">
|
|
<div class="card border-0 shadow-sm text-success-emphasis bg-success-subtle">
|
|
<div class="card-body">
|
|
<h5 class="card-title">Shipped This Month</h5>
|
|
<p class="card-text h2"><?php echo htmlspecialchars($shipped_orders_month_count); ?></p>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="row">
|
|
<div class="col-12 mb-4">
|
|
<div class="card border-0 shadow-sm">
|
|
<div class="card-body">
|
|
<h1 class="h4 mb-0">Welcome, <?php echo htmlspecialchars($_SESSION['user_name']); ?>!</h1>
|
|
<p class="text-muted">Your current role: <?php echo htmlspecialchars($user_role); ?></p>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="col-12">
|
|
<div class="card border-0 shadow-sm">
|
|
<div class="card-body">
|
|
<h5 class="card-title">Order List</h5>
|
|
<?php if (empty($orders)): ?>
|
|
<p>No orders found.</p>
|
|
<?php else: ?>
|
|
<div class="table-responsive">
|
|
<table class="table table-hover">
|
|
<thead>
|
|
<tr>
|
|
<th>Order Number</th>
|
|
<th>Date</th>
|
|
<th>Sales Rep</th>
|
|
<th>Text</th>
|
|
<th>Status</th>
|
|
<th>Actions</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
<?php foreach ($orders as $order): ?>
|
|
<tr>
|
|
<td><?php echo htmlspecialchars($order['order_number']); ?></td>
|
|
<td><?php echo htmlspecialchars($order['order_date']); ?></td>
|
|
<td><?php echo htmlspecialchars($order['sales_rep_name']); ?></td>
|
|
<td><?php echo htmlspecialchars(substr($order['order_text'], 0, 50)); ?>...</td>
|
|
<td><span class="badge bg-secondary"><?php echo htmlspecialchars($order['status']); ?></span></td>
|
|
<td>
|
|
<a href="view_order.php?id=<?php echo $order['id']; ?>" class="btn btn-sm btn-outline-primary">View</a>
|
|
<?php if ($user_role === 'Sales Rep' && $order['sales_rep_id'] == $user_id): ?>
|
|
<a href="view_order.php?id=<?php echo $order['id']; ?>" class="btn btn-sm btn-outline-secondary">Edit</a>
|
|
<?php endif; ?>
|
|
</td>
|
|
</tr>
|
|
<?php endforeach; ?>
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
<?php endif; ?>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<?php require_once __DIR__ . '/partials/footer.php'; ?>
|