<head>

<?php
if ($_SESSION['page_view']=='5') {
$table_name = $_SESSION['db_prefix']."_trusses_temp";
$truss_query = "SELECT * FROM $table_name WHERE job_no = '".$_SESSION['job_no']."' AND (select_it = 'yes' OR select_it = 'add') AND user =  '".$_SESSION['username']."' ORDER BY truss_type";
$truss_result = mySQL_query($truss_query);
$truss_num = mysql_num_rows($truss_result);
}

if ($_SESSION['page_view']=='6') {
$table_name = $_SESSION['db_prefix']."_trusses_temp";
$truss_query = "SELECT * FROM $table_name WHERE job_no = '".$_SESSION['job_no']."' AND user =  '".$_SESSION['username']."' ORDER BY truss_type";
$truss_result = mySQL_query($truss_query);
$truss_num = mysql_num_rows($truss_result);
}

$table_name = $_SESSION['db_prefix']."_cutting_temp";
$timber_query = "SELECT * FROM $table_name WHERE job_id = '".$_SESSION['job_no']."'  AND user =  '".$_SESSION['username']."' ";
$timber_result = mySQL_query($timber_query);
$timber_num = mysql_num_rows($timber_result);

$table_name = $_SESSION['db_prefix']."_plates_temp";
$plates_query = "SELECT * FROM $table_name WHERE job_id = '".$_SESSION['job_no']."'  AND user =  '".$_SESSION['username']."' ";
$plates_result = mySQL_query($plates_query);
$plates_num = mysql_num_rows($plates_result);

$table_name = $_SESSION['db_prefix']."_captured_jobs";
$length_query = "SELECT S5_timber, S7_timber, timber_cost, plate_area, plate_charge  FROM $table_name WHERE job_no  = '".$_SESSION['job_no']."' ";
$length_result = mySQL_query($length_query);
$s5_length = mysql_result($length_result, 0, "S5_timber");
$s7_length = mysql_result($length_result, 0, "S7_timber");
$timber_cost = mysql_result($length_result, 0, "timber_cost");
$plate_area = mysql_result($length_result, 0, "plate_area");
$plate_charge = mysql_result($length_result, 0, "plate_charge");

$timber_message=0;
$plate_message=0;
?>

</head>
<?php include('../include/wait.php') ?>

<body onLoad="MM_showHideLayers('wait','','hide')" onFocus="MM_showHideLayers('wait','','hide');"  onBlur="MM_showHideLayers('wait','','hide');">


<div class="page_middle_frame">
<form action="trussesJobWiz8.php" method="get" >
<table id="tableMain" width="595">
    <!--begin of tableMain-->
    <tr>
