
<?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");
	include('../include/formValidations.php');
		//$message = mysql_real_escape_string($_GET['message']);
		
	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) );	
	
    $cust_no = mysql_real_escape_string($_GET['search_select']);	
    
    $suppliersQuery = "SELECT CompanyName,  CustNo FROM customer WHERE active=" . 1 ." ORDER BY CompanyName ASC";
	$suppliersResult = execsql($suppliersQuery);
	$suppliersNum = mysql_num_rows($suppliersResult);
	$w = 0;
	$arrId = array ();
	$arrName = array ();
	while ($w < $suppliersNum) {
		$arrId[$w] = mysql_result($suppliersResult, $w, "CustNo");
		$arrName[$w] = mysql_result($suppliersResult, $w, "CompanyName");
		$w++;
	} 
	
	if($cust_no=="0") {   	
	       $query = "SELECT  date, delNote, orderNo,  CustNo FROM sales WHERE date >= '".$start."' AND  date <= '".$end."' GROUP BY delNote ";
		}
		else {
		  $query = "SELECT  delNote, orderNo, date, CustNo FROM sales WHERE CustNo='".$cust_no."' AND date >= '".$start."' AND  date <= '".$end."' GROUP BY delNote ";
		}	
		
	$result = mySQL_query($query) or die ("There is an error the database, the list cannot be displayed completely");
	$i = 0;
	$num = mysql_num_rows($result);
	
	
  if ($cust_no!="0") {
    $customerQuery = "SELECT CompanyName,  CustNo FROM customer WHERE CustNo=$cust_no";
	$customerResult = execsql($customerQuery);
	$customerNum = mysql_num_rows($customerResult);
	$customer_name = mysql_result($customerResult, 0, "CompanyName");
  }
  else {
	  $customer_name= "all";
  }
	
		
	
?>

<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(){
  $("#DelNoText").autocomplete("delivery_autocomplete.php", {
        selectFirst: true
  });
 });
</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>



<script type="text/JavaScript">
<!--
function MM_popupMsg(msg) { //v1.0
  alert(msg);
}

function MM_callJS(jsStr) { //v2.0
  return eval(jsStr)
}
//-->
</script>

<link
	href="calendar.css" rel="stylesheet" type="text/css" />
<script language="javascript" src="../include/calendar.js"></script>
<script type="text/javascript" src="../css/tablesort.js"></script>
	
<link href="../css/main.css" rel="stylesheet" type="text/css" />
	   <div class="headerbox2">Deliveries</div><br /><br />
	  


<div id="coloredContainer">
  <form action="deliveries.php" method="get" name="dateform" class="today" id="dateform">
    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="Select" />
      <input name="date" type="hidden" id="date" value="1" />
      <br />
    </label>
    Select customer:
    <select name="search_select" id="search_select" >
      <option value="<?php echo $cust_no ?>" selected="selected"><?php echo $customer_name ?></option>
      <option value="0">All</option>
      <?php
    	for ($i = 0; $i < count($arrId); $i++) {
		echo "<option value=" . "\"" . $arrId[$i] . "\"" . ">" . $arrName[$i] . "</option>";
		$_SESSION['CustomerName'] = $arrName[$i];
		//foreach($arrId as $key=>$value){
			//echo "<option value=\"delivery.php?productId=".$arrId[$i]."&cat=$cat&formEnabled2=1&date=$date" . "\"" . ">" . $arrName[$i] . "</option>";	
	    }
     ?>
    </select>
    <br />
  </form>
</div>
<br />
<div id="coloredContainer";>
<form id="form1" name="form1" method="get" action="viewDelivery.php">
  Search for delivery note
&nbsp;&nbsp;
<input type="text" name="DelNoText" id="DelNoText" onkeypress="return numbersonly(this, event)"/>
<input name="Submit" type="submit" class="smalllinkbuttonblue" id="button" value="Search" />
</form>

</div>	<br />

	<div id="coloredContainer"><font size="3"><b>Deliveries to <?php echo $customer_name ?>.</b></font><br />
	  <b>Period from:&nbsp;&nbsp;&nbsp; <?php echo date("d-m-Y", strtotime($start )) ?>&nbsp;&nbsp;&nbsp; to&nbsp;&nbsp;&nbsp; <?php echo date("d-m-Y", strtotime($end )) ?></b></div>
	<br />
	

