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

$page_name = "Stores-main";
include ("../navigation/access.php");

if ($permission_num==1) {
	
$cust_id = $_GET['cust_id'];
if ($cust_id!='All') {
$table_name = $_SESSION['db_prefix']."_customer";
$comp_query = "SELECT comp_name  FROM $table_name WHERE cust_id = '".$cust_id."' ";
$comp_result = $con->query($comp_query);
$row = mysqli_fetch_assoc($comp_result);
$comp_name = $row['comp_name'];
}
else {
$comp_name = 'All';
}

include ("../navigation/store_sidebar.php");
include('../include/period_select_date.php');

$table_name = $_SESSION['db_prefix']."_customer";
$cust_query = "SELECT  cust_id, comp_name FROM $table_name ORDER BY comp_name ASC";
$cust_result = $con->query($cust_query);
$cust_num = mysqli_num_rows($cust_result);
	$w = 0;
	$arrId4 = array ();
	$arrName4 = array ();
	while ($w < $cust_num) {
		$row = mysqli_fetch_assoc($cust_result);
		$arrId4[$w] = $row['cust_id'];
		$arrName4[$w] = $row['comp_name'];
		$w++;
	}

$table_name = $_SESSION['db_prefix']."_pallets";
$table_name2 = $_SESSION['db_prefix']."_orders";
if ($cust_id=='All') {	 	
$display_query = "SELECT order_no, delivery_no, order_reference, quantity, ret_quantity FROM $table_name WHERE (($table_name.quantity-$table_name.ret_quantity)>0)";
}
else {
$display_query = "SELECT $table_name.order_no, $table_name.delivery_no, $table_name.order_reference, $table_name.quantity, $table_name.ret_quantity FROM $table_name, $table_name2 WHERE $table_name2.cust_id = '".$cust_id."' AND $table_name.order_reference = $table_name2.order_reference AND (($table_name.quantity-$table_name.ret_quantity)>0)";
}
$display_result = $con->query($display_query);
$display_num = mysqli_num_rows($display_result);

?>
<!--style sheets-->
<link href="../css/main.css" rel="stylesheet" type="text/css">
<link rel="stylesheet" type="text/css" href="../css/scroll_table.css" />
       
</head>

<body>

<div class="main_frame">
<div class="page_head_frame">
    <label class="label_page_head">Outstanding pallets</label></div>


<div class="page_middle_frame">


<div class="page_inside_frame">
<div style="float:left;">
Select single customer:
<form action="costing_tiles_list.php" method="get" name="form_2">
<select name="customer_select" autofocus class="selectbox300" id="customer_select" onchange="window.location.href= this.form.customer_select.options[this.form.customer_select.selectedIndex].value">
  <option value="<?php echo $cust_id ?>" selected="selected" ><?php echo $comp_name ?></option>
  <option value="outstanding_pallets_list.php?cust_id=All&date=1&date1=<?php echo $start ?>&date2=<?php echo $end ?>">All</option>
  <?php
    	for ($j = 0; $j < count($arrId4); $j++) {
 ?>			
  <option value="outstanding_pallets_list.php?cust_id=<?php echo $arrId4[$j] ?>&date=1&date1=<?php echo $start ?>&date2=<?php echo $end ?>"><?php echo $arrName4[$j] ?></option>	
 <?php }
     ?>
</select>
</form>
</div>

<script type="text/javascript" language="javascript" src="../jscript/jquery.dataTables.js"></script>
<script type="text/javascript" charset="utf-8">
			$(document).ready(function() {
				$('#scroll_table').dataTable( {
					"sScrollY": "500px",
					"bPaginate": false,
					"bScrollCollapse": true,
					"aaSorting": [[ 0, "desc" ]],
					"aoColumns": [
								{"iDataSort": 1},
								{"bVisible": false},
                             	{"iDataSort": 3},
								{"bVisible": false},
								{"iDataSort": 5},
								{"bVisible": false},
								{"iDataSort": 7},
								{"bVisible": false},
								{"bSortable": true},
								{"bSortable": true},
								{"bSortable": true}
								 ]
				} );
			} );
		</script>
       
   
          
          <table width="662" id="scroll_table" class="data_display"  >
	<thead>
		<tr>
		  <th width="63">Order no</th>
          <th width="34">&nbsp;</th>
		  <th width="34">Delivery date</th>
          <th width="34">&nbsp;</th>
          <th width="34">Delivery no</th>
          <th width="34">&nbsp;</th>
		  <th width="200">Customer</th>
          <th width="34">&nbsp;</th>
		  <th align="center" width="70">Qty delivered</th>
          <th align="center" width="70">Qty returned</th>
          <th align="center" width="70" >Qty outstanding</th>
		</tr>
	</thead>
	<tbody>
  <?php
    $b9=0;
	$line_text="";
	$total_quantity = '';
	$total_ret_quantity = '';
	$total_balance = '';
	while($row = $display_result->fetch_assoc()) {
			$b1 = $row["order_no"];
			$b1a = $row["order_reference"];
			$b3 = $row["delivery_no"];
			$b4 = $row["quantity"];
		 	$b5 = $row["ret_quantity"];
		 
		 
		 	$table_name = $_SESSION['db_prefix']."_orders";
			$order_query = "SELECT cust_id, order_date, pallets FROM $table_name WHERE order_reference = '".$b1a."'  ";
			$order_result = $con->query($order_query);
			$row = mysqli_fetch_assoc($order_result);
			$b2 = $row['order_date'];
			$a2 = $row['cust_id'];
			$a15 = $row['pallets'];
						
			$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 = '".$a2."' ";
			$customer_result = $con->query($customer_query);
			$customer_num = mysqli_num_rows($customer_result);
			if ($customer_num!=0) {
				  	$row = mysqli_fetch_assoc($customer_result);
					$type = $row['account_type'];

			if ($type=="corporate") { 
				$a2a = $row["comp_name"]; 
			} 
			else {
				$a2a = $row["title"]." ".$row["name_2"]." ".$row["comp_name"];
				$a2aa = $row["comp_name"];
 			}	
			$a2b = $row["bag_box"];
			$a2c = $row["box_no"];
			$a2d = $row["postal_place"];
			$a2e = $row["phone"];
			$a2f = $row["cell_1"];
			$a2g = $row["status"];
			$a2h="";
			}
			else {
			$a2a = '';
			$a2b = '';
			$a2c = '';
			$a2d = '';
			$a2e = '';
			$a2f = '';
			$a2g = '';
			$a2h="";
			}
		 
		 $b6 = $b4-$b5;	
		 
	$balance = $b4 - $b5;
	$total_quantity = $total_quantity + $b4;
	$total_ret_quantity = $total_ret_quantity + $b5;
	$total_balance = $total_quantity - $total_ret_quantity;	 
		 
	$table_name = $_SESSION['db_prefix']."_order_lines";
	$line_query = "SELECT  product_item_id, quantity  FROM $table_name WHERE order_reference = '".$b1a."' ";
	$line_result = $con->query($line_query);
	
	$total=0;
	$line_text="";
			
	while($row = $line_result->fetch_assoc()) {
		$product_item_id = $row["product_item_id"];
		$quantity = $row["quantity"];
	
		$table_name = $_SESSION['db_prefix']."_product_store";
		$name_query = "SELECT item_name FROM $table_name WHERE product_item_id = '".$product_item_id."'  ";
		$name_result = $con->query($name_query);
		$row = mysqli_fetch_assoc($name_result);
		$product_item_name = $row['item_name'];
			  
	   	$line_text = $line_text."<tr><td>".$product_item_name."</td><td>".$quantity."</td><td align=right>--</td><td align=right>--</td></tr>";
	}
		 
		 $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><b>Click for details</b>";
		 
		 $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 for details</b>";	
		 
		 $table_name = $_SESSION['db_prefix']."_deliver_product";
		 $pallets_query = "SELECT pallets FROM $table_name WHERE delivery_no = '".$b3."'  ";
		 $pallets_result = $con->query($pallets_query);
		 $row = mysqli_fetch_assoc($pallets_result);
		 $pallets_del = $row['pallets'];
		 	 
		 
		 $table_name = $_SESSION['db_prefix']."_deliver_product_lines";
		 $delivery_query = "SELECT product_item_id, quantity FROM $table_name WHERE delivery_no = '".$b3."'  ";
		 $delivery_result = $con->query($delivery_query);
		 
		 $deliveryTT_line = '';
		 while($row = $delivery_result->fetch_assoc()) {
			$product_item_id_del = $row["product_item_id"];
			$quantity_del = $row["quantity"];

		 $table_name = $_SESSION['db_prefix']."_product_store";
		 $name_query = "SELECT item_name FROM $table_name WHERE product_item_id = '".$product_item_id_del."'  ";
		 $name_result = $con->query($name_query);
		 $row = mysqli_fetch_assoc($name_result);
		 $store_item_name_del = $row['item_name'];

		 $deliveryTT_line = $deliveryTT_line.'<tr><td >'.$store_item_name_del.'</td><td>'.$quantity_del.'</td></tr>';
	}
		 
		 $deliveryTT = 'Delivery no: '.$b3.'<br><table><tr><td width=450>Item</td><td width=200>Quantity</td></tr>'.$deliveryTT_line.'<tr><td>Pallets</td><td>'.$pallets_del.'</td></tr></table><br><b>Click for details.</b>';
		 		
		?>
		<tr >
		  <td ><a href="order.php?order_reference=<?php echo $b1a ?> " onmouseover="ddrivetip('<?php echo $orderTT?>', 450)"; onmouseout="hideddrivetip()"><?php echo $b1 ?></a></td>
          <td ><?php echo $b1 ?></td>
		  <td ><?php echo date("d-m-Y", strtotime($b2)) ?></td>
          <td ><?php echo $b2 ?></td>
          <td ><a href="tile_deliver_display.php?delivery_no=<?php echo $b3 ?>" onMouseover="ddrivetip('<?php echo $deliveryTT?>', 300)"; onMouseout="hideddrivetip()"><?php echo $b3 ?></a></td>
          <td ><?php echo $b3 ?></td>
		  <td ><a href="account_details.php?cust_id=<?php echo $a2 ?>" onmouseover="ddrivetip('<?php echo $customerTT?>', 250)"; onmouseout="hideddrivetip()"><?php echo $a2a ?></a></td>
          <td ><?php echo $a2aa ?></td>
			<td align="center"><?php echo $b4?></td>
			<td align="center"><?php echo $b5?></td>
			<td align="center"><?php echo $b6?></td>
		</tr>
        
		<?php
	}
	
	?>
	</tbody>

<tfoot>
<tr>
    <td colspan="8" 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>
</div>
</div>



<?php include ('../navigation/side_bar.php'); ?>
</body>
<?php } else { ?>
<script> needToConfirm = false; </script>
<?php
    $_SESSION['denied_return'] = "store/store_main.php?branch=$date=";	?>
<body onLoad="accessdenied(); return false">
</body>
<?php }
?>