<td height="93"> <!--center of tableMain-->
  <div class="wizard_frame">
    <label class="label_std" style="font-size:18px">Trusses</label>
    <table width="588" id="table_details_display">
      <tr>
        <th width="96" align="left">Truss Ref</th>
        <th width="59" align="center">Qty</th>
        <th width="87" align="center">Span</th>
        <th width="121" align="center">FT</th>
        <th width="168" align="center">Total
          FT</th>
      </tr>
      <?php
				$i=0;
				$sub_tot=0;
				$a6_total=0;
				 while ($i<$truss_num){
					 $a0 = mysql_result($truss_result, $i, "truss_id");
					 $a1 = mysql_result($truss_result, $i, "truss_type");
					 $a2 = mysql_result($truss_result, $i, "qty");
					 $a3 = mysql_result($truss_result, $i, "span");
					 $a4 = mysql_result($truss_result, $i, "select_it");
					 $a5 = mysql_result($truss_result, $i, "press_points");
					 
				 ?>
      <tr <?php if ($a4=='add') {?> style="background-color:#FFE9BB" <?php } ?> >
        <td align="left" valign="top"><?php echo $a1 ?></td>
        <td align="center" valign="top"><?php echo $a2 ?></td>
        <td align="center" valign="top"><?php echo $a3 ?></td>
        <td align="center" valign="top"><?php  echo $a5?></td>
        <td align="center" valign="top"><?php $a6 = $a2*$a5; echo $a6?></td>
      </tr>
      <?php 
			    $a6_total=$a6_total+$a6;
				$i++;
				 }
				 ?>
      <tr>
        <td height="34" valign="bottom">&nbsp;</td>
        <td valign="bottom">&nbsp;</td>
        <td colspan="2" align="right" valign="bottom"><label class="label_std">Total FT:</label></td>
        <td align="center" valign="bottom"><?php echo number_format($a6_total, 0, '.', ',');  ?></td>
      </tr>
    </table>
    </div><br />

    <div class="wizard_frame">
    <label class="label_std" style="font-size:18px">Timber class 5</label>
    
    <table width="596" cellpadding="0">
    
      <tr>
        <th width="224" align="left">Item</th>
        <th width="88" align="center">Qty</th>
        <th width="78" align="center">Total length</th>
        <th width="88" align="center">All total</th>
        <th width="104" align="center">Cost</th>
        
       
      </tr>
      
      <?php 
			 $sub_total=0;
				$sub_total2=0;
				$total=0;
			$S5_meter_cost_total=0;	  			 					  				
					  $table_name = $_SESSION['db_prefix']."_store_items";
					  $size_query = "SELECT store_item_id, width, depth, class FROM $table_name WHERE subCategory='timber' AND class='S5' AND width > '50' AND depth < '50'  GROUP BY `depth`, `width` , `class`  ";
					  $size_result = mySQL_query($size_query);
					  $size_num = mysql_num_rows($size_result);
					 
					  $n=0;	
					  
					  while ($n<$size_num) {
						$show='no'; 
						$width = mysql_result($size_result, $n, "width");  
						$depth = mysql_result($size_result, $n, "depth");
						$class = mysql_result($size_result, $n, "class");
						
						$table_name = $_SESSION['db_prefix']."_store_items";
						$name_query = "SELECT store_item_id, width, depth, length, class FROM $table_name WHERE subCategory='timber' AND width = '".$width."' AND depth = '".$depth."'  AND class='S5' ORDER BY length ASC";
						//echo $name_query;
						
						$name_result = mySQL_query($name_query);
						$name_num = mysql_num_rows($name_result);
						$t=0;
						while  ($t<$name_num) { 
							$store_item_id = mysql_result($name_result, $t, "store_item_id");
								$table_name = $_SESSION['db_prefix']."_cutting_temp";
								$check_query = "SELECT quantity FROM $table_name WHERE store_item_id = '".$store_item_id."' AND job_id =  '".$_SESSION['job_no']."' AND user =  '".$_SESSION['username']."' ";
								
								$check_result = mySQL_query($check_query);
								$check_num = mysql_num_rows($check_result);
								if ($check_num!=0) {
								  $show='yes';
								}
								$t++;
								
						}
						
						if ($show=='yes') {
					  ?>
      
      <tr>
        <td  colspan="8" style="border-bottom:hidden;">
          <table width="563" id="table_details_display">
            <tr>
              <td colspan="5" align="left" valign="top"><label class="label_std"><?php echo $depth." x ".$width."&nbsp;(".$class.")" ?></label></td>
              </tr>
            <?php
				$j=0;
				$b6=0;
				$meter_cost=0;
				$S7_meter_cost_total=0;	
				while ($j<$timber_num){
					 //$b0 = mysql_result($timber_result, $j, "bill_id");
					 $b1 = mysql_result($timber_result, $j, "store_item_id");
					 $b2 = mysql_result($timber_result, $j, "quantity");
					 $table_name = $_SESSION['db_prefix']."_store_items";
						$item_query = "SELECT item_name, length, width, depth, class FROM $table_name WHERE store_item_id = '".$b1."' ";
						$item_result = mySQL_query($item_query);
						$width_sel = mysql_result($item_result, 0, "width");
						$depth_sel = mysql_result($item_result, 0, "depth");
						$class_sel = mysql_result($item_result, 0, "class");
						$b3 = mysql_result($item_result, 0, "item_name");
						$b4 = mysql_result($item_result, 0, "length");
					 $b5 = ($b4*$b2)/1000;
					 
					 	$timber = $depth_sel.'x'.$width_sel.' ('.$class_sel.')';
					 	$table_name = $_SESSION['db_prefix']."_timber_meter_price";
						$price_query = "SELECT price FROM $table_name WHERE timber = '".$timber."' ";
						$price_result = mySQL_query($price_query);
						$price_num = mysql_num_rows($price_result);
						$meter_price = mysql_result($price_result, 0, "price");
									 
					 if ( ($width==$width_sel) & ($depth==$depth_sel) & ($class==$class_sel) ) {
				 ?>
            
            <tr>
              <td width="219" align="left" valign="top"><?php echo $b4 ?> mm</td>
              <td width="86" align="center" valign="top"><?php echo $b2 ?><?php $b6 = $b6+$b5; $meter_cost = $meter_price*$b6;?></td>
              <td width="86" align="center" valign="top"><?php echo number_format($b5 , 2, '.', ',')?>&nbsp;m</td>
              <td width="76" align="right" valign="top">&nbsp;</td>
              <td width="72" align="right" valign="top">&nbsp;</td>
              </tr>
            <?php 
			 }
			 ?>
            <?php
				$j++;
				 }
				 ?>
            <tr style="border-bottom:hidden;">
              <td align="left" valign="top">&nbsp;</td>
              <td align="left" valign="top">&nbsp;</td>
              <td align="left" valign="top">&nbsp;</td>
              <td align="right" valign="top"><?php echo number_format($b6, 2, '.', ',') ?> m<?php $sub_total=$sub_total+$b6?></td>
              <td align="right" valign="top"><?php echo number_format($meter_cost, 2, '.', ','); $S5_meter_cost_total=$S5_meter_cost_total+$meter_cost; ?>&nbsp;</td>
              </tr>
            </table>
          <?php
		}
		?>
        </td>
        </tr>
     
      <?php
	  $n++;
					  }
	  ?>
      <tfoot>
      <tr>
        <td colspan="5" align="left" valign="center" style="border-bottom:hidden;">
        <table width="563" id="table_details_display">
          <tr>
            <td width="399" align="right">Totals:</td>
            <td width="76" align="right"><b><?php echo number_format($sub_total, 2, '.', ',') ?>&nbsp;m</b></td>
            <td width="72" align="right"><?php echo number_format($S5_meter_cost_total, 2, '.', ',') ?>&nbsp;</td>
            </tr>
          </table>
          </td>
        
      </tr>
      </tfoot>
    </table>
    
      <label class="label_std" style="font-size:18px"><br>
        Timber class 7</label>
      <table width="596" cellpadding="0">
        <tr>
          <th width="224" align="left">Item</th>
          <th width="88" align="center">Qty</th>
          <th width="78" align="center">Total length</th>
          <th width="88" align="center">All total</th>
          <th width="104" align="center">Cost</th>
        </tr>
        <?php 
			 $sub_total=0;
				$sub_total2=0;
				$total=0;
			$S7_meter_cost_total=0;	  			 					  				
					  $table_name = $_SESSION['db_prefix']."_store_items";
					  $size_query = "SELECT store_item_id, width, depth, class FROM $table_name WHERE subCategory='timber' AND class='S7' AND width > '50' AND depth < '50'  GROUP BY `depth`, `width` , `class`  ";
					  $size_result = mySQL_query($size_query);
					  $size_num = mysql_num_rows($size_result);
					 
					  $n=0;	
					  
					  while ($n<$size_num) {
						$show='no'; 
						$width = mysql_result($size_result, $n, "width");  
						$depth = mysql_result($size_result, $n, "depth");
						$class = mysql_result($size_result, $n, "class");
						
						$table_name = $_SESSION['db_prefix']."_store_items";
						$name_query = "SELECT store_item_id, width, depth, length, class FROM $table_name WHERE subCategory='timber' AND width = '".$width."' AND depth = '".$depth."'  AND class='S7' ORDER BY length ASC";
						//echo $name_query;
						
						$name_result = mySQL_query($name_query);
						$name_num = mysql_num_rows($name_result);
						$t=0;
						while  ($t<$name_num) { 
							$store_item_id = mysql_result($name_result, $t, "store_item_id");
								$table_name = $_SESSION['db_prefix']."_cutting_temp";
								$check_query = "SELECT quantity FROM $table_name WHERE store_item_id = '".$store_item_id."' AND job_id =  '".$_SESSION['job_no']."' AND user =  '".$_SESSION['username']."' ";
								
								$check_result = mySQL_query($check_query);
								$check_num = mysql_num_rows($check_result);
								if ($check_num!=0) {
								  $show='yes';
								}
								$t++;
								
						}
						
						if ($show=='yes') {
					  ?>
        <tr>
          <td  colspan="8" style="border-bottom:hidden;"><table width="563" id="table_details_display">
            <tr>
              <td colspan="5" align="left" valign="top"><label class="label_std"><?php echo $depth." x ".$width."&nbsp;(".$class.")" ?></label></td>
            </tr>
            <?php
				$j=0;
				$b6=0;
				$meter_cost=0;
				
				while ($j<$timber_num){
					 //$b0 = mysql_result($timber_result, $j, "bill_id");
					 $b1 = mysql_result($timber_result, $j, "store_item_id");
					 $b2 = mysql_result($timber_result, $j, "quantity");
					 $table_name = $_SESSION['db_prefix']."_store_items";
						$item_query = "SELECT item_name, length, width, depth, class FROM $table_name WHERE store_item_id = '".$b1."' ";
						$item_result = mySQL_query($item_query);
						$width_sel = mysql_result($item_result, 0, "width");
						$depth_sel = mysql_result($item_result, 0, "depth");
						$class_sel = mysql_result($item_result, 0, "class");
						$b3 = mysql_result($item_result, 0, "item_name");
						$b4 = mysql_result($item_result, 0, "length");
					 $b5 = ($b4*$b2)/1000;
					 
					 	$timber = $depth_sel.'x'.$width_sel.' ('.$class_sel.')';
					 	$table_name = $_SESSION['db_prefix']."_timber_meter_price";
						$price_query = "SELECT price FROM $table_name WHERE timber = '".$timber."' ";
						$price_result = mySQL_query($price_query);
						$price_num = mysql_num_rows($price_result);
						$meter_price = mysql_result($price_result, 0, "price");
						
					
									 
					 if ( ($width==$width_sel) & ($depth==$depth_sel) & ($class==$class_sel) ) {
				 ?>
            <tr>
              <td width="219" align="left" valign="top"><?php echo $b4 ?> mm</td>
              <td width="86" align="center" valign="top"><?php echo $b2 ?>
                <?php $b6 = $b6+$b5; $meter_cost = $meter_price*$b6;?></td>
              <td width="86" align="center" valign="top"><?php echo number_format($b5 , 2, '.', ',')?>&nbsp;m</td>
              <td width="76" align="right" valign="top">&nbsp;</td>
              <td width="72" align="right" valign="top">&nbsp;</td>
            </tr>
            <?php 
			 }
			 
			 ?>
            <?php
				$j++;
				 }
				 ?>
            <tr style="border-bottom:hidden;">
              <td align="left" valign="top">&nbsp;</td>
              <td align="left" valign="top">&nbsp;</td>
              <td align="left" valign="top">&nbsp;</td>
              <td align="right" valign="top"><?php echo number_format($b6, 2, '.', ',') ?> m
                <?php $sub_total=$sub_total+$b6?></td>
              <td align="right" valign="top"><?php echo number_format($meter_cost, 2, '.', ','); $S7_meter_cost_total=$S7_meter_cost_total+$meter_cost; ?>&nbsp;</td>
            </tr>
          </table>
            <?php
		}
		?></td>
        </tr>
        <?php
	  $n++;
	}
	$all_meter_cost_total = $S5_meter_cost_total + $S7_meter_cost_total; 
	  ?>
        <tfoot>
          <tr>
            <td colspan="5" align="left" valign="center" style="border-bottom:hidden;"><table width="563" id="table_details_display">
              <tr>
                <td width="399" align="right">Totals:</td>
                <td width="76" align="right"><b><?php echo number_format($sub_total, 2, '.', ',') ?>&nbsp;m</b></td>
                <td width="72" align="right"><?php echo number_format($S7_meter_cost_total, 2, '.', ',') ?>&nbsp;</td>
                
              </tr>
            </table></td>
          </tr>
        </tfoot>
      </table>
      <br>
      
	Total timber cost on this cutting bill: <?php echo number_format($all_meter_cost_total, 2, '.', ',') ?>
    &nbsp;&nbsp;
 Quoted cost
   <?php echo number_format($timber_cost, 2, '.', ',') ?>
    <?php if ($all_meter_cost_total>$timber_cost) { $timber_message=1; } ?>
    &nbsp;&nbsp; Result: 
    <?php if ($timber_cost>0) { $excess_timber = (($timber_cost-$all_meter_cost_total)/$timber_cost)*100;} else { $excess_timber = 0; } echo number_format($excess_timber, 2, '.', ',') ?>
