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



		$today=date('Y-m-d');

        $cat = mysql_real_escape_string($_GET['cat']);
		$product = mysql_real_escape_string($_GET['productSelect']);
		$color = mysql_real_escape_string($_GET['colorSelect']);
						
		If ($product=="double") {$productTitle = "Double Roman";}
		If ($product=="DR") {$productTitle = "Double Roman Antique";}
		If ($product=="slate") {$productTitle = "Slate";}
		If ($product=="mono") {$productTitle = "Mono Ridge";}
		If ($product=="rake") {$productTitle = "Rake Verge";}
		If ($product=="tapered hip") {$productTitle = "Tapered Hip Starter";}
		If ($product=="tapered ridge") {$productTitle = "Tapered Ridge";}
		If ($product=="v hip") {$productTitle = "V Hip Starter";}
		If ($product=="v ridge") {$productTitle = "V Ridge";}
		If ($product=="All") {$productTitle = "All";}
		
		If ($color=="Black") {$colorTitle = "Black";}
		If ($color=="Brown") {$colorTitle = "Brown";}
		If ($color=="Grey") {$colorTitle = "Grey";}
		If ($color=="Kalahari") {$colorTitle = "Kalahari";}
		If ($color==" Red") {$colorTitle = "Red";}
		If ($color=="terra") {$colorTitle = "Terra Cotta";}
		If ($color=="All") {$colorTitle = "All";}
		
				
		if ($product == "All") {
		  $productsearch = "%";
		 }
		else
		 {
		  $productsearch = "%".$product."%";
		 }
		 
		 if ($color == "All") {
		  $colorsearch = "%";
		 }
		else
		 {
		  $colorsearch = "%".$color."%";
		 }
		 
		   
		   
		
        $q = "SELECT * FROM nam_products WHERE category ='$cat' AND productName LIKE '$productsearch' AND productName LIKE '$colorsearch' AND isRemoved != '1'  ORDER BY productName ASC";
		$result = execsql($q);
    	$i = 0;
    	$num = mysql_num_rows($result);
		
		$message="End of list";
    	
?>
<script type="text/javascript" src="../css/tablesort.js"></script>
<link href="../css/main.css" rel="stylesheet" type="text/css" />
<link rel="stylesheet" href="../css/dhtmlwindow.css" type="text/css" />
<script type="text/javascript" src="../css/dhtmlwindow.js"></script>
<link rel="stylesheet" href="../css/modal.css" type="text/css" />
<script type="text/javascript" src="../css/modal.js"></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>


<table id="tableMain" width="595">
  <!--begin of tableMain-->
  <tr>
    <td ><div class="headerbox2">Stock List <?php echo $cat ?> </div>
      <p class="main">
        <label class="std">To filter this list, click:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </label>
        <input type="button" name="button" id="button" class="linkbutton" value="Filter" onclick="changewindow=dhtmlmodal.open('editBox', 'iframe', 'changeTilesStockList.php?productSelect=<?php echo $product?>&colorSelect=<?php echo $color ?>&cat=<?php echo $cat ?>&productTitle=<?php echo $productTitle ?>&colorTitle=<?php echo $colorTitle ?> ', 'Change list view', 'width=330px,height=170px,center=1,resize=0,scrolling=0')
