<?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 => 'supplierID',
	3 => 'pending',
	4 => 'paymenttype',
    5 => 'amount',
    6 => 'id'
);

$sql = "SELECT id FROM supplier_payment_headerwo";
$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.supplierID,  cc.paymenttype,cc.id,cm.sname,cc.received_amount  FROM supplier_payment_header as cc LEFT JOIN supplier_master as cm on cm.id =cc.supplierID WHERE 1=1 and cc.status='' and cc.payment_through='Accounts'";
if( !empty($requestData['search']['value']) ) {   

	$sql.=" AND ( cc.cashno LIKE '".$requestData['search']['value']."%' ";    
	$sql.=" OR cc.collectiondate LIKE '".$requestData['search']['value']."%' ";
	$sql.=" OR cm.sname LIKE '".$requestData['search']['value']."%' ";
	$sql.=" OR cc.pending LIKE '".$requestData['search']['value']."%' ";
	$sql.=" OR cc.paymenttype LIKE '".$requestData['search']['value']."%' ";
	$sql.=" OR cc.received_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.=" group by cashno 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");
	
//sname, 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["sname"];
	//$nestedData[] = $row["pending"];
    $nestedData[] = $row["paymenttype"];
    
	
	//$date = new DateTime($row["employee_joining_date"]); 
	//$nestedData[] = $date->format('d-m-Y'); //only fetching date
	$nestedData[] = $row["received_amount"];
	
	$edit  = "";
	if($userpermissionEdit==1) {
		$edit .="<a href='paypurchaseEdit.php?id=".$row['id']."' id='supplierMaster_".$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);  

?>
