<?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/item_sales.php';

$database = new Database();
$db = $database->getConnection();
 
$item_sales = new ItemSales($db);
$data = json_decode(file_get_contents("php://input"));
$item_sales->sales_date = $data->sales_date;
$stmt = $item_sales->getAllRows();

$num = $stmt->rowCount();
$arr=array();
$arr["records"]=array();

if($num>0){
    while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){
        extract($row);
        $d=array(
            "id" => (int)$id,
            "sales_date" => $sales_date,
            "store" => $store,
            "buyer" => $buyer,
            "item_code" => $item_code,
            "item_name" => $item_name,
            "quantity" => number_format($quantity),
            "price" => number_format($price),
            "amount" => number_format($quantity*$price)
        );
        $total += $quantity*$price ;
        array_push($arr["records"], $d);
    }   
    $arr["total"] = number_format($total);
}
echo json_encode($arr);
?>