<?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/expense.php';
session_start();
$database = new Database();
$db = $database->getConnection();
 
$expense = new Expense($db);
$data = json_decode(file_get_contents("php://input"));

$expense->city = $data->city;
$expense->date = $data->date;
$expense->accounting_view = isset($_SESSION['accounting_view'])?$_SESSION['accounting_view']:"";

$stmt = $expense->getAllRows();
$num = $stmt->rowCount();

$arr=array();
$arr["records"]=array();
$net_amount=0;
if($num>0){
    while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){
        extract($row);
        $detail = array(
			"id" => $id,
			"city" => $city,
            "voucher_no" => $voucher_no,
			"expense_reason" => $expense_reason,
            "particular" => $particular,
            "exp_for" => isset( $exp_for)?$exp_for:"",
	        "expense_by" => $expense_by,
	        "date" => $date,
	        "amount" => number_format($amount),
            "quantity" => number_format($quantity),
            "total_amount" => number_format($quantity*$amount)
        );
        $net_amount += $quantity*$amount;

        array_push($arr["records"], $detail);
    }
    $arr["net_amount"] = number_format($net_amount);

}
echo json_encode($arr);
?>