<?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"] == 4)
		{
			if($itemcode == "")
			{
				$err = $err."Item Identification Required!<br>";
			}
			else
			{
				//$itemstockdata = DB::queryFirstRow("SELECT stock_item.item_name, item_price.id, item_price.sell_price FROM item_price INNER JOIN stock_item ON item_price.item = stock_item.id AND stock_item.bar_code=%s WHERE item_price.shop=%i UNION SELECT stock_item.item_name, item_price.id, item_price.sell_price FROM item_price INNER JOIN stock_item ON item_price.item = stock_item.id AND stock_item.alt_barcode=%s WHERE item_price.shop=%i", $itemcode, $_SESSION["connecteduserbranch"], $itemcode, $_SESSION["connecteduserbranch"]);	
				
				$itemstockdata = DB::queryFirstRow("SELECT stock_item.item_name, item_price.id, item_price.sell_price FROM item_price INNER JOIN stock_item ON item_price.item = stock_item.id AND stock_item.web_product_id=%s UNION SELECT stock_item.item_name, item_price.id, item_price.sell_price FROM item_price INNER JOIN stock_item ON item_price.item = stock_item.id AND stock_item.web_product_id=%s" , $itemcode, $itemcode);	
				
				if(DB::count() != 1)
				{
					$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, 'name' => $itemstockdata['item_name'], 'priceid' => $itemstockdata['id'], 'price' => number_format($itemstockdata['sell_price'], 2));
		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);
	}
?>