<?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, expensedate, expensetype, expense, paymenttype, silverAQUA, availableAmount, Amount
$columns = array( 
// datatable column index  => database column name
	0 => 'cashno', 
	1 => 'expensedate',
	2 => 'expensetype',
	3 => 'expense',
    4 => 'amount',
    5 => 'id'
);

$sql = "SELECT id FROM cash_expense_entry";
$query=mysqli_query($conn, $sql) or die("Debugger level 1: ".$sql);
$totalData = mysqli_num_rows($query);
$totalFiltered = $totalData;  

$sql = "SELECT ee.cashno, ee.expensedate,ee.empname, ee.expense_details,ee.expensetype,ee.amount,ee.id,em.expense as expensename FROM cash_expense_entry as ee LEFT JOIN expense_master as em ON em.id=ee.expensetype WHERE 1=1 and ee.flag=1 and ee.status=''";
if( !empty($requestData['search']['value']) ) {   

	$sql.=" AND ( ee.cashno LIKE '".$requestData['search']['value']."%' ";    
	$sql.=" OR ee.expensedate LIKE '".$requestData['search']['value']."%' ";
	$sql.=" OR em.expense LIKE '".$requestData['search']['value']."%' ";
	$sql.=" OR ee.empname LIKE '".$requestData['search']['value']."%' ";
	$sql.=" OR ee.expense_details LIKE '".$requestData['search']['value']."%' ";
    $sql.=" OR ee.amount LIKE '".$requestData['search']['value']."%' ";
    $sql.=" OR ee.id LIKE '".$requestData['search']['value']."%' )";
}
$query=mysqli_query($conn, $sql) or die("Debugger level 1: ".$sql);
$totalFiltered = mysqli_num_rows($query); 

$sql.=" ORDER BY id desc  LIMIT ".$requestData['start']." ,".$requestData['length']."   ";

$query=mysqli_query($conn, $sql) or die("Debugger level 1: ".$sql);
	
//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["expensedate"]); 
	$nestedData[] = $date->format('d-m-Y'); //only fetching date
	$nestedData[] = $row["empname"];
	$nestedData[] = $row["expensename"];
	$nestedData[] = $row["expense_details"];
    
    //$nestedData[] = $row["silverAQUA"];
    //$nestedData[] = $row["availableAmount"];
	
	//$date = new DateTime($row["employee_joining_date"]); 
	//$nestedData[] = $date->format('d-m-Y'); //only fetching date
    $tempvalue = number_format((float)$row["amount"], 2, '.', '');	$nestedData[] = str_replace('.00', '', number_format($tempvalue, 2, '.', ''));
	
	$edit  = "";
	if($userpermissionEdit==1) {
		$edit .="<a href='expenseEntryEdit.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='ConfirmEEDelete(".$row['id'].")' href='fetchPagesData/deleteExpenseEntryGridData.php?eeid=".$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);  

?>
