35900-vm/stats.php
2025-11-21 11:55:41 +00:00

134 lines
5.7 KiB
PHP

<?php
require_once __DIR__ . '/db/config.php';
$sales_data = [];
$error_message = '';
$total_sales = 0;
$total_quantity = 0;
$sales_by_product = [];
try {
$pdo = db();
$stmt = $pdo->query("SELECT product_name, quantity, sale_date FROM sales ORDER BY sale_date DESC");
$sales_data = $stmt->fetchAll();
if ($sales_data) {
$total_sales = count($sales_data);
$total_quantity = array_sum(array_column($sales_data, 'quantity'));
foreach ($sales_data as $sale) {
if (!isset($sales_by_product[$sale['product_name']])) {
$sales_by_product[$sale['product_name']] = 0;
}
$sales_by_product[$sale['product_name']] += $sale['quantity'];
}
arsort($sales_by_product);
}
} catch (PDOException $e) {
$error_message = "Error fetching sales data: " . $e->getMessage();
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Sales Stats - Sales Tracker</title>
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Poppins:wght@400;600;700&display=swap" rel="stylesheet">
<link rel="stylesheet" href="assets/css/custom.css?v=<?php echo time(); ?>">
</head>
<body>
<div class="mobile-container">
<header class="app-header">
<a href="index.php" class="back-button">&larr;</a>
<h1>Sales Statistics</h1>
</header>
<main class="main-content">
<?php if ($error_message): ?>
<div class="alert error">
<?php echo htmlspecialchars($error_message); ?>
</div>
<?php elseif (empty($sales_data)) : ?>
<div class="alert">
No sales data yet. Add a sale to see statistics here.
</div>
<?php else: ?>
<div class="stats-summary">
<div class="summary-card">
<span class="value"><?php echo $total_sales; ?></span>
<span class="label">Total Sales</span>
</div>
<div class="summary-card">
<span class="value"><?php echo $total_quantity; ?></span>
<span class="label">Items Sold</span>
</div>
</div>
<h2 class="h5 mt-4 mb-3 text-white">Sales by Product</h2>
<div class="table-responsive">
<table class="table sales-table">
<thead>
<tr>
<th>Product</th>
<th>Total Quantity Sold</th>
</tr>
</thead>
<tbody>
<?php foreach ($sales_by_product as $product_name => $quantity): ?>
<tr>
<td><?php echo htmlspecialchars($product_name); ?></td>
<td><?php echo htmlspecialchars((string)$quantity); ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
<h2 class="h5 mt-4 mb-3 text-white">Recent Sales</h2>
<div class="table-responsive">
<table class="table sales-table">
<thead>
<tr>
<th>Product</th>
<th>Quantity</th>
<th>Date</th>
</tr>
</thead>
<tbody>
<?php foreach ($sales_data as $sale): ?>
<tr>
<td><?php echo htmlspecialchars($sale['product_name']); ?></td>
<td><?php echo htmlspecialchars((string)$sale['quantity']); ?></td>
<td><?php echo htmlspecialchars(date("M d, Y H:i", strtotime($sale['sale_date']))); ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
<?php endif; ?>
</main>
</div>
<!-- Bottom Navigation -->
<nav class="bottom-nav">
<a href="index.php" class="nav-item">
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M3 9l9-7 9 7v11a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2z"></path><polyline points="9 22 9 12 15 12 15 22"></polyline></svg>
<span>Home</span>
</a>
<a href="add_sale.php" class="nav-item">
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><circle cx="12" cy="12" r="10"></circle><line x1="12" y1="8" x2="12" y2="16"></line><line x1="8" y1="12" x2="16" y2="12"></line></svg>
<span>Add Sale</span>
</a>
<a href="stats.php" class="nav-item active">
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M2.5 2v6h6M2.66 15.57a10 10 0 1 0 .57-8.38"/></svg>
<span>Stats</span>
</a>
</nav>
</body>
</html>