<?php
	header('Content-Type: application/json');
	include("includes/dbconfig.php");
	
	session_write_close();
	
	$err = "";
	
	$server_response = array();
	
	$itemcode = $_POST['itemcode'];
	
	if(isset($_SESSION['connecteduserid']))
	{
		if($_SESSION["connectedusergroup"] == 3 || $_SESSION["connectedusergroup"] == 2)
		{
			if($itemcode == "")
			{
				$err = $err."Item Identification Required!<br>";
			}
			else
			{
                          $branch = 0;
                          if($_SESSION['shop']){ $branch = $_SESSION['shop'];} elseif($_SESSION["connecteduserbranch"] > 0){ $branch = $_SESSION["connecteduserbranch"];}
				$itemstockdata = DB::queryFirstRow("SELECT item_stock_level.id, stock_item.item_name, stock_item.bar_code AS bar_code FROM item_stock_level INNER JOIN stock_item ON item_stock_level.item = stock_item.id AND stock_item.bar_code=%s WHERE item_stock_level.shop=%i UNION SELECT  item_stock_level.id, stock_item.item_name, stock_item.alt_barcode AS bar_code FROM item_stock_level INNER JOIN stock_item ON item_stock_level.item = stock_item.id AND stock_item.alt_barcode=%s WHERE item_stock_level.shop=%i", $itemcode,  $branch, $itemcode,  $branch);			
				if(DB::count() == 1)
				{
					
				}
				else
				{
					$err = $err."Item Not Identified!<br>";
				}
			}
		}
		else
		{
			$err = $err."Limited Access!<br>";
		}
	}
	else
	{
		$err = $err."Un Authenticated User Access Detected!<br>";
	}
	
	if($err == "")
	{
		$server_response[] = array('isdataobtained' => 1, 'stockid' => $itemstockdata['id'], 'name' => $itemstockdata['item_name'], 'barcode' => $itemstockdata['bar_code']);
		echo json_encode($server_response);
	}
	else
	{
		$errormessage = "<div class=\"alert alert-danger\"><button type=\"button\" class=\"close\" data-dismiss=\"alert\"aria-hidden=\"true\">&times;</button>" . $err . "</div>";
		$server_response[] = array('isdataobtained' => 0, 'message' => $errormessage);
		echo json_encode($server_response);
	}
?>