<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />


<?php


if ($new=='0') {
$table_name = $_SESSION['db_prefix']."_pallets";
$pallets_query = "SELECT order_no, SUM(quantity) AS summed_quantity, SUM(ret_quantity) AS summed_ret_quantity FROM $table_name WHERE branch = 'Francistown' GROUP BY order_no HAVING(summed_quantity-summed_ret_quantity) > 0 ";
$pallets_result = mySQL_query($pallets_query);
$pallets_num = mysql_num_rows($pallets_result);

}


if ($new=='1') {
		$table_name = $_SESSION['db_prefix']."_tile_orders";
		$cust_id_query = "SELECT cust_id FROM $table_name WHERE order_no = '".$order_no."' ";
		$cust_id_result = mySQL_query($cust_id_query);
		$cust_id = mysql_result($cust_id_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") { 
				$cust_name = mysql_result($customer_result, 0, "comp_name"); 
			} 
		else {
				$cust_name = mysql_result($customer_result, 0, "title")." ".mysql_result($customer_result, 0, "name_2")." ".mysql_result($customer_result, 0, "comp_name");
 			}
			
		$table_name = $_SESSION['db_prefix']."_pallets";
		$pallets_query = "SELECT * FROM $table_name WHERE order_no = '".$order_no."'  ";
		$pallets_result = mySQL_query($pallets_query);
		$pallets_num = mysql_num_rows($pallets_result);
		
?>
<script language="JavaScript" src="../jscript/alert_message.js"  type="text/javascript"></script>
<script type="text/javascript">
function validate_pallets(input,alerttxt,maxvalue)
{
with (input)
  {
  if (value>maxvalue)
    {
    alertMessage(1, '', 'You are to return more <br> pallets as the balance outstanding.<br>Please enter a new quantity');
	input.value = "";
	return false;
	}
  else
    {
    return true;
    }
  }
}
</script>

<?php		
	
}
?>



<link href="../css/main.css" rel="stylesheet" type="text/css" />
<script language="JavaScript" src="../jscript/gen_validatorv4.js"  type="text/javascript" xml:space="preserve"></script>
<script language="JavaScript" src="../jscript/show_hide_layers.js"  type="text/javascript"></script>
<link rel="stylesheet" type="text/css" href="../css/scroll_table.css" />
<link href="../css/main.css" rel="stylesheet" type="text/css">
<link rel="stylesheet" type="text/css" href="../css/tabs.css" />  
</head>

<body>

<div class="page_middle_frame" style="height:600px;" > 


<br />


<ul class="tabs">
      <li>
          <input type="radio" name="tabs" id="tab1">
          <label class="tab" for="tab1"><a class="tabs" href="pallets_return.php?branch=Gaborone&new=0&order_no=">Gaborone</a></label>
<div id="tab-content1" class="tab-content animated fadeIn" style="height:500px;">

</div>
 </li>
        <li>
          <input type="radio" checked  name="tabs" id="tab2">
          <label  class="tab" for="tab2"><a class="tabs" href="pallets_return.php?branch=Francistown&new=0&order_no=">Francistown</a></label>
<div id="tab-content2" class="tab-content animated fadeIn"> 
 
