aa21123a / reports.php
Funybubble's picture
make the backend php - Follow Up Deployment
84e853d verified
```php
<?php
require_once 'config.php';
// JWT verification middleware
verifyToken();
// GET sales report
if ($_SERVER['REQUEST_METHOD'] === 'GET') {
$start_date = $_GET['start_date'] ?? date('Y-m-01');
$end_date = $_GET['end_date'] ?? date('Y-m-t');
// Total sales
$stmt = $pdo->prepare("SELECT SUM(total_amount) as total_sales FROM orders WHERE created_at BETWEEN ? AND ?");
$stmt->execute([$start_date, $end_date]);
$total_sales = $stmt->fetch(PDO::FETCH_ASSOC);
// Top products
$stmt = $pdo->prepare("
SELECT p.name, SUM(oi.quantity) as total_sold, SUM(oi.quantity * oi.unit_price) as revenue
FROM order_items oi
JOIN products p ON oi.product_id = p.id
JOIN orders o ON oi.order_id = o.id
WHERE o.created_at BETWEEN ? AND ?
GROUP BY p.id
ORDER BY total_sold DESC
LIMIT 5
");
$stmt->execute([$start_date, $end_date]);
$top_products = $stmt->fetchAll(PDO::FETCH_ASSOC);
// Report data
$report = array(
'total_sales' => $total_sales['total_sales'] ?? 0,
'start_date' => $start_date,
'end_date' => $end_date,
'top_products' => $top_products
);
echo json_encode($report);
}
?>
```
You'll also need to create the database tables. Here's the SQL schema: