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

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

$userpermissionEdit 	= $_POST['userpermissionEdit'];
$userpermissionDelete 	= $_POST['userpermissionDelete'];
//cashno,collectiondate,loaner, pending, paymenttype, availableAmount, amount
$columns = array( 
// datatable column index  => database column name
	0 => 'cashno', 
	1 => 'collectiondate',
	2 => 'customerID',
	3 => 'pending',
	4 => 'paymenttype',
    5 => 'amount',
    6 => 'id'
);

$sql = "SELECT id FROM collection_customer where status='' ";
$query=mysqli_query($conn, $sql) or die("Error Logger for BillCollection : Step 1");
$totalData = mysqli_num_rows($query);
$totalFiltered = $totalData;  

$sql = "SELECT cc.cashno,cc.collectiondate,cc.customerID, cc.pending, cc.paymenttype, cc.amount,cc.id,cm.cname ";
$sql.=" FROM collection_customer as cc LEFT JOIN customer_master as cm on cm.id =cc.customerID WHERE 1=1 and cc.status=''";
if( !empty($requestData['search']['value']) ) {   

	$sql.=" AND ( cc.cashno LIKE '".$requestData['search']['value']."%' ";    
	$sql.=" OR cc.collectiondate LIKE '".$requestData['search']['value']."%' ";
	$sql.=" OR cm.cname LIKE '".$requestData['search']['value']."%' ";
	$sql.=" OR cc.pending LIKE '".$requestData['search']['value']."%' ";
	$sql.=" OR cc.paymenttype LIKE '".$requestData['search']['value']."%' ";
	$sql.=" OR cc.amount LIKE '".$requestData['search']['value']."%' ";
    $sql.=" OR cc.id LIKE '".$requestData['search']['value']."%' )";
}
$query=mysqli_query($conn, $sql) or die("Error Logger for BillCollection : Step 2");
$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 Logger for BillCollection : Step 3");
	
//cname, city, pincode, mobile, gst_type, ccode, open_balance
$data = array();
while( $row=mysqli_fetch_array($query) ) { 
	$nestedData=array(); 

	$nestedData[] = $row["cashno"];
    //$nestedData[] = $row["collectiondate"];
    $date = new DateTime($row["collectiondate"]); 
	$nestedData[] = $date->format('d-m-Y'); //only fetching date
	$nestedData[] = $row["cname"];
	$nestedData[] = $row["pending"];
    $nestedData[] = $row["paymenttype"];
    
	
	//$date = new DateTime($row["employee_joining_date"]); 
	//$nestedData[] = $date->format('d-m-Y'); //only fetching date
	$nestedData[] = $row["amount"];
	
	$edit  = "";
	if($userpermissionEdit==1) {
		$edit .="<a href='collectionEdit.php?id=".$row['id']."' id='customerMaster_".$row['id']."' class='btn btn-primary'>Edit</a>";
	}

	$Delete  = "";
	if($userpermissionDelete==1) {
		//$Delete .="<a  id='expenseEntryGridDelete' class='btn btn-primary eeGridDelete' onclick='ConfirmCCDelete(".$row['id'].")' >Delete</a>";
	}
	$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);  

?>
