<?php
include("../include/session.php");
	
		
	if (isset($_GET['new_button']) )
	   {
		 $action='issueWiz0.php';
	   }
		 
	if (isset($_GET['finish_button']) )
	  {
		$action='issuedReport.php?date=0&productId=';
	  }
	
	
	$d = date("d");
	$m = date("m");
	$y = date("Y");
	//$modDate=date('Y-m-d');
	$active="1";
	
	$productId = $_SESSION['productId'];
	$quantity = $_SESSION['quantity'];
	$costs = $_SESSION['productPrice'];
	$category = $_SESSION['category'];
	$subCategory = $_SESSION['subCategory'];
	$company = $_SESSION['account_no'];
	$status = $_SESSION['issued_to'];
	$order = $_SESSION['order'];
	
	if ($status=="employee") { 	$name = $_SESSION['empId']; }
	if ($status=="representative") { 	$name = $_SESSION['repId']; }
	if ($status=="department") { 	$name = $_SESSION['department']; }
	
	if ($subCategory=="Fuel")  {
		      $name = $_SESSION['repId'];
		      $reg_no = $_SESSION['regNo'];
		      $mileage = $_SESSION['mileage'];
			  $status = "transport";
		   }
		   else {
			 $reg_no = "";
		     $mileage = "";  
		   }
		   
	$empId = $_SESSION['empId'];
	
		   
	$notes = $_SESSION['notes'];
	$date = $_SESSION['supplyDate'];
	
	$userId = $_SESSION['username'];
	
	date_default_timezone_set('Africa/Khartoum');
	$entryDate = date("Y-m-d H:i:s",time()-3600); 
	
	
	
	if ($subCategory=="Fuel")  {
	       $entry="34";
		   $reference=$_SESSION['account_no'];}
		   else{
			   if ($status=="employee") { $entry="37"; }
	           if ($status=="representative") { $entry="38"; }
	           if ($status=="department"){ $entry="36"; }
		     	$reference=$name;
	}
		
		$supplyQuery = "INSERT INTO rad_store_supply (productId, quantity, costs, category, subCategory, company, status, name, reg_no, order_no, mileage, notes, date) VALUES ('".$productId."', '".$quantity."', '".$costs."','".$category."','".$subCategory."', '".$company."', '".$status."', '".$name."', '".$reg_no."', '".$order."', '".$mileage."', '".$notes."','".$date."')";
	
	$supplyResult = mySQL_query($supplyQuery);
	
	    $loanQuery = "INSERT INTO loan (empId, productId, quantity, comments, price, date) VALUES ('".$empId."', '".$productId."', '".$quantity."','".$notes."','".$costs."', '".$date."')";
	    $loanResult = mySQL_query($loanQuery);
	
	
	$quantityQuery = "SELECT quantity FROM products WHERE productId = '$productId'";
	$quantityResult = mySQL_query($quantityQuery);
	$totalQuantity = mysql_result($quantityResult, 0, "quantity") - $quantity;
	
	
	$stockQuery = "UPDATE products SET  quantity = '".$totalQuantity."' , lastModDate = '".$entryDate."'  WHERE productId = '".$productId."'";
	$stockResult = mySQL_query($stockQuery);
		
		
	$historyQuery = "INSERT INTO itemhistory (productId, entry, reference, quantity, total, date, entryDate, userId) VALUES ('".$productId."','".$entry."','".$reference."','".$quantity."', '".$totalQuantity."','".$date."','".$entryDate."','".$userId."')";
	$historyResult = mySQL_query($historyQuery);	

	
	$actionQuery = "INSERT INTO action_tracking (entry_date, user, action, details) VALUES ('".$entryDate."' ,'".$userId."','Store issue' ,'".$productId."' )";
	$actionResult = mySQL_query($actionQuery);
	

     $_SESSION['changed'] = "";
	 $_SESSION['changed2'] = "";
	 $_SESSION['productId'] ="";
	 $_SESSION['productName'] ="";
	 $_SESSION['productPrice'] ="";
     $_SESSION['companyName'] = "";
	 $_SESSION['account_no'] = "";
	 $_SESSION['category'] = "";
	 $_SESSION['subCategory'] = "";
	 $_SESSION['supplyDate'] = date('Y-m-d');
	 $_SESSION['repId'] = "";
	 $_SESSION['rep'] = "";
	 $_SESSION['reason'] = "";
	 $_SESSION['regNo'] = "";
	 $_SESSION['mileage'] = "";
	 $_SESSION['quantity'] = "";
	 $_SESSION['driver'] = "";
	 $_SESSION['empId'] = "";
	 $_SESSION['employee'] = "";
	 $_SESSION['department'] = "";
	 $_SESSION['issued_to'] = "";
	 $_SESSION['department_select']="";
	 $_SESSION['notes']="";

?>

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