<?php
include ("../../include/session.php");
if(!$session->isRadstoneStoreUser() && !$session->isAdmin() && !$session->isRadstoneReceptionUser() ){
	header("Location: ../employeeMenu.php");
} else {
		include("../../include/db.php");
		$loanId = mysql_real_escape_string($_GET['loanId']);
		$productId = mysql_real_escape_string($_GET['productId']);
		$quantity = mysql_real_escape_string($_GET['quantity']);
		$empId = mysql_real_escape_string($_GET['empId']);
		$date = date("Y-m-d");
		$q = "UPDATE loan SET returned = '1', dateReturned = CURDATE() WHERE loanId = '$loanId'";
		$q1 = "UPDATE products SET quantity = quantity + '$quantity' WHERE productId = '$productId'";
		$qP = "SELECT quantity FROM products WHERE productId = '$productId'";
		$rP = execsql($qP);
		$amount = mysql_result($rP, 0, "quantity") + $quantity;
		
		$qE = "SELECT empFName, empLName FROM employees WHERE empId = '$empId'";
		$rE = execsql($qE);
		$name = mysql_result($rE, 0, "empFName") . " " . mysql_result($rE, 0, "empLName");
		$q2 = "INSERT INTO itemhistory (productId, details, quantity, total, date) VALUES ('$productId', 'Returned Item: <a href=\"../HR/searchAndDisplayE.php?id=$empId\">$name</a>', '$quantity', '$amount', '$date')";
		execsql($q);
		execsql($q1);
		execsql($q2);
	//	echo $q;
		header("Location: ../searchAndDisplayE.php?id=$empId&type=loan");
		
		
}