<?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 => 'sname',
	2 => 'productcode',
    3 => 'productname',
	4 => 'prate',
	5 => 'srate'
);

$sql = "SELECT pmc.productID as productID, cm.sname as sname, pmc.productcode as productcode, pmc.productname as productname, pmc.purchase_rate as prate, pmc.sale_rate as srate FROM product_mapping_supplier as pmc  join supplier_master as cm  on cm.id=pmc.supplierID  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.sname as sname, pmc.productcode as productcode, pmc.productname as productname, pmc.purchase_rate as prate, pmc.sale_rate as srate FROM product_mapping_supplier as pmc  join supplier_master as cm  on cm.id=pmc.supplierID  where pmc.sale_rate>0";
if( !empty($requestData['search']['value']) ) {   

	$sql.=" AND (productID LIKE '".$requestData['search']['value']."%' ";    
	$sql.=" OR sname 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']."   ";

$query=mysqli_query($conn, $sql) or die("customergriddata.php: get product_master Data");
	
//0 => 'productID','sname','pcode','pname', 'prate','srate',
$data = array();
while( $row=mysqli_fetch_array($query) ) { 
	$nestedData=array(); 

	$nestedData[] = $row["productID"];
    //$nestedData[] = $row["collectiondate"];
   
	$nestedData[] = $row["sname"];
    $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);  

?>
