<?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, dc_num, 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 => 'invoice_no', 
	2 => 'sale_date',
	3 => 'customer_id',
    4 => 'itemcount',
	5 => 'tamount'
	
    
);

$sql = "SELECT id FROM saleheader";
$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.invoice_no, sh.sale_date, sh.customer_id,  sh.po_num, sh.itemcount, sh.tamount, sh.create_date,sh.id,cm.cname FROM saleheader as sh LEFT JOIN customer_master as cm on cm.id = sh.customer_id WHERE 1=1 and sh.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.sale_date LIKE '".$requestData['search']['value']."%' ";
	$sql.=" OR cm.cname LIKE '".$requestData['search']['value']."%' ";
    $sql.=" OR sh.itemcount LIKE '".$requestData['search']['value']."%' ";
    $sql.=" OR sh.tamount 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, dc_num, po_num,saleGSTNO, itemcount, 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["sale_date"]); 
    $nestedData[] = $date->format('d-m-Y'); //only fetching date
    $nestedData[] = $row["cname"];
    $nestedData[] = $row["itemcount"];
    $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='saleEdit.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='saleinvoice.php?saleID=".$row['id']."' target='_blank' class='btn btn-primary'>Print</a>&nbsp;";
		
		//$Print .="<a href='saleinvoice.php?saleID=".$row['id']."' target='_blank' class='btn btn-primary'>Print</a>&nbsp;<a href='saleinvoice_tamil.php?saleID=".$row['id']."' target='_blank' class='btn btn-primary'>TamilPrint</a>&nbsp;<a href='spdf_gst.php?saleID=".$row['id']."' target='_blank' class='btn btn-primary'>SPRINT</a>&nbsp;<a href='spdf_gst_tamil.php?saleID=".$row['id']."' target='_blank' class='btn btn-primary'>TSPRINT</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);  

?>
