<?php
// Include your database connection code here, and ensure $conn is defined.
include_once 'shikanisha.kts.php';

if ($_SERVER['REQUEST_METHOD'] === 'GET') {
    $response = array();

    // Execute the SQL query for the current month
    $sql = "SELECT p.payment_method, SUM(p.amount) AS tills
            FROM payments AS p
            JOIN transaction_receipts AS tr ON p.trans_id = tr.trans_id
            WHERE DATE_FORMAT(tr.seller_date, '%Y-%m') = DATE_FORMAT(NOW(), '%Y-%m')
            GROUP BY p.payment_method;";
    $result = mysqli_query($conn, $sql);

    if ($result) {
        $response['success'] = true;
        $response['reportData'] = array();
        $response['monthlyTotal'] = 0;

        while ($row = mysqli_fetch_assoc($result)) {
            $response['reportData'][] = $row;
            $response['monthlyTotal'] += $row['tills'];
        }
    } else {
        $response['success'] = false;
        $response['message'] = 'Error executing the SQL query.';
    }

    // Return the response as JSON
    header('Content-type: application/json');
    echo json_encode($response);
} else {
    // Handle non-GET requests
    http_response_code(405);
    echo json_encode(array("message" => "Method not allowed"));
}
?>