%&nbsp;
    <input name="excess_timber" type="hidden" value="<?php echo $excess_timber ?>">
    <input name="all_meter_cost_total" type="hidden" value="<?php echo $all_meter_cost_total ?>">
    <input name="timber_cost" type="hidden" value="<?php echo $timber_cost ?>">
    </div>
   <br />
   <div class="wizard_frame">
    <label class="label_std" style="font-size:18px">Plates</label>
    <table id="table_details_display">
      <tr>
        <th width="146" height="32" align="left">Item</th>
        <th width="134" align="center">Qty</th>
        <th width="126" align="center">Total area (m<sup>2</sup>)</th>
        <th width="172" align="left">&nbsp;</th>
      </tr>
      <?php
				$k=0;
				$c6tot=0;
				while ($k<$plates_num){
					 $c0 = mysql_result($plates_result, $k, "plate_id");
					 $c1 = mysql_result($plates_result, $k, "store_item_id");
					 $c2 = mysql_result($plates_result, $k, "quantity");
					 $table_name = $_SESSION['db_prefix']."_store_items";
						$item_query = "SELECT item_name, area FROM $table_name WHERE store_item_id = '".$c1."' ";
						$item_result = mySQL_query($item_query);
						$c3 = mysql_result($item_result, 0, "item_name");
						$c4 = mysql_result($item_result, 0, "area");
					 $c5 = $c4*$c2;
				 ?>
      <tr>
        <td align="left" valign="top"><?php echo $c3 ?></td>
        <td align="center" valign="top"><?php echo $c2 ?></td>
        <td align="center" valign="top"><?php echo number_format($c5, 8, '.', ',') ?>
          <?php $c6tot = $c6tot + $c5?></td>
        <td align="right" valign="top">&nbsp;</td>
      </tr>
      <?php 
			    $k++;
				 }
				 ?>
      <tr>
        <td height="33" align="left" valign="bottom">&nbsp;</td>
        <td align="right" valign="bottom">Total:</td>
        <td align="center" valign="bottom"><label class="label_std"><?php echo number_format($c6tot, 8, '.', ',') ?></label></td>
        <td align="left" valign="bottom"><b>m<sup>2</sup></b>          <!--<label class="label_std">estimated:&nbsp;&nbsp;<?php echo $plate_area ?>&nbsp;m<sup>2</sup></label>--></td>
        </tr>
    </table>
   <?php $total_plate_charge = $c6tot * $plate_charge; $quoted_charge = $plate_area*$plate_charge; if ($quoted_charge>0) { $excess_plate = (($quoted_charge-$total_plate_charge)/$quoted_charge)*100; } else {$excess_plate = 0; }?> <br>