needToConfirm = false;
changewindow.onclose=function(){return window}; return false" />
        
      <label class="std"></label>
      </p></td>
    <!--center of tableMain-->
  </tr>
  <tr>
    <td><!--center of tableMain-->
      <table id="tableList" class="sortable-onload-0 rowstyle-alt no-arrow "  >
        <thead>
          <tr>
            <th width="243" class="sortable-text">Product Name<br /></th>
            
            <th width="74" class="sortable-numeric">Stock</th>
            <th width="65" class="sortable-numeric">Reserved</th>
            <th width="80" class="sortable-numeric">Available</th>
            <th width="80" class="sortable-numeric">Minimum</th>
            <!--        <th>Reserved Stock</th>-->
          </tr>
          <tr>
            <th width="243" >(Click for item history.)</th>
            
            <th width="74">&nbsp;</th>
            <th width="65">&nbsp;</th>
            <th width="80">&nbsp;</th>
            <th width="80">&nbsp;</th>
            <!--        <th>Reserved Stock</th>-->
          </tr>
        </thead>
        <tbody>
          <?php 

    	while($i < $num){
	       $b1 = mysql_result($result, $i, "productId");
	       $b2 = mysql_result($result, $i, "productName");
	       $b3 = mysql_result($result, $i, "category");
	       $b5 = mysql_result($result, $i, "quantity");
	       $b6 = mysql_result($result, $i, "minimumStock");
		   $b7=0;
		   $b8=0;
		   $b7a=0;
		   
		   $reservedQuery = "SELECT productId, SUM(reserved) AS summedReserved FROM nam_orders WHERE productId = $b1 AND delDate > '2000-01-01' GROUP BY productId";
		   $reservedResult = execsql($reservedQuery);
		   $reservedResult_num = mysql_num_rows($reservedResult);
		   
		   $tbaReservedQuery = "SELECT productId, SUM(reserved) AS summedReserved FROM nam_orders WHERE productId = $b1  AND delDate < '2000-01-01' GROUP BY productId";
		   $tbaReservedResult = execsql($tbaReservedQuery);
		   $tbaReservedResult_num = mysql_num_rows($tbaReservedResult);
		   
		   if ($reservedResult_num != 0) {
		           $b7 = mysql_result($reservedResult, 0, "summedReserved"); 
		   }
		   else {
			   $b7=0; 
		   }
		   
		   if ($tbaReservedResult_num != 0) {
			     $b8  = mysql_result($tbaReservedResult, 0, "summedReserved");
			   }
			   else {
				   $b8=0;
			   }
		   $b7a = $b7 + $b8;
		   
		   $reservedQuery = "SELECT productId, client, orderNo, reserved, deldate FROM nam_orders WHERE productId = $b1  AND reserved > 0  ORDER BY delDate DESC";
		   
		$reservedResult = execsql($reservedQuery);
		$j = 0;
	    $o_num = mysql_num_rows($reservedResult);
		$toolTipText = "";
		while ($j < $o_num) {
		$o0 = mysql_result($reservedResult, $j, "orderNo");
		$o1 = mysql_result($reservedResult, $j, "client"); 
		$o2 = mysql_result($reservedResult, $j, "reserved"); 
		//$toolTipText =  $toolTipText . $o0." ".$o1." ".$o2."<br>";
		$o3a = mysql_result($reservedResult, $j, "delDate"); 
		if ($o3a<'2000-01-01') {
		  $o3='tba &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';}
		else {
		  $o3 = date("d-m-Y",strtotime($o3a));
		  }
		
		if ($o3a<'2000-01-01') {
		   	$toolTipText = "<font color=blue>". $toolTipText . $o3."&nbsp;&nbsp;&nbsp;".$o0."&nbsp;&nbsp;&nbsp;".$o1."&nbsp;&nbsp;&nbsp;".$o2."</font> <br>"; } 
			else  {
		
		if ($o3a<$today) {
		   	$toolTipText = "<font color=red>". $toolTipText . $o3."&nbsp;&nbsp;&nbsp;".$o0."&nbsp;&nbsp;&nbsp;".$o1."&nbsp;&nbsp;&nbsp;".$o2."</font> <br>"; } 
			else  {
			
		if ($o3a==$today) {
		   	$toolTipText = "<font color=green>". $toolTipText . $o3."&nbsp;&nbsp;&nbsp;".$o0."&nbsp;&nbsp;&nbsp;".$o1."&nbsp;&nbsp;&nbsp;".$o2."</font> <br>"; }
			else
			{
		   	$toolTipText = "<font color=black>". $toolTipText . $o3."&nbsp;&nbsp;&nbsp;".$o0."&nbsp;&nbsp;&nbsp;".$o1."&nbsp;&nbsp;&nbsp;".$o2."</font> <br>"; }
			}
			}
		$j++;
		}
		
    
    ?>
          <tr class="alt" >
            <td><a href="stockMovement.php?productIdSelect=<?php echo $b1 ?>&date=0" onmouseover="ddrivetip('<?php echo $b2."<br>(".$b1.")<br>".$cat."<br><b>Click for item history</b>"?>','lightyellow', 150)" onmouseout="hideddrivetip()">
              <?php echo $b2?>
            </a></td>
            <?php 
        if($cat == "Tiles and Accessories"){
        	?>
            <td align="right"><?php	//	$actStock = $b5 + $b7;
		
			echo number_format($b5);
		
		?></td>
            <td align="right"><a href="showReserved.php?productId=<?php echo $b1 ?>" onmouseover="ddrivetip('<?php echo "<b>reserved on orders:</b><br>". $toolTipText . "<br><b>click to view complete list</b>" ?>','#FFFFCC', 300)" onmouseout="hideddrivetip();"><?php echo number_format($b7a)?></a></td>
            <?php 
        }
        ?>
            <td align="right"><?php
         $directAvailStock = $b5 - $b7;
		 $availStock = $b5 - $b7 - $b8;
			
			?>
              <a href="showReserved.php?productId=<?php echo $b1 ?>" onmouseover="ddrivetip('<?php echo "<b>reserved on fixed orders:&nbsp;&nbsp;".number_format($b7)."</b><br>"."<b>reserved on non-fixed orders:&nbsp;&nbsp;".number_format($b8)."</b><br>"."<b>directly available:&nbsp;&nbsp;".number_format($directAvailStock)."</b><br>"."<b>actual available:&nbsp;&nbsp;".number_format($availStock)."</b><br>" ."<br><b>click to view complete list</b>" ?>','#FFFFCC', 300)" onmouseout="hideddrivetip();">
              <?php 
			if($availStock<0){
				echo "<font color=\"red\">";
				}
			else {
			 if($availStock<$b6){
				echo "<font color=\"orange\">";
				}
		       else{ echo "<font color=\"black\">";
		      }	
		   } 
		   echo number_format($availStock)?>
              </a></td>
            <td align="right"><?php echo number_format($b6)?></td>
          </tr>
          <?php 
		$i++;
    	}
    	?>
        </tbody>
      </table>
      <b><font color='#FF0000'>
      <?php  echo $message ?>
    </font></b> <br /></td>
  </tr>
</table>
<!--end of tableMain-->
<?php 
    	include("../footer.php");
    }
//    
	
?>
