<?php
	include("../../include/session.php");
	include("../../include/db.php");
	if(!$session -> isAdmin()){
		header("Location: ../main.php");
	}else{
		$id = mysql_real_escape_string($_POST['productId']);
		$count = mysql_real_escape_string($_POST['stock']);
		$cat = mysql_real_escape_string($_GET['cat']);
		$date = date("Y-m-d");
		$q = "SELECT quantity FROM products WHERE productId = '$id'";
		$result = execsql($q);
		$currentQ = mysql_result($result, 0, "quantity");
		$figure = $count - $currentQ;
		//echo $currentQ;
		$q1 = "UPDATE products SET quantity = '$count' WHERE productId = '$id'";
		execsql($q1);
		$q2 = "INSERT INTO itemhistory (productId, details, quantity, total, date) VALUES ('$id', 'Stock Count', '$figure','$count', '$date')";
		execsql($q2);
		//echo $q2;
		$q3 = "INSERT INTO stockloss (productId, amount, date) VALUES ('$id', '$figure', '$date')";
		//echo "<br><br>".$q3;
		execsql($q3);
		$return = mysql_real_escape_string($_POST['submitok']);
		$cat = mysql_real_escape_string($_POST['cat']);
			    if ($return == "Save and New") {
			      
			 header("Location: ../stockCount.php?cat=$cat");
		} else {
		          if ($cat=="Tiles and Accessories") {
						header("Location: ../showAllProducts.php?cat=$cat"); }
				   else {
				        header("Location: ../showAllProductsAll.php?CategorySelect=$cat"); }
		}
		
		

	}