<?php
include 'includes/config.php';

// Get current financial year range (April 1st to March 31st)
$currentYear = date('Y');
$month = date('m');
$startDate = ($month >= 4) ? "$currentYear-04-01" : ($currentYear - 1) . "-04-01";
$endDate = ($month >= 4) ? ($currentYear + 1) . "-03-31" : "$currentYear-03-31";

// Query to get sales data for the current financial year
$sql = "
    SELECT DATE(date_sale) as sale_date, SUM(sale_qty) as total_sales
    FROM stock_sale
    WHERE date_sale BETWEEN :start_date AND :end_date
    GROUP BY sale_date
    ORDER BY sale_date ASC
";

$stmt = $conn->prepare($sql);
$stmt->bindValue(':start_date', $startDate);
$stmt->bindValue(':end_date', $endDate);
$stmt->execute();
$salesData = $stmt->fetchAll(PDO::FETCH_ASSOC);

// Prepare the data for JSON
$result = [];
foreach ($salesData as $row) {
    $result[] = [
        'date' => $row['sale_date'],
        'total_sales' => (float)$row['total_sales']
    ];
}

echo json_encode($result);
?>
