<?php 
require "../commonincludefiles.php";
	 
 if($_SESSION["cCompany_id"]!="" && isset($_SESSION["cCompany_id"])){
      $company_id	=	$_SESSION["cCompany_id"];
	  
	  $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)) {
    	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,"voucher_check")=="0"){
							$fi = explode(',',$value);
							for ($q=0;$q < count($fi);$q++){
								$fi[$q] = "'".$fi[$q]."'";
							}
							$value = implode(',',$fi);
							$field = "voucher_check";
							$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,"voucher_check")=="0")
						{
							
							 $field = "voucher_check";
							 $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;
 }
	  
	   
	  $objVoucherMasterMgmt = new VoucherMasterMgmt();
	

	 if ($sort != "") 
		{
		 $order_field = $sort;
		 $order_type  = $dir;		
		}
      $rs = $objVoucherMasterMgmt->getAllRecords($limit,$start,$condition,"","");
      $total = $objVoucherMasterMgmt->getcountRecords(0,0,$condition,"","");
	
	 
	  $i = 0;   
	
	  $VoucherMasterData = array("count" => $total, "certificates" => array());

       while($row = dbObject($rs)) {
		 $VoucherMasterData["certificates"][$i] = $row;   
         $i++;
      }

	// return response to client
	echo json_encode($VoucherMasterData);
	}
?>