<?php
require_once("config/configDB.php"); 
require "userPermission.php";

// storing  request (ie, get/post) global array to a variable  
$requestData= $_REQUEST;

//productcode, productname, purchase_rate, gst, hsn_code,prodct_description, create_date
$columns = array( 
// datatable column index  => database column name
	0 => 'productID', 
	1 => 'cname',
	2 => 'productcode',
    3 => 'productname',
	4 => 'prate',
	5 => 'srate'
);

$sql = "SELECT pmc.productID as productID, cm.cname as cname, pmc.productcode as productcode, pmc.productname as productname, pmc.purchase_rate as prate, pmc.sale_rate as srate FROM product_mapping_customer as pmc  join customer_master as cm  on cm.id=pmc.customerID  where pmc.sale_rate>0";
$query=mysqli_query($conn, $sql) or die("get product_master Data");
$totalData = mysqli_num_rows($query);
$totalFiltered = $totalData;  

$sql = "SELECT pmc.productID as productID, cm.cname as cname, pmc.productcode as productcode, pmc.productname as productname, pmc.purchase_rate as prate, pmc.sale_rate as srate FROM product_mapping_customer as pmc  join customer_master as cm  on cm.id=pmc.customerID  where pmc.sale_rate>0";
if( !empty($requestData['search']['value']) ) {   

	$sql.=" AND (productID LIKE '".$requestData['search']['value']."%' ";    
	$sql.=" OR cname LIKE '".$requestData['search']['value']."%' ";
   $sql.=" OR productcode LIKE '".$requestData['search']['value']."%' ";
    $sql.=" OR productname LIKE '".$requestData['search']['value']."%') ";
   // $sql.=" OR prate LIKE '".$requestData['search']['value']."%' ";
//	$sql.=" OR srate LIKE '".$requestData['search']['value']."%') ";
    //$sql.=" OR id LIKE '".$requestData['search']['value']."%' )";
}
$query=mysqli_query($conn, $sql) or die("customergriddata.php: get product_master Data");
$totalFiltered = mysqli_num_rows($query); 

$sql.=" ORDER BY ". $columns[$requestData['order'][0]['column']]."   ".$requestData['order'][0]['dir']."  LIMIT ".$requestData['start']." ,".$requestData['length']."   ";
//print_r($sql);die;
$query=mysqli_query($conn, $sql) or die("customergriddata.php: get product_master Data");
	
//0 => 'productID','cname','pcode','pname', 'prate','srate',
$data = array();
while( $row=mysqli_fetch_array($query) ) { 
	$nestedData=array(); 

	$nestedData[] = $row["productID"];
    //$nestedData[] = $row["collectiondate"];
   
	$nestedData[] = $row["cname"];
    $nestedData[] = $row["productcode"];
    $nestedData[] = $row["productname"];
    $nestedData[] = $row["prate"];
	
	//$date = new DateTime($row["employee_joining_date"]); 
	//$nestedData[] = $date->format('d-m-Y'); //only fetching date
    $nestedData[] = $row["srate"];
    //$nestedData[] = "";
	
	$data[] = $nestedData;
}

$json_data = array(
			"draw"            => intval( $requestData['draw'] ),   
			"recordsTotal"    => intval( $totalData ),  
			"recordsFiltered" => intval( $totalFiltered ), 
			"data"            => $data   
			);

echo json_encode($json_data);  

?>
