<?php
require_once("config/configDB.php"); 
$username=$_SESSION['username'];
// storing  request (ie, get/post) global array to a variable  
$requestData= $_REQUEST;

//saleheader (invoice_no, sale_date, customer_id, dc_num, po_num,saleGSTNO, itemcount, tamount, create_date, flag)
$userpermissionEdit 	= $_POST['userpermissionEdit'];
$userpermissionDelete 	= $_POST['userpermissionDelete'];


$columns = array( 
// datatable column index  => database column name
	0 => 'id',
	1 => 'invoice_no', 
	2 => 'sale_date'
	
	
    
);

$sql = "SELECT id FROM product_rate_hdr";
$query=mysqli_query($conn, $sql) or die("Error logged for sale Dashboard: Step 1");
$totalData = mysqli_num_rows($query);
$totalFiltered = $totalData;  


$sql = "SELECT sh.invoice_no, sh.create_date,sh.id from product_rate_hdr as sh WHERE 1=1 and sh.status='' group by sh.invoice_no, sh.create_date ";

if( !empty($requestData['search']['value']) ) {   
	$sql.=" AND (sh.id LIKE '".$requestData['search']['value']."%' ";
	$sql.=" OR  sh.invoice_no LIKE '".$requestData['search']['value']."%' ";    
	
	
    $sql.=" OR sh.create_date LIKE '".$requestData['search']['value']."%' )";
	
    
}
$query=mysqli_query($conn, $sql) or die("Error logged for sale Dashboard: Step 2".$sql);
$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("Error logged for sale Dashboard: Step 3");


//(invoice_no, sale_date, customer_id, dc_num, po_num,saleGSTNO, itemcount, tamount, create_date, flag)
$data = array();
while( $row=mysqli_fetch_array($query) ) { 
	$nestedData=array(); 

	$nestedData[] = $row["invoice_no"];
    //$nestedData[] = $row["collectiondate"];
   
	$date = new DateTime($row["create_date"]); 
    $nestedData[] = $date->format('d-m-Y'); //only fetching date
   
    
    
	//$nestedData[] = "<a href='productMasterEdit.php?id=".$row['id']."' id='productMaster_".$row['id']."' class='btn btn-primary'>Edit</a>";
	$edit  = "";
	if($userpermissionEdit==1) {
		$edit .="<a href='product_rate_Edit.php?id=".$row['id']."' class='btn btn-primary'>Edit</a>";		
	}

	$Delete  = "";
	if($userpermissionDelete==1) {
		//$Delete .="<a  id='' class='btn btn-primary'>Delete</a>";
		$Delete .= '';
	}

	


	
	$nestedData[] = $edit."&nbsp".$Delete;

    $data[] = $nestedData;
}

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

echo json_encode($json_data);  

?>
