<?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->df = $data->df;
$item_sales->dt = $data->dt;
$item_sales->voucher_no = $data->voucher_no;
$item_sales->city = $data->city;
$item_sales->name = $data->name;

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

$arr=array();
$arr["records"]=array();
$total_amount_T=$discount_T=$payment_T=$balance_T=0;
if($num>0){
    while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){
        extract($row);
        $pu=array(
            "id" => (int)$id,
            "sales_date" => $sales_date,
            "voucher_no" => $voucher_no,
            "city" => $store,
            "name" => $name,
            "total_amount" => number_format($total_amount),
            "discount" => number_format($discount),
            "payment" => number_format($payment),
            "balance" => number_format($total_amount-$discount-$payment)
        );
        
        $total_amount_T +=$total_amount;
        $payment_T +=$payment;
        $discount_T +=$discount;
        $balance_T +=$total_amount-$discount-$payment;
    array_push($arr["records"], $pu);
    }
    $arr["total_amount_T"] = number_format($total_amount_T);
    $arr["discount_T"] = number_format($discount_T);
    $arr["payment_T"] = number_format($payment_T);
    $arr["balance_T"] = number_format($balance_T);
}
    echo json_encode($arr);
?>