<!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 = mySQL_query($comp_query);
$comp_name = mysql_result($comp_result, 0, "comp_name");
}
else {
$comp_name = 'All';
}

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

$table_name = $_SESSION['db_prefix']."_customer";
$custQuery = "SELECT  cust_id, comp_name FROM $table_name ORDER BY comp_name ASC";
$custResult = execsql($custQuery);
$custNum = mysql_num_rows($custResult);
	$w = 0;
	$arrId4 = array ();
	$arrName4 = array ();
	while ($w < $custNum) {
		$arrId4[$w] = mysql_result($custResult, $w, 'cust_id');
		$arrName4[$w] = mysql_result($custResult, $w, '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) AND $table_name.del_date > '2020-11-30' ";
}
else {
$display_query = "SELECT $table_name.order_no, $table_name.delivery_no, $table_name.order_reference, $table_name.del_date, $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) AND $table_name.del_date > '2020-11-30' ";
}
$display_result = execsql($display_query);
$i = 0;
$display_num = mysql_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": [[ 3, "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 = '';
	$i=0;
	while($i < $display_num){
		 $b1 = mysql_result($display_result, $i, "order_no");
		 $b1a = mysql_result($display_result, $i, "order_reference");
		 
		 	$table_name = $_SESSION['db_prefix']."_orders";
			$order_query = "SELECT cust_id, order_date, pallets FROM $table_name WHERE order_reference = '".$b1a."'  ";
			$order_result = mySQL_query($order_query);
			$b2 = mysql_result($order_result, 0, "order_date");
		    $a2 = mysql_result($order_result, 0, "cust_id");
			$a15 = mysql_result($order_result, 0, "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 = 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");
				$a2aa = 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='';
			}
		 $b3 = mysql_result($display_result, $i, "delivery_no");	
		 $b4 = mysql_result($display_result, $i, "quantity");
		 $b5 = mysql_result($display_result, $i, "ret_quantity");
		 $b6 = $b4-$b5;	
		 $b7 = mysql_result($display_result, $i, "del_date");
		 
		 
	$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  store_item_id, quantity  FROM $table_name WHERE order_reference = '".$b1a."' ";
	$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");
				
		$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");
		
			  
	   	$line_text = $line_text."<tr><td>".$store_item_name."</td><td>".$quantity."</td><td align=right>--</td><td align=right>--</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><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']."_tile_deliver";
		 $pallets_query = "SELECT pallets FROM $table_name WHERE delivery_no = '".$b3."'  ";
		 $pallets_result = mySQL_query($pallets_query);
		 $pallets_del = mysql_result($pallets_result, 0, "pallets");
		 
		 
		 $table_name = $_SESSION['db_prefix']."_tile_deliver_lines";
		 $delivery_query = "SELECT store_item_id, quantity FROM $table_name WHERE delivery_no = '".$b3."'  ";
		 $delivery_result = mySQL_query($delivery_query);
		 $delivery_num = mysql_num_rows($delivery_result);
		 $deliveryTT_line = '';
		 $d=0;
		 while ($d<$delivery_num) {
		 $store_item_id_del = mysql_result($delivery_result, $d, "store_item_id");
		 $quantity_del = mysql_result($delivery_result, $d, "quantity");
		 
		 $table_name = $_SESSION['db_prefix']."_store_items";
		 $name_query = "SELECT item_name FROM $table_name WHERE store_item_id = '".$store_item_id_del."'  ";
		 $name_result = mySQL_query($name_query);
		 $store_item_name_del = mysql_result($name_result, 0, "item_name");
		 
		 $deliveryTT_line = $deliveryTT_line.'<tr><td >'.$store_item_name_del.'</td><td>'.$quantity_del.'</td></tr>';
		 
		 $d++;
		 }
		 
		 $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($b7)) ?></td>
          <td ><?php echo $b7 ?></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
		$i++;
		  
	}
	
	?>
	</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 }
?>

