<?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";
$schoolid 	= $_SESSION['aCollegeId'];


$data = "";

$condition = '';
$qs = '';
$order_field = "";
$order_type  = "";

$start   = isset($_REQUEST['start'])  ? $_REQUEST['start']  :  0;
$limit   = isset($_REQUEST['limit'])  ? $_REQUEST['limit']  : 30;
$sort    = isset($_REQUEST['sort'])   ? $_REQUEST['sort']   : '';
$dir     = isset($_REQUEST['dir'])    ? $_REQUEST['dir']    : 'ASC';
$filters = isset($_REQUEST['filter']) ? $_REQUEST['filter'] : null;
							
if(is_array($filters)){
	$encoded = false;
} else{
	$encoded = true;
    $filters = json_decode($filters);
}

//$condition .= " and lbm.school_id = '".$schoolid ."'";
$qs = '';

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;
            	case 'list' :
					if (strstr($value,',')){
                    	if(strcmp($field,"book_available")=="Yes"){
							$fi = explode(',',$value);
							for ($q=0;$q < count($fi);$q++){
								$fi[$q] = "'".$fi[$q]."'";
							}
							$value = implode(',',$fi);
							$field = "book_available";
							$qs .= " AND ".$field." IN (".$value.")";
						}
						if (strstr($value,',')){
                    		$fi = explode(',',$value);
                    		for ($q=0;$q<count($fi);$q++){
                       			$fi[$q] = "'".$fi[$q]."'";
                   			}
                   			$value = implode(',',$fi);
                   			$qs .= " AND ".$field." IN (".$value.")";
						}
                  	} else{
						if(strcmp($field,"book_available")=="Yes"){
							$field = "book_available";
							$qs .= " AND ".$field." = '".$value."'";
						} else{
		                 	$qs .= " AND ".$field." = '".$value."'";
						}
                	}	
            		break;
            	case 'boolean' : $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;
		}
	}
    $condition .= $qs; 
}
if ($sort != ""){
	$order_field = "book_id";
	$order_type  = $dir;		
} else{
  	$order_field = $sort;
	$order_type  = $dir;
}
if($_SESSION["aCollegeId"]!="" && isset($_SESSION["aCollegeId"])){
	$objBookMgmt = new BookMgmt();
	
	$schoolid 	= $_SESSION['aCollegeId'];
	
	//$condition .=" AND lbm.school_id='$schoolid'";
	$condition .=" AND lbm.book_active='1'";
	
	$result = $objBookMgmt->getAllRecords_with_publisher($limit ,$start,$condition,$order_field,$order_type) ;
	$result1 = $objBookMgmt->getAllRecords_with_publisher(0 ,0,$condition,$order_field,$order_type) ;
	
	$total = dbRows($result1);
	$i=0;
	$bookData = array("count" => $total, "rows" => array());
	//for pubisher
	
	
	
	if(dbRows($result) > 0) {
		$temparray = array();
		while($row = dbArray($result)){
			
			$temparray["book_id"]  = $row["book_id"];
			$temparray["book_registerno"]   = $row["book_registerno"];
			
			if($row["book_registerid"]==1)
			{
			   $temparray["book_registerid"]  = "General";
			}else
			{
			   $temparray["book_registerid"]  = "Central Goverment";
			   
			}
			$temparray["book_name"]   = $row["book_name"];
			$temparray["book_language"]   = $row["book_language"];

			$temparray["branch_name"]   = $row["branch_name"];
			$temparray["book_shelf_no"]   = $row["book_shelf_no"];
			//$temparray["publisher_id"]   = $row["publisher_id"];
			
			$temparray["book_category"]   = $row["book_category"];
			$temparray["book_edition"]   = $row["book_edition"];
			$temparray["book_author"]   = $row["book_author"];
			
				
			$temparray["publisher_name"]   = $row["publisher_name"];
		
			$temparray["book_vender"]   = $row["book_vender"];
			$temparray["book_divno"]   = $row["book_divno"];
			$temparray["book_price"]   = $row["book_price"];
			$temparray["book_rack"]   = $row["book_rack"];
			$temparray["book_gift"]   = $row["book_gift"];
			$temparray["book_purchasedate"]   = $row["book_purchasedate"];
			
			if($row["book_available"]==0)
			{
			   $temparray["book_available"]  = "No";
			}else
			{
			   $temparray["book_available"]  = "Yes";
			   
			}
			 $bookData["rows"][$i] = $temparray;   
			 $i++;
		}										
	}				   		 
	echo json_encode($bookData);	
}	
?>