<?php
include("../include/session.php");
	
	$d = date("d");
	$m = date("m");
	$y = date("Y");
	$modDate=date('Y-m-d');
	$active="1";
	
	$item_name = trim($_SESSION['item_name']); 
	$category = trim($_SESSION['category']);
	$subCat = trim($_SESSION['subCategory']);
	$item_type = trim($_SESSION['item_type']);
	$unit = trim($_SESSION['unit']);
	
	$location = trim($_SESSION['location']);
	$returnable = trim($_SESSION['returnable']);
	$minimumStock = trim($_SESSION['minimumStock']);
	$date = date("Y-m-d");
	$price = trim($_SESSION['price']);
	$productSpecs = trim($_SESSION['productSpecs']);
	$ref_code = $_SESSION['ingredient_code'];
	
	date_default_timezone_set('Africa/Gaborone');
	$entry_date = date("Y-m-d H:i:s", time()); 
	
	if ($category=='Product') {
		  	$table_name = $_SESSION['db_prefix']."_product_store";
		  	$stockQuery = "INSERT INTO $table_name (item_name, item_type, category, subCategory, location, unit, minimumStock, returnable, date, price, productSpecs, stockTake) VALUES ( '".$_SESSION['item_name']."', '".$item_type."', '".$category."', '".$subCat."', '".$location."','".$unit."', '".$minimumStock."', '".$returnable."', '".$date."', '".$price."', '".$productSpecs."', 'S' )";
		  	$stockResult = $con->query($stockQuery);
		  
		  	$id_query = "SELECT product_item_id  FROM $table_name ORDER BY product_item_id DESC LIMIT 1  ";
    		$id_result = $con->query($id_query);
			$row = mysqli_fetch_assoc($id_result);
			$product_item_id = $row["product_item_id"];
			
			$table_name2 = $_SESSION['db_prefix']."_product_history";	
			$historyQuery = "INSERT INTO $table_name2 (product_item_id, entry, reference, quantity, total, price, date, entryDate, userId) VALUES ('".$product_item_id."', '10', '','0', '0', '".$price."', '".$date."','".$entry_date."','".$_SESSION['username']."')";
			$historyResult = $con->query($historyQuery);
			
			$table_name = $_SESSION['db_prefix']."_tracking_store";
			$trackingQuery = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entry_date."' , '".$_SESSION['username']."', '12', '".$product_item_id."' ,'','','' )";
			$trackingResult = $con->query($trackingQuery);
	
	
	}
	else {
		  	$table_name = $_SESSION['db_prefix']."_store_items";
			$stockQuery = "INSERT INTO $table_name (item_name, ref_code, category, subCategory, location, unit, minimumStock, returnable, date, price, productSpecs, stockTake) VALUES ( '".$_SESSION['item_name']."', '".$ref_code."', '".$category."', '".$subCat."', '".$location."','".$unit."', '".$minimumStock."', '".$returnable."', '".$date."', '".$price."', '".$productSpecs."', 'S' )";
		  	$stockResult = $con->query($stockQuery);
		  
		  	$id_query = "SELECT store_item_id  FROM $table_name ORDER BY store_item_id DESC LIMIT 1  ";
    		$id_result = $con->query($id_query);
			$row = mysqli_fetch_assoc($id_result);
			$store_item_id = $row["store_item_id"];
			
			$table_name2 = $_SESSION['db_prefix']."_item_history";	
			$historyQuery = "INSERT INTO $table_name2 (store_item_id, entry, reference, quantity, total, price, date, entryDate, userId) VALUES ('".$store_item_id."', '10', '','0', '0', '".$price."', '".$date."','".$entry_date."','".$_SESSION['username']."')";
			$historyResult = $con->query($historyQuery);
			
			$table_name = $_SESSION['db_prefix']."_tracking_store";
			$trackingQuery = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entry_date."' , '".$_SESSION['username']."', '10', '".$store_item_id."' ,'','','' )";
			$trackingResult = $con->query($trackingQuery);
		  
	}
	

	
	if (isset($_GET['new_button']) )
	   {
		 $action='storeWizbase.php?error=0&p=1&s=0';
	   }
		 
	if (isset($_GET['finish_button']) )
	  {
		if ($category=='Product') {  
				$action='product_item_display.php?product_item_id='.$product_item_id ;
		}
		else {
			$action='store_item_display.php?store_item_id='.$store_item_id ;
		}
	  }
	
?>
<?php
      $_SESSION['item_name'] ="";
	 $_SESSION['color'] ="";
     $_SESSION['suppliersName'] = "";
	 $_SESSION['category'] = "";
	 $_SESSION['subCategory'] = "";
	 $_SESSION['location'] = "";
	 $_SESSION['unit'] = "";
	 
	 
	
	 $_SESSION['quantity'] = "";
	 $_SESSION['minimumStock'] = "";
	 $_SESSION['price'] = "";
		 
	 $_SESSION['date'] = "";
	 $_SESSION['productSpecs'] = "";
	 $_SESSION['ingredient_code'] = "";
	 $_SESSION['p'] = 1;
	 $_SESSION['error']=0;
	 $_SESSION['changed']="";
	 $_SESSION['changed2']="";
	 
?>

<script language="javascript" type="text/javascript">
        
		  window.location="<?php echo $action ?> ";
        
       </script>
