<?php

require_once __DIR__ . '/db_config.php';
$r = array();

if (isset($_POST["email"]) ) {
 
$user_id=$_POST["email"];
$company=$_POST["cname"];
date_default_timezone_set("Asia/Calcutta");   //India time (GMT+5:30)
$datetime=date('Y-m-d H:i:s');
$date=date('Y-m-d');

//$user_id="7899133721@b2l.in";
//$company="Zkitchen";

$con = mysqli_connect(DB_SERVER, DB_USER, DB_PASSWORD,DB_DATABASE) or die(mysql_error());

$query="SELECT SUM(CASE WHEN (type='credit' AND DATE(datetime)='$date') THEN amount else 0 END) AS today_credit,
SUM(CASE WHEN (type='debit' AND DATE(datetime)='$date') THEN amount else 0 END) AS today_debit,
SUM(CASE WHEN (type='debit') THEN amount else 0 END) AS total_debit,
SUM(CASE WHEN (type='credit') THEN amount else 0 END) AS total_credit
FROM mst_credit_debit_amount
WHERE companyname='$company' AND delete_status='0' AND mst_id='$user_id'";

$result = mysqli_query($con,$query);
if(!empty($result) && mysqli_num_rows($result) > 0){
    while ($row = mysqli_fetch_array($result,MYSQLI_ASSOC)) {
        if($row["today_credit"] == null ) $row["today_credit"] = 0;
        if($row["today_debit"] == null ) $row["today_debit"] = 0;
        if($row["total_debit"] == null ) $row["total_debit"] = 0;
        if($row["total_credit"] == null ) $row["total_credit"] = 0;

        array_push($r, $row);
    }
}
mysqli_close($con);

}

echo json_encode($r,JSON_UNESCAPED_SLASHES);


?>