<?php
global $con;
include_once('functions.php');
if(stripos($_SERVER['SCRIPT_NAME'],"/admin/")===false?false:true){
	include_once("../connect.php");
	
}else{
	include_once("connect.php");
	
}
include_once("../classes/Inventory.class.php");
$function = $_REQUEST['f'];
if($function == "getInventoryItem"){	
	getInventoryItem($_REQUEST['iid']);
}elseif($function == "deleteInventoryItem"){
	deleteInventoryItem($_REQUEST['iid']);
}elseif($function == "saveInventoryItem"){
	saveInventoryItem($_REQUEST);
}elseif($function == "assignLocationByUPC"){
	$slot = isset($_REQUEST['slot'])?$_REQUEST['slot']:null;
	if($_REQUEST['slot'] == ""){
		$slot = null;
	}
	assignLocationByUPC($_REQUEST['UPC'], $_REQUEST['loc'], $slot);
}elseif($function == "addUPCtoItem"){
	addUPCtoItem($_REQUEST['line'], $_REQUEST['sku'], $_REQUEST['upc'], $_REQUEST['qty']);
}
elseif($function == "getItemLineBySKU"){
	getItemLineBySKU($_REQUEST['sku']);
}
elseif($function == "getItemByUPC"){
	getItemByUPC($_REQUEST['UPC']);
}elseif($function == "deleteLocationByUPC"){
	deleteLocation($_REQUEST['UPC'], $_REQUEST['slot']);
}elseif($function == "saveQtyByUPC"){
	saveQtyByUPC($_REQUEST['UPC'], $_REQUEST['qty']);
}elseif($function == "getInventoryStatistics"){
	$data = getInventoryStatistics($_REQUEST['line'], $_REQUEST['group']);
	$data = json_encode($data);
	echo $data;
}
function saveQtyByUPC($upc, $qty){
	global $con;
	$query = "SELECT * FROM INVENTORY JOIN PRODUCT_UPC USING(`Line`, `SKU`) WHERE UPC LIKE '$upc' OR SKU LIKE '$upc'";
	$result = mysqli_query($con, $query);
	
	$return = new stdClass();
	if($result){
		$row = mysqli_fetch_array($result);
		$item = new Item("%", $row['Line'], $row['SKU']);
		$query = "UPDATE INVENTORY SET Qty = $qty WHERE `Line` = '" . $item->Line . "' AND SKU = '" . $item->SKU . "'";
		if(mysqli_query($con,$query)){
			$return->Result = True;
			//$return->Data = $item;
			echo json_encode($return);	
		}
		
	}else{
		$return->Result = false;
		$return->Error = "upc_not_exists";
		echo json_encode($return);
	}
	
}
function getInventoryStatistics($Line = "%", $Group = "%"){
	global $con;
	if($Line == ""){
		$Line = "%";
	}
	if($Group == ""){
		$Group = "%";
	}
	$query = "SELECT SUM(AvgCost*Qty) TotalCost, SUM(Qty) TotalQty, SUM(PriceA*Qty) TotalPrice FROM INVENTORY WHERE Line LIKE '$Line' AND `Group` LIKE '$Group';";
	//echo $query;
	$result = mysqli_query($con, $query);
	$row = mysqli_fetch_array($result, MYSQLI_ASSOC);
	return $row;
}
function getItemByUPC($upc){
	global $con;
	$query = "SELECT * FROM INVENTORY JOIN PRODUCT_UPC USING(`Line`, `SKU`) WHERE UPC LIKE '$upc' OR SKU LIKE '$upc'";
	$result = mysqli_query($con, $query);
	
	$return = new stdClass();
	//UPC EXISTS
	if(mysqli_num_rows($result)){
		$row = mysqli_fetch_array($result);
		$item = new Item("%", $row['Line'], $row['SKU']);
		$return->Result = True;
		$return->Data = $item;
		echo json_encode($return);
	}else{
		$return->Result = false;
		$return->Error = "upc_not_exists";
		echo json_encode($return);
	}
	
	
	
	return;
}
function getItemLineBySKU($sku){
	global $con;
	$item = new Item(null,"%",$sku);
	echo $item->Line;
	return;
	
}
function getInventoryItem($itemID){
	global $con;
	$item = explode(",", $itemID);
	//var_dump($item);
	$query = "SELECT * FROM INVENTORY WHERE `Group` = '" . $item[0] . "' AND `Line` = '" . $item[1] . "' AND SKU = '" . $item[2] . "'";
	$result = mysqli_query($con, $query);
	$row = mysqli_fetch_assoc($result);
	
	$row['Image'] = hasImage($row['Line'], $row['SKU'], true)?getImage($row['Line'], $row['SKU'], true):false;
	echo json_encode($row);
	return true;
}

