
<?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");
		//$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) );	
	
     
	
	$query = "SELECT saleId, productId, delNote, orderNo, date, CustNo, batchCodeOne, batchCodeTwo FROM sales WHERE date >= '$start' AND date <= '$end' AND  ( batchCodeOne <> '' OR  batchCodeTwo <> '' ) ORDER BY date DESC, delNote DESC ";
	$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);
	
		
	
?>
<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">Batch Codes</div><br />
	   <br /><br />


<div id="coloredContainer">
  <form action="batch_codes.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(2010, 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(2010, 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" />
    </label>
  </form>
</div>
	
	<br />
	

<table width="600" class="sortable-onload-0r rowstyle-alt no-arrow " id="tableList" >
	<thead>
		<tr>
		  <th width="62" class="sortable-text">Batch Code</th>
		    <th width="72" class="sortable-numeric">Del  No</th>
			<th width="72" class="sortable-date-dmy">Date</th>
			<th width="140" class="sortable-text">Account</th>
			<th width="70" class="sortable-numeric">Order No </th>
			<th width="156" class="sortable-text" >Delivered to</th>
		</tr>
	</thead>
	<tbody>
	<?php 
	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++;
			   }
		$b10  = mysql_result($result, $i, "batchCodeOne");	
		$b11  = mysql_result($result, $i, "batchCodeTwo");	
	    
	    $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="";
		}
	
		 ?>
         <?php if ($b10!="") { ?>
		<tr>
		  <td><?php echo $b10 ?></td>
		  <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()";></a><a href="" onmouseover="ddrivetip('<?php echo "<b>Deliver to:</b><br>".  $p4."<br>". $p5."<br>".$p6."<br>".$p7; ?>.','#FFFFCC', 175)" onmouseout="hideddrivetip()";></a><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 }
	   if ($b11!="") { ?>
		 <tr>
		   <td><?php echo $b11 ?></td>
		  <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()";></a><a href="" 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()";></a><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 }
		?>
		
	<?php 
	$i++;
	}
	?>
	</tbody>
</table>
      <br />
      <?php
	} 
	include("../footer.php");
    
?>
	   