<?php
    include("../include/session.php");
    if(!$session->isRadstoneStoreUser() && !$session->isAdmin() && !$session->isRadstoneReceptionUser()){
	header("Location: ../Store/storeMenu.php");
    }
    else {
    	include("../storeHeader.php");
//        include("../include/db.php");

    $today = date("Y-m-d");
     
	
	$productId = mysql_real_escape_string($_GET['productId']);	   
		
    $reservedQuery = "SELECT productId, Client, CustNo, orderNo, quantity, reserved, date, delDate FROM orders WHERE (productId = $productId) AND (reserved > 0) ORDER BY deldate DESC";
	$reservedResult = mySQL_query($reservedQuery) or die ("Error in query:   $reservedQuery . * ". mySQL_error());
	$i = 0;
   	$num = mysql_num_rows($reservedResult);
	
	$productNameQuery = "SELECT productName, quantity, minimumStock FROM products WHERE productId = $productId";
	$productNameResult = mySQL_query($productNameQuery) or die ("Error in query:   $productNameQuery . * ". mySQL_error());
	$b1 = mysql_result($productNameResult, 0, "productName");
	$s1 = mysql_result($productNameResult, 0, "quantity");
    $s2 = mysql_result($productNameResult, 0, "minimumStock");	
?>
<style type="text/css">
<!--
.style3 {font-size: x-small;
	font-weight: bold;
	cursor: pointer;
}
-->
</style>
<script type="text/JavaScript">
<!--
function MM_callJS(jsStr) { //v2.0
  return eval(jsStr)
}
//-->
</script>
<div class="headerbox">Reserved Item List</div>
<div class="menubox">
  <div align="right"><img src="../../Images/back_btn.png" alt="." width="75" height="25" class="style3" onclick="MM_callJS('history.go(-1);')" /></div>
</div>
<div align="right"><br />
  <br />
  <br />
</div>
<div class="subheaderbox"><b><?php echo $b1 ?></b> reserved for following orders. <br />
<br />

</div><br /><br /><br />
Present stock:&nbsp;&nbsp;<b> <?php echo number_format($s1) ?></b> &nbsp;&nbsp;&nbsp;&nbsp;Minimum stock for this product:&nbsp;&nbsp; <b><?php echo number_format($s2) ?></b><br />
<br />

<table width="598" border="0" cellpadding="0" cellspacing="0" class="rowstyle-alt colstyle-alt no-arrow" id="test1">
    <thead>
      <tr>
        <th width="68" class="sortable-text">Order</th>
        <th width="80" class="sortable-date">Date</th>
        <th width="80" class="sortable-date">Del Date </th>
        <th width="216" class="sortable-text">Customer</th>
        <th width="66" class="sortable-numeric">Qty</th>
        <th width="88" class="sortable-numeric">Reserved</th>
        <!--        <th>Reserved Stock</th>-->
      </tr>
    </thead>
    <tbody>
    <?php 
 		$total = 0;
		$dayTotal = 0;
		$nonTotal = 0 ;
    	while($i < $num){
	       $b2 = mysql_result($reservedResult, $i, "orderNo");
	       $b3 = mysql_result($reservedResult, $i, "custNo");
		   if ($b3 == 0) {
		   $c2 = "--";
		   $c3 = mysql_result($reservedResult, $i, "custNo");
		   }
		else {
		   $custNameQuery = "SELECT custNo, AccNo, CompanyName FROM customer WHERE custNo = $b3 ";
		   $nameResult = execsql($custNameQuery);
		   $c5 = mysql_result($nameResult, 0, "custNo");
		   $c2 = mysql_result($nameResult, 0, "AccNo");
		   $c3 = mysql_result($nameResult, 0, "CompanyName");	
		   }
		   
	       $b5 = mysql_result($reservedResult, $i, "quantity");
	       $b6 = mysql_result($reservedResult, $i, "reserved");
		   $b7 = mysql_result($reservedResult, $i, "date");
		   $b8 = mysql_result($reservedResult, $i, "client");
		   $b9 = mysql_result($reservedResult, $i, "delDate");
		   if ( $b9 <= $today AND $b9 > "2010-01-01" ) { 
		     $dayTotal += $b6; 
			 }
			 
		   if (  $b9 < "2010-01-01" ) { 
		     $nonTotal += $b6; 
			 }
		   
		   if ( $b9 > $today ) {	 
		   $total += $b6;
		     }
		   
	$orderQuery = "SELECT orders.quantity, products.productName FROM orders, products WHERE (orders.productId = products.productID)  AND orders.orderNo = $b2 ";
		$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++;
		}
        
    ?> 
    <tr class="alt">
       <td><a href="viewOrders.php?OrderNoText=<?php echo $b2 ?>" onmouseover="ddrivetip('<?php echo "on order: <br>" . $toolTipText . "<br><b>Click to view order<b>" ?>','#FFFFCC', 200)" onmouseout="hideddrivetip()";><?php echo $b2 ?></a></td>
       <td><?php echo date("d-m-Y", strtotime($b7));?></td>
        <td><?php if ($b9 <= $today ) { echo "<font color=red>" ;} else { echo "<font color=black>" ;}
		if ($b9<"2000-01-01") {echo "t.b.a."; } else {echo date("d-m-Y ", strtotime($b9)); }
		"</font>" ?></td>
        <td><?php if ($b3 == 0) { ?><a href="#" onmouseover="ddrivetip('<?php echo 'no account for this client.<br>' ?>','#FFFFCC', 150)" onmouseout="hideddrivetip()";><?php echo $b8; ?></a>
		    <?php }
		  else { ?>
		  <a href="customerDetails.php?custNo=<?php echo $c5 ?>" onMouseover="ddrivetip('<?php echo $c5." - ". $c2." - ".$c3 . "<br><b>Click to view account details<b>" ?>','#FFFFCC', 300)" onMouseout="hideddrivetip()";><?php echo $c3?></a>
		  <?php } ?>	  </td>
        <td><div align="right">
          <?php echo $b5 ?>
        </div></td>
        <td><div align="right">
          <?php echo $b6 ?>
        </div></td>
      </tr>
   
    
<?php 
		$i++;
    	}
    	?></tbody>
		 <tr class="alt">
		   <td colspan="5"><div align="right"><strong>reserved up today </strong></div></td>
		   <td><div align="right"><strong>
	       <?php echo number_format($dayTotal)?>
	       </strong></div></td>
  </tr>
		 <tr class="alt">
		   <td colspan="5"><div align="right"><strong>present stock</strong></div></td>
		   <td><div align="right"><strong>
		     <?php echo number_format($s1) ?>
		   </strong></div></td>
  </tr>
		 <tr class="alt">
      <td colspan="5"><div align="right"><strong>balance today</strong></div></td>
      <td>
        <div align="right"><b><?php echo number_format($s1-$dayTotal) ?>
        <?php 
		       if ( ($s1-$dayTotal) < $s2 ) {
			   $line = "<font color=orange><b>Stock is sufficient to fulfil all orders, but below the minimum required stock</font></b>";
			   }
			   if ( ($s1-$dayTotal) >= $s2 ) {
			   $line = "<b>Stock is sufficient to fulfil all orders</b>";
			   } 
			   if ( ($s1-$dayTotal) <= 0 ) {
		       $line = "<font color=red><b>The present stock is less than on order, production of balance required</font></b>";
			   }   ?>
        </b></div></td>
    </tr>
		 <tr class="alt">
		   <td colspan="5"><div align="right"><strong>reserved after today:</strong></div></td>
		   <td><div align="right"><strong>
		     <?php echo number_format($total)?>
		   </strong></div></td>
  </tr>
		 <tr class="alt">
		   <td colspan="5"><div align="right"><strong>to produce for orders: </strong></div></td>
		   <td><div align="right"><strong>
		     <?php $displayTotal = number_format($total-($s1-$dayTotal));
			   if ($displayTotal<=0) { $displayTotal=0; }
			   echo $displayTotal; ?>
		   </strong></div>
		     <div align="right"></div>
           <div align="right"></div>		   </td>
  </tr>
		 <tr class="alt">
		   <td colspan="5"><div align="right"><strong>non reserved: </strong></div></td>
		   <td><div align="right"><strong>
		     <?php echo number_format($nonTotal)?>
	       </strong></div></td>
  </tr> 
</table>
<?php echo $line; ?>
    	<?php 
    	include("../footer.php");
    }
//    
	
?>