function deleteInventoryItem($itemID){
	global $con;
	$item = explode(",", $itemID);
	$query = "DELETE FROM INVENTORY WHERE `Group` = '" . mysqli_real_escape_string($con, $item[0]) . "' AND `Line` = '" . mysqli_real_escape_string($con, $item[1]) . "' AND `SKU` = '" . mysqli_real_escape_string($con, $item[2]) . "';";
	$result = mysqli_query($con, $query);
	//echo $query;
	if($result){
		echo "true";
	}else{
		echo "false";
	}
}
function saveInventoryItem($data){
	global $con;
	$data = array_map(array($con, 'real_escape_string'), $data);
	//$data['ButtonImage']
	$query = "INSERT INTO INVENTORY (`Group`, `Line`, `SKU`, `Description`, `Notes`, `Qty`, `Core`, `AvgCost`, `PriceA`, PriceB, PriceC, PriceD, PriceE, Interchange) VALUES
	('" . $data['Group'] . "', '" . $data['Line'] . "', '" . $data['SKU'] . "', '" . $data['Description'] . "', '" . $data['Notes'] . "', " . $data['Qty'] . ", '" . $data['Core'] . "',
	'" . $data['AvgCost'] . "', '" . $data['PriceA'] . "', '" . $data['PriceB'] . "', '" . $data['PriceC'] . "', '" . $data['PriceD'] . "', '" . $data['PriceE'] . "', '" . $data['Interchange'] . "')
	ON DUPLICATE KEY UPDATE `Description` = '" . $data['Description'] . "', `Notes` = '" . $data['Notes'] . "', `Qty` = " . $data['Qty'] . ",
	`AvgCost` = '" . $data['AvgCost'] . "',`PriceA` = '" . $data['PriceA'] . "' , `PriceB` = '" . $data['PriceB'] . "', `PriceC` = '" . $data['PriceC'] . "', `PriceD` = '" . $data['PriceD'] . "', `PriceE` = '" . $data['PriceE'] . "', `Interchange` = '" . $data['Interchange'] . "', `Core` = '" . $data['Core'] . "'";
	$result = mysqli_query($con, $query);
	//echo $query;
	if($result){
		echo "true";
	}else{
		echo "false";
	}
}
function assignLocationByUPC($upc, $newLocation, $slot=null){
	global $con;
	$return = new stdClass();
	$query = "SELECT * FROM INVENTORY JOIN PRODUCT_UPC USING(`Line`, `SKU`) WHERE UPC LIKE '$upc' OR SKU LIKE '$upc'";
	$result = mysqli_query($con, $query);
	//UPC EXISTS
	if(mysqli_num_rows($result)){
		$row = mysqli_fetch_array($result);
		$item = new Item(null, $row['Line'], $row['SKU']);
		$locations = $item->getLocations();

		//die("is not same");
		//if a slot has been passed -> set to that slot
		if(isset($slot)){
			$query = "UPDATE INVENTORY SET `$slot` = '$newLocation' WHERE `Line` = '" . $item->Line . "' AND `SKU` LIKE '" . $item->SKU . "'";
			if(mysqli_query($con, $query)){
				$item = new Item(null, $item->Line, $item->SKU);
				$return->Result = true;
				$return->Data = $item;
			}else{
				$return->Result = false;
				$return->Error = mysqli_error($con);
			}
		}
		//no slot passed
		//--send list of locations to user select from
		else{
			$return->Result = false;
			$return->Data = $locations;
			$return->Item = $item;
			$return->Error = "location_exists";
			
		}
	
		
		$clean = utf8ize($return);
		echo json_encode($clean);
		
	}
	//UPC DOES NOT EXISTS
	else{
		$return->Result = false;
		$return->Error = "upc_not_exists";
		echo json_encode($return);
	}
	
}

