<?php
include("../include/session.php");
		
	$sub_cat= $_SESSION['subCategory'];
	$order_no= $_SESSION['order_no'];
	$store_item_id = $_SESSION['store_item_id'];
	//$store_item_name =$_SESSION['store_item_name'];
	$delivered_to =$_SESSION['delivered_to'];
	
	$quantity  = $_SESSION['quantity'];
	$price = $_SESSION['price'];
	$date = $_SESSION['deliver_date'];
	
	
	$entry = "30";
	
	if (isset($_GET['new_button']) )
	   {
		 $action='deliverWiz0.php';
	   }
		 
	if (isset($_GET['finish_button']) )
	  {
		$action='internal_supplies_list.php?date=' ;
	  }
	
	
	$d = date("d");
	$m = date("m");
	$y = date("Y");
	$modDate=date('Y-m-d');
	$active="1";
	
	$email_message='';
	
			
	$userId = $_SESSION['username'];
	date_default_timezone_set('Africa/Gaborone');
	$entryDate = date("Y-m-d H:i:s", time()); 
	
	
	$table_name = $_SESSION['db_prefix']."_store_deliver";
	$deliveryQuery = "INSERT INTO $table_name (store_item_id, sub_cat, order_no, delivered_to, quantity, price, date) VALUES ('".$store_item_id."', '".$sub_cat."', '".$order_no."', '".$delivered_to."', '".$quantity."', '".$price."', '".$date."' )";
	$deliveryResult = mySQL_query($deliveryQuery);
	
	
	
	$table_name2 = $_SESSION['db_prefix']."_store_items";
	$quantityQuery = "SELECT quantity, price, minimumStock, subCategory, item_name FROM $table_name2 WHERE store_item_id = '".$store_item_id."' ";
	$quantityResult = execsql($quantityQuery);
	$total_quantity = mysql_result($quantityResult, 0, "quantity") - $quantity;
	$price = mysql_result($quantityResult, 0, "price");
	$min_stock = mysql_result($quantityResult, 0, "minimumStock");
	$sub_category = mysql_result($quantityResult, 0, "subCategory");
	$item_name = mysql_result($quantityResult, 0, "item_name");
		
	$stockQuery = "UPDATE $table_name2 SET  quantity = '".$total_quantity."' , lastModDate = '".$date."'  WHERE store_item_id = '".$store_item_id."'";
	$stockResult = mySQL_query($stockQuery);
	
		
	$table_name3 = $_SESSION['db_prefix']."_item_history";		
	$historyQuery = "INSERT INTO $table_name3 (store_item_id, entry, reference, quantity, total, date, entryDate, userId) VALUES ('".$store_item_id."' ,'".$entry."','".$delivered_to."','".$quantity."', '".$total_quantity."','".$date."','".$entryDate."','".$userId."')";
	$historyResult = mySQL_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 ('".$entryDate."' ,'".$_SESSION['username']."', '19', '".$store_item_id."', '".$delivered_to."' ,'','' )";
$trackingResult = mySQL_query($trackingQuery);	
	
	$_SESSION['category'] ="";
	 $_SESSION['subCategory'] ="";
     $_SESSION['store_item_id'] ="";
	 $_SESSION['store_item_name'] ="";
	 $_SESSION['order_no'] ="";
	 $_SESSION['delivered_to'] = "";
	 $_SESSION['emp_id'] = "";
     $_SESSION['delivered_to'] = "";
	 $_SESSION['selected'] = "";
	 $_SESSION['quantity'] = "";
	 $_SESSION['price'] = "";
	 $_SESSION['deliver_date']  = date('Y-m-d');
	 $_SESSION['p'] = 1;
     $_SESSION['error']=0;
	 $_SESSION['changed']="";
	 $_SESSION['changed2']="";
	
?>

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

