<?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->df = $data->df;
$expense->dt = $data->dt;
$expense->city = $data->city;
$expense->expense_reason = $data->expense_reason;
$expense->exp_for = isset($data->exp_for)?$data->exp_for:"";
$expense->remark = isset($data->remark)?$data->remark:"";
$expense->accounting_view = isset($_SESSION['accounting_view'])?$_SESSION['accounting_view']:"";
$expense->entry_by = isset($_SESSION['userrole'])?$_SESSION['userrole']:"";

$stmt = $expense->search();
$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,
	        "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);
?>