<?php
include("../include/session.php");
include("../include/db.php");
	
	//echo $_SESSION['empId'];
	
	$cat = $_SESSION['store'];
	$sub_cat = $_SESSION['sub_cat'];
	$productId = $_SESSION['productId'];
	if($_SESSION['store'] == "Tiles and Accessories" ) { 
	$returned_by = $_SESSION['CustNo'];
	$status = "comp";
	}
	else {
	$returned_by = $_SESSION['empId'];
	$status = "emp";
	}
	
	
	$quantity  = $_SESSION['quantity'];
	$date = $_SESSION['receiveDate'];
		
	if (isset($_GET['new_button']) )
	   {
		 $action='returnWiz0.php';
	   }
		 
	if (isset($_GET['finish_button']) )
	  {
		$action='returnList.php?date=0&category='.$cat ;
	  }
	  
	$ret_stock = $_SESSION['retStock'] ;  
	
	
	
	if($_SESSION['store'] == "Tiles and Accessories" ) {
	   $reference=$_SESSION['orderNo'];	
	   $entry=40;
	}
	else {
		$reference=$_SESSION['empId'];
		$entry=41;
	}
	
	$userId = $_SESSION['username'];
	date_default_timezone_set('Africa/Khartoum');
	$entryDate = date("Y-m-d H:i:s",time()-3600); 
	
	$returnQuery = "INSERT INTO nam_rad_returns (productId, category, sub_category, quantity, ret_stock,  reference, returned_by, status,  date) VALUES ('".$productId."', '".$cat."', '".$sub_cat."', '".$quantity."', '".$ret_stock."', '".$reference."', '".$returned_by."', '".$status."', '".$date."')" ;
	$returnResult = mySQL_query($returnQuery);
	
	
	
if($_SESSION['retStock'] == "yes" ) {	
	
	$quantityQuery = "SELECT quantity, price FROM nam_products WHERE productId = '$productId'";
	$quantityResult = execsql($quantityQuery);
	$totalQuantity = mysql_result($quantityResult, 0, "quantity") + $quantity;
	
	
	$stockQuery = "UPDATE nam_products SET suppliersName = '', quantity = '".$totalQuantity."' , lastModDate = '".$date."',   WHERE productId = '".$productId."'";
	$stockResult = mySQL_query($stockQuery);
	
}
else {
$totalQuantity = $quantity;	
}
	
			
	$historyQuery = "INSERT INTO nam_itemhistory (productId, entry, reference, quantity, total, date, entryDate, userId) VALUES ('".$productId."','".$entry."','".$reference."','".$quantity."', '".$totalQuantity."','".$date."','".$entryDate."','".$userId."')";
	$historyResult = mySQL_query($historyQuery);	


//echo $historyQuery;

	$actionQuery = "INSERT INTO nam_action_tracking (entry_date, user, action, details) VALUES ('".$entryDate."' ,'".$userId."','Returns' ,'".$productId."' )";
	$actionResult = mySQL_query($actionQuery);
	
     $_SESSION['store'] ="";
	 $_SESSION['sub_cat'] ="";
     $_SESSION['productId'] ="";
	 $_SESSION['productName'] ="";
	 $_SESSION['CustNo'] ="";
     $_SESSION['CustName'] = "";
	 $_SESSION['empId'] ="";
     $_SESSION['empName'] = "";
	 $_SESSION['retStock'] = "";
	 $_SESSION['loan'] = "";
	 $_SESSION['orderNo'] = "";
	 $_SESSION['quantity'] = "";
	 $_SESSION['loan'] = "";
	 $_SESSION['receiveDate'] = date('Y-m-d');
?>

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

