<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<?php
include ("../navigation/header.php");
include ("../navigation/store_sidebar.php");

	$s = $_GET['s'];
	$p = $_GET['p'];
	$_SESSION['p']=$p;
	$_SESSION['s']=$s;
	
	
	if (isset($_GET['prev_button']) )
	   {
		 $_SESSION['p']= $_SESSION['p']-1;
	   }
		 
	if (isset($_GET['next_button']) )
	  {
		$_SESSION['p']=$_SESSION['p']+1;
	  }

	if ($s==1) {
	  	$_SESSION['order_reference'] = $_GET['order_no_select'];	
	   	$_SESSION['return_date'] = $_GET['return_date'];
		$_SESSION['authorised_by' ]= $_GET['authorised_by_select'];	
		$_SESSION['received_by'] = $_GET['issued_to_select'];	
		
		$table_name = $_SESSION['db_prefix']."_orders";
		$order_query = "SELECT order_no, cust_id FROM $table_name WHERE order_reference = '".$_SESSION['order_reference']."' ";
		$order_result = $con->query($order_query);
		$row = mysqli_fetch_assoc($order_result);
		$_SESSION['order_no'] = $row["order_no"];
		$cust_id = $row["cust_id"];
		
		$table_name2 = $_SESSION['db_prefix']."_customer";
				$customer_query = "SELECT account_type, title, name_2, comp_name FROM $table_name2 WHERE cust_id ='".$cust_id."' ";
				$customer_result = $con->query($customer_query);
				$row = mysqli_fetch_assoc($customer_result);
				$account_type = $row["account_type"];
								
				if ($account_type=="corporate") { 
					$_SESSION['cust_name'] = $row["comp_name"];
				} 
				else {
					$_SESSION['cust_name'] = $row["title"]." ".$row["name_2"]." ".$row["comp_name"];
 				}	
				
	};
	
if($s==2) {
		$table_name = $_SESSION['db_prefix']."_order_return_lines_temp";
		$delete_query = "DELETE FROM $table_name WHERE  user_name = '".$_SESSION['username']."'   "; 
		$delete_result = $con->query($delete_query);
		
$line_num = $_GET['line_num'];

$i=0;
while ($i<$line_num) {
if (isset($_GET['line_checkbox'.$i])) {
$product_item_id =	$_GET['category'.$i];

$table_name = $_SESSION['db_prefix']."_product_store";
$item_query = "SELECT mix_design_id FROM $table_name WHERE product_item_id = '".$product_item_id."' ";
$item_result = $con->query($item_query);
$row = mysqli_fetch_assoc($item_result);
$mix_design_id = $row["mix_design_id"];

$table_name = $_SESSION['db_prefix']."_product_store";
$recycle_item_query = "SELECT product_item_id FROM $table_name WHERE mix_design_id  = '".$mix_design_id."' AND subCategory = 'Production' ";
$recycle_item_result = $con->query($recycle_item_query);
$row = mysqli_fetch_assoc($recycle_item_result);
$recycle_product_item_id = $row['product_item_id'];



if (isset($_GET['return_checkbox'.$i])) {$to_return='yes';} else {$to_return='no';}

$quantity = $_GET['quantity_text'.$i];

	$table_name = $_SESSION['db_prefix']."_order_lines";
	$delivery_query = "SELECT SUM(delivered-returned) AS summed_delivered, order_line_id  FROM $table_name WHERE order_reference = '".$_SESSION['order_reference']."' AND product_item_id = '".$product_item_id."' ";
	$delivery_result = $con->query($delivery_query);
	$row = mysqli_fetch_assoc($delivery_result);
	$delivery = $row["summed_delivered"];
	$order_line_id = $row["order_line_id"];
					
					
if 	($quantity>$delivery) { $quantity = $delivery; $error=1; } else {$error=0;}


if(isset($_GET['line_checkbox'.$i]) ) {
$table_name = $_SESSION['db_prefix']."_order_return_lines_temp";
$check_query = "SELECT product_item_id FROM $table_name WHERE product_item_id = '".$product_item_id."' ";
$check_result = $con->query($check_query);
$check_num = mysqli_num_rows($check_result);
	
if ($check_num==0) {
$table_name = $_SESSION['db_prefix']."_order_return_lines_temp";
$issued_query = "INSERT INTO $table_name ( product_item_id, recycle_product_item_id, quantity, user_name, error, to_return, order_line_id) VALUES ('".$product_item_id."', '".$recycle_product_item_id."', '".$quantity."', '".$_SESSION['username']."', '".$error."', '".$to_return."', '".$order_line_id."' ) ";
$issued_result = $con->query($issued_query);
}
else {
$error=2;
}
}
$i++;
}
}
}
	if($s==3) {
	   
	 
	};
	
	if($s==4) {
	 
	}
	
	?>

