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

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



$fromDate 	= '';
if($_POST['SalaryfromDate']){
	$invoicedate_temp   = new DateTime(stripslashes($_POST['SalaryfromDate']));
	$fromDate 		= $invoicedate_temp->format('Y-m-d');
}
$toDate 	= '';
if($_POST['SalarytoDate']) {
	$invoicedate_temp1   = new DateTime(stripslashes($_POST['SalarytoDate']));
	$toDate 		= $invoicedate_temp1->format('Y-m-d');
}

//cname, city, pincode, mobile, gst_type, ccode, open_balance
$columns = array( 
// datatable column index  => database column name
	 
	0 => 'ename',
	1 => 'eaddress',
	2 => 'mobile',
	3 => 'ecode',
	4 => 'shiftworkamt',
	5 => 'extrahoursamt',
);


$sql = "SELECT lm.ename as ename,sum(lad.shifttype) as shifttype,sum(lad.extraworkinghrs) as extraworkinghrs,lad.enameid 
FROM employee_attendance_dtl as lad LEFT JOIN employee_master as lm ON lm.id=lad.enameid where lad.isdelete=1 ";

if($fromDate !=''  && $toDate !='') {
	$sql.=" and lad.invoicedate BETWEEN '".$fromDate."' AND '".$toDate."'";

} 

$sql.="  GROUP BY lad.enameid ";

if( !empty($requestData['search']['value']) ) {   

	$sql.=" AND (  ename LIKE '".$requestData['search']['value']."%' ";	
    $sql.="  )";
}


$query=mysqli_query($conn, $sql) or die("laboursgriddata.php: get labours Data step 2");
$totalFiltered = mysqli_num_rows($query); 
$totalData = mysqli_num_rows($query);
$totalFiltered = $totalData;  

$sql.=" ORDER BY ". $columns[$requestData['order'][0]['column']]."   ".$requestData['order'][0]['dir']."  LIMIT ".$requestData['start']." ,".$requestData['length']."   ";

$query=mysqli_query($conn, $sql) or die("laboursgriddata.php: get labours Data step 3");
	
//cname, city, pincode, mobile, gst_type, ccode, open_balance
$data = array();
while( $row=mysqli_fetch_array($query) ) { 
	$nestedData=array(); 
    $edit_id  			= $row["enameid"];
    $nestedData[] 		= $row["ename"];
    $Details_Result1 	= mysqli_query($conn,"SELECT  lad.invoicedate  FROM employee_attendance_dtl as lad 
    WHERE  lad.enameid='$edit_id'  and LAST_DAY(invoicedate) LIMIT 1");
    $DResult1   		= mysqli_fetch_array($Details_Result1,MYSQLI_ASSOC);
    $invoicedate   		= new DateTime($DResult1['invoicedate']);

    //$nestedData[] = $invoicedate->format('d-m-Y');
	$Details_Result  	= mysqli_query($conn,"SELECT ename, id,extrahoursamt ,shiftworkamt FROM employee_master WHERE id=".$edit_id." ");
	$DResult   				= mysqli_fetch_array($Details_Result,MYSQLI_ASSOC);
	$DResult_shiftworkamt   = $DResult['shiftworkamt'];
	$DResult_extrahoursamt  = $DResult['extrahoursamt'];
	//$nestedData[] = $row["eaddress"];
    $nestedData[] 		= $row["shifttype"];
    $nestedData[] 		= $row["extraworkinghrs"];
    $nestedData[] 		= $DResult_shiftworkamt;
    $nestedData[] 		= $DResult_extrahoursamt;
	$nestedData[] 		= ($row["shifttype"]*$DResult_shiftworkamt)+($row["extraworkinghrs"]*$DResult_extrahoursamt);
	
	$edit  = "";
	if($fromDate !=''  && $toDate !='') {
		//$edit .="<a id='laboursMaster_".$row['enameid']."' onclick='ConfirmlaboursPaySalary(".$row['enameid'].")'  class='btn btn-primary'>Pay</a>&nbsp";
		$edit .="<a href='accEmpPrePayList.php?eid=".$row['enameid']."&SalaryfromDate=".$fromDate."&SalarytoDate=".$toDate."' id='laboursMaster_".$row['enameid']."'   class='btn btn-primary'>PaySlip View</a>&nbsp";

	} else {

		$edit .="Select Date Between &nbsp";

	}

	$Delete  		= "";
	$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);  

?>
