<?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_once '../config/database.php';
include_once '../objects/purchase.php';

$database = new Database();
$db = $database->getConnection();
 
$purchase = new Purchase($db);
$data = json_decode(file_get_contents("php://input"));
$purchase->df = isset($_GET['df']) ? $_GET['df'] : null;
$purchase->dt = isset($_GET['dt']) ? $_GET['dt'] : null;

$stmt = $purchase->search();
$num = $stmt->rowCount();

$arr=array();
$arr["records"]=array();
$total_amount = 0;
if($num>0){
    while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){
        extract($row);
        $purchase=array(
            $id,
			$date,
            $voucher_no,
			$supplier,
            number_format($amount)
        );
        $total_amount +=$amount;
        array_push($arr["records"], $purchase);
    }
    $arr["total_amount"] = number_format($total_amount);
}
echo json_encode($arr);
?>