<?php
/* Database connection start */
require_once("../includes/config.php");
/* Database connection end */
// storing  request (ie, get/post) global array to a variable  
$get_opr_rgts = getOprRights($_SESSION['userid'],$_REQUEST['pid'],$link1);

$gatepassdata= $_REQUEST;
## selected  Location
if(!empty($_REQUEST['location'])){
	$loc="location_code='".$_REQUEST['location']."'";
}else{
	$loc="location_code='".$_REQUEST['location']."'";
}
if(!empty($_REQUEST['stock_type'])){
	$stocktype="stock_type='".$_REQUEST['stock_type']."'";
	$itemtype="itemtypeid='".$_REQUEST['stock_type']."'";
}else{
	$stocktype=" 1";
	$itemtype="1";
}
/////
if(!empty($_REQUEST['prod_cat'])){
	$prodcat="productid='".$_REQUEST['prod_cat']."'";
}
else{
	$prodcat="1";
}
////
if(!empty($_REQUEST['product_sub_category'])){
	$psc="psubcatid='".$_REQUEST['product_sub_category']."'";
}
else{
	$psc="1";
}
/////
if(!empty($_REQUEST['brandname'])){
	$brand="brandid='".$_REQUEST['brandname']."'";
}
else{
	$brand="1";
}
$part_str = "";
///// make partcode string on the basis of selection
if(!empty($_REQUEST['prod_cat']) || !empty($_REQUEST['product_sub_category']) || !empty($_REQUEST['brandname'])){
	$res_part = mysqli_query($link1,"select partcode from partcode_master where ".$prodcat." and ".$psc." and ".$brand." and ".$itemtype."");
	while($row = mysqli_fetch_array($res_part)){
		if($part_str == ""){
			$part_str .= "'".$row['partcode']."'";
		}else{
			$part_str .= ",'".$row['partcode']."'";
		}
	}
	if($part_str){}else{$part_str="''";}
	$partstr = " and partcode in (".$part_str.")";
}else{
	$partstr = "";
}
///////make array of item type
$arr_itemtype = array();
$res_it = mysqli_query($link1,"select typeid,parttype from part_type_master");
while($row_it = mysqli_fetch_assoc($res_it)){
	$arr_itemtype[$row_it['typeid']] = $row_it['parttype'];
}
///////////////
$columns = array( 
// datatable column index  => database column name
	0 => 'id',
	1 => 'partcode',
	2 => 'stock_type', 
	3 => 'mount_qty',
	4 => 'ok_qty',
	5 => 'faulty_qty',
	6 => 'missing_qty'
);

// getting total number records without any search
$sql = "SELECT *";
$sql.=" FROM inventory_status where ".$loc." and ".$stocktype." ".$partstr."";
$query=mysqli_query($link1, $sql) or die("inventory-status-grid-data.php: get inventory status master1");
$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 inventory_status where ".$loc." and ".$stocktype." ".$partstr."";
if( !empty($gatepassdata['search']['value']) ) {   // if there is a search parameter, $gatepassdata['search']['value'] contains search parameter
	$sql.=" AND (partcode LIKE '".$gatepassdata['search']['value']."%'"; 
	$sql.=" OR location_code LIKE '".$gatepassdata['search']['value']."%'";  
	$sql.=" OR stock_type LIKE '".$gatepassdata['search']['value']."%' )";
}
$query=mysqli_query($link1, $sql) or die("inventory-status-grid-data.php: get inventory status master2");
$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("inventory-status-grid-data.php: get inventory status master3");


$data = array();
$j=1;
while( $row=mysqli_fetch_array($query) ) {  // preparing an array
	$bin_alloc=  mysqli_num_rows(mysqli_query($link1,"select id from bin_allocation where location_code='".$row["location_code"]."' and partcode='".$row["partcode"]."'"));
    
	$nestedData=array();      
	$nestedData[] = $j; 
	$nestedData[] = $row["partcode"];
	$nestedData[] = getAnyDetails($row["partcode"],"part_name","partcode","partcode_master",$link1);
	$nestedData[] = $arr_itemtype[$row["stock_type"]];
	$nestedData[] = $row["mount_qty"];
	$nestedData[] = $row["ok_qty"];
	$nestedData[] = $row["damage_qty"];
	$nestedData[] = $row["faulty_qty"];
	$nestedData[] = $row["p_rejected_qty"];
	$nestedData[] = $row["missing_qty"];
	$nestedData[] = $row["purchase_unit"];
	
	$pg = getAnyDetails($row["partcode"],"part_group","partcode","partcode_master",$link1);
	if($pg!=""){
		$nestedData[] = "<div align='center'><a onClick = openModel_new('".$pg."'); title='Group'><i class='fa fa-eye fa-lg faicon' title='Group'></i></a></div>";
	}else{
		$nestedData[] = "";
	}
       
	if($bin_alloc > 0){
            $nestedData[] = "<div align='center'><a onClick = openModel('".$row["partcode"]."','".$row["location_code"]."'); title='Bin Allocation'><i class='fa fa-eye fa-lg faicon' title='Bin Allocation'></i></a></div>";
//		$nestedData[] = "<div align='center'><a href='bin_allocation_inventory.php?id=".base64_encode($bin_alloc[1])."&partcode=".$row["partcode"]."&location=".$_REQUEST['location']."&stock_type=".$_REQUEST['stock_type']."&prod_cat=".$_REQUEST['prod_cat']."&product_sub_category=".$_REQUEST['product_sub_category']."&brandname=".$_REQUEST['brandname']."".$pagenav."' title='Bin Allocation'><i class='fa fa-eye fa-lg faicon' title='Bin Allocation'></i></a></div>";
			$nestedData[] = "<div align='center'><a href='bin_change_inventory.php?id=".base64_encode($row["partcode"])."&location=".$_REQUEST['location']."&stock_type=".$_REQUEST['stock_type']."&prod_cat=".$_REQUEST['prod_cat']."&product_sub_category=".$_REQUEST['product_sub_category']."&brandname=".$_REQUEST['brandname']."".$pagenav."' title='Bin Change'><i class='fa fa-edit fa-lg faicon' title='Bin Change'></i></a></div>";
	}else{
		$nestedData[] = "";
		$nestedData[] = "";
	}
	
	////// check if print right is assigned
	if(($get_opr_rgts["print"]=="Y") && ($bin_alloc > 0)){
		$nestedData[] = "<div align='center'><a href='barcode_change_print.php?part_id=".base64_encode($row['partcode'])."&location=".base64_encode($row['location_code'])."".$pagenav."' target='_blank' title='Barcode Print'><i class='fa fa-print fa-lg faicon' title='Barcode 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
?>
