<?php
include("../include/session.php");
if( !$session->isAdmin() && !$session->isRadstoneReceptionUser() && !$session->isRadstoneStoreUser() ){
	//header("Location: ../Store/storeMenu.php");?>
	<script language="javascript" type="text/javascript">
        <!--
		  window.location="denied.php";
        -->
       </script>
	   <?php 
}
else {
	include("../storeHeader.php");
	//$orderNo = mysql_real_escape_string($_GET['OrderNoText']);
	include('../include/formValidations.php');
	require_once('classes/tc_calendar.php');
	$date = mysql_real_escape_string($_GET['date']);
	
	if ($date==1) {
	$start = mysql_real_escape_string($_GET['date1']);
    $end = mysql_real_escape_string($_GET['date2']);
	}
	else {
	$start = date("Y-m-01");
	$end = date("Y-m-d");
	}
	
		
	if ($date!=1) {
	   $ds = "1"; }
	   else {
		$ds = date("d", strtotime($start) );
	   }
	$ms = date("m", strtotime($start) );
	$ys = date("Y", strtotime($start) );
	
	
	$de = date("d", strtotime($end) );
	$me = date("m", strtotime($end) );
	$ye = date("Y", strtotime($end) );	
	
	
	$outstandingQuery = "SELECT  * FROM orders  WHERE date > '".$start."' AND date <= '". $end ."'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 type="text/javascript" src="../css/jquery.autocomplete.js"></script>
<link rel="stylesheet" type="text/css" href="../css/jquery.autocomplete.css" />
<script type="text/javascript">
 $(document).ready(function(){
  $("#OrderNoText").autocomplete("order_autocomplete_2.php", {
        selectFirst: true
  });
 });
</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;
	}
}
/
function MM_goToURL() { //v3.0
  var i, args=MM_goToURL.arguments; document.MM_returnValue = false;
  for (i=0; i<(args.length-1); i+=2) eval(args[i]+".location='"+args[i+1]+"'");
}
//-->
</script>    

<script type="text/JavaScript">
<!--
function MM_callJS(jsStr) { //v2.0
  return eval(jsStr)
}
//-->
</script>

<script type="text/javascript">
<!--
function MM_goToURL() { //v3.0
  var i, args=MM_goToURL.arguments; document.MM_returnValue = false;
  for (i=0; i<(args.length-1); i+=2) eval(args[i]+".location='"+args[i+1]+"'");
}
//-->
</script>
    
    
    <link href="../css/main.css" rel="stylesheet" type="text/css" />
	<table id="tableMain" width="595"> <!--begin of tableMain-->
    <tr>
      <td >
    
	<div class="headerbox2">Orders </div>
	
	<br />
<br />


	<div id="coloredContainer">
	  <form action="orderList.php" method="get" name="dateform" class="today" id="dateform">
	    <span class="style7"> from:
	    &nbsp;&nbsp;
	    <?php
		$myCalendar = new tc_calendar("date1", true);
		$myCalendar->setIcon("../img/iconCalendar.gif");
		$myCalendar->setDate($ds, $ms, $ys);
		$myCalendar->setPath("./");
		$myCalendar->setYearInterval(2000, 2030);
	 	$myCalendar->writeScript();?>
	    &nbsp;&nbsp;to:&nbsp;&nbsp;
	    <?php
		$myCalendar = new tc_calendar("date2", true);
		$myCalendar->setIcon("../img/iconCalendar.gif");
		$myCalendar->setDate($de, $me, $ye);
		$myCalendar->setPath("./");
		$myCalendar->setYearInterval(2000, 2030);
		$myCalendar->writeScript();?>
	    &nbsp;&nbsp;
	    <label>
	      <input name="button5" type="submit" class="smalllinkbuttonblue" id="button5" value="Go" />
	      <input name="date" type="hidden" id="date" value="1" />
	      </label>
	  </form>
	  </div><br />

	<div id="coloredContainer";>
	  <form id="form1" name="form1" method="get" action="displayOrder.php">
	    Search for order no&nbsp;&nbsp;
	    <input type="text" name="OrderNoText" id="OrderNoText" onkeypress="return numbersonly(this, event)"/>
	    <input name="Submit" type="submit" class="smalllinkbuttonblue" id="button" value="Search" />
	    </form>
	  </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="top">

        <br />
<div id="coloredContainer"><img src="../../images/round_red.png" width="16" height="16" />=delivered<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 </div> <br />
</td>
      </tr>
    </table>
	<table  class="sortable-onload-1r rowstyle-alt no-arrow " id="tableList" >
    <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 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 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 orders.quantity, products.productName, orders.reserved, orders.complete, orders.delCol FROM orders, products WHERE (orders.productId = products.productID)  AND orders.orderNo = $b7 ";
	$orderResult = execsql($orderQuery);
	
	
	$deliverQuery = "SELECT deliverTo, address, place, contact FROM 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, "orders.quantity");
		$o1 = mysql_result($orderResult, $j, "products.productName"); 
		$o2 = mysql_result($orderResult, $j, "orders.reserved"); 
		$o3 = mysql_result($orderResult, $j, "orders.complete"); 
		// echo "order completed: ".$o3;
		$o8 = mysql_result($orderResult, $j, "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 >
        
    <td><a href="displayOrder.php?OrderNoText=<?php echo $b7 ?>&edit=0" 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="" 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="displayCustomer.php?CustNo=<?php echo $b8_0 ?>" onmouseover="ddrivetip('<?php echo "Account:<br>".$toolTipText4 ?>','', 300)" onmouseout="hideddrivetip()";>
              <?php echo $b2?>
              </a>
              </td>
 		    <td><a href="" onmouseover="ddrivetip('<?php echo "<b>still outstanding on this order:</b><br>". $toolTipText2 ?>','', 200)" onmouseout="hideddrivetip();">
 		      <?php if ($o2 == 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");
}
?>
