<!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 = mySQL_query($order_query);
		$_SESSION['order_no'] = mysql_result($order_result, 0, "order_no");	
		$cust_id = mysql_result($order_result, 0, "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 = mySQL_query($customer_query);
				$customer_num = mysql_num_rows($customer_result);	
				$account_type = mysql_result($customer_result, 0, "account_type");
				if ($account_type=="corporate") { 
					$_SESSION['cust_name'] = mysql_result($customer_result, 0, "comp_name"); 
				} 
				else {
					$_SESSION['cust_name'] = mysql_result($customer_result, 0, "title")." ".mysql_result($customer_result, 0, "name_2")." ".mysql_result($customer_result, 0, "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']."'   "; 
		execsql($delete_query);
		
$line_num = $_GET['line_num'];

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


//$store_item_id = $category;
//$store_reg_id = strstr($category, '|', true);
//$store_item_id = ltrim(strstr($category, '|'), '| ');

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";
					////if ($store_reg_id!=0) {
//					$delivery_query = "SELECT SUM(delivered) AS summed_delivered, delivered_price, store_reg_id  FROM $table_name WHERE job_card_no = '".$_SESSION['job_card_no']."' AND store_item_id = '".$store_item_id."'  AND store_reg_id = '".$store_reg_id."' ";
//					}
//					else {
					$delivery_query = "SELECT SUM(delivered-returned) AS summed_delivered, order_line_id  FROM $table_name WHERE order_reference = '".$_SESSION['order_reference']."' AND store_item_id = '".$store_item_id."' ";
					
					//}
					$delivery_result = mySQL_query($delivery_query);
					$delivery_num = mysql_num_rows($delivery_result);
					$delivery = mysql_result($delivery_result, 0, "summed_delivered");
					//$price = mysql_result($delivery_result, 0, "delivered_price");
					$order_line_id  = mysql_result($delivery_result, 0, "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 store_item_id FROM $table_name WHERE store_item_id = '".$store_item_id."' ";
$check_result = mySQL_query($check_query);
$check_num = mysql_num_rows($check_result);
	
if ($check_num==0) {
$table_name = $_SESSION['db_prefix']."_order_return_lines_temp";
$issued_query = "INSERT INTO $table_name ( store_item_id, quantity, user_name, error, to_return, order_line_id) VALUES ('".$store_item_id."','".$quantity."', '".$_SESSION['username']."', '".$error."', '".$to_return."', '".$order_line_id."' ) ";
execsql($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'); ?>


