<?php 
include("../config.php");
$sd = mysqli_real_escape_string($conn, date('Y-m-d', strtotime($_GET['start_date'])));
$ed = mysqli_real_escape_string($conn, date('Y-m-d', strtotime($_GET['end_date'])));
$select  = "SELECT * FROM `users` WHERE `status` ='0'";
$result  = mysqli_query($conn,$select);
$total   = mysqli_num_rows($result);

$table       	= '';
$total_count    = 0;
if(count($total) > 0) {
	$total_fright = 0;
	while ($row = mysqli_fetch_array($result)) {
		if(!empty($sd) && !empty($ed)){
			
			$data = "SELECT (SELECT count(*) FROM vendor_master WHERE date(`created_datetime`) >= '".$sd."' AND date(`created_datetime`) <= '".$ed."' AND created_id = '".$row["id"]."') as vdata,
  				(SELECT count(*) FROM account_group WHERE date(`created_datetime`) >= '".$sd."' AND date(`created_datetime`) <= '".$ed."' AND created_id = '".$row["id"]."') as ag_data,
  				(SELECT count(*) FROM act_master WHERE date(`created_datetime`) >= '".$sd."' AND date(`created_datetime`) <= '".$ed."' AND created_id = '".$row["id"]."') as am_data,
  				(SELECT count(*) FROM truck_mst WHERE date(`created_datetime`) >= '".$sd."' AND date(`created_datetime`) <= '".$ed."' AND created_id = '".$row["id"]."') as truck_mst_data,
  				(SELECT count(*) FROM driver_mst WHERE date(`created_datetime`) >= '".$sd."' AND date(`created_datetime`) <= '".$ed."' AND created_id = '".$row["id"]."') as driver_data,
  				(SELECT count(*) FROM company_mst WHERE date(`created_datetime`) >= '".$sd."' AND date(`created_datetime`) <= '".$ed."' AND created_id = '".$row["id"]."') as company_data,
  				(SELECT count(*) FROM branch_mst WHERE date(`created_datetime`) >= '".$sd."' AND date(`created_datetime`) <= '".$ed."' AND created_id = '".$row["id"]."') as branch_data,
  				(SELECT count(*) FROM document_mst WHERE date(`created_datetime`) >= '".$sd."' AND date(`created_datetime`) <= '".$ed."' AND created_id = '".$row["id"]."') as document_data,
  				(SELECT count(*) FROM emp_mst WHERE date(`created_datetime`) >= '".$sd."' AND date(`created_datetime`) <= '".$ed."' AND created_id = '".$row["id"]."') as emp_data
  				";

  			$data_res = mysqli_query($conn,$data);
  			$data_row = mysqli_fetch_assoc($data_res);

  			// UPDATE
  			$datas = "SELECT (SELECT count(*) FROM vendor_master WHERE date(`updated_datetime`) >= '".$sd."' AND date(`updated_datetime`) <= '".$ed."' AND updated_id = '".$row["id"]."') as vdata,

			(SELECT count(*) FROM account_group WHERE date(`updated_datetime`) >= '".$sd."' AND date(`updated_datetime`) <= '".$ed."' AND updated_id = '".$row["id"]."') as ag_data,
			(SELECT count(*) FROM act_master WHERE date(`updated_datetime`) >= '".$sd."' AND date(`updated_datetime`) <= '".$ed."' AND updated_id = '".$row["id"]."') as am_data,
			(SELECT count(*) FROM truck_mst WHERE date(`updated_datetime`) >= '".$sd."' AND date(`updated_datetime`) <= '".$ed."' AND updated_id = '".$row["id"]."') as truck_mst_data,
			(SELECT count(*) FROM driver_mst WHERE date(`updated_datetime`) >= '".$sd."' AND date(`updated_datetime`) <= '".$ed."' AND updated_id = '".$row["id"]."') as driver_data,
			(SELECT count(*) FROM company_mst WHERE date(`updated_datetime`) >= '".$sd."' AND date(`updated_datetime`) <= '".$ed."' AND updated_id = '".$row["id"]."') as company_data,
			(SELECT count(*) FROM branch_mst WHERE date(`updated_datetime`) >= '".$sd."' AND date(`updated_datetime`) <= '".$ed."' AND updated_id = '".$row["id"]."') as branch_data,
			(SELECT count(*) FROM document_mst WHERE date(`updated_datetime`) >= '".$sd."' AND date(`updated_datetime`) <= '".$ed."' AND updated_id = '".$row["id"]."') as document_data,
			(SELECT count(*) FROM emp_mst WHERE date(`updated_datetime`) >= '".$sd."' AND date(`updated_datetime`) <= '".$ed."' AND updated_id = '".$row["id"]."') as emp_data
			";

			$datas_res = mysqli_query($conn,$datas);
			$datas_row = mysqli_fetch_assoc($datas_res);
		}
		$table .= '<tr class="text-center">';
		$table .= '<td>'.$row["name"].'</td>';
		$table .= '<td>'.$data_row['vdata'].'</td>';
		$table .= '<td>'.$datas_row['vdata'].'</td>';
		$table .= '<td>'.$data_row['ag_data'].'</td>';
		$table .= '<td>'.$datas_row['ag_data'].'</td>';
		$table .= '<td>'.$data_row['am_data'].'</td>';
		$table .= '<td>'.$datas_row['am_data'].'</td>';
		$table .= '<td>'.$data_row['truck_mst_data'].'</td>';
		$table .= '<td>'.$datas_row['truck_mst_data'].'</td>';
		$table .= '<td>'.$data_row['driver_data'].'</td>';
		$table .= '<td>'.$datas_row['driver_data'].'</td>';
		$table .= '<td>'.$data_row['company_data'].'</td>';
		$table .= '<td>'.$datas_row['company_data'].'</td>';
		$table .= '<td>'.$data_row['branch_data'].'</td>';
		$table .= '<td>'.$datas_row['branch_data'].'</td>';
		$table .= '<td>'.$data_row['document_data'].'</td>';
		$table .= '<td>'.$datas_row['document_data'].'</td>';
		$table .= '<td>'.$data_row['emp_data'].'</td>';
		$table .= '<td>'.$datas_row['emp_data'].'</td>';
		$table .= '</tr>';
	}
	
}
$data = array('status' => 'success', 'total_count' => $total, 'tbody' => $table);
	echo json_encode($data);
    die();
?>