<!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) {

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

include('../include/period_select_date.php');

$table_name2 = $_SESSION['db_prefix']."_deliver_product";
$deliver_query = "SELECT delivery_no, order_no, order_reference, cust_id, deliver_date, delivery_address FROM $table_name2 WHERE  deliver_date BETWEEN '".$start."' AND '".$end."'  ORDER BY deliver_date DESC ";
$deliver_result = $con->query($deliver_query);
$deliver_num = mysqli_num_rows($deliver_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">Delivered Products</label>
  </div>

<div class="page_middle_frame">   

<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": "400px",
					"bPaginate": false,
					"bScrollCollapse": true,
					"aaSorting": [[ 0, "desc" ]],
					"aoColumns": [
								{"iDataSort": 6},
								{"iDataSort": 5},
								{"iDataSort": 7},
								{"bSortable": true},
								{"bSortable": true},
								{"bVisible": false},
								{"bVisible": false},
								{"bVisible": false}
							 ]
				} );
			} );
		</script>
       
        
<?php
$page='product_delivered_list.php';
$reference_name='';
$reference='';
$reference_name_2='';
$reference_2='';
include('../include/period_select_block_2.php');
?>

<div class="page_inside_frame">            
          <table width="671" id="scroll_table" class="data_display" >
            <thead>
            <tr>
              <th align="center" >Del note<br />no</th>
              <th >Delivery date</th>
              <th >Order no</th>
              <th >Name</th>
              <th >Delivered to</th>
              <th width="27">&nbsp;</th>
              <th width="27">&nbsp;</th>
              <th width="27">&nbsp;</th>
              </tr>
            </thead>
            <?php 

$a7tot=0;
$price=0;
$all_total = 0;
while($row = $deliver_result->fetch_assoc()) {
		$a1 = $row["delivery_no"];
		$a2 = $row["cust_id"];
		$a4 = $row["order_no"];
		$a4a = $row["order_reference"];
		$a5 = $row["deliver_date"];
		$a6 = $row["delivery_address"];
		
		
	     
	     $table_name3 = $_SESSION['db_prefix']."_customer";
         $customer_query = "SELECT comp_name, status, bag_box, box_no, postal_place, phone, contact_1 FROM $table_name3 WHERE cust_id = '".$a2."' ";
		 $customer_result = $_result = $con->query($customer_query);
		 $customer_num = mysqli_num_rows($customer_result);
		 if ($customer_num!=0) {
			   	$row = mysqli_fetch_assoc($customer_result);
				$a2a = $row["comp_name"];
				$a2b = $row["bag_box"];
		 		$a2c = $row["box_no"];
		 		$a2d = $row["postal_place"];
		 		$a2e = $row["phone"];
		 		$a2f = $row["contact_1"];
		 		$a3 = $row["status"];
		 
		 }
	
	$table_name = $_SESSION['db_prefix']."_deliver_product_lines";
	$line_query = "SELECT  product_item_id, quantity  FROM $table_name WHERE delivery_no = '".$a1."' ";
	$line_result = $con->query($line_query);
	$line_num = mysqli_num_rows($line_result);
		
	$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></tr>";
	}
		
	$table_name = $_SESSION['db_prefix']."_order_lines";
	$order_query = "SELECT product_item_id, quantity, delivered FROM $table_name WHERE order_reference = '".$a4a."' ";
	$order_result = $con->query($order_query);
	
	$order_total=0;
	$order_all_total=0;
	$order_line_text="";
	while($row = $order_result->fetch_assoc()) {
			$product_item_id = $row["product_item_id"];
			$order_quantity = $row["quantity"];
			$order_delivered = $row["delivered"];

	
			$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'];

			
			$sub_total =  $order_quantity * $price;
			$total = $total + $sub_total;
	
	$balance = $order_quantity - $order_delivered;
	
	$order_line_text = $order_line_text."<tr><td>".$product_item_name."</td><td>".$order_quantity."</td><td>".$order_delivered."</td><td>".$balance."</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>";
	
	$deliverTT ="<table id=table_tool_tip width=250 ><tr><td>Item</td><td>Quantity</td></tr>".$line_text."<tr><td>&nbsp;</td><td>&nbsp;</td></tr></table><br><b>Click for details</b>";
	
	$orderTT ="<table id=table_tool_tip width=250 ><tr><td>Item</td><td>Quantity</td><td>Delivered</td><td>Balance</td></tr>".$order_line_text."<tr><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td></tr></table><br><b>Click for details</b>";
	
	?>
            <tr>               
              <td width="58"align="left"  style="padding-left:0px;"><a href="deliver_product_display.php?delivery_no=<?php echo $a1 ?> " onmouseover="ddrivetip('<?php echo $deliverTT?>', 250)"; onmouseout="hideddrivetip()"><?php echo $a1 ?></a></td>
              <td width="92" align="left"><?php echo date("d-m-Y", strtotime($a5))?></td>
              <td width="63" align="left"><a href="order.php?order_reference=<?php echo $a4a ?>" onmouseover="ddrivetip('<?php echo $orderTT?>', 250)"; onmouseout="hideddrivetip()"><?php echo $a4 ?></a></td>
              <td width="139" align="left"><a href="account_details.php?cust_id=<?php echo $a2 ?>" onmouseover="ddrivetip('<?php echo $customerTT?>', 250)"; onmouseout="hideddrivetip()"><?php echo $a2a ?></a></td>
              <td width="264" align="left"><?php echo $a6 ?></td>
              <td align="left"><?php echo $a5 ?></td>
              <td align="left"><?php echo $a1 ?></td>
              <td align="left"><?php echo $a4 ?></td>
            </tr>
            <?php
}
?>
            <tfoot>
            </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?date=";	?>
<body onLoad="accessdenied(); return false">
</body>
<?php }
?>

