<?php
	include("../../include/session.php");
	include("../../include/db.php");
if(!$session->isRadstoneStoreUser() && !$session->isAdmin() && !$session->isRadstoneReceptionUser() ){
	header("Location: ../Store/storeMenu.php");
	}else{
		$cat = mysql_real_escape_string($_POST['cat']);	
		$productId = mysql_real_escape_string($_POST['productId']);
		$for = mysql_real_escape_string($_POST['for']);
		$use = mysql_real_escape_string($_POST['for_select']);
		$quantity = mysql_real_escape_string($_POST['quantity']);
		$comments = mysql_real_escape_string($_POST['comments']);
		$date = isset($_REQUEST["date1"]) ? $_REQUEST["date1"] : "";
		$department = mysql_real_escape_string($_POST['RadioGroup1']);
		$empId = mysql_real_escape_string($_POST['empId']);
		$return = mysql_real_escape_string($_POST['submitok2']);
		
		$repId = mysql_real_escape_string($_POST['repId']);
		$chargeTo = mysql_real_escape_string($_POST['chargeTo']);
		
		$userId = $_SESSION['username'];
	    date_default_timezone_set('Africa/Khartoum');
	    $entryDate = date("Y-m-d H:i:s",time()-3600); 
		
		if ($empId!="") {
		$employeeQuery = "SELECT empFName, empLName FROM employees WHERE empId = '$empId'";
		$employeeResult = execsql($employeeQuery);
		$employeeFirst = mysql_result($employeeResult, 0, "empFName") ;
		$employeeLast = mysql_result($employeeResult, 0, "empLName") ;
		$employeeName = $employeeFirst." ".$employeeLast;
		}
		
		$quantityQuery = "SELECT quantity, price FROM products WHERE productId = '$productId'";
		$quantityResult = execsql($quantityQuery);
		$totalQuantity = mysql_result($quantityResult, 0, "quantity") - $quantity;
		$price = mysql_result($quantityResult, 0, "price");
		
		//updating stocklevel and itemhistory for all		
		$updateProductsQuery = "UPDATE products SET quantity = quantity - '$quantity' WHERE productId = '$productId'";
		$updateProductsResult = execsql($updateProductsQuery);
		
		
		//updating specific issue list
		echo $department."<br>";
		if ($department=="employee") {
		$employeeQuery = "INSERT INTO loan (productId, empId, quantity, comments, date,  price) VALUES ('".$productId."', '".$empId."', '".$quantity."', '$comments','".$date."', '".$price."')";
		$employeeResult = execsql($employeeQuery);
		
		$itemHistoryQuery = "INSERT INTO itemhistory (productId, details, reference, quantity, total, date, entryDate, userId) VALUES ('".$productId."','Assigned to: ','".$empId."','".$quantity."', '".$totalQuantity."','".$date."','".$entryDate."','".$userId."')";
		
		$itemHistoryResult = execsql($itemHistoryQuery);
		}
		
		if ($department=="radstone") {
		$productionQuery = "INSERT INTO radstoneusage (productId, reason, quantity, comments, date, costs) VALUES ('".$productId."', '".$use."', '".$quantity."', '$comments' , '".$date."','".$price."')";
		$productionResult = execsql($productionQuery);
		$itemHistoryQuery = "INSERT INTO itemhistory (productId, details, quantity, total, date, entryDate, userId) VALUES ('".$productId."','supplied to: ".$use."','".$quantity."', '".$totalQuantity."','".$date."','".$entryDate."','".$userId."')";
		//echo "query: ".$itemHistoryQuery;
		$itemHistoryResult = execsql($itemHistoryQuery);
		}
		
		if ($department=="tocotex") {	
		$thirdPartyQuery = "INSERT INTO rad_tocotex_usage (productId, comp_name, representative, quantity, comments, date, costs) VALUES ('".$productId."', '".$chargeTo."', '$repId', '".$quantity."', '$comments' ,'".$date."','".$price."')";
		$thirdPartyResult = execsql($thirdPartyQuery);
		$itemHistoryQuery = "INSERT INTO itemhistory (productId, details, quantity, total, date, entryDate, userId) VALUES ('".$productId."','supplied to: ".$chargeTo."','".$quantity."', '".$totalQuantity."','".$date."','".$entryDate."','".$userId."')";
		$itemHistoryResult = execsql($itemHistoryQuery);
	   }
	   
	   if ($department=="third party") {	
		$thirdPartyQuery = "INSERT INTO thirdpartyusage (productId, name, quantity, comments, date, costs) VALUES ('".$productId."', '".$for."', '".$quantity."', '$comments' ,'".$date."','".$price."')";
		$thirdPartyResult = execsql($thirdPartyQuery);
		$itemHistoryQuery = "INSERT INTO itemhistory (productId, details, quantity, total, date, entryDate, userId) VALUES ('".$productId."','supplied to: ".$for."','".$quantity."', '".$totalQuantity."','".$date."','".$entryDate."','".$userId."')";
		$itemHistoryResult = execsql($itemHistoryQuery);
	   }
	   
	   
		header("Location: ../fromStore.php?cat=$cat&subCatSelect=All");
		
		
	 // echo "<br>finished";
}
?>
