<?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'];
	$employee_name =$_SESSION['employee_name'];
	$department =$_SESSION['department'];
	//$delNote = $_SESSION['deliveryNote'];
	$quantity  = $_SESSION['quantity'];
	$price = $_SESSION['price'];
	$date = $_SESSION['deliver_date'];
	
	$ip_address = $_SERVER['REMOTE_ADDR'];
	$prev_page = $_SERVER['HTTP_REFERER'];
	$table_name = "test_tracking_log";
	$track_check_query = "INSERT INTO $table_name (entry_date, user, company, page, location, prev_page) VALUES ('".$entry_date."' ,'".$_SESSION['username']."','Tlhomeso', 'deliverWiz6.php', '".$ip_address."', '".$prev_page."' )";
	execsql($track_check_query);
	
	
	$entry = "30";
	
	if (isset($_GET['new_button']) )
	   {
		 $action='deliverWiz0.php';
	   }
		 
	if (isset($_GET['finish_button']) )
	  {
		$action='stock_delivered.php?date=0' ;
	  }
	
	
	$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/Khartoum');
	$entryDate = date("Y-m-d H:i:s",time()-3600); 
	
	$table_name = $_SESSION['db_prefix']."_store_deliver";
	$deliveryQuery = "INSERT INTO $table_name (store_item_id, sub_cat, order_no, delivered_employee, delivered_department, quantity, price, date) VALUES ('".$store_item_id."', '".$sub_cat."', '".$order_no."', '".$employee_name."', '".$department."', '".$quantity."', '".$price."', '".$date."' )";
	$deliveryResult = mySQL_query($deliveryQuery);
	
	//$receive_query = "SELECT  receive_id FROM $table_name ORDER BY receive_id DESC LIMIT 1";
//    $receive_result = mySQL_query($receive_query);
//    $reference = mysql_result($receive_result, 0, "receive_id");
	
	$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."', price_2 = '".$price."'  WHERE store_item_id = '".$store_item_id."'";
	$stockResult = mySQL_query($stockQuery);
	
	if ($_SESSION['selected']=="employee") {$reference=$_SESSION['emp_id']; $entry="31";}
	if ($_SESSION['selected']=="department") {$reference=$department; $entry="30";}
	
	$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."','".$reference."','".$quantity."', '".$total_quantity."','".$date."','".$entryDate."','".$userId."')";
	$historyResult = mySQL_query($historyQuery);
	
	if ($_SESSION['selected']=="employee") {
	$table_name = $_SESSION['db_prefix']."_employee_issue";
	$issue_query = "INSERT INTO $table_name (emp_id, store_item_id, receive_date, quantity, cost ) VALUES ( '".$_SESSION['emp_id']."' , '".$store_item_id."' , '".$date."', '".$quantity."', '".$price."' ) ";
    execsql($issue_query);	
	}
	
	if ($entry=="31") {
		$store_action="Issue employee";
		$track_entry = 19;
		}
	else {
		$store_action="Issue department";
		$track_entry = 18;
		}
		
	if ($total_quantity<=$min_stock) {
	  if (($sub_category!='Timber') AND ($sub_category!='Mitek') AND ($sub_category!='Sundries')) {
			    $sub_category='Other';
		}	
		include('../include/min_stock_alert.php');
		
		$_SESSION['delivery_no'] = $reference;
		
		$table_name = $_SESSION['db_prefix']."_min_stock_report";
		$minimum_query = "INSERT INTO $table_name (store_item_id, stock_date, supplied, new_stock_level, report, supplied_to, supply_no ) VALUES ('".$store_item_id."', '".$entryDate."', '".$quantity."', '".$total_quantity."', 'minimum stock', '".$store_action."', '".$_SESSION['delivery_no']."' ) ";
		execsql($minimum_query);
		
		$email_message = $email_message.'<tr><td>'.$item_name.'</td><td>'.$sub_category.'</td><td align=center>'.$total_quantity.'</td><td align=center>'.$min_stock.'</td></tr>';
	}
	
//if ($email_message!='') {
//		  include('../include/min_stock_email_alert.php');
//	}		
	
	
	
$table_name = $_SESSION['db_prefix']."_tracking_store";
$trackingQuery = "INSERT INTO $table_name (entry_date, user, action, details_1, details_2, details_3, details_4) VALUES ('".$entryDate."' ,'".$_SESSION['username']."', '".$track_entry."', '".$store_item_id."', '".$reference."' ,'','' )";
$trackingResult = mySQL_query($trackingQuery);	
	
	$_SESSION['category'] ="";
	 $_SESSION['subCategory'] ="";
     $_SESSION['store_item_id'] ="";
	 $_SESSION['store_item_name'] ="";
	 $_SESSION['order_no'] ="";
	 $_SESSION['department'] = "";
	 $_SESSION['emp_id'] = "";
     $_SESSION['employee_name'] = "";
	 $_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>