<!--style sheets-->
<link rel="stylesheet" type="text/css" href="../css/wizard.css" />

<link href="../css/wait.css" rel="stylesheet" type="text/css">
<link rel="stylesheet" href="../window/dhtmlwindow.css" type="text/css" />
<link rel="stylesheet" href="../window/modal.css" type="text/css" />

<!--jscript-->
<script type="text/javascript" src="../window/dhtmlwindow.js"></script>
<script type="text/javascript" src="../window/modal.js"></script>
<script language="JavaScript" src="../jscript/gen_validatorv4.js"  type="text/javascript" xml:space="preserve"></script>
<script type="text/javascript" src="../jscript/jquery.1.4.2.js"></script>
<script language="JavaScript" src="../jscript/cancel_confirmation.js"  type="text/javascript"></script>
<script language="JavaScript" src="../jscript/confirm_exit.js"  type="text/javascript"></script>
<script language="JavaScript" src="../jscript/show_hide_layers.js"  type="text/javascript"></script>
<script language="JavaScript" src="../jscript/alert_message.js"  type="text/javascript"></script>

<div class="main_frame">

<div class="page_head_frame">
          <label class="label_page_head">Returns from order no: </label>
          &nbsp;&nbsp;
          <label class="label_page_head_2"><?php echo $_SESSION['order_no'].'&nbsp;&nbsp;'.$_SESSION['cust_name'] ?></label>
        </div>
        <br /><br /><br /><br />
        <table >
     	<tr>
      	<td >
        <div class="wizard_paging_frame">
        <table align="left">
          <tr>
            <td><?php if ($_SESSION['p']==1) { ?>
              <div class="active_step_frame">
                <div class="active_step_head_label">&nbsp;1</div></div>
                <div class="active_step_label">
                  Customer</div>
              <?php } else { ?>
              <div class="de_active_step_frame">
                <div class="de_active_step_head_label">&nbsp;1</div></div>
                <div class="de_active_step_label">
                  Customer</div>
              <?php } ?></td>
            <td><?php if ($_SESSION['p']==2) { ?>
              <div class="active_step_frame">
                <div class="active_step_head_label">&nbsp;2</div></div>
                <div class="active_step_label">
                  Items</div>
             <?php } else { ?>
              <div class="de_active_step_frame">
                <div class="de_active_step_head_label">&nbsp;2</div></div>
                <div class="de_active_step_label">
                  Items</div>
            <?php } ?></td>
            <td><?php if ($_SESSION['p']==3) { ?>
              <div class="active_step_frame">
                <div class="active_step_head_label">&nbsp;3</div></div>
                <div class="active_step_label">
                  Finish</div>
             <?php } else { ?>
              <div class="de_active_step_frame">
                <div class="de_active_step_head_label">&nbsp;3</div></div>
                <div class="de_active_step_label">
                  Finish</div>
              <?php } ?></td>
          </tr>
        </table>
        </div>
        </td>
       </tr>
    <tr>
    <td>
        
        <?php 
	if ($_SESSION['p']==1) { include("returnWiz1.php"); }
	if ($_SESSION['p']==2) { include("returnWiz2.php"); }
	if ($_SESSION['p']==3) { include("returnWiz4.php"); }
	
    ?></td>
    </tr>
  </table>
</div>


<?php include ('../navigation/side_bar.php'); ?>


