<?php
include('../include/session.php');

date_default_timezone_set('Africa/Gaborone');
$entryDate = date("Y-m-d H:i:s"); 

$today = date("Y-m-d");

$store_item_id = $_GET['store_item_id'];

$table_name = $_SESSION['db_prefix']."_store_items";
$saved_item_query = "SELECT * FROM $table_name WHERE store_item_id = '".$store_item_id."' ";
$saved_item_result = mySQL_query($saved_item_query);
$saved_item_num = mysql_num_rows($saved_item_result);

$item_name_saved = mysql_result($saved_item_result, 0, 'item_name');
$location_saved = mysql_result($saved_item_result, 0, 'location');
$supplier_id_saved = mysql_result($saved_item_result, 0, 'supplier_id');
$category_saved = mysql_result($saved_item_result, 0, 'category');
$sub_category_saved = mysql_result($saved_item_result, 0, 'subCategory');
$unit_saved = mysql_result($saved_item_result, 0, 'unit');
$min_stock_saved = mysql_result($saved_item_result, 0, 'minimumStock');
$price_saved = mysql_result($saved_item_result, 0, 'price');
$purchase_price_saved = mysql_result($saved_item_result, 0, 'purchase_price');
$specs_saved = mysql_result($saved_item_result, 0, 'productSpecs');




$item_name = trim($_GET['item_text']);
$location = $_GET['location_select'];
$supplier_id = $_GET['supplier_id_select'];
$category = $_GET['category_select'];
$sub_category = $_GET['sub_category_select'];
$unit = $_GET['unit_select'];
$min_stock = $_GET['minimum_stock_text'];
$price = trim($_GET['base_price_text']);
$purchase_price = trim($_GET['purchase_price_text']);
$specs = $_GET['specs_text'];


$table_name = $_SESSION['db_prefix']."_store_items";
$edit_query = "UPDATE $table_name SET item_name = '".$item_name."', location = '".$location."', supplier_id = '".$supplier_id."', category = '".$category."', subCategory = '".$sub_category."', unit = '".$unit."', price = '".$price."', purchase_price = '".$purchase_price."', minimumStock = '".$min_stock."',  lastModDate = '".$today."', productSpecs = '".$specs."' WHERE store_item_id = '".$store_item_id."' ";
execsql($edit_query);


$table_name = $_SESSION['db_prefix']."_tracking_store";

if ($item_name_saved!=$item_name) {
	    $trackingQuery = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entryDate."' ,'".$_SESSION['username']."', '24', '".$store_item_id."' ,'".$item_name_saved."','".$item_name."','Item name' )";
$trackingResult = mySQL_query($trackingQuery);
}

if ($location_saved!=$location) {
	    $trackingQuery = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entryDate."' ,'".$_SESSION['username']."', '24', '".$store_item_id."' ,'".$location_saved."','".$location."','Stores location' )";
$trackingResult = mySQL_query($trackingQuery);
}

if ($supplier_id_saved!=$supplier_id) {
	    $trackingQuery = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entryDate."' ,'".$_SESSION['username']."', '24', '".$store_item_id."' ,'".$supplier_id_saved."','".$supplier_id."','Supplier' )";
$trackingResult = mySQL_query($trackingQuery);
}

if ($category_saved!=$category) {
	    $trackingQuery = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entryDate."' ,'".$_SESSION['username']."', '24', '".$store_item_id."' ,'".$category_saved."','".$category."','Category' )";
$trackingResult = mySQL_query($trackingQuery);
}

if ($sub_category_saved!=$sub_category) {
	    $trackingQuery = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entryDate."' ,'".$_SESSION['username']."', '24', '".$store_item_id."' ,'".$sub_category_saved."','".$sub_category."','Sub category' )";
$trackingResult = mySQL_query($trackingQuery);
}

if ($unit_saved!=$unit) {
	    $trackingQuery = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entryDate."' ,'".$_SESSION['username']."', '24', '".$store_item_id."' ,'".$unit_saved."','".$unit."','Unit' )";
$trackingResult = mySQL_query($trackingQuery);
}

if ($min_stock_saved!=$min_stock) {
	    $trackingQuery = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entryDate."' ,'".$_SESSION['username']."', '24', '".$store_item_id."' ,'".$min_stock_saved."','".$min_stock."','Min stock' )";
$trackingResult = mySQL_query($trackingQuery);
}

if ($price_saved!=$price) {
	    $trackingQuery = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entryDate."' ,'".$_SESSION['username']."', '24', '".$store_item_id."' ,'".$price_saved."','".$price."','Price' )";
$trackingResult = mySQL_query($trackingQuery);
}

if ($purchase_price_saved!=$purchase_price) {
	    $trackingQuery = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entryDate."' ,'".$_SESSION['username']."', '24', '".$store_item_id."' ,'".$purchase_price_saved."','".$purchase_price."','Purchase price' )";
		$trackingResult = mySQL_query($trackingQuery);
}

if ($specs_saved!=$specs) {
	    $trackingQuery = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entryDate."' , '".$_SESSION['username']."', '24', '".$store_item_id."' ,'".$specs_saved."','".$specs."','Specifications' )";
$trackingResult = mySQL_query($trackingQuery);
}



	if (isset($_GET['save_close_button']) )
	   { ?>
       <script>
	 		 window.location="store_item_display.php?store_item_id=<?php echo $store_item_id?>";
		</script>
        <?php		 
	   }
	   
	   if (isset($_GET['save_button']) )
	   {  ?>
       <script>
	          window.location="store_item_edit.php?store_item_id=<?php echo $store_item_id?>";
	   </script>
        <?php		 
	   }
	   ?>