Total plate charge on this cutting bill: <?php echo number_format($total_plate_charge, 2, '.', ',') ?>&nbsp; Quoted costs <?php echo number_format($quoted_charge, 2, '.', ',') ?>
  <?php if ($total_plate_charge>$quoted_charge) { $plate_message=1; } ?>
&nbsp;Result: <?php echo number_format($excess_plate, 2, '.', ',') ?>% 
<input name="excess_plate" type="hidden" value="<?php echo $excess_plate ?>">
  <input name="total_plate_charge" type="hidden" value="<?php echo $total_plate_charge ?>">
  <input name="quoted_charge" type="hidden" value="<?php echo $quoted_charge ?>">
   </div>
<?php if (($timber_message==1) && ($plate_message==0)) { ?> <script>alertMessage(1, '', 'The total timber costs exceeds quoted charge.<br>You need to check the quantities again.');</script><?php } ?>
<?php if (($timber_message==0) && ($plate_message==1)) { ?> <script>alertMessage(1, '', 'The total plate charge exceeds quoted charge.<br>You need to check the quantities again.');</script><?php } ?>
<?php if (($timber_message==1) && ($plate_message==1)) { ?> <script>alertMessage(1, '', 'Both total timber costs and total plate charge exceeding quoted charges.<br>You need to check the quantities again.');</script><?php } ?>  
  </td>
