<?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']);
	$unit = trim($_SESSION['unit']);
	
	$item_color  = trim($_SESSION['color']);
	$item_antique  = trim($_SESSION['antique']);
	$location = trim($_SESSION['location']);
	$minimumStock = trim($_SESSION['minimumStock']);
	$date = date("Y-m-d");
	$price = trim($_SESSION['price']);
	$purchase_price = trim($_SESSION['purchase_price']);
	$productSpecs = trim($_SESSION['productSpecs']);
	
	date_default_timezone_set('Africa/Gaborone');
	$entry_date = date("Y-m-d H:i:s", time()); 
	
	
	$item_type = '';
	if ($_SESSION['subCategory']=='Tiles') { $item_type = $_SESSION['tiles']; }
	if ($_SESSION['subCategory']=='Accessories') { $item_type = $_SESSION['accessories']; }
	if ($_SESSION['subCategory']=='Consumables') { $item_type = $_SESSION['oxides']; }
	
		
			
	$table_name = $_SESSION['db_prefix']."_store_items";		
	$stockQuery = "INSERT INTO $table_name (branch, item_name, item_type, category, subCategory, location, unit, item_color, item_antique,  minimumStock, date, price, purchase_price, productSpecs, stockTake) VALUES ( '".$_SESSION['userbranch']."', '".$_SESSION['item_name']."', '".$item_type."', '".$category."', '".$subCat."', '".$location."','".$unit."', '".$item_color."', '".$item_antique."', '".$minimumStock."', '".$date."', '".$price."', '".$purchase_price."', '".$productSpecs."', 'S' )";
	$stockResult = mySQL_query($stockQuery);
			
	
    $id_query = "SELECT store_item_id  FROM $table_name ORDER BY store_item_id DESC LIMIT 1  ";
    $id_result = mySQL_query($id_query);
    $store_item_id = mysql_result($id_result, 0, "store_item_id");
		
	$table_name2 = $_SESSION['db_prefix']."_item_history";	
	$historyQuery = "INSERT INTO $table_name2 (branch, store_item_id, entry, reference, quantity, total, price, date, entryDate, userId) VALUES ('".$_SESSION['userbranch']."', '".$store_item_id."', '10', '','0', '0', '".$price."', '".$date."','".$entry_date."','".$_SESSION['username']."')";
	$historyResult = mySQL_query($historyQuery);	

	$table_name = $_SESSION['db_prefix']."_tracking_store";
	$trackingQuery = "INSERT INTO $table_name (entry_date, branch, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entry_date."' , '".$_SESSION['userbranch']."', '".$_SESSION['username']."', '10', '".$store_item_id."' ,'','','' )";
	$trackingResult = mySQL_query($trackingQuery);
	
	
	if (isset($_GET['new_button']) )
	   {
		 $action='storeWizbase.php?error=0&p=1&s=0';
	   }
		 
	if (isset($_GET['finish_button']) )
	  {
		$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['tiles'] = "";
//	 $_SESSION['antique'] = "";	
//	 $_SESSION['accessories'] = ""; 
//	 $_SESSION['color'] = "";
//	 
//	
//	 $_SESSION['quantity'] = "";
//	 $_SESSION['minimumStock'] = "";
//	 $_SESSION['price'] = "";
//	 $_SESSION['purchase_price'] = "";
//	 
//	 $_SESSION['date'] = "";
//	 $_SESSION['productSpecs'] = "";
//	 $_SESSION['p'] = 1;
//	 $_SESSION['error']=0;
//	 $_SESSION['changed']="";
//	 $_SESSION['changed2']="";
	 
?>

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