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

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

//purchaseheader (invoice_no, purchase_date, supplier_id, vehicle, po_num,purchaseGSTNO, remark, tamount, create_date, flag)
$userpermissionEdit 	= $_POST['userpermissionEdit'];
$userpermissionDelete 	= $_POST['userpermissionDelete'];
$userpermissionPrint 	= $_POST['userpermissionPrint'];

$columns = array( 
// datatable column index  => database column name
	0 => 'id',
	1 => 'invoice_no', 
	2 => 'purchase_date',
	3 => 'supplier_id',
    4 => 'remark',
	5 => 'tamount'
	
    
);

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

$sql = "SELECT sh.invoice_no, sh.purchase_date, sh.supplier_id,  sh.po_num, sh.remark, sh.tamount, sh.create_date,sh.id,cm.sname,cm.mobile FROM purchaseheaderwo as sh LEFT JOIN supplier_master as cm on cm.id = sh.supplier_id WHERE 1=1 and status='' ";

if( !empty($requestData['search']['value']) ) {   
	$sql.=" AND (sh.id LIKE '".$requestData['search']['value']."%' ";
	$sql.=" OR  sh.invoice_no LIKE '".$requestData['search']['value']."%' ";    
	$sql.=" OR sh.purchase_date LIKE '".$requestData['search']['value']."%' ";
	$sql.=" OR cm.sname LIKE '".$requestData['search']['value']."%' ";
		$sql.=" OR cm.mobile LIKE '".$requestData['search']['value']."%' ";
    $sql.=" OR sh.remark LIKE '".$requestData['search']['value']."%' ";
    $sql.=" OR sh.tamount LIKE '".$requestData['search']['value']."%' )";
	
    
}
$query=mysqli_query($conn, $sql) or die("Error logged for purchase 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 purchase Dashboard: Step 3".$sql);


//(invoice_no, purchase_date, supplier_id, vehicle, po_num,purchaseGSTNO, remark, tamount, create_date, flag)
$data = array();
while( $row=mysqli_fetch_array($query) ) { 
	$nestedData=array(); 

	$nestedData[] = $row["invoice_no"];
    //$nestedData[] = $row["collectiondate"];
   
	$date = new DateTime($row["purchase_date"]); 
    $nestedData[] = $date->format('d-m-Y'); //only fetching date
    $nestedData[] = $row["sname"];
     $nestedData[] = $row["mobile"];
    $nestedData[] = $row["remark"];
    $tempvalue = number_format((float)$row["tamount"], 2, '.', '');
	$nestedData[] = str_replace('.00', '', number_format($tempvalue, 2, '.', ''));
	
	//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='purchaseEditwo.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($userpermissionEdit==1) {
		
		
		//$Print .="<a href='purchaseinvoice.php?purchaseID=".$row['id']."' target='_blank' class='btn btn-primary'>Print</a>&nbsp<a href='purchasebill.php?purchaseID=".$row['id']."' target='_blank' class='btn btn-primary'>PDF</a>";
		
		//$PDF .="<a href='purchasebill.php?purchaseID=".$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);  

?>