</tr>
      <tr>
      <td><table width="590" height="50" align="left" id="table_button_bar">
        <tr>
          <td width="117"><input type="button" name="lastprev_button" class="imagebutton" value="Prev" onClick="needToConfirm = false;  MM_showHideLayers('wait','','show'); MM_goToURL('parent','trussesJobWizbase.php?s=&amp;p=6');return document.MM_returnValue"  tabindex="2"/></td>
          <td width="115">&nbsp;</td>
          <td width="99"><input name="p" type="hidden" id="p" value="7" />
            <input name="s" type="hidden" id="s" value="7" /></td>
          <td width="125" align="right"><input name="cancelbutton" type="button" class="imagebutton" id="cancelbutton" onClick="needToConfirm = false; cancel_confirmation(1, '../production/production_main.php')" value="Cancel" tabindex="3"/></td>
          <td width="110" align="right"><input type="submit" name="next_button" class="imagebutton" value="Finish" onClick="needToConfirm = false; MM_showHideLayers('wait','','show'); "  tabindex="1"/></td>
        </tr>
      </table></td>
      </tr>
      </table>
</form>
</div>
<script>
    $(document).ready(function(){
        //iterate through each textboxes and add keyup
        //handler to trigger sum event
        $(".text100").each(function() {
             $(this).keyup(function(){
                calculateSum();
            });
        });
 
    });
 
    function calculateSum() {
         var sum = 0;
        //iterate through each textboxes and add the values
        $(".text100").each(function() {
             //add only if the value is number
            if(!isNaN(this.value) && this.value.length!=0) {
                sum += parseFloat(this.value);
            }
         });
        //.toFixed() method will roundoff the final sum to 2 decimal places
        $("#sum").html(sum.toFixed(2));
		var  sum3 = sum1 + sum2;
    }
</script>

<script>
    $(document).ready(function(){
        //iterate through each textboxes and add keyup
        //handler to trigger sum event
        $(".text100a").each(function() {
             $(this).keyup(function(){
                calculateSum2();
            });
        });
 
    });
 
    function calculateSum2() {
         var sum2 = 0;
        //iterate through each textboxes and add the values
        $(".text100a").each(function() {
             //add only if the value is number
            if(!isNaN(this.value) && this.value.length!=0) {
                sum2 += parseFloat(this.value);
            }
         });
        //.toFixed() method will roundoff the final sum to 2 decimal places
        $("#sum2").html(sum2.toFixed(2));
		var  sum3 = sum1 + sum2;
    }
</script>

</body>      