<?php
 require "../commonincludefiles.php";
 
 $company_id	=	$_SESSION["cCompany_id"];
 $year_id	=	$_SESSION["cYear"];

	// collect request parameters
	$start   = isset($_REQUEST['start'])  ? $_REQUEST['start']  :  0;
	$limit   = isset($_REQUEST['limit'])  ? $_REQUEST['limit']  : 30000;
	$sort    = isset($_REQUEST['sort'])   ? $_REQUEST['sort']   : '';
	$dir     = isset($_REQUEST['dir'])    ? $_REQUEST['dir']    : 'ASC';
	$filters = isset($_REQUEST['filter']) ? $_REQUEST['filter'] : null;

// initialize variables
$where = '';
$qs = '';

if(is_array($filters)){
	$encoded = false;
} else{
	$encoded = true;
	$filters = json_decode($filters);
}

if(is_array($filters)){
	for ($i=0;$i<count($filters);$i++){
    	$filter = $filters[$i];
		if($encoded){
			$field = $filter->field;
			$value = $filter->value;
			$compare = isset($filter->comparison) ? $filter->comparison : null;
			$filterType = $filter->type;
		} else{
			$field = $filter['field'];
			$value = $filter['data']['value'];
			$compare = isset($filter['data']['comparison']) ? $filter['data']['comparison'] : null;
			$filterType = $filter['data']['type'];
		}
		switch($filterType){
			case 'string' : $qs .= " AND ".$field." LIKE '%".$value."%'"; break;
		}
	}
    $where .= $qs; 
}

if($_SESSION["cCompany_id"]!="" && isset($_SESSION["cCompany_id"])){

	$objUserMgmt = new UserMgmt();
	
	if($_SESSION["cUserRole"]=="admin")
	{
	  $where.=" And lu.company_id= '".$_SESSION["cCompany_id"]."'";
	}
	
	
	$arr = array();
	$k=0;
	$order_field = $sort;
	$order_type = $dir;
	
	$rs = $objUserMgmt->getAllRecords($limit,$start,$where,$order_field,$order_type);
	
	$total = dbRows($rs);
			
	while($obj = dbObject($rs)) {
	
		$arr[$k] = $obj;   
		$k++;
	}	
	// return response to client
	echo '{"total":"'.$total.'","data":'.json_encode($arr).'}';
}
?>