<table id="tableList" class="sortable-onload-1r rowstyle-alt no-arrow " >
	<thead>
		<tr>
		    <th width="63" class="sortable-numeric">Del  No</th>
			<th width="78" class="sortable-date-dmy">Date</th>
			<th width="130" class="sortable-text">Account</th>
			<th width="81" class="text">Order No </th>
			<th width="225" >Delivered to</th>
		</tr>
	</thead>
	<tbody>
	<?php 
	
	$i=0;
	while ($i < $num) {
	    $b0 = mysql_result($result, $i, "orderNo");
		$b1 = mysql_result($result, $i, "date");
		$b2_0 = mysql_result($result, $i, "custNo");
		if ($b2_0 == 0) {
		   $b2 = "--";
		   $b3 = mysql_result($result, $i, "client");
		   }
		else {
		   $custNameQuery = "SELECT custNo, AccNo, CompanyName FROM customer WHERE custNo = $b2_0 ";
		   $nameResult = execsql($custNameQuery);
		   $b5 = mysql_result($nameResult, 0, "custNo");
		   $b2 = mysql_result($nameResult, 0, "AccNo");
		   $b3 = mysql_result($nameResult, 0, "CompanyName");	
		   }
		$b4 = mysql_result($result, $i, "delNote");	
		  //procedure for showing deliverynote in balloon
		  
		$deliveryQuery = "SELECT productId, quantity, date FROM sales WHERE delNote = '$b4' ";		
	        $deliveryQueryResult = mySQL_query($deliveryQuery) or die ("<font color=red> There is an error in the database, the list cannot be displayed completely ". "(error no 100)</font>");
			$i_z = 0;
			$num_z = mysql_num_rows($deliveryQueryResult);
			$z0 = mysql_result($deliveryQueryResult, 0, "date");
			$list="";
			while ($i_z < $num_z) {
			   $z2 = mysql_result($deliveryQueryResult, $i_z, "productId");
			   $z3 = mysql_result($deliveryQueryResult, $i_z, "quantity"); 
		       $delProductQuery = "SELECT productName from products WHERE productId=$z2 ";
			   $delProductResult = mySQL_query($delProductQuery) or die ( $errorMessage ='<font color=red> ! There are errors in the database, the list cannot be displayed completely (error no 100)</font>');
			   $z1 = mysql_result($delProductResult, 0, "productName");
			   $list = $list."<br>".$z3."  ".$z1;
			   $i_z++;
			   }
		//$b5  = mysql_result($result, $i, "delAddress");	
	    
	    $orderQuery = "SELECT orders.quantity, products.productName FROM orders, products WHERE (orders.productId = products.productID)  AND orders.orderNo = $b0 ";
		$orderResult = execsql($orderQuery);
		$j = 0;
	    $o_num = mysql_num_rows($orderResult);
		$toolTipText = "";
		while ($j < $o_num) {
		$o0 = mysql_result($orderResult, $j, "orders.quantity");
		$o1 = mysql_result($orderResult, $j, "products.productName"); 
		$toolTipText =  $toolTipText . $o0." ".$o1."<br>";
		
		$j++;
		}
		if ($b5==1) { $toolTipText =  $toolTipText . "<br><font color=red><b>(This order is canceled.)</b></font>"; }
		if ($b5==2) { $toolTipText =  $toolTipText . "<br><font color=orange><b>(This order is suspended.)</b></font>"; }
	    
		$deliveredQuery = "SELECT deliverTo, address, place, contact FROM delivered_address WHERE delNoteNo = $b4 ";
	    $deliveredResult = execsql($deliveredQuery);
		$deliveredNum = mysql_num_rows($deliveredResult);
		if ($deliveredNum!=0) {
	      	$p4 = mysql_result($deliveredResult, 0, "deliverTo"); 
			$p5 = mysql_result($deliveredResult, 0, "address"); 
			$p6 = mysql_result($deliveredResult, 0, "place"); 
			$p7 = mysql_result($deliveredResult, 0, "contact");
		}
		else  {
			$p4="";
			$p5="";
			$p6="";
			$p7="";
		}
		
		
		
		 ?>
		<tr>
		  <td><a href="viewDelivery.php?DelNoText=<?php echo $b4 ?>" onmouseover="ddrivetip('delivery date: <?php echo date("d-m-Y ", strtotime($z0)) ?> <br><b>delivered:</b><?php echo $list ?> <br>','#FFFFCC', 200)" onmouseout="hideddrivetip()";><?php echo $b4 ?></a></td>
		  <td><?php echo date("d-m-Y", strToTime($b1))?></td>
		  <td>
		  <a href="displayCustomer.php?CustNo=<?php echo $b5 ?>" onMouseover="ddrivetip('<?php echo $b5." - ". $b2." - ".$b3 . "<br><b>Click to view account details<b>" ?>','#FFFFCC', 300)" onMouseout="hideddrivetip()";><?php echo $b3?></a>
		  </td>
		  <td><a href="displayOrder.php?OrderNoText=<?php echo $b0 ?>&edit=0" onmouseover="ddrivetip('<?php echo "on order: <br>" . $toolTipText  ?>','#FFFFCC', 200)" onmouseout="hideddrivetip()";>
		    <?php if ($b5==1) {echo "<font color=red>".$b0."</font>";} else { if ($b5==2) {echo "<font color=orange>".$b0."</font>";} else { echo $b0;}}?>
		  </a></td>
		  <td><a href="" onmouseover="ddrivetip('<?php echo "<b>Deliver to:</b><br>".  $p4."<br>". $p5."<br>".$p6."<br>".$p7; ?>.','#FFFFCC', 175)" onmouseout="hideddrivetip()";><?php echo $p4 ?></a></td>
	    
	</tr>	
	<?php 
	$i++;
	}
	?>
    
	</tbody>
</table>
      <br />
      <?php
	} 
	include("../footer.php");
    
?>
	   