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

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $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;
        $reportData = '';
        $monthlyTotal = 0;

        while ($row = mysqli_fetch_assoc($result)) {
            $reportData .= '<tr>
                <td>' . $row['payment_method'] . '</td>
                <td>' . $row['tills'] . '</td>
            </tr>';
            $monthlyTotal += $row['tills'];
        }

        $response['reportData'] = $reportData;
        $response['monthlyTotal'] = $monthlyTotal;
    } 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-POST requests
    // You may return an error response or redirect to another page
}
?>
