<?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";
	$arr = array();
	$total = 0;
 	$product_id=	$_SESSION["cCompany_id"];
	$k=0;

$customer_id = Escape($_POST['customer_id']);
if($customer_id !="") {
	
	$condition .= " AND customer_id='".$customer_id."'"; 
  
    $objCustmerProductMgmt = new CustmerProductMgmt();  

	$result = $objCustmerProductMgmt->getAllRecords(0,0,$condition,$order_field,$order_type) ;
	$num = dbRows($result);
	
	$i = 0;   
	while($mainrow = dbArray($result)){
		$product_ids[$i] = $mainrow['product_id']; 
		$purchase_date[$i]= $mainrow['purchase_date'];
		$i++;
		  
	}
	 //$product_ids =explode(',',$product_ids);
	 
	 $objProductMgmt = new ProductMgmt();	
	 	
	 $where.=" And company_id= '".$_SESSION["cCompany_id"]."'";	
		
	 $rs = $objProductMgmt->getAllRecords($limit,$start,$where,$order_field,$order_type);
	 $total = $objProductMgmt->getCountRecords(0,0,$where,"","");
	// $arr = array("total" => $total, "data" => array());
	$i=0;
	 if	(dbRows($rs) > 0) 
	 {
				$temparray = array();
				while($row = dbArray($rs)){
					
					$temparray["product_id"]  = $row["product_id"];
					$temparray["product_name"]   = $row["product_name"];
					$temparray["product_price"]  = $row["product_price"];
					$temparray["product_active"]   = $row["product_active"];
					$temparray["product_description"]   = $row["product_description"];
					if(in_array($row["product_id"],$product_ids))
					{
						$temparray["selected"]="true";
						$temparray["purchasedate"]=$purchase_date[$i];
					}
					else
					{
						$temparray["selected"]="false";
						$temparray["purchasedate"]="";
					}
					$arr[$i] = $temparray;   
					$i++;
			}		
			echo '{"total":"'.$total.'","data":'.json_encode($arr).'}';				
		}
		
		 	 	 	 	
}
else
{
	$objProductMgmt = new ProductMgmt();		
		
		$where.=" And company_id= '".$_SESSION["cCompany_id"]."'";	
	$rs = $objProductMgmt->getAllRecords($limit,$start,$where,$order_field,$order_type);
	
	$total = $objProductMgmt->getCountRecords(0,0,$where,"","");;
	
	if($total > 0) {
		while($obj = dbObject($rs)) {
		 $arr[$k] = $obj;  
		 $k++;
		}
	}
	
echo '{"total":"'.$total.'","data":'.json_encode($arr).'}';
	
}	
?>