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



//a.id,a.scode,a.sname,a.mobile,a.invoicedate,a.pamount,a.category

	$columns = array( 
		// datatable column index  => database column name
			0 => 'invoice',
			1 => 'invoicedate',
			2 => 'cname',
			3 => 'amount',
			4 => 'pay_type',
			5 => 'id'
			
		);

		$sql = "SELECT sup_p.cashno as invoice,sup_p.sales_inv, sup_p.collectiondate as invoicedate,sup_p.customerID as customerid, sup_p.received_amount as amount,sup_p.discount as discount, sup_p.paymenttype as pay_type,sup_p.paymentreference,sup_p.id,sm.cname,sup_p.paymenttype,sm.area_tamil FROM customer_payment_header  as sup_p  
		LEFT JOIN  customer_master as sm ON sm.id=sup_p.customerid  WHERE  sup_p.status=''";

	$query=mysqli_query($conn, $sql) or die("brandgriddata.php: get brand Data step 1");
	$totalData = mysqli_num_rows($query);
	$totalFiltered = $totalData;
	
	if( !empty($requestData['search']['value']) ) {   
		$sql.=" AND (  cashno LIKE '".$requestData['search']['value']."%' ";
		$sql.=" OR collectiondate LIKE '".$requestData['search']['value']."%' ";
		$sql.=" OR cname LIKE '".$requestData['search']['value']."%' ";
		$sql.=" OR received_amount LIKE '".$requestData['search']['value']."%' ";
		$sql.=" OR paymenttype LIKE '".$requestData['search']['value']."%' ";
		$sql.=" OR paymentreference LIKE '".$requestData['search']['value']."%' ";

		$sql.=" OR sup_p.id LIKE '".$requestData['search']['value']."%' )";
	}

	

$query=mysqli_query($conn, $sql) or die("brandgriddata.php: get brand Data 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("brandgriddata.php: get brand Data step 3".$sql);
	

$data = array();
while( $row=mysqli_fetch_array($query) ) { 
	$nestedData=array(); 

	$edit  = "";

	$nestedData[] = $row["invoice"];
	$nestedData[] = $row["invoicedate"];	
	$nestedData[] = $row["cname"]."(".$row["area_tamil"].")";
	$nestedData[] = $row["amount"];
	$nestedData[] = $row["discount"];
	$nestedData[] = $row["pay_type"];
	$nestedData[] = $row["paymenttype"];


	if($userpermissionEdit==1) {
		$edit .="<a href='paydiesel_salesEdit.php?spid=".$row['id']."'  id='accOver_".$row['id']."' class='btn btn-primary'>Edit</a>&nbsp";
	}	
	

	$Delete  = "";
	if($userpermissionDelete==1) {
		//$Delete .="<a id='accOverDelete_".$row['id']."' class='btn btn-primary' onclick='ConfirmaccOverDelete(".$row['id'].")' >Delete</a>";
	}
	
	
	$sinv=$row['sales_inv'];
	
	if($sinv!='')
	{
	
	$Print='';
	}
	else
	{
	
	$Print ="<a href='collectioninvoiceest.php?saleID=".$row['id']."' target='_blank' class='btn btn-primary'>Print</a>";
	}
	
	$nestedData[] = $edit."&nbsp".$Print;
	
	$data[] = $nestedData;
}

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

echo json_encode($json_data);  

?>
