<?php
include("../include/session.php");
if( !$session->isAdmin() && !$session->isRadstoneReceptionUser()){
	//header("Location: ../Store/storeMenu.php");?>
	<script language="javascript" type="text/javascript">
        <!--
		  window.location="denied.php";
        -->
       </script>
	   <?php 
}
else {
	<?php
     
     session_start();
	 $_SESSION['store'] ="";
	 $_SESSION['sub_cat'] ="";
     $_SESSION['productId'] ="";
	 $_SESSION['productName'] ="";
	 $_SESSION['accountNo'] ="";
     $_SESSION['supplierName'] = "";
	 $_SESSION['deliveryNote'] = "";
	 $_SESSION['quantity'] = "";
	 $_SESSION['price'] = "";
	 $_SESSION['receiveDate']  = date('Y-m-d');
	 $_SESSION['p'] = 1;
     $_SESSION['error']=0;
	 $_SESSION['changed']="";
	 $_SESSION['changed2']="";
	 	 
	include("../storeHeader.php");
	//$orderNo = mysql_real_escape_string($_GET['OrderNoText']);
	$today = date("Y-m-d");
	
	$outstandingQuery = "SELECT  * FROM nam_orders WHERE reserved != 0 AND canceled = 0  GROUP BY orderNo ORDER BY orderNo  DESC";
	$outstandingResult = execsql($outstandingQuery);
	$outstandingNum = mysql_num_rows($outstandingResult);
	
	?>
    <script type="text/javascript" src="../css/jquery.min.js"></script>    
	<script type="text/javascript" src="../css/tablesort.js"></script>
    <link rel="stylesheet" type="text/css" href="../css/formwizard.css" />
    <script language="JavaScript" src="gen_validatorv4.js" type="text/javascript" xml:space="preserve"></script>
    
<script language="javascript" type="text/javascript" >
<!--
function confirmRefresh() {
var okToRefresh = confirm("Do you really want to cancel this entry?");
var url='outstandingOrder.php'
if (okToRefresh)
	{
			window.location=url;
	}
}
// -->
</script>    
    
    
    <table id="tableMain" width="595"> <!--begin of tableMain-->
    <tr>
      <td >
    
	<div class="headerbox2">Outstanding orders </div>
    
    <p class="wizard"><label class="std">Click on the order in the list below to make a delivery. </label></p>
       
    <div align="right">Number of outstanding orders:&nbsp;<b> <?php echo $outstandingNum ?></b></div>
	
    </td> <!--center of tableMain-->
</tr>
<tr>
<td> <!--center of tableMain-->
    	
	<table width="594" border="0" cellspacing="0" cellpadding="0">
      <tr>
        <td width="543" align="right" valign="middle"><img src="../../Images/round_green.png" alt="." width="16" height="16" />=not delivered <img src="../../Images/round_orange.png" alt="." width="16" height="16" />=partly delivered </td>
      </tr>
    </table>
	<table id="tableList" class="sortable-onload-0r rowstyle-alt no-arrow " >
    <thead>
    <tr>
    	<th width="55" class="sortable-numeric">Order</th>
		<th width="80" class="sortable-date-dmy">date</th>
    	<th width="81" class="sortable-text">del/col</th>
    	<th width="81" class="sortable-date-dmy">del/col date </th>
    	<th width="256" class="sortable-text">Client</th>
    	<th width="47" class="">&nbsp;</th>
    </tr>
    </thead>
    <tbody>
	<?php	$i = 0;
    
	while($i < $outstandingNum){

		$b1 = mysql_result($outstandingResult, $i, "productId");
 	    $b2 = mysql_result($outstandingResult, $i, "reserved");
       	$b3 = mysql_result($outstandingResult, $i, "quantity");
        $b4 = mysql_result($outstandingResult, $i, "price");
        
	    $b6 = mysql_result($outstandingResult, $i, "date");
		$b10 = mysql_result($outstandingResult, $i, "delDate");
        $b7 = mysql_result($outstandingResult, $i, "orderNo");
		
		$b8_0 = mysql_result($outstandingResult, $i, "custNo");
		
		if ($b8_0 == 0) {
		   $b8 = "#";
		   $b5 = mysql_result($outstandingResult, $i, "client");
		   }
		else {
		   $custNameQuery = "SELECT CompanyName, PostalAddress, Phone, ContactName, Cell FROM nam_customer WHERE custNo = $b8_0 ";
		   $nameResult = execsql($custNameQuery);
		   $b2 = mysql_result($nameResult, 0, "CompanyName");
		   $b8 = mysql_result($nameResult, 0, "PostalAddress");
		   $b9 = mysql_result($nameResult, 0, "Phone");	
		   $b15 = mysql_result($nameResult, 0, "ContactName");	
		   $b16 = mysql_result($nameResult, 0, "Cell");	
		   }
		   
		$b11 = mysql_result($outstandingResult, $i, "delCol");
		if ($b11=='D') { $b11="deliver";}
		if ($b11=='C') { $b11="collect";}
		
		$statusQuery = "SELECT DISTINCT orderNo, SUM(quantity) AS SQuantity, SUM(reserved) AS SReserved FROM nam_orders WHERE orderNo = $b7 GROUP BY orderNo";
	$statusResult = mySQL_query($statusQuery) or die ("Error in query:   $statusQuery . * ". mySQL_error());
		  
	$s1 = mysql_result($statusResult, 0, "SQuantity");
	$s2 = mysql_result($statusResult, 0, "SReserved");	
	$b4 = $s1-$s2;
		
		
			 
	 $orderQuery = "SELECT nam_orders.quantity, nam_products.productName, nam_orders.reserved, nam_orders.complete, nam_orders.delCol FROM nam_orders, products WHERE (nam_orders.productId = nam_products.productID)  AND nam_orders.orderNo = $b7 ";
	$orderResult = execsql($orderQuery);
	
	
	$deliverQuery = "SELECT deliverTo, address, place, contact FROM nam_delivery_address WHERE orderNo = $b7 ";
	$deliverResult = execsql($deliverQuery);
	$deliverNum = mysql_num_rows($deliverResult);
	
	if ($deliverNum!=0) {
	    $o4 = mysql_result($deliverResult, 0, "deliverTo"); 
		$o5 = mysql_result($deliverResult, 0, "address"); 
		$o6 = mysql_result($deliverResult, 0, "place"); 
		$o7 = mysql_result($deliverResult, 0, "contact");
	}
	else  {
		$o4="";
		$o5="";
		$o6="";
		$o7="";
	}
	
		$j = 0;
	    $o_num = mysql_num_rows($orderResult);
		
		
		$toolTipText = "";
		$toolTipText2 = "";
		$toolTipText3 = "";
		$toolTipText4 = "";
		while ($j < $o_num) {
		$o0 = mysql_result($orderResult, $j, "nam_orders.quantity");
		$o1 = mysql_result($orderResult, $j, "nam_products.productName"); 
		$o2 = mysql_result($orderResult, $j, "nam_orders.reserved"); 
		$o3 = mysql_result($orderResult, $j, "nam_orders.complete"); 
		 
		$o8 = mysql_result($orderResult, $j, "nam_orders.delCol");
		if ($o8=='D') {$o8a="Deliver to:";}
		if ($o8=='C') {$o8a="Collect by:";}
		$toolTipText =  $toolTipText . $o0." ".$o1."<br>";
		if ($o2>0) {
		   $toolTipText2 = $toolTipText2 . $o2."  ".$o1."<br>";}
		$toolTipText3 = "<b>".$o8a."</b><br>"
		                   .$o4."<br>"
						   .$o5."<br>"
						   .$o6."<br>"
						   .$o7."<br>"; 
		$toolTipText4 = "<b>".$b2."</b><br>"
		                   .$b8."<br>"
						   .$b9."<br>"
						   .$b15."<br>"
						   .$b16."<br>";
		$j++;
		}
	?>
		<tr onclick="document.location='outWiz0.php?DelNoText=&OrderNoText=<?php echo $b7?>'" style="cursor:pointer">
    <td><a href="outWiz0.php?DelNoText=&OrderNoText=<?php echo $b7?>" onmouseover="ddrivetip('<?php echo "<b>on order: </b><br>" . $toolTipText . "<br><b>Click to view order<b>" ?>','', 300)" onmouseout="hideddrivetip()";></a><a href="outWiz0.php?DelNoText=&OrderNoText=<?php echo $b7?>" onmouseover="ddrivetip('<?php echo "<b>on order: </b><br>" . $toolTipText ?>','', 300)" onmouseout="hideddrivetip()";></a><a href="outWiz0.php?DelNoText=&OrderNoText=<?php echo $b7?>" onmouseover="ddrivetip('<?php echo "<b>on order: </b><br>" . $toolTipText ?>','', 300)" onmouseout="hideddrivetip()";></a><a href="outWiz0.php?DelNoText=&OrderNoText=<?php echo $b7?>" onmouseover="ddrivetip('<?php echo "<b>on order: </b><br>" . $toolTipText ?>','', 300)" onmouseout="hideddrivetip()";>
      <?php echo $b7?>
    </a></td>	
			<td><?php echo date("d-m-Y", strtotime($b6))?></td>
			<td><a href="outWiz0.php?DelNoText=&OrderNoText=<?php echo $b7?>" onmouseover="ddrivetip('<?php echo  $toolTipText3 ?>','', 200)" onmouseout="hideddrivetip()";></a><a href="outWiz0.php?DelNoText=&OrderNoText=<?php echo $b7?>" onmouseover="ddrivetip('<?php echo  $toolTipText3 ?>','', 200)" onmouseout="hideddrivetip()";></a><a href="outWiz0.php?DelNoText=&OrderNoText=<?php echo $b7?>" onmouseover="ddrivetip('<?php echo  $toolTipText3 ?>','', 200)" onmouseout="hideddrivetip()";></a><a href="outWiz0.php?DelNoText=&OrderNoText=<?php echo $b7?>" onmouseover="ddrivetip('<?php echo  $toolTipText3 ?>','', 200)" onmouseout="hideddrivetip()";>
			  <?php echo $b11?>
			</a></td>
			<td> <?php if ($b10 <= $today ) { echo "<font color=red>" ;} else { echo "<font color=black>" ;}
			if ($b10<"2000-01-01") {echo "t.b.a."; } else {echo date("d-m-Y ", strtotime($b10)); }
			"</font>" ?>
			</a></td>
			<td>
              <a href="outWiz0.php?DelNoText=&OrderNoText=<?php echo $b7?> " onmouseover="ddrivetip('<?php echo "Account:<br>".$toolTipText4 ?>','', 300)" onmouseout="hideddrivetip()";></a><a href="outWiz0.php?DelNoText=&OrderNoText=<?php echo $b7?> " onmouseover="ddrivetip('<?php echo "Account:<br>".$toolTipText4 ?>','', 300)" onmouseout="hideddrivetip()";>
              <?php echo $b2?>
              </a>
              </td>
 		    <td><a href="outWiz0.php?DelNoText=&OrderNoText=<?php echo $b7?>" onmouseover="ddrivetip('<?php echo "<b>still outstanding on this order:</b><br>". $toolTipText2 ?>','', 200)" onmouseout="hideddrivetip();"><?php if ($o3 == 0) { echo "<img src=../../Images/round_red.png width=16 height=16 border=0 />";}
		            else { if ($s1-$s2 == 0) { echo "<img src=../../Images/round_green.png width=16 height=16 border=0 />";}
					       else if ($s1-$s2 != 0) {  echo "<img src=../../Images/round_orange.png width=16 height=16 border=0 />";}
						   }
		  ?></a></td>
	  </tr>


	<?php		$i++;
	}
	
	?>
	</tbody>
	</table>
    
    </td>
</tr>
</table>  <!--end of tableMain-->
<?php 	
	include("../footer.php");
}
?>
