<?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.,
 *
 */
 

  //  set_time_limit(0);

  require "../commonincludefiles.php";
	$arr = array();
	$total = 0;
 	$product_id=	$_SESSION["cCompany_id"];

	
		$start   = isset($_REQUEST['start'])  ? $_REQUEST['start']  :  0;
		$limit   = isset($_REQUEST['limit'])  ? $_REQUEST['limit']  : 0;
		$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);
		}

		$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 'boolean' : $qs .= " AND ".$field." = ".($value); break;
				case 'list' :
				
					if (strstr($value,',')){
						   if(strcmp($field,"customer_type")==0){
								$fi = explode(',',$value);
								for ($q=0;$q < count($fi);$q++){
									$fi[$q] = "'".$fi[$q]."'";
								}
								$value = implode(',',$fi);
								$field = "customer_type";
								$qs .= " AND ".$field." IN (".$value.")";
								
						
						   }
						   else{
								$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,"customer_type")==0)
							{
								
								 $field = "customer_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 ($sort != ""){
			$order_field = $sort;
			$order_type  = $dir;		 		  
		}
	$k=0;
	
	
	$objCustomerMgmt = new CustomerMgmt();	
	
	$customer_id		= Escape($_REQUEST['customer_id']);
	
	if($customer_id !="")
	{
		$where.=" And lc.customer_id='".$customer_id."'";	
		$condition.=" And customer_id='".$customer_id."'";
	}
	
	$where.=" And lc.company_id= '".$_SESSION["cCompany_id"]."'";
	$condition.=" And company_id= '".$_SESSION["cCompany_id"]."'";
	
	if($order_field!="")
	{
		$order_field="lc.".$order_field;
	}	
		
	$rs = $objCustomerMgmt->getAllRecords($limit,$start,$where,$order_field,$order_type);
	
	$total = $objCustomerMgmt->getCountRecords(0,0,$condition,"","");
	$AccountMasterMgmt = new AccountMasterMgmt();	

	$i = 0;   
	if($total > 0) {
		$temparray = array();
	 	while($row = dbArray($rs)){
			
				$temparray["customer_id"]   	  	 = $row["customer_id"];
				$temparray["customer_orgname"]		 = $row["customer_orgname"];
				$temparray["customer_address"]   	 = $row["customer_address"];
				$temparray["district_id"] 			 = $row["district_id"];
				$temparray["customer_phone1"] 		 = $row["customer_phone1"];
				$temparray["customer_phone2"]    	 = $row["customer_phone2"];
				$temparray["customer_email"]    	 = $row["customer_email"];
				$temparray["customer_balance"] 		 = $row["customer_balance"];
				$temparray["customer_baltype"] 		 = $row["customer_baltype"];
				$temparray["customer_website"] 		 = $row["customer_website"];
				$temparray["customer_cpname1"] 	 	 = $row["customer_cpname1"];
				$temparray["customer_phone2"]    	 = $row["customer_phone2"];
				$temparray["customer_cpdesignation1"]     = $row["customer_cpdesignation1"];
				$temparray["customer_cpmobile1"] 		 = $row["customer_cpmobile1"];
				$temparray["customer_cpemail1"] 	 = $row["customer_cpemail1"];
				$temparray["customer_tinno"]     = $row["customer_tinno"];
				$temparray["customer_cstno"]     = $row["customer_cstno"];
				$temparray["customer_servicetaxno"] 		 = $row["customer_servicetaxno"];
				$temparray["customer_panno"] 	 = $row["customer_panno"];
				$temparray["customer_remark"] 	 = $row["customer_remark"];
				$temparray["account_id"]     = $AccountMasterMgmt->getIdByName($row["customer_orgname"]);
				$griddata[$i] = $temparray;   
				$i++;
			}		
	}
		echo '{"total":"'.$total.'","data":'.json_encode($griddata).'}';
?>