<?php

include("../../include/session.php");
include("../../include/db.php");
if(!$session->isRadstoneStoreUser() && !$session->isAdmin()){
	header("Location: ../storeMenu.php");
}else{
	$productName = mysql_real_escape_string($_POST['productName']);
	$q = "SELECT productName FROM products WHERE productName = '$productName'";
	$r = execsql($q);
	$n = mysql_num_rows($r);
	if($n > 0){
		$category = mysql_real_escape_string($_GET['category']);
		$subCat = mysql_real_escape_string($_POST['subCategory']);
		$minimumStock = mysql_real_escape_string($_POST['minimumStock']);
		$price = mysql_real_escape_string($_POST['price']);
		$productSpecs = mysql_real_escape_string($_POST['productSpecs']);
		
		header("Location: ../product.php?message=$productName&category=$category&subCategory=$subCat&minimumStock=$minimumStock&price=$price&productSpecs=$productSpecs");

	}else{
		
		$category = mysql_real_escape_string($_GET['category']);
		$subCat = mysql_real_escape_string($_POST['subCategory']);
		$unit = mysql_real_escape_string($_POST['unit']);
		$shelf = mysql_real_escape_string($_POST['shelf']);
		$minimumStock = mysql_real_escape_string($_POST['minimumStock']);
		$dep = mysql_real_escape_string($_POST['depreciation']);
		$returnable = mysql_real_escape_string($_POST['returnable']);
		$date = date("Y-m-d");
		$price = mysql_real_escape_string($_POST['price']);
		$productSpecs = mysql_real_escape_string($_POST['productSpecs']);
		$userId = $_SESSION['username'];
	   	date_default_timezone_set('Africa/Khartoum');
	    $entryDate = date("Y-m-d H:i:s",time()-3600); 
		$quantity ="0";
		
		$qry = "INSERT INTO products (productName, category, subCategory, shelf, unit, returnable, minimumStock, depreciation, date, price, productSpecs) VALUES ('$productName', '".$category."', '".$subCat."', '".$shelf."','".$unit."','".$returnable."', '$minimumStock', '".$dep."','".$date."', '$price', '$productSpecs')";
		$result = execsql($qry);


		$qry1 = "SELECT productId FROM products ORDER BY productId DESC LIMIT 1";
		$result3 = execsql($qry1);
		$productId = mysql_result($result3, 0, "productId");
		$q3 = "INSERT INTO itemhistory (productId, details, quantity, total, date, entryDate, userId) VALUES ('".$productId."','Added as New Item','".$quantity."', '".$quantity."','".$date."','".$entryDate."','".$userId."')";

		$result4 = execsql($q3);
		
			header("Location: ../product.php?category=&subCategory=");
		

	}
}

?>