<?php
session_start();
// Database connection
require_once 'shikanisha.kts.php';
date_default_timezone_set('Africa/Nairobi');

// Check if 'user' exists in the $_SESSION array, and set a default value if not found
$user = isset($_SESSION['user']) ? $_SESSION['user'] : 'DefaultUser';

// Define the start and end date range
$start_datetime = date('Y-m-d 04:00:00');
$end_datetime = date('Y-m-d 23:00:00');

// Check for the HTTP request method
if ($_SERVER['REQUEST_METHOD'] === 'GET') {
    // Build the SQL query to get all products within the specified date range
    $sql = "SELECT amount_received, payment_method, phone_number, seller_date, seller_name, total, trans_id, table_name
            FROM transaction_receipts
            WHERE seller_name='$user' AND seller_date BETWEEN '$start_datetime' AND '$end_datetime'  AND (amount_received = total OR amount_received > total)";

    // Execute the query and fetch the results
    $result = $conn->query($sql);
    $data = [];

    if ($result) {
        if ($result->num_rows > 0) {
            while ($row = $result->fetch_assoc()) {
                $data[] = $row;
            }
        }
    }

    // Return the data as JSON
    header('Content-Type: application/json');
    echo json_encode($data);
}
?>