<?php 
if ($new=='0') { ?>
<div class="page_inside_frame" style="height:400px;"><br />
<div align="left">
<img src="../images/info_green.png" width="16" height="16">
<label class="label_info">Click order number to return pallets on that order.</label>          
</div>
<table  id="table_details_display">
  <thead>
  <tr>
    <td width="85">Order no</td>
    <td width="86">Order date</td>
    <td width="200">Customer</td>
    <td align="center" width="50">Qty delivered</td>
    <td align="center" width="50">Qty returned</td>
    <td align="center" width="50">Qty outstanding</td>
  </tr>
  </thead>
  <tbody>
<?php
$total_quantity = '';
$total_ret_quantity = '';
$total_balance = '';
$i=0;
while ($i<$pallets_num) {
	$order_no = mysql_result($pallets_result, $i, "order_no");	
	$summed_quantity = mysql_result($pallets_result, $i, "summed_quantity");
	$summed_ret_quantity = mysql_result($pallets_result, $i, "summed_ret_quantity");
	
	$table_name = $_SESSION['db_prefix']."_tile_orders";
	$order_query = "SELECT cust_id, order_date, pallets FROM $table_name WHERE order_no = '".$order_no."' ";
	$order_result = mySQL_query($order_query);
	$cust_id = mysql_result($order_result, 0, "cust_id");
	$order_date = mysql_result($order_result, 0, "order_date");
	$a15 = mysql_result($order_result, 0, "pallets");
	
	$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");
	$table_name = $_SESSION['db_prefix']."_customer";
			$customer_query = "SELECT status, account_type, comp_name, name_2, title, bag_box, box_no, postal_place, phone, cell_1  FROM $table_name WHERE cust_id = '".$cust_id."' ";
			$customer_result = mySQL_query($customer_query);
			$customer_num = mysql_num_rows($customer_result);
			if ($customer_num!=0) {
			$type = mysql_result($customer_result, 0, "account_type");
			if ($type=="corporate") { 
				$a2a = mysql_result($customer_result, 0, "comp_name"); 
			} 
			else {
				$a2a = mysql_result($customer_result, 0, "title")." ".mysql_result($customer_result, 0, "name_2")." ".mysql_result($customer_result, 0, "comp_name");
 			}	
			$a2b = mysql_result($customer_result, 0, "bag_box");
			$a2c = mysql_result($customer_result, 0, "box_no");
			$a2d = mysql_result($customer_result, 0, "postal_place");
			$a2e = mysql_result($customer_result, 0, "phone");
			$a2f = mysql_result($customer_result, 0, "cell_1");
			$a2g = mysql_result($customer_result, 0, "status");
			$a2h="";
			}
			else {
			$a2a='';
			}
	
	$balance = $summed_quantity - $summed_ret_quantity;
	$total_quantity = $total_quantity + $summed_quantity;
	$total_ret_quantity = $total_ret_quantity + $summed_ret_quantity;
	$total_balance = $total_quantity - $total_ret_quantity;
	
	$table_name = $_SESSION['db_prefix']."_tile_order_lines";
	$line_query = "SELECT  store_item_id, quantity, purchase_price   FROM $table_name WHERE order_no = '".$order_no."' ";
	$line_result = mySQL_query($line_query);
	$line_num = mysql_num_rows($line_result);
		
	$l=0;
	$total=0;
	$line_text="";
	$products=mysql_result($line_result, 0, "store_item_id");
		
	while($l<$line_num) {
		$store_item_id = mysql_result($line_result, $l, "store_item_id");
		$quantity = mysql_result($line_result, $l, "quantity");
		$price = mysql_result($line_result, $l, "purchase_price");
		
		
		$table_name = $_SESSION['db_prefix']."_store_items";
		$name_query = "SELECT item_name FROM $table_name WHERE store_item_id = '".$store_item_id."'  ";
		$name_result = mySQL_query($name_query);
		$store_item_name = mysql_result($name_result, 0, "item_name");
		$sub_total =  $quantity * $price;
		$total = $total + $sub_total;
			  
	   	$line_text = $line_text."<tr><td>".$store_item_name."</td><td>".$quantity."</td><td align=right>".number_format($price, 2, '.', ',')."</td><td align=right>".number_format($sub_total, 2, '.', ',')."</td></tr>";
	   $l++;
	}
		 
		 $customerTT ="<table id=table_tool_tip width=250 ><tr><td  width=75>Company:</td><td width=175>".$a2a."</td></tr><tr><td>Address:</td><td>".$a2b."&nbsp;".$a2c."<br>".$a2d."</td></tr><tr><td>Phone:</td><td>".$a2e."</td></tr><tr><td>Contact:</td><td>".$a2f."</td></tr></table><br>";
		 
		 $orderTT ="<table id=table_tool_tip width=450 ><tr><td>Item</td><td>Quantity</td><td align=center>Price</td><td align=center>Total</td></tr>".$line_text."<tr><td>Estimated pallets: ".$a15."</td><td>&nbsp;</td><td align=center>&nbsp;</td><td align=center>&nbsp;</td></tr></table><br><b>Click to return pallets</b>";			
		?>
  <tr>
    <td><a href="pallets_return.php?branch=Gaborone&new=1&order_no=<?php echo $order_no ?>" onMouseover="ddrivetip('<?php echo $orderTT ?>', 450)"; onMouseout="hideddrivetip()"><?php echo $order_no ?></a></td>
    <td><?php echo date("d-m-Y", strtotime($order_date)) ?></td>
    <td><a href=" " onMouseover="ddrivetip('<?php echo $customerTT ?>', 250)"; onMouseout="hideddrivetip()"><?php echo $a2a ?></a></td>
    <td align="center"><?php echo $summed_quantity ?></td>
    <td align="center"><?php echo $summed_ret_quantity ?></td>
    <td align="center"><?php echo $balance ?></td>
   </tr>
<?php
$i++;
}
?>
</tbody>
<tfoot>
<tr>
    <td colspan="3" align="right">Totals: &nbsp;</td>
    <td align="center"><?php echo $total_quantity ?></td>
    <td align="center"><?php echo $total_ret_quantity ?></td>
    <td align="center"><?php echo $total_balance ?></td>
    
  </tr>
  </tfoot>
</table>
  

</div>
<?php } else { ?>
<form name="form2" action="pallets_return_gab_save.php" method="get">
<div class="page_inside_frame" style="height:400px;">
Order no: <?php echo $order_no ?>
&nbsp;&nbsp;Customer: <?php echo $cust_name ?>
<br />
<br />
<table  id="table_details_display">
  <thead>
  <tr>
    <td width="85">Order no</td>
    <td width="86">Delivery no</td>
    <td width="106">Delivery date</td>
    <td width="126">Qty delivered</td>
    <td width="126">Qty returned</td>
    <td width="93">Balance</td>
    <td width="93">Return</td>
  </tr>
  </thead>
  <tbody>
<?php
$total_quantity = '';
$total_ret_quantity = '';

$i=0;
while ($i<$pallets_num) {
	$order_no = mysql_result($pallets_result, $i, "order_no");	
	$delivery_no = mysql_result($pallets_result, $i, "delivery_no");
	$del_date = mysql_result($pallets_result, $i, "del_date");
	$quantity = mysql_result($pallets_result, $i, "quantity");
	$ret_quantity = mysql_result($pallets_result, $i, "ret_quantity");
	
	
	$balance = $quantity - $ret_quantity;
	$total_quantity = $total_quantity + $quantity;
	$total_ret_quantity = $total_ret_quantity + $ret_quantity;
	$total_balance = $total_quantity - $total_ret_quantity;

?>
  <tr>
    <td><?php echo $order_no ?></td>
    <td><?php echo $delivery_no ?><input name="delivery_no<?php echo $i ?>" type="hidden" value="<?php echo $delivery_no ?>" /></td>
    <td><?php echo date("d-m-Y", strtotime($del_date)) ?></td>
    <td><?php echo $quantity ?></td>
    <td><?php echo $ret_quantity ?></td>
    <td><?php echo $balance ?></td>
    <td><input type="text" name="pallets_text<?php echo $i?>" id="pallets_text<?php echo $i?>"  class="text50" value="" onfocus = "this.select();" onkeypress="return numbersonly(this, event)" onkeyup="validate_pallets(pallets_text<?php echo $i?>, '', <?php echo $balance ?>)" style="width:50px; text-align:right" autocomplete="off" /></td>
  </tr>
<?php
$i++;
}
?>
</tbody>
<tfoot>
<tr>
    <td colspan="3" align="right">Totals: &nbsp;</td>
    <td><?php echo $total_quantity ?></td>
    <td><?php echo $total_ret_quantity ?></td>
    <td><?php echo $total_balance ?></td>
    <td>&nbsp;</td>
  </tr>
  </tfoot>
</table>
<div align="right"><br />
<input name="line_num" type="hidden" value="<?php echo $pallets_num ?>" /><input name="order_no" type="hidden" value="<?php echo $order_no ?>" />
<input name="new_button" type="submit" class="imagebutton" value="Save &amp; new" />
<input name="close_button" type="submit" class="imagebutton" value="Save &amp; close" />
</div>
</form>
<?php } ?>
<br />

</div>
        </li>
        <li>
          <input type="radio" name="tabs" id="tab3">
          <label class="tab"for="tab3"><a class="tabs" href="pallets_return.php?branch=Combined&new=0&order_no=">Combined</a></label>
           
 <div id="tab-content3" class="tab-content animated fadeIn"> 
 </div>
        </li> 


</ul>
</div>


</body>


</html>