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

$userId = $_SESSION['username'];
date_default_timezone_set('Africa/Gaborone');
$entryDate = date("Y-m-d H:i:s", time());

$table_name = $_SESSION['db_prefix']."_order_return";
$add_supply_query = "SELECT order_return_no  FROM $table_name ORDER BY order_return_no DESC LIMIT 1  ";
$add_supply_result = mySQL_query($add_supply_query);
$add_supply_num = mysql_num_rows($add_supply_result);
if ($add_supply_num!=0) {
$order_return_no = mysql_result($add_supply_result, 0, "order_return_no");
}
else {
	$order_return_no=0;
}
$order_return_no=$order_return_no+1;

$cust_id =$_GET['cust_id'];

$table_name = $_SESSION['db_prefix']."_order_return";
$return_query = "INSERT INTO $table_name (order_return_no, order_no, order_reference, cust_id, return_date, returned_to, authorised_by ) VALUES ( '".$order_return_no."', '".$_SESSION['order_no']."', '".$_SESSION['order_reference']."', '".$cust_id."', '".$_SESSION['return_date']."', '".$_SESSION['received_by']."', '".$_SESSION['authorised_by']."' ) ";
execsql($return_query);

$table_name = $_SESSION['db_prefix']."_order_return";
$supply_id_query = "SELECT order_return_id  FROM $table_name ORDER BY order_return_id DESC LIMIT 1  ";
$supply_id_result = mySQL_query($supply_id_query);
$order_return_id = mysql_result($supply_id_result, 0, "order_return_id");

	

$table_name3 = $_SESSION['db_prefix']."_order_return_lines_temp";
$items_query = "SELECT * FROM $table_name3 WHERE  user_name =  '".$_SESSION['username']."' ";
$items_result = mySQL_query($items_query);
$items_num = mysql_num_rows($items_result);

$i=0;
while ($i<$items_num) {
   $store_item_id = mysql_result($items_result, $i, "store_item_id");
   $quantity = mysql_result($items_result, $i, "quantity");
   $price = mysql_result($items_result, $i, "price");
   $to_return = mysql_result($items_result, $i, "to_return");
      
   $table_name2 = $_SESSION['db_prefix']."_store_items";
   $stock_total_query = "SELECT item_name, quantity FROM $table_name2 WHERE store_item_id = '".$store_item_id."'";
   $stock_total_result = mySQL_query($stock_total_query);
   $total_quantity = mysql_result($stock_total_result ,0, "quantity");
   $item_name = mysql_result($stock_total_result ,0, "item_name");
   
   $table_name4 = $_SESSION['db_prefix']."_order_return_lines";  
   $insert_item_query = "INSERT INTO $table_name4 (order_no, order_reference, order_return_no, cust_id, store_item_id, quantity, price, issue_date, stock_return ) VALUES ('".$_SESSION['order_no']."', '".$_SESSION['order_reference']."', '".$order_return_no."', '".$cust_id."', '".$store_item_id."', '".$quantity."', '".$price."', '".$_SESSION['return_date']."', '".$to_return."'  ) ";
execsql($insert_item_query);


if ($to_return=='yes') { 
   $table_name2 = $_SESSION['db_prefix']."_store_items";
   $stock_total_query = "SELECT quantity FROM $table_name2 WHERE store_item_id = '".$store_item_id."'";
   $stock_total_result = mySQL_query($stock_total_query);
   $total_quantity = mysql_result($stock_total_result ,0, "quantity");
   $total_quantity = $total_quantity + $quantity;
      
   $stockQuery = "UPDATE $table_name2 SET quantity = '".$total_quantity."' , lastModDate = '".$_SESSION['return_date']."' WHERE store_item_id = '".$store_item_id."'";
   $stockResult = mySQL_query($stockQuery);
   }
   else {
	 $total_quantity=0;
   }
   
	 
$table_name = $_SESSION['db_prefix']."_order_lines";
$update_query = "UPDATE $table_name SET returned = returned + '".$quantity."' WHERE order_reference = '".$_SESSION['order_reference']."' AND store_item_id = '".$store_item_id."'";
execsql($update_query);		

$table_name2 = $_SESSION['db_prefix']."_item_history";	
$historyQuery = "INSERT INTO $table_name2 (store_item_id, entry, reference, reference_2, reference_3, quantity, total, price, date, entryDate, userId) VALUES ('".$store_item_id."', '18', '".$_SESSION['order_no']."','".$order_return_id."', '".$order_return_no."', '".$quantity."', '".$total_quantity."', '".$price."', '".$_SESSION['return_date']."','".$entryDate."','".$_SESSION['username']."')";
$historyResult = mySQL_query($historyQuery);


$i++;
}
	
$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']."', '27', '".$order_return_no."', '".$_SESSION['order_no']."' ,''".$_SESSION['order_reference']."'','' )";
$trackingResult = mySQL_query($trackingQuery);	


$table_name = $_SESSION['db_prefix']."_order_return_lines_temp";
$delete2_query = "DELETE FROM $table_name WHERE  user_name =  '".$_SESSION['username']."' "; 
execsql($delete2_query);

	
	if (isset($_GET['new_button']) )
	   {
		 $action='returnWiz0.php';
	   }
		 
	if (isset($_GET['finish_button']) )
	  {
		$action='order_return.php?order_return_no='.$order_return_no;
	  }
	
 	 $_SESSION['order_no'] = '';
	 $_SESSION['cust_name'] = '';
  	 $_SESSION['order_reference'] = '';	
	 $_SESSION['additional_note'] = '';	
     $_SESSION['return_date']  = date("Y-m-d");
	 	 
	 $_SESSION['authorised_by' ]= '';
	 $_SESSION['received_by'] = '';
	 
	 $_SESSION['store_item_id'] = '';
	 $_SESSION['store_item_name'] = '';
	 
	 $_SESSION['purchase_line_id'] = '';
	 $_SESSION['sup_name'] = '';
	 $_SESSION['total'] = '';
	 
	 $_SESSION['value'] = '';
	 

	
?>

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

