<?php
/**
 * This is general file which contains
 * connection file, common function file, etc.,
 *
 * PHP version 5
 *
 * @category Admin_Tool
 * @package  Admin_Tool
 * @author   Aum Infosys <admin@auminfosys.com>
 * @license  ACE
 * @version  CVS:1.0.1
 * @link     http://auminfosys.com/
 * @filesource
 */
 
/**
 * This is general file which contains
 * connection file, common function file, etc.,
 *
 */
 require "../commonincludefiles.php";

 $school_id	=	$_SESSION["aCollegeId"];
 $year_id	=	$_SESSION["aYear"];

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

	// GridFilters sends filters as an Array if not json encoded
	if (is_array($filters)) {
    	$encoded = false;
	} else {
    	$encoded = true;
    	$filters = json_decode($filters);
	}

	// initialize variables
	$where = "";
	//$where .= " and sec.school_id = '".$school_id ."'";
	$qs = '';

	if (is_array($filters)) {
    	for ($i=0;$i<count($filters);$i++){
        	$filter = $filters[$i];
			// assign filter data (location depends if encoded or not)
        	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;
			case 'list' :
                if (strstr($value,',')){
                       if(strcmp($field,"member_name")==0){
							$fi = explode(',',$value);
							for ($q=0;$q < count($fi);$q++){
								$fi[$q] = "'".$fi[$q]."'";
							}
							$value = implode(',',$fi);
							$field = "member_type";
							$qs .= " AND ".$field." IN (".$value.")";
						}
				}
				else
				{
						if(strcmp($field,"member_name")==0)
						{
							
							 $field = "member_type";
							 $qs .= " AND ".$field." = '".$value."'";
						 }
						
						 else
						 {
		                     $qs .= " AND ".$field." = '".$value."'";
						 }
                }	
				break;
            case 'numeric' :
                switch ($compare) {
                    case 'eq' : $qs .= " AND ".$field." = ".$value; break;
                    case 'lt' : $qs .= " AND ".$field." < ".$value; break;
                    case 'gt' : $qs .= " AND ".$field." > ".$value; break;
                }
            break;
            case 'date' :
                switch ($compare) {
                    case 'eq' : $qs .= " AND ".$field." = '".date('Y-m-d',strtotime($value))."'"; break;
                    case 'lt' : $qs .= " AND ".$field." < '".date('Y-m-d',strtotime($value))."'"; break;
                    case 'gt' : $qs .= " AND ".$field." > '".date('Y-m-d',strtotime($value))."'"; break;
                }
            break;
        }
    }
    $where .= $qs;
}

$sortorder="";
				  

if($_SESSION["aCollegeId"]!=""){
	
	
	$objIssueDetailMgmt = new IssueDetailMgmt();
	
	$where .= " AND issue_isreturned = '1' AND issue_islost = '0'";
	$arr = array();
	$k=0;
	
	$rs = $objIssueDetailMgmt->isReturn($limit,$start,$where,$sort,$dir);
	$total = dbRows($rs);
	
	while($obj = dbObject($rs)) {
	
		$arr[$k] = $obj;  
	
	   $k++;
	 }
 }
// return response to client
echo '{"total":"'.$total.'","data":'.json_encode($arr).'}';

?>