//
//function assignLocationByUPC($upc, $newLocation, $slot=null){
//	global $con;
//	$return = new stdClass();
//	$query = "SELECT * FROM INVENTORY JOIN PRODUCT_UPC USING(`Line`, `SKU`) WHERE UPC LIKE '$upc'";
//	$result = mysqli_query($con, $query);
//	//UPC EXISTS
//	if(mysqli_num_rows($result)){
//		$row = mysqli_fetch_array($result);
//		$item = new Item(null, $row['Line'], $row['SKU']);
//		$locations = $item->getLocations();
//		//HAS LOCATION
//		//	-NO
//		if($item->Location == "" || $item->Location === null){
//			$newLocation = mysqli_real_escape_string($con, $newLocation);
//			$query = "UPDATE INVENTORY SET Location = '$newLocation' WHERE `Line` = '" . $item->Line . "' AND `SKU` LIKE '" . $item->SKU . "'";
//			if(mysqli_query($con, $query)){
//				$item = new Item(null, $item->Line, $item->SKU);
//				$return->Result = true;
//				$return->Data = $item;
//			}else{
//				$return->Result = false;
//				$return->Error = mysqli_error($con);
//			}
//		}
//		//HAS LOCATION
//		//	-YES
//		else{
//			
//			//IS THE SAME?
//			//	-YES
//			if(in_array($newLocation, $locations)){
//				
//				
//				$item = new Item(null, $item->Line, $item->SKU);
//				
//				$return->Result = true;
//				$return->Data = $item;
//			}
//			//IS THE SAME?
//			//	-NO
//			else{
//				//die("is not same");
//				//if a slot has been passed -> set to that slot
//				if(isset($slot)){
//					$query = "UPDATE INVENTORY SET `$slot` = '$newLocation' WHERE `Line` = '" . $item->Line . "' AND `SKU` LIKE '" . $item->SKU . "'";
//					if(mysqli_query($con, $query)){
//						$item = new Item(null, $item->Line, $item->SKU);
//						$return->Result = true;
//						$return->Data = $item;
//					}else{
//						$return->Result = false;
//						$return->Error = mysqli_error($con);
//					}
//				}
//				//no slot passed
//				//--send list of locations to user select from
//				else{
//					$return->Result = false;
//					$return->Data = $locations;
//					$return->Error = "location_exists";
//					
//				}
//			}
//		}
//		//var_dump($return);
//		
//		$clean = utf8ize($return);
//		echo json_encode($clean);
//		
//	}
//	//UPC DOES NOT EXISTS
//	else{
//		$return->Result = false;
//		$return->Error = "upc_not_exists";
//		echo json_encode($return);
//	}
//	
//}
function deleteLocation($upc, $slot){
	global $con;
	$query = "SELECT * FROM INVENTORY JOIN PRODUCT_UPC USING(`Line`, `SKU`) WHERE UPC LIKE '$upc' OR SKU LIKE '$upc'";
	$result = mysqli_query($con, $query);
	//UPC EXISTS
	if(mysqli_num_rows($result)){
		$row = mysqli_fetch_array($result);
		$item = new Item(null, $row['Line'], $row['SKU']);
		$query = "UPDATE INVENTORY SET `$slot` = '' WHERE `SKU` = '" . $item->SKU . "' AND `Line` = '" . $item->Line . "'";
		if(mysqli_query($con, $query)){
			$item = new Item(null, $row['Line'], $row['SKU']);
			$item->organizeLocations();
			$locations = $item->getLocations();
			$return->Result = true;
			$return->Data = $locations;
			echo json_encode($return);		
		}else{
			$return->Result = false;
			$return->Error = "Error deleting location, query would not run";
			echo json_encode($return);		
		}
		
	}
	
	
	
}
function addUPCtoItem($line, $sku, $upc, $qty){
	global $con;
	$item = new Item(null, $line, $sku);
	if($item->Exists){
		$query = "INSERT INTO PRODUCT_UPC (UPC, Qty, `Line`, SKU) VALUES ('$upc', '$qty', '$line', '$sku')";
		if($result = mysqli_query($con, $query)){
			$return->Result = true;
			echo json_encode($return);
		}else{
			$return->Result = false;
			$return->Error = mysqli_error($result);
			echo json_encode($return);
		}
		
		
	}else{
		$return->Result = false;
		$return->Error = "item_not_found";
		echo json_encode($return);
	}
	
	
}
?>