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

$database = new Database();
$db = $database->getConnection();
 
$sales_detail = new salesDetail($db);

$data = json_decode(file_get_contents("php://input"));

$sales_detail->df = $data->df;
$sales_detail->dt = $data->dt;
$sales_detail->category = $data->category;
$sales_detail->brand = $data->brand;
$sales_detail->item_code = $data->item_code;
$sales_detail->item_name = $data->item_name;

$stmt = $sales_detail->searchItemsales();
$num = $stmt->rowCount();

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

$total_quantity = $total_amount = $total_payment = 0;

if($num>0){

    while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){
        extract($row);
        $item=array(
            "id" => (int) $id,
            "date" => $sales_date,
            "category" => ($category)?$category:"",
            "brand" => ($brand)?$brand:"",
			"item_code" => ($item_code)?$item_code:"",
			"item_name" => ($item_name)?$item_name:"",
            "quantity" => number_format($quantity),
            "price" => number_format($price),
            "amount" => number_format($quantity * $price),
            "voucher_no" => ($voucher_no)?$voucher_no:"",
            "car_no" => ($car_no)?$car_no:"",
            "payment" => number_format($payment),
        );
        $total_quantity += $quantity;
        $total_amount += $quantity * $price;
        $total_payment += $payment;
        array_push($arr["records"], $item);
    }
    $arr["total_quantity"] = number_format($total_quantity);
    $arr["total_amount"] = number_format($total_amount);
    $arr["total_payment"] = number_format($total_payment);
}
echo json_encode($arr);
?>