File size: 1,330 Bytes
84e853d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
```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: