<?php
/* Database connection start */
require_once("../includes/config.php");
$get_opr_rgts = getOprRights($_SESSION['userid'],$_REQUEST['pid'],$link1);
/////////check approval is applicable or not
$apply_app = getAnyDetails(base64_decode($_REQUEST['pid']),"apply_approval","tabid","tab_master",$link1);
///// get access location details
$access_loc = getAccessLocation($_SESSION['userid'],$link1);
/////get status//
$arrstatus = getFullStatus("",$link1);
/* Database connection end */
// storing  request (ie, get/post) global array to a variable  
$gatepassdata= $_REQUEST;

## selected  Location
if($_REQUEST['location']!==""){
	$location="and location_code ='".$_REQUEST['location']."'";
}else{
	$location="";
}
## selected  status
if($_REQUEST['stock_type']!==""){
	$stock_conver="and conversion_type ='".$_REQUEST['stock_type']."'";
}else{
	$stock_conver="";
}
## selected  date range
if ($_REQUEST['daterange'] != ""){
	$seldate = explode(" - ",$_REQUEST['daterange']);
	$fromdate = $seldate[0];
	$todate = $seldate[1];
}
else{
	$seldate = $today;
	$fromdate = date("Y-m-01");
	$todate = $today;
}
$columns = array( 
// datatable column index  => database column name
	0 => 'id', 
	1 => 'location_code',
	2 => 'conversion_type',
	4 => 'status'
);


// getting total number records without any search
$sql = "SELECT *";
$sql.=" FROM stock_conversion_master where entry_date >='".$fromdate."' and entry_date <='".$todate."'  ".$location." ".$stock_conver."";
$query=mysqli_query($link1, $sql) or die("stock_conversion-grid-data.php: ER1");
$totalData = mysqli_num_rows($query);
$totalFiltered = $totalData;  // when there is no search parameter then total number rows = total number filtered rows.


$sql = "SELECT *";
$sql.=" FROM stock_conversion_master where entry_date >='".$fromdate."' and entry_date <='".$todate."' ".$location." ".$stock_conver."";
if( !empty($gatepassdata['search']['value']) ) {   // if there is a search parameter, $gatepassdata['search']['value'] contains search parameter
	$sql.=" OR ( location_code LIKE '".$gatepassdata['search']['value']."%'";    
	$sql.=" OR conversion_type LIKE '".$gatepassdata['search']['value']."%'";
	$sql.=" OR status LIKE '".$gatepassdata['search']['value']."%' )";
}
$query=mysqli_query($link1, $sql) or die("stock_conversion-grid-data.php: ER2");
$totalFiltered = mysqli_num_rows($query); // when there is a search parameter then we have to modify total number filtered rows as per search result. 
$sql.=" ORDER BY ". $columns[$gatepassdata['order'][0]['column']]."   ".$gatepassdata['order'][0]['dir']."  LIMIT ".$gatepassdata['start']." ,".$gatepassdata['length']."   ";
/* $gatepassdata['order'][0]['column'] contains colmun index, $gatepassdata['order'][0]['dir'] contains order such as asc/desc  */	

$query=mysqli_query($link1, $sql) or die("stock_conversion-grid-data.php: ER3");

$data = array();
$j=1;
while( $row=mysqli_fetch_array($query) ) {  // preparing an array
	$nestedData=array(); 
	///// check this user have right to view the details
    if($get_opr_rgts['view']=="Y"){
        $viewicon = "<div align='center'><a href='stock_conversion_view.php?id=".base64_encode($row['system_ref_no'])."&stock_type=".$_REQUEST['stock_type']."&location=".$_REQUEST['location']."&daterange=".$_REQUEST['daterange']."".$pagenav."' title='view'><i class='fa fa-eye fa-lg faicon' title='View Stock Conversion'></i></a></div>";	
    }else{
        $viewicon = "";
    }	
    if($apply_app=="Y"){
		if($get_opr_rgts["approval"]=="Y"){
		if($row['status']==3){
				$apicon = "<div align='center'><a href='stock_conversion_approval.php?id=".base64_encode($row['system_ref_no'])."&stock_type=".$_REQUEST['stock_type']."&location=".$_REQUEST['location']."&daterange=".$_REQUEST['daterange']."".$pagenav."' title='Take Action'><i class='fa fa-legal fa-lg faicon' title='Take Action'></i></a></div>";
		}else if($row["status"]!='7'){   
                   
				$actiontaken = getAnyDetails($row["system_ref_no"],"action_taken","ref_no","approval_activity",$link1);
				$apicon = $arrstatus[$actiontaken];
			}else{
                         $apicon = "Not Authorized";   
                        }	
		}else{
			$apicon = "Not Authorized";
		}
	}
	$nestedData[] = $j; 
	$nestedData[] = getAnyDetails($row["location_code"],"locationname","location_code","location_master",$link1)."(".$row["location_code"].")";
        $nestedData[] = $row["system_ref_no"];
	$nestedData[] = dt_format($row["entry_date"]);	
        $nestedData[] = $row["conversion_type"];
	$nestedData[] = $arrstatus[$row["status"]];
        $nestedData[] = $apicon;
	$nestedData[] = $viewicon;
	////// check if print right is assigned
	if($get_opr_rgts["print"]=="Y"){
		$nestedData[] = "<div align='center'><a href='stock_conversion_print.php?id=".base64_encode($row['system_ref_no'])."&stock_type=".$_REQUEST['stock_type']."&location=".$_REQUEST['location']."&daterange=".$_REQUEST['daterange']."".$pagenav."' target='_blank' title='Print'><i class='fa fa-print fa-lg faicon' title='Print'></i></a></div>";
	}else{
		$nestedData[] = "";
	}

	
	
	$data[] = $nestedData;
	$j++;
}

$json_data = array(
			"draw"            => intval( $gatepassdata['draw'] ),   // for every request/draw by clientside , they send a number as a parameter, when they recieve a response/data they first check the draw number, so we are sending same number in draw. 
			"recordsTotal"    => intval( $totalData ),  // total number of records
			"recordsFiltered" => intval( $totalFiltered ), // total number of records after searching, if there is no searching then totalFiltered = totalData
			"data"            => $data   // total data array
			);

echo json_encode($json_data);  // send data as json format
?>
