<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>


<?php
include ("../navigation/header.php");

$page_name = "Stores-main";
include ("../navigation/access.php");

if ($permission_num==1) {
	include ("../navigation/store_sidebar.php");
	
$table_name = $_SESSION['db_prefix']."_store_items";
$timber_query = "SELECT  width, depth, class FROM $table_name WHERE subCategory = 'Timber' AND depth <> '' AND class='S5' GROUP BY depth, width ";
$timber_result = mySQL_query($timber_query);
$timber_num = mysql_num_rows($timber_result);

$table_name = $_SESSION['db_prefix']."_store_items";
$timber2_query = "SELECT  width, depth, class FROM $table_name WHERE subCategory = 'Timber' AND depth <> '' AND class='S7' GROUP BY depth, width ";
$timber2_result = mySQL_query($timber2_query);
$timber2_num = mysql_num_rows($timber2_result);

$table_name = $_SESSION['db_prefix']."_store_items";
$timber3_query = "SELECT  width, depth, class FROM $table_name WHERE subCategory = 'Timber' AND depth <> '' AND class<>'S5' AND class<>'S7' AND class<>'Plate'  GROUP BY depth, width, class ";
$timber3_result = mySQL_query($timber3_query);
$timber3_num = mysql_num_rows($timber3_result);

?>
<!--style sheets-->
<link rel="stylesheet" type="text/css" href="../css/tooltip.css" />

<!--jscript-->
<script type="text/javascript" src="../jscript/tooltip.js"></script>



</head>

<body>
<div class="main_frame" style="height:auto;">
  <table id="tableMain"  >
    <!--begin of tableMain-->
    <tr>
      <td width="830" height="62" valign="top" ><div class="page_head_frame">
          <table width="809" border="0" cellspacing="0" cellpadding="0">
            <tr>
              <td><label class="label_page_head">Store </label></td>
              <td width="171" align="right">&nbsp;</td>
            </tr>
          </table>
        </div>
        <br />
        <br /></td>
      <!--center of tableMain--> 
    </tr>
    
      <td align="left"><div  class="page_middle_frame">
          <label class="label_std">Timber stock class 5</label>
          <table width="542"  id="table_list" >
            <tr>
              <th>&nbsp;</th>
              <th colspan="5"><div align="center">length (m)</div></th>
            <tr>
              <th width="93" rowspan="2" align="center">size</th>
              <th width="82" rowspan="2" align="center">stock </th>
              <th colspan="2" align="center">reserved</th>
              <th width="86" rowspan="2" align="center">available</th>
              <th width="82" rowspan="2" align="center">min stock</th>
            <tr>
              <th width="88" align="center">estimated</th>
              <th width="83" align="center">actual</th>
              <?php 
	$table_name5 = $_SESSION['db_prefix']."_captured_timber";
	$table_name6 = $_SESSION['db_prefix']."_captured_jobs";
	$table_name7 = $_SESSION['db_prefix']."_customer";
	$table_name9 = $_SESSION['db_prefix']."_captured_cash_sale";
	
	$s=0;
	$min_stock_length='';
	
	while ($s<$timber_num) {
		$tip_prereserved_text='';
		$tip_prereserved_cash_text='';
		$tip_reserved_text='';
		$tip_reserved_cash_text='';
		$width = mysql_result($timber_result, $s, "width");
		$depth = mysql_result($timber_result, $s, "depth");
		$class = mysql_result($timber_result, $s, "class");
		
		$table_name = $_SESSION['db_prefix']."_store_items";
   	 	$stock_query = "SELECT  quantity, length, minimumStock FROM $table_name WHERE width = '".$width."' and depth = '".$depth."'  AND class='S5'  ";
		$stock_result = mySQL_query($stock_query);
		$stock_num = mysql_num_rows($stock_result);
		$i=0;
		$length_tot=0;
		$min_stock_length=0;
		while ($i<$stock_num) {
			$length = mysql_result($stock_result, $i, "length");
			$quantity = mysql_result($stock_result, $i, "quantity");
			$minimum = mysql_result($stock_result, $i, "minimumStock");
			$length_tot=$length_tot + ($length*$quantity);
			$min_stock_length =  $min_stock_length + ($length*$minimum);
		$i++;
		}	
		
		
		//$reserved_query = "SELECT SUM(prereserved) AS tot_prereserved, SUM(reserved) AS tot_reserved FROM $table_name5 WHERE cap_depth = '".$depth."' AND cap_width = '".$width."' AND cap_class = 'S5' AND cap_timber_group <> 'Cash sale' ";
//		//echo '<br>'.$reserved_query;
//		$reserved_result = mySQL_query($reserved_query);
//		$tot_prereserved = mysql_result($reserved_result, 0, "tot_prereserved");
//		$tot_reserved = mysql_result($reserved_result, 0, "tot_reserved");
//		$balance = ($length_tot/1000) - $tot_prereserved - $tot_reserved;
		
		
		$table_name8 = $_SESSION['db_prefix']."_captured_timber";
		$pre_reserved_query = "SELECT SUM(prereserved) AS tot_prereserved FROM $table_name8 WHERE cap_depth = '".$depth."' AND cap_width = '".$width."' AND cap_class='S5' AND cap_timber_group <> 'Cash sale' GROUP BY cap_depth, cap_width, cap_class  ";
		$pre_reserved_result = mySQL_query($pre_reserved_query);
		$pre_reserved_num = mysql_num_rows($pre_reserved_result);
		if ($pre_reserved_num!=0) {
		$tot_prereserved = mysql_result($pre_reserved_result, 0, "tot_prereserved");
		}
		else {
		$tot_prereserved = 0;
		}
				
		$table_name8 = $_SESSION['db_prefix']."_captured_timber";
		$reserved_query = "SELECT SUM(reserved) AS tot_reserved FROM $table_name8 WHERE cap_depth = '".$depth."' AND cap_width = '".$width."' AND cap_class='S5' GROUP BY cap_depth, cap_width, cap_class  ";
		$reserved_result = mySQL_query($reserved_query);
		$reserved_num = mysql_num_rows($reserved_result);
		if ($reserved_num!=0) {
		$tot_reserved = mysql_result($reserved_result, 0, "tot_reserved");
		}
		else {
		$tot_reserved = 0;
		}
		$balance = ($length_tot/1000) - $tot_prereserved - $tot_reserved;
		
		$prereserved_query = "SELECT cap_job, job_no, cust_id, project_no, project_place, SUM(prereserved) AS summed_prereserved FROM $table_name5, $table_name6 WHERE cap_job=job_no AND cap_width = '".$width."' AND cap_depth = '".$depth."' AND cap_class= 'S5' AND prereserved<>0 GROUP BY cap_job, cap_depth, cap_width, cap_class  ORDER BY cap_job ASC";
		$prereserved_result = mySQL_query($prereserved_query);
		$prereserved_num = mysql_num_rows($prereserved_result);
		$j=0;
		while ($j<$prereserved_num) {
		  $job_no = mysql_result($prereserved_result, $j, "cap_job");
		  $cust_id = mysql_result($prereserved_result, $j, "cust_id");
		  $project_no = mysql_result($prereserved_result, $j, "project_no");
		  $project_place = mysql_result($prereserved_result, $j, "project_place");
		  $estimated = mysql_result($prereserved_result, $j, "summed_prereserved");
		  $table_name7 = $_SESSION['db_prefix']."_customer";
			$cust_query = "SELECT comp_name FROM $table_name7 WHERE  cust_id = '".$cust_id."' ";
			$cust_result = mySQL_query($cust_query);
			$comp_name = mysql_result($cust_result, 0, "comp_name");
			
		$tip_prereserved_text =  '<tr><td><label class=tip_text_label>'.$job_no.' </label></td><td><label class=tip_text_label>Project</label></td><td><label class=tip_text_label> '.$comp_name.'</label></td><td><label class=tip_text_label> '.$project_no.', '.$project_place.'</label></td><td align=right><label class=tip_text_label> '.$estimated.'</label></td></tr>'.$tip_prereserved_text;
		  $j++;
		}	
			
		//$prereserved_cash_query = "SELECT cap_job, job_no, cust_id, project_no, project_place, SUM(prereserved) AS summed_prereserved FROM $table_name5, $table_name9 WHERE cap_job=job_no AND cap_width = '".$width."' AND cap_depth = '".$depth."' AND cap_class= 'S5' AND prereserved<>0 GROUP BY cap_job, cap_depth, cap_width, cap_class  ORDER BY cap_job ASC";
//		$prereserved_cash_result = mySQL_query($prereserved_cash_query);
//		$prereserved_cash_num = mysql_num_rows($prereserved_cash_result);
//		$m=0;
//		while ($m<$prereserved_cash_num) {
//		  $job_no_cash = mysql_result($prereserved_cash_result, $m, "cap_job");
//		  $cust_id_cash = mysql_result($prereserved_cash_result, $m, "cust_id");
//		  $project_no_cash = mysql_result($prereserved_cash_result, $m, "project_no");
//		  $project_place_cash = mysql_result($prereserved_cash_result, $m, "project_place");
//		  $estimated_cash = mysql_result($prereserved_cash_result, $m, "summed_prereserved");
//		  $table_name7 = $_SESSION['db_prefix']."_customer";
//			$cust_query = "SELECT comp_name FROM $table_name7 WHERE  cust_id = '".$cust_id_cash."' ";
//			$cust_result = mySQL_query($cust_query);
//			$comp_name_cash = mysql_result($cust_result, 0, "comp_name");
//			
//		$tip_prereserved_cash_text =  '<tr><td><label class=tip_text_label>'.$job_no_cash.' </label></td><td><label class=tip_text_label>Cash sale</label></td><td><label class=tip_text_label> '.$comp_name_cash.'</label></td><td><label class=tip_text_label> '.$project_no_cash.', '.$project_place_cash.'</label></td><td align=right><label class=tip_text_label> '.$estimated_cash.'</label></td></tr>'.$tip_prereserved_cash_text;
//		  $m++;
//		}		
			  
		$tip_prereserved_head='Estimated reserved:<br><table border=1 cellpadding=5 style=border-collapse:collapse; ><tr><td><label class=tip_text_label_bold>Job no</label></td><td><label class=tip_text_label_bold>Type</label></td><td><label class=tip_text_label_bold>Customer</label></td><td><label class=tip_text_label_bold>Location</label></td><td><label class=tip_text_label_bold>Estimated</label></td></tr>';
		$tip_prereserved_foot='</table><br>Click for details.';
		$prereservedTT = $tip_prereserved_head.$tip_prereserved_text.$tip_prereserved_foot;
  		
		
		$reserved_query = "SELECT cap_job, job_no, cust_id, project_no, project_place, SUM(reserved) AS summed_reserved FROM $table_name5, $table_name6 WHERE cap_job=job_no AND cap_width = '".$width."' AND cap_depth = '".$depth."' AND cap_class= 'S5' AND reserved<>0 GROUP BY cap_job, cap_depth, cap_width, cap_class  ORDER BY cap_job ASC";
		$reserved_result = mySQL_query($reserved_query);
		$reserved_num = mysql_num_rows($reserved_result);
		$k=0;
		while ($k<$reserved_num) {
		  $job_no = mysql_result($reserved_result, $k, "cap_job");
		  $cust_id = mysql_result($reserved_result, $k, "cust_id");
		  $project_no = mysql_result($reserved_result, $k, "project_no");
		  $project_place = mysql_result($reserved_result, $k, "project_place");
		  $actual = mysql_result($reserved_result, $k, "summed_reserved");
		  $table_name7 = $_SESSION['db_prefix']."_customer";
			$cust_query = "SELECT comp_name FROM $table_name7 WHERE  cust_id = '".$cust_id."' ";
			$cust_result = mySQL_query($cust_query);
			$comp_name = mysql_result($cust_result, 0, "comp_name");
		 	 	
		  $tip_reserved_text =  '<tr><td><label class=tip_text_label> '.$job_no.'</label></td><td><label class=tip_text_label>Project</label></td><td><label class=tip_text_label> '.$comp_name.'</label></td><td><label class=tip_text_label> '.$project_no.', '.$project_place.'</label></td><td align=right><label class=tip_text_label> '.$actual.'</label></td></tr>'.$tip_reserved_text;
		  $k++;
		}
		
		$reserved_cash_query = "SELECT cap_job, job_no, cust_id, project_no, project_place, SUM(reserved) AS summed_reserved FROM $table_name5, $table_name9 WHERE cap_job=job_no AND cap_width = '".$width."' AND cap_depth = '".$depth."' AND cap_class= 'S5' AND reserved<>0 GROUP BY cap_job, cap_depth, cap_width, cap_class  ORDER BY cap_job ASC";
		$reserved_cash_result = mySQL_query($reserved_cash_query);
		$reserved_cash_num = mysql_num_rows($reserved_cash_result);
		$p=0;
		while ($p<$reserved_cash_num) {
		  $job_no = mysql_result($reserved_cash_result, $p, "cap_job");
		  $cust_id = mysql_result($reserved_cash_result, $p, "cust_id");
		  $project_no = mysql_result($reserved_cash_result, $p, "project_no");
		  $project_place = mysql_result($reserved_cash_result, $p, "project_place");
		  $actual = mysql_result($reserved_cash_result, $p, "summed_reserved");
		  $table_name7 = $_SESSION['db_prefix']."_customer";
			$cust_query = "SELECT comp_name FROM $table_name7 WHERE  cust_id = '".$cust_id."' ";
			$cust_result = mySQL_query($cust_query);
			$comp_name = mysql_result($cust_result, 0, "comp_name");
		 	 	
		  $tip_reserved_cash_text =  '<tr><td><label class=tip_text_label> '.$job_no.'</label></td><td><label class=tip_text_label>Cash sale</label></td><td><label class=tip_text_label> '.$comp_name.'</label></td><td><label class=tip_text_label> '.$project_no.', '.$project_place.'</label></td><td align=right><label class=tip_text_label> '.$actual.'</label></td></tr>'.$tip_reserved_cash_text;
		  $p++;
		}
		
		$tip_reserved_head='Actual reserved:<br><table border=1 cellpadding=5 style=border-collapse:collapse; ><tr><td><label class=tip_text_label_bold>Job no</label></td><td><label class=tip_text_label_bold>Type</label></td><td><label class=tip_text_label_bold>Customer</label></td><td><label class=tip_text_label_bold>Location</label></td><td><label class=tip_text_label_bold>Actual</label></td></tr>';
		$tip_reserved_foot='</table><br>Click for details.';
		$reservedTT = $tip_reserved_head.$tip_reserved_text.$tip_reserved_cash_text.$tip_reserved_foot;
  		?>
            <tr>
              <td><a href="store_timber_list.php?width=<?php echo $width?>&depth=<?php echo $depth?>&class=S5&single=yes" onMouseover="ddrivetip('Click for detailed list.', 115)"; onMouseout="hideddrivetip()"><?php echo $depth." x ".$width."(".$class.")" ?></a></td>
              <td align="right"><?php echo number_format($length_tot/1000, 2, '.', ',') ?></td>
              <td align="right"><a href="timber_reserved_list.php?width=<?php echo $width?>&depth=<?php echo $depth?>&class=S5" onMouseover="ddrivetip('<?php echo $prereservedTT ?>  ', 450)"; onMouseout="hideddrivetip()"><?php echo number_format($tot_prereserved, 2, '.', ',') ?></a></td>
              <td align="right"><a href="timber_reserved_list.php?width=<?php echo $width?>&depth=<?php echo $depth?>&class=S5" onMouseover="ddrivetip('<?php echo $reservedTT ?>  ', 450)"; onMouseout="hideddrivetip()"><?php echo number_format($tot_reserved, 2, '.', ',') ?></a></td>
              <td align="right"><?php echo number_format($balance, 2, '.', ',') ?></td>
              <td align="right"><?php echo number_format($min_stock_length/1000, 2, '.', ',') ?></td>
            </tr>
            <?php
	$s++;
	}
	?>
          </table>
          <br />
          <label class="label_std">Timber stock class 7</label>
          <table width="542"  id="table_list">
            <tr>
              <th>&nbsp;</th>
              <th colspan="5"><div align="center">length (m)</div></th>
            </tr>
            <tr>
              <th width="95" rowspan="2">size</th>
              <th width="83" rowspan="2">stock</th>
              <th colspan="4">reserved</th>
            </tr>
            <tr>
              <th width="82" align="center">estimated</th>
              <th width="86" align="center">actual</th>
              <th width="86" align="center">available</th>
              <th width="82" align="center">min stock</th>
              <?php 
	$s=0;
	while ($s<$timber2_num) {
		$tip_prereserved_text='';
		$tip_prereserved_cash_text='';
		$tip_reserved_text='';
		$tip_reserved_cash_text='';
		$width = mysql_result($timber2_result, $s, "width");
		$depth = mysql_result($timber2_result, $s, "depth");
		$class = mysql_result($timber2_result, $s, "class");
		
		$table_name = $_SESSION['db_prefix']."_store_items";
   	 	$stock_query = "SELECT  quantity, length, minimumStock FROM $table_name WHERE width = '".$width."' and depth = '".$depth."' AND class='S7' ";
		$stock_result = mySQL_query($stock_query);
		$stock_num = mysql_num_rows($stock_result);
		$i=0;
		$length_tot=0;
		$min_stock_length=0;;
		while ($i<$stock_num) {
			$length = mysql_result($stock_result, $i, "length");
			$quantity = mysql_result($stock_result, $i, "quantity");
			$minimum = mysql_result($stock_result, $i, "minimumStock");
			$length_tot=$length_tot + ($length*$quantity);
			$min_stock_length =  $min_stock_length + ($length*$minimum);
		$i++;
		}	
		
		//$table_name8 = $_SESSION['db_prefix']."_captured_timber";
//		$reserved_query = "SELECT SUM(prereserved) AS tot_prereserved, SUM(reserved) AS tot_reserved FROM $table_name8 WHERE cap_depth = '".$depth."' AND cap_width = '".$width."' AND cap_class = 'S7' AND cap_timber_group <> 'Cash sale' ";
//		$reserved_result = mySQL_query($reserved_query);
//		$tot_prereserved = mysql_result($reserved_result, 0, "tot_prereserved");
//		$tot_reserved = mysql_result($reserved_result, 0, "tot_reserved");
//		
//		$balance = ($length_tot/1000) - $tot_prereserved - $tot_reserved;

		$table_name8 = $_SESSION['db_prefix']."_captured_timber";
		$pre_reserved_query = "SELECT SUM(prereserved) AS tot_prereserved FROM $table_name8 WHERE cap_depth = '".$depth."' AND cap_width = '".$width."' AND cap_class='S7' AND cap_timber_group <> 'Cash sale' GROUP BY cap_depth, cap_width, cap_class  ";
		$pre_reserved_result = mySQL_query($pre_reserved_query);
		$pre_reserved_num = mysql_num_rows($pre_reserved_result);
		if ($pre_reserved_num!=0) {
		$tot_prereserved = mysql_result($pre_reserved_result, 0, "tot_prereserved");
		}
		else {
		$tot_prereserved = 0;
		}
				
		$table_name8 = $_SESSION['db_prefix']."_captured_timber";
		$reserved_query = "SELECT SUM(reserved) AS tot_reserved FROM $table_name8 WHERE cap_depth = '".$depth."' AND cap_width = '".$width."' AND cap_class='S7' GROUP BY cap_depth, cap_width, cap_class  ";
		$reserved_result = mySQL_query($reserved_query);
		$reserved_num = mysql_num_rows($reserved_result);
		if ($reserved_num!=0) {
		$tot_reserved = mysql_result($reserved_result, 0, "tot_reserved");
		}
		else {
		$tot_reserved = 0;
		}
		$balance = ($length_tot/1000) - $tot_prereserved - $tot_reserved;
		
		$prereserved_query = "SELECT cap_job, job_no, cust_id, project_no, project_place, SUM(prereserved) AS summed_prereserved FROM $table_name5, $table_name6 WHERE cap_job=job_no AND cap_width = '".$width."' AND cap_depth = '".$depth."' AND cap_class= 'S7' AND prereserved<>0 GROUP BY cap_job, cap_depth, cap_width, cap_class  ORDER BY cap_job ASC";
		$prereserved_result = mySQL_query($prereserved_query);
		$prereserved_num = mysql_num_rows($prereserved_result);
		$j=0;
		while ($j<$prereserved_num) {
		  $job_no = mysql_result($prereserved_result, $j, "cap_job");
		  $cust_id = mysql_result($prereserved_result, $j, "cust_id");
		  $project_no = mysql_result($prereserved_result, $j, "project_no");
		  $project_place = mysql_result($prereserved_result, $j, "project_place");
		  $estimated = mysql_result($prereserved_result, $j, "summed_prereserved");
		  $table_name7 = $_SESSION['db_prefix']."_customer";
			$cust_query = "SELECT comp_name FROM $table_name7 WHERE  cust_id = '".$cust_id."' ";
			$cust_result = mySQL_query($cust_query);
			$comp_name = mysql_result($cust_result, 0, "comp_name");
		 	 	
		  $tip_prereserved_text =  '<tr><td><label class=tip_text_label>'.$job_no.' </label></td><td><label class=tip_text_label>Project</label></td><td><label class=tip_text_label> '.$comp_name.'</label></td><td><label class=tip_text_label> '.$project_no.', '.$project_place.'</label></td><td align=right><label class=tip_text_label> '.$estimated.'</label></td></tr>'.$tip_prereserved_text;
		  $j++;
		}
		
		//$prereserved_cash_query = "SELECT cap_job, job_no, cust_id, project_no, project_place, SUM(prereserved) AS summed_prereserved FROM $table_name5, $table_name9 WHERE cap_job=job_no AND cap_width = '".$width."' AND cap_depth = '".$depth."' AND cap_class= 'S7' AND prereserved<>0 GROUP BY cap_job, cap_depth, cap_width, cap_class  ORDER BY cap_job ASC";
//		$prereserved_cash_result = mySQL_query($prereserved_cash_query);
//		$prereserved_cash_num = mysql_num_rows($prereserved_cash_result);
//		$n=0;
//		while ($n<$prereserved_cash_num) {
//		  $job_no_cash = mysql_result($prereserved_cash_result, $n, "cap_job");
//		  $cust_id_cash = mysql_result($prereserved_cash_result, $n, "cust_id");
//		  $project_no_cash = mysql_result($prereserved_cash_result, $n, "project_no");
//		  $project_place_cash = mysql_result($prereserved_cash_result, $n, "project_place");
//		  $estimated_cash = mysql_result($prereserved_cash_result, $n, "summed_prereserved");
//		  $table_name7 = $_SESSION['db_prefix']."_customer";
//			$cust_query = "SELECT comp_name FROM $table_name7 WHERE  cust_id = '".$cust_id_cash."' ";
//			$cust_result = mySQL_query($cust_query);
//			$comp_name_cash = mysql_result($cust_result, 0, "comp_name");
//			
//		$tip_prereserved_cash_text =  '<tr><td><label class=tip_text_label>'.$job_no_cash.' </label></td><td><label class=tip_text_label>Cash sale</label></td><td><label class=tip_text_label> '.$comp_name_cash.'</label></td><td><label class=tip_text_label> '.$project_no_cash.', '.$project_place_cash.'</label></td><td align=right><label class=tip_text_label> '.$estimated_cash.'</label></td></tr>'.$tip_prereserved_cash_text;
//		  $n++;
//		}
		
		$tip_prereserved_head='Estimated reserved:<br><table border=1 cellpadding=5 style=border-collapse:collapse; ><tr><td><label class=tip_text_label_bold>Job no</label></td><td><label class=tip_text_label_bold>Type</label></td><td><label class=tip_text_label_bold>Customer</label></td><td><label class=tip_text_label_bold>Location</label></td><td><label class=tip_text_label_bold>Estimated</label></td></tr>';
		$tip_prereserved_foot='</table><br>Click for details.';
		$prereservedTT = $tip_prereserved_head.$tip_prereserved_text.$tip_prereserved_foot;
  		
		
		$reserved_query = "SELECT cap_job, job_no, cust_id, project_no, project_place, SUM(reserved) AS summed_reserved FROM $table_name5, $table_name6 WHERE cap_job=job_no AND cap_width = '".$width."' AND cap_depth = '".$depth."' AND cap_class= 'S7' AND reserved<>0 GROUP BY cap_job, cap_depth, cap_width, cap_class  ORDER BY cap_job ASC";
		$reserved_result = mySQL_query($reserved_query);
		$reserved_num = mysql_num_rows($reserved_result);
		$k=0;
		while ($k<$reserved_num) {
		  $job_no = mysql_result($reserved_result, $k, "cap_job");
		  $cust_id = mysql_result($reserved_result, $k, "cust_id");
		  $project_no = mysql_result($reserved_result, $k, "project_no");
		  $project_place = mysql_result($reserved_result, $k, "project_place");
		  $actual = mysql_result($reserved_result, $k, "summed_reserved");
		  $table_name7 = $_SESSION['db_prefix']."_customer";
			$cust_query = "SELECT comp_name FROM $table_name7 WHERE  cust_id = '".$cust_id."' ";
			$cust_result = mySQL_query($cust_query);
			$comp_name = mysql_result($cust_result, 0, "comp_name");
		 	 	
		  $tip_reserved_text =  '<tr><td><label class=tip_text_label> '.$job_no.'</label></td><td><label class=tip_text_label>Project</label></td><td><label class=tip_text_label> '.$comp_name.'</label></td><td><label class=tip_text_label> '.$project_no.', '.$project_place.'</label></td><td align=right><label class=tip_text_label> '.$actual.'</label></td></tr>'.$tip_reserved_text;
		  $k++;
		}
		
		$reserved_cash_query = "SELECT cap_job, job_no, cust_id, project_no, project_place, SUM(reserved) AS summed_reserved FROM $table_name5, $table_name9 WHERE cap_job=job_no AND cap_width = '".$width."' AND cap_depth = '".$depth."' AND cap_class= 'S7' AND reserved<>0 GROUP BY cap_job, cap_depth, cap_width, cap_class  ORDER BY cap_job ASC";
		$reserved_cash_result = mySQL_query($reserved_cash_query);
		$reserved_cash_num = mysql_num_rows($reserved_cash_result);
		$q=0;
		while ($q<$reserved_cash_num) {
		  $job_no = mysql_result($reserved_cash_result, $q, "cap_job");
		  $cust_id = mysql_result($reserved_cash_result, $q, "cust_id");
		  $project_no = mysql_result($reserved_cash_result, $q, "project_no");
		  $project_place = mysql_result($reserved_cash_result, $q, "project_place");
		  $actual = mysql_result($reserved_cash_result, $q, "summed_reserved");
		  $table_name7 = $_SESSION['db_prefix']."_customer";
			$cust_query = "SELECT comp_name FROM $table_name7 WHERE  cust_id = '".$cust_id."' ";
			$cust_result = mySQL_query($cust_query);
			$comp_name = mysql_result($cust_result, 0, "comp_name");
		 	 	
		  $tip_reserved_cash_text =  '<tr><td><label class=tip_text_label> '.$job_no.'</label></td><td><label class=tip_text_label>Cash sale</label></td><td><label class=tip_text_label> '.$comp_name.'</label></td><td><label class=tip_text_label> '.$project_no.', '.$project_place.'</label></td><td align=right><label class=tip_text_label> '.$actual.'</label></td></tr>'.$tip_reserved_text;
		  $q++;
		}
		
		$tip_reserved_head='Actual reserved:<br><table border=1 cellpadding=5 style=border-collapse:collapse; ><tr><td><label class=tip_text_label_bold>Job no</label></td><td><label class=tip_text_label_bold>Type</label></td><td><label class=tip_text_label_bold>Customer</label></td><td><label class=tip_text_label_bold>Location</label></td><td><label class=tip_text_label_bold>Actual</label></td></tr>';
		$tip_reserved_foot='</table><br>Click for details.';
		$reservedTT = $tip_reserved_head.$tip_reserved_text.$tip_reserved_cash_text.$tip_reserved_foot;
  		?>
            </tr>
            <tr>
              <td><a href="store_timber_list.php?width=<?php echo $width?>&amp;depth=<?php echo $depth ?>&class=S7&single=yes"><?php echo $depth." x ".$width."(".$class.")" ?></a></td>
              <td align="right"><?php echo number_format($length_tot/1000, 2, '.', ',') ?></td>
              <td align="right"><a href="timber_reserved_list.php?width=<?php echo $width?>&depth=<?php echo $depth?>&class=S7 " onMouseover="ddrivetip('<?php echo $prereservedTT ?>  ', 500)"; onMouseout="hideddrivetip()"><?php echo number_format($tot_prereserved, 2, '.', ',') ?></a></td>
              <td align="right"><a href="timber_reserved_list.php?width=<?php echo $width?>&depth=<?php echo $depth?>&class=S7" onMouseover="ddrivetip('<?php echo $reservedTT ?>  ', 400)"; onMouseout="hideddrivetip()"><?php echo number_format($tot_reserved, 2, '.', ',') ?></a></td>
              <td align="right"><?php echo number_format($balance, 2, '.', ',') ?></td>
              <td align="right"><?php echo number_format($min_stock_length/1000, 2, '.', ',') ?></td>
            </tr>
            <?php
	$s++;
	}
	?>
          </table>
     <br>
     
  <label class="label_std">Timber stock other</label>
  <table width="542"  id="table_list">
    <tr>
      <th>&nbsp;</th>
      <th colspan="5"><div align="center">length (m)</div></th>
    </tr>
    <tr>
      <th width="95" rowspan="2">size</th>
      <th width="83" rowspan="2">stock</th>
      <th colspan="4">reserved</th>
    </tr>
    <tr>
      <th width="82" align="center">estimated</th>
      <th width="86" align="center">actual</th>
      <th width="86" align="center">available</th>
      <th width="82" align="center">min stock</th>
      <?php 
	 
	$s=0;
	while ($s<$timber3_num) {
		$tip_prereserved_text='';
		$tip_prereserved_cash_text='';
		$tip_reserved_text='';
		$tip_reserved_cash_text='';
		$width = mysql_result($timber3_result, $s, "width");
		$depth = mysql_result($timber3_result, $s, "depth");
		$class = mysql_result($timber3_result, $s, "class");
		
		$table_name = $_SESSION['db_prefix']."_store_items";
   	 	$stock_query = "SELECT  quantity, length, minimumStock FROM $table_name WHERE width = '".$width."' and depth = '".$depth."' AND class='".$class."' ";
		$stock_result = mySQL_query($stock_query);
		$stock_num = mysql_num_rows($stock_result);
		$i=0;
		$length_tot=0;
		$min_stock_length=0;
		while ($i<$stock_num) {
			$length = mysql_result($stock_result, $i, "length");
			$quantity = mysql_result($stock_result, $i, "quantity");
			$minimum = mysql_result($stock_result, $i, "minimumStock");
			$length_tot=$length_tot + ($length*$quantity);
			$min_stock_length =  $min_stock_length + ($length*$minimum);
		$i++;
		}	
		
		$table_name8 = $_SESSION['db_prefix']."_captured_timber";
		$pre_reserved_query = "SELECT SUM(prereserved) AS tot_prereserved FROM $table_name8 WHERE cap_depth = '".$depth."' AND cap_width = '".$width."' AND cap_class='".$class."' AND cap_timber_group <> 'Cash sale' GROUP BY cap_depth, cap_width, cap_class  ";
		$pre_reserved_result = mySQL_query($pre_reserved_query);
		$pre_reserved_num = mysql_num_rows($pre_reserved_result);
		if ($pre_reserved_num!=0) {
		$tot_prereserved = mysql_result($pre_reserved_result, 0, "tot_prereserved");
		}
		else {
		$tot_prereserved = 0;
		}
				
		$table_name8 = $_SESSION['db_prefix']."_captured_timber";
		$reserved_query = "SELECT SUM(reserved-delivered) AS tot_reserved FROM $table_name8 WHERE cap_depth = '".$depth."' AND cap_width = '".$width."' AND cap_class='".$class."' GROUP BY cap_depth, cap_width, cap_class  ";
		$reserved_result = mySQL_query($reserved_query);
		$reserved_num = mysql_num_rows($reserved_result);
		if ($reserved_num!=0) {
		$tot_reserved = mysql_result($reserved_result, 0, "tot_reserved");
		}
		else {
		$tot_reserved = 0;
		}
		$balance = ($length_tot/1000) - $tot_prereserved - $tot_reserved;
		
		$prereserved_query = "SELECT cap_job, job_no, cust_id, project_no, project_place, SUM(prereserved) AS summed_prereserved FROM $table_name5, $table_name6 WHERE cap_job=job_no AND cap_width = '".$width."' AND cap_depth = '".$depth."' AND cap_class='".$class."' AND prereserved<>0 GROUP BY cap_job, cap_depth, cap_width, cap_class  ORDER BY cap_job ASC";
		$prereserved_result = mySQL_query($prereserved_query);
		$prereserved_num = mysql_num_rows($prereserved_result);
		$j=0;
		while ($j<$prereserved_num) {
		  $job_no = mysql_result($prereserved_result, $j, "cap_job");
		  $cust_id = mysql_result($prereserved_result, $j, "cust_id");
		  $project_no = mysql_result($prereserved_result, $j, "project_no");
		  $project_place = mysql_result($prereserved_result, $j, "project_place");
		  $estimated = mysql_result($prereserved_result, $j, "summed_prereserved");
		  $table_name7 = $_SESSION['db_prefix']."_customer";
			$cust_query = "SELECT comp_name FROM $table_name7 WHERE  cust_id = '".$cust_id."' ";
			$cust_result = mySQL_query($cust_query);
			$comp_name = mysql_result($cust_result, 0, "comp_name");
		 	 	
		  $tip_prereserved_text =  '<tr><td><label class=tip_text_label>'.$job_no.' </label></td><td><label class=tip_text_label>Project</label></td><td><label class=tip_text_label> '.$comp_name.'</label></td><td><label class=tip_text_label> '.$project_no.', '.$project_place.'</label></td><td align=right><label class=tip_text_label> '.$estimated.'</label></td></tr>'.$tip_prereserved_text;
		  $j++;
		}
		
		//$prereserved_cash_query = "SELECT cap_job, job_no, cust_id, project_no, project_place, SUM(prereserved) AS summed_prereserved FROM $table_name5, $table_name9 WHERE cap_job=job_no AND cap_width = '".$width."' AND cap_depth = '".$depth."' AND cap_class='".$class."' AND prereserved<>0 GROUP BY cap_job, cap_depth, cap_width, cap_class  ORDER BY cap_job ASC";
//		$prereserved_cash_result = mySQL_query($prereserved_cash_query);
//		$prereserved_cash_num = mysql_num_rows($prereserved_cash_result);
//		$n=0;
//		while ($n<$prereserved_cash_num) {
//		  $job_no_cash = mysql_result($prereserved_cash_result, $n, "cap_job");
//		  $cust_id_cash = mysql_result($prereserved_cash_result, $n, "cust_id");
//		  $project_no_cash = mysql_result($prereserved_cash_result, $n, "project_no");
//		  $project_place_cash = mysql_result($prereserved_cash_result, $n, "project_place");
//		  $estimated_cash = mysql_result($prereserved_cash_result, $n, "summed_prereserved");
//		  $table_name7 = $_SESSION['db_prefix']."_customer";
//			$cust_query = "SELECT comp_name FROM $table_name7 WHERE  cust_id = '".$cust_id_cash."' ";
//			$cust_result = mySQL_query($cust_query);
//			$comp_name_cash = mysql_result($cust_result, 0, "comp_name");
//			
//		$tip_prereserved_cash_text =  '<tr><td><label class=tip_text_label>'.$job_no_cash.' </label></td><td><label class=tip_text_label>Cash sale</label></td><td><label class=tip_text_label> '.$comp_name_cash.'</label></td><td><label class=tip_text_label> '.$project_no_cash.', '.$project_place_cash.'</label></td><td align=right><label class=tip_text_label> '.$estimated_cash.'</label></td></tr>'.$tip_prereserved_cash_text;
//		  $n++;
//		}
		
		$tip_prereserved_head='Estimated reserved:<br><table border=1 cellpadding=5 style=border-collapse:collapse; ><tr><td><label class=tip_text_label_bold>Job no</label></td><td><label class=tip_text_label_bold>Type</label></td><td><label class=tip_text_label_bold>Customer</label></td><td><label class=tip_text_label_bold>Location</label></td><td><label class=tip_text_label_bold>Estimated</label></td></tr>';
		$tip_prereserved_foot='</table><br>Click for details.';
		$prereservedTT = $tip_prereserved_head.$tip_prereserved_text.$tip_prereserved_cash_text.$tip_prereserved_foot;
  		
		
		$reserved_query = "SELECT cap_job, job_no, cust_id, project_no, project_place, SUM(reserved) AS summed_reserved FROM $table_name5, $table_name6 WHERE cap_job=job_no AND cap_width = '".$width."' AND cap_depth = '".$depth."' AND cap_class='".$class."' AND reserved<>0 GROUP BY cap_job, cap_depth, cap_width, cap_class  ORDER BY cap_job ASC";
		$reserved_result = mySQL_query($reserved_query);
		$reserved_num = mysql_num_rows($reserved_result);
		$k=0;
		while ($k<$reserved_num) {
		  $job_no = mysql_result($reserved_result, $k, "cap_job");
		  $cust_id = mysql_result($reserved_result, $k, "cust_id");
		  $project_no = mysql_result($reserved_result, $k, "project_no");
		  $project_place = mysql_result($reserved_result, $k, "project_place");
		  $actual = mysql_result($reserved_result, $k, "summed_reserved");
		  $table_name7 = $_SESSION['db_prefix']."_customer";
			$cust_query = "SELECT comp_name FROM $table_name7 WHERE  cust_id = '".$cust_id."' ";
			$cust_result = mySQL_query($cust_query);
			$comp_name = mysql_result($cust_result, 0, "comp_name");
		 	 	
		  $tip_reserved_text =  '<tr><td><label class=tip_text_label> '.$job_no.'</label></td><td><label class=tip_text_label>Project</label></td><td><label class=tip_text_label> '.$comp_name.'</label></td><td><label class=tip_text_label> '.$project_no.', '.$project_place.'</label></td><td align=right><label class=tip_text_label> '.$actual.'</label></td></tr>'.$tip_reserved_text;
		  $k++;
		}
		
		$reserved_cash_query = "SELECT cap_job, job_no, cust_id, project_no, project_place, SUM(reserved) AS summed_reserved FROM $table_name5, $table_name9 WHERE cap_job=job_no AND cap_width = '".$width."' AND cap_depth = '".$depth."' AND cap_class='".$class."' AND reserved<>0 GROUP BY cap_job, cap_depth, cap_width, cap_class  ORDER BY cap_job ASC";
		$reserved_cash_result = mySQL_query($reserved_cash_query);
		$reserved_cash_num = mysql_num_rows($reserved_cash_result);
		$r=0;
		while ($r<$reserved_cash_num) {
		  $job_no = mysql_result($reserved_cash_result, $r, "cap_job");
		  $cust_id = mysql_result($reserved_cash_result, $r, "cust_id");
		  $project_no = mysql_result($reserved_cash_result, $r, "project_no");
		  $project_place = mysql_result($reserved_cash_result, $r, "project_place");
		  $actual = mysql_result($reserved_cash_result, $r, "summed_reserved");
		  $table_name7 = $_SESSION['db_prefix']."_customer";
			$cust_query = "SELECT comp_name FROM $table_name7 WHERE  cust_id = '".$cust_id."' ";
			$cust_result = mySQL_query($cust_query);
			$comp_name = mysql_result($cust_result, 0, "comp_name");
		 	 	
		  $tip_reserved_cash_text =  '<tr><td><label class=tip_text_label> '.$job_no.'</label></td><td><label class=tip_text_label>Cash sale</label></td><td><label class=tip_text_label> '.$comp_name.'</label></td><td><label class=tip_text_label> '.$project_no.', '.$project_place.'</label></td><td align=right><label class=tip_text_label> '.$actual.'</label></td></tr>'.$tip_reserved_text;
		  $r++;
		}
		
		
		$tip_reserved_head='Actual reserved:<br><table border=1 cellpadding=5 style=border-collapse:collapse; ><tr><td><label class=tip_text_label_bold>Job no</label></td><td><label class=tip_text_label_bold>Type</label></td><td><label class=tip_text_label_bold>Customer</label></td><td><label class=tip_text_label_bold>Location</label></td><td><label class=tip_text_label_bold>Actual</label></td></tr>';
		$tip_reserved_foot='</table><br>Click for details.';
		$reservedTT = $tip_reserved_head.$tip_reserved_text.$tip_reserved_cash_text.$tip_reserved_foot;
  		?>
    </tr>
    <tr>
      <td><a href="store_timber_list.php?width=<?php echo $width?>&amp;depth=<?php echo $depth ?>&class=<?php echo $class ?>&single=yes"><?php echo $depth." x ".$width." (".$class.")" ?></a></td>
      <td align="right"><?php echo number_format($length_tot/1000, 2, '.', ',') ?></td>
      <td align="right"><a href="timber_reserved_list.php?width=<?php echo $width?>&depth=<?php echo $depth?>&class=<?php echo $class ?>" onMouseover="ddrivetip('<?php echo $prereservedTT ?>  ', 500)"; onMouseout="hideddrivetip()"><?php echo number_format($tot_prereserved, 2, '.', ',') ?></a></td>
      <td align="right"><a href="timber_reserved_list.php?width=<?php echo $width?>&depth=<?php echo $depth?>&class=<?php echo $class ?>" onMouseover="ddrivetip('<?php echo $reservedTT ?>  ', 400)"; onMouseout="hideddrivetip()"><?php echo number_format($tot_reserved, 2, '.', ',') ?></a></td>
      <td align="right"><?php echo number_format($balance, 2, '.', ',') ?></td>
      <td align="right"><?php echo number_format($min_stock_length/1000, 2, '.', ',') ?></td>
    </tr>
    <?php
	$s++;
	}
	?>
  </table>
  <p>&nbsp;</p>        
          
        </div></td>
    </tr>
    <tr>
      <td align="left">&nbsp;</td>
    </tr>
  </table>
  <!--end of tableMain--> 
  
</div>
</body>
<?php } else {
$_SESSION['denied_return'] = $_SESSION['location'];	
include ("../navigation/store_sidebar.php");?>
<body onLoad="accessdenied(); return false">
</body>
<?php }
?>
<?php include ('../navigation/side_bar.php'); ?>
</html>