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

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

//saleheader (invoice_no, sale_date, customer_id, vehicle, po_num,saleGSTNO, itemcount, tamount, create_date, flag)
$userpermissionEdit 	= $_POST['userpermissionEdit'];
$userpermissionDelete 	= $_POST['userpermissionDelete'];
$userpermissionPrint 	= $_POST['userpermissionPrint'];

$columns = array( 
// datatable column index  => database column name
	0 => 'id',
	1 => 'advance_no', 
	2 => 'advance_date',
	3 => 'employee_id',
    4 => 'itemcount',
	5 => 'tamount'
	
    
);

$sql = "SELECT id FROM employee_advance_collection where   status=''";
$query=mysqli_query($conn, $sql) or die("Error logged for sale Dashboard: Step 1".$sql);
$totalData = mysqli_num_rows($query);
$totalFiltered = $totalData;  

$sql = "SELECT sh.advance_no, sh.advance_date, sh.employee_id, sh.advance_amount,sh.transaction_mode, sh.remarks,sh.advance_collection_amount, sh.id,em.ename FROM employee_advance_collection as sh LEFT JOIN employee_master as em on em.id = sh.employee_id WHERE 1=1 and  sh.status='' ";

if( !empty($requestData['search']['value']) ) {   
	$sql.=" AND (sh.id LIKE '".$requestData['search']['value']."%' ";
	$sql.=" OR  sh.advance_no LIKE '".$requestData['search']['value']."%' ";    
	$sql.=" OR sh.advance_date LIKE '".$requestData['search']['value']."%' ";
	$sql.=" OR sh.transaction_mode LIKE '".$requestData['search']['value']."%' ";
	$sql.=" OR em.ename LIKE '".$requestData['search']['value']."%' ";
    $sql.=" OR sh.advance_amount LIKE '".$requestData['search']['value']."%' ";
    $sql.=" OR sh.remarks LIKE '".$requestData['search']['value']."%' )";
	
    
}
$query=mysqli_query($conn, $sql) or die("Error logged for sale Dashboard: Step 2".$sql);
$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 logged for sale Dashboard: Step 3".$sql);


//(invoice_no, sale_date, customer_id, vehicle, po_num,saleGSTNO, itemcount, tamount, create_date, flag)
$data = array();
while( $row=mysqli_fetch_array($query) ) { 
	$nestedData=array(); 

	$nestedData[] = $row["advance_no"];
    //$nestedData[] = $row["collectiondate"];
   
	$date = new DateTime($row["advance_date"]); 
    $nestedData[] = $date->format('d-m-Y'); //only fetching date
    $nestedData[] = $row["ename"];
    
	$nestedData[] = number_format((float)$row["advance_amount"], 2, '.', '');
	$nestedData[] = number_format((float)$row["advance_collection_amount"], 2, '.', '');
	 $nestedData[] = $row["transaction_mode"];
    $nestedData[] = $row["remarks"];
    
	
	//number_format((float)$row["tamount"], 2, '.', '');
	
	
    
    
	//$nestedData[] = "<a href='productMasterEdit.php?id=".$row['id']."' id='productMaster_".$row['id']."' class='btn btn-primary'>Edit</a>";
	$edit  = "";
	if($userpermissionEdit==1) {
		$edit .="<a href='advance_collect_Edit.php?id=".$row['id']."' class='btn btn-primary'>Edit</a>";		
	}

	$Delete  = "";
	if($userpermissionDelete==1) {
		//$Delete .="<a  id='' class='btn btn-primary'>Delete</a>";
		$Delete .= '';
	}

	$Print  = "";
	if($userpermissionPrint==1) {
		//$Print .="<a href='saleinvoice.php?saleID=".$row['id']."' target='_blank' class='btn btn-primary'>Print</a>&nbsp";
		//<a href='salebill.php?saleID=".$row['id']."' target='_blank' class='btn btn-primary'>PDF</a>&nbsp<a href='salebillsmalpdf.php?saleID=".$row['id']."' target='_blank' class='btn btn-primary'>SPDF</a>
		
		//$PDF .="<a href='salebill.php?saleID=".$row['id']."' target='_blank' class='btn btn-primary'>Save PDF</a>";
	}


	
	$nestedData[] = $edit."&nbsp".$Delete."&nbsp".$Print;

    $data[] = $nestedData;
}

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

echo json_encode($json_data);  

?>
