<?php
    // required headers
    header("Access-Control-Allow-Origin: *");
    header("Content-Type: application/json; charset=UTF-8");
    header("Access-Control-Allow-Methods: POST");
    header("Access-Control-Max-Age: 3600");
    header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");
     
    // include database and object files
    include_once '../config/database.php';
    include_once '../objects/check_out.php';
     
    // instantiate database
    $database = new Database();
    $db = $database->getConnection();
     
    // initialize object
    $checkout = new CheckOut($db);
    $data = json_decode(file_get_contents("php://input"));

    $checkout->df = isset($_GET['df']) ? $_GET['df'] : null;
    $checkout->dt = isset($_GET['dt']) ? $_GET['dt'] : null;

    $stmt = $checkout->checkoutReportSearch();
    $num = $stmt->rowCount();

    $arr=array();
    $arr["records"]=array();

    $total_charges=$total_tax_charges=$total_balance=0;
    if($num>0){
        while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){
            extract($row);
            $ex=array(
                $id,
                $invoice_id,
                $reservation_no,
                $agent_name,
                $room_no,
                ($guest_name)?$guest_name:"",
				($checkin_date)?$checkin_date:"",
                ($checkout_date)?$checkout_date:"",
                $charges,
                number_format(round(($charges - ($charges / 1.07)), 2), 2, '.', ''),
                number_format(round($charges / 1.07, 2), 2, '.', ''),
                $agent_id
            );
            $total_charges += $charges;
            $total_tax_charges += $charges - ($charges/1.07);
            $total_balance += $charges/1.07;
            array_push($arr["records"], $ex);
        }
        $arr["total_charges"] = number_format(round($total_charges, 2), 2, '.', '');
        $arr["total_tax_charges"] = number_format(round($total_tax_charges, 2), 2, '.', '');
        $arr["total_balance"] = number_format(round($total_balance, 2), 2, '.', '');  // Applying rounding for consistency
        
    }
    echo json_encode($arr);
?>