<?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 = $con->query($add_supply_query);
$add_supply_num = mysqli_num_rows($add_supply_result);
if ($add_supply_num!=0) {
$row = mysqli_fetch_assoc($add_supply_result);
$order_return_no = $row["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']."' ) ";
$return_result = $con->query($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 = $con->query($supply_id_query);
$row = mysqli_fetch_assoc($supply_id_result);
$order_return_id = $row["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 = $con->query($items_query);

$i=0;
while($row = $items_result->fetch_assoc()) {
	$product_item_id = $row["product_item_id"];
	$recycle_product_item_id = $row["recycle_product_item_id"];
	$quantity = $row["quantity"];
    $price = $row["price"];
    $to_return = $row["to_return"];
      
    
   $table_name4 = $_SESSION['db_prefix']."_order_return_lines";  
   $insert_item_query = "INSERT INTO $table_name4 (order_no, order_reference, order_return_no, cust_id, product_item_id, quantity, price, issue_date, stock_return ) VALUES ('".$_SESSION['order_no']."', '".$_SESSION['order_reference']."', '".$order_return_no."', '".$cust_id."', '".$product_item_id."', '".$quantity."', '".$price."', '".$_SESSION['return_date']."', '".$to_return."'  ) ";
$insert_item_result = $con->query($insert_item_query);


if ($to_return=='yes') { 
   $table_name2 = $_SESSION['db_prefix']."_product_store";      
   $recycle_query = "UPDATE $table_name2 SET recycle_qty = recycle_qty + '".$quantity."' WHERE product_item_id = '".$recycle_product_item_id."'";
   $recycle_result = $con->query($recycle_query);
   
   $table_name = $_SESSION['db_prefix']."_product_store";
   $recycle_stock_query = "SELECT recycle_qty FROM $table_name WHERE product_item_id = '".$recycle_product_item_id."'";
   $recycle_stock_result = $con->query($recycle_stock_query);
   $row = mysqli_fetch_assoc($recycle_stock_result);
   $recycle_stock = $row['recycle_qty'];
   
   $table_name2 = $_SESSION['db_prefix']."_recycle_history";	
   $recycle_history_query = "INSERT INTO $table_name2 (product_item_id, batch_code, base_item_id, entry, recycle_no, recycle_stock, date, recycle_reason, recycle_notes, entryDate, userId) VALUES ('".$product_item_id."', 'Return from order', '".$recycle_product_item_id."', '11', '".$quantity."', '".$recycle_stock."', '".$_SESSION['return_date']."',  'Order returned', 'Return no: ".$order_return_no."', '".$entryDate."','".$_SESSION['username']."')";
   $recycle_history_result = $con->query($recycle_history_query);
   }
      
	 
$table_name = $_SESSION['db_prefix']."_order_lines";
$update_query = "UPDATE $table_name SET returned = returned + '".$quantity."' WHERE order_reference = '".$_SESSION['order_reference']."' AND product_item_id = '".$product_item_id."'";
$update_result = $con->query($update_query);

//$table_name2 = $_SESSION['db_prefix']."_product_history";	
//$history_query = "INSERT INTO $table_name2 (product_item_id, entry, reference, reference_2, reference_3, quantity, total, price, date, entryDate, userId) VALUES ('".$product_item_id."', '18', '".$_SESSION['order_no']."','".$order_return_id."', '".$order_return_no."', '".$quantity."', '', '".$price."', '".$_SESSION['return_date']."','".$entryDate."','".$_SESSION['username']."')";
//$history_result = $con->query($history_query);


$i++;
}
	
$table_name = $_SESSION['db_prefix']."_tracking_store";
$tracking_query = "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']."','' )";
$tracking_result = $con->query($tracking_query);


$table_name = $_SESSION['db_prefix']."_order_return_lines_temp";
$delete2_query = "DELETE FROM $table_name WHERE  user_name =  '".$_SESSION['username']."' "; 
$delete2_result = $con->query($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['product_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>

