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

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


$userpermissionEdit 	= $_POST['userpermissionEdit'];
$userpermissionDelete 	= $_POST['userpermissionDelete'];
$userpermissionPrint 	= $_POST['userpermissionPrint'];
//cname, city, pincode, mobile, gst_type, ccode, open_balance
$columns = array( 
// datatable column index  => database column name
	0 => 'id', 
	1 => 'employee_name',
	2 => 'given_date',
	3 => 'total_salary',
    4 => 'paidamount',	
	5 => 'paytype',
	6 => 'givenby',
    
);

$sql = "SELECT id FROM employee_payment where isdelete=1 and status=''";
$query=mysqli_query($conn, $sql) or die("productgriddata.php: get product Data step 1");
$totalData = mysqli_num_rows($query);
$totalFiltered = $totalData;  

$sql = "SELECT employee_name, given_date,fromdate,todate, paidamount,ecode, givenby, refno,paytype,id ";
$sql.=" FROM employee_payment WHERE 1=1 and isdelete=1 and status=''";
if( !empty($requestData['search']['value']) ) {   

	$sql.=" AND ( id  LIKE '".$requestData['search']['value']."%' ";    		//$sql.=" OR productname LIKE '".$requestData['search']['value']."%' ";
	$sql.=" OR employee_name LIKE '".$requestData['search']['value']."%' ";
    $sql.=" OR given_date LIKE '".$requestData['search']['value']."%' ";
    $sql.=" OR paidamount LIKE '".$requestData['search']['value']."%' ";
	$sql.=" OR givenby LIKE '".$requestData['search']['value']."%' ";
    $sql.=" OR paytype LIKE '".$requestData['search']['value']."%' ";
	$sql.=" OR refno LIKE '".$requestData['search']['value']."%' )";
}
$query=mysqli_query($conn, $sql) or die("productgriddata.php: get product Data 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("productgriddata.php: get product Data step 3");
	
//cname, city, pincode, mobile, gst_type, ccode, open_balance
$data = array();
while( $row=mysqli_fetch_array($query) ) { 
	$nestedData=array(); 

	$nestedData[] = $row["id"];
	$nestedData[] = $row["employee_name"];
    //$nestedData[] = $row["collectiondate"];   
	$nestedData[] = $row["given_date"];
	$nestedData[] = $row["fromdate"]." To ".$row["todate"];
	$nestedData[] = $row["paidamount"];
	$nestedData[] = $row["paytype"];
    $nestedData[] = $row["givenby"];
    
	
    
	
	//$date = new DateTime($row["employee_joining_date"]); 
	//$nestedData[] = $date->format('d-m-Y'); //only fetching date
	//$nestedData[] = $row["open_balance"];
	
	$edit  = "";
	if($userpermissionEdit==1) {
		$edit .="<a href='employeePaymentEdit.php?id=".$row['id']."' id='driverBatta_".$row['id']."' class='btn btn-primary'>Edit</a>&nbsp";
		$edit .="<a href='reportEmpPDF.php?ecode=".$row['ecode']."&&startDate=".$row['fromdate']."&&endDate=".$row['todate']."' id='driverBatta_".$row['id']."' class='btn btn-primary' target='_blank'>Print</a>&nbsp";
	}

	$Delete  = "";
	if($userpermissionDelete==1) {
		$Delete .="<a href='employeePaymentDelete.php?id=".$row['id']."' id='driverBatta_".$row['id']."' class='btn btn-danger'>Delete</a>&nbsp";
	}
	
	$Print  = "";
	if($userpermissionPrint==1) {
		$Print .="<a href='Emp_salaryslip.php?id=".$row['id']."' id='driverBatta_".$row['id']."' class='btn btn-primary' target='_blank'>Print</a>&nbsp";
	}
	
	
	$nestedData[] = $Print."&nbsp".$Delete;
	
	$data[] = $nestedData;
}

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

echo json_encode($json_data);  

?>
