<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<?php
include ("../navigation/header.php");
$page_name = "Sales-main";
include ("../navigation/access.php");

if ($permission_num==1) {
	include ("../navigation/production_sidebar.php");

$job_no = $_GET['job_no'];
$quote='no';
$table_name = $_SESSION['db_prefix']."_captured_jobs";
$job_query = "SELECT * FROM $table_name WHERE job_no = '".$job_no."' ";
$job_result = mySQL_query($job_query);
$cust_id = mysql_result($job_result, 0, "cust_id");	

$table_name2 = $_SESSION['db_prefix']."_customer";
		$customer_query = "SELECT * FROM $table_name2 WHERE cust_id ='".$cust_id."' ";
		$customer_result = mySQL_query($customer_query);
		$customer_num = mysql_num_rows($customer_result);	
		$a1 = mysql_result($customer_result, 0, "account_type");
			if ($a1=="corporate") { 
				$name = mysql_result($customer_result, 0, "comp_name"); 
			} 
			else {
				$name = mysql_result($customer_result, 0, "title")." ".mysql_result($customer_result, 0, "name_2")." ".mysql_result($customer_result, 0, "comp_name");
 			}
	
$table_name6 = $_SESSION['db_prefix']."_cutting_bill";
$bill_id_query = "SELECT bill_id FROM $table_name6 WHERE job_no = '".$job_no."' ";
$bill_id_result = mySQL_query($bill_id_query);
$bill_id = mysql_result($bill_id_result, 0, "bill_id");


$table_name = $_SESSION['db_prefix']."_captured_jobs";
$job_query = "SELECT * FROM $table_name WHERE job_no = '".$job_no."' ";
$job_result = mySQL_query($job_query);
$job_no = mysql_result($job_result, 0, "job_no");
$cust_id = mysql_result($job_result, 0, "cust_id");


$table_name = $_SESSION['db_prefix']."_cutting_bill_lines";
$check_query = "SELECT job_no FROM $table_name WHERE job_no = '".$job_no."' AND type='truss' ";
$check_result = mySQL_query($check_query);
$check_num = mysql_num_rows($check_result);
if ($check_num>0) {
	   		$table_name = $_SESSION['db_prefix']."_cutting_bill_lines";  
}
else {
			$table_name = $_SESSION['db_prefix']."_cutting_bill_lines_done";
} 
$truss_query = "SELECT * FROM $table_name WHERE bill_id = '".$bill_id."' AND type='truss' ORDER BY truss_type";
$truss_result = mySQL_query($truss_query);
$truss_num = mysql_num_rows($truss_result);

$table_name = $_SESSION['db_prefix']."_cutting_bill_lines";
$check_query = "SELECT job_no FROM $table_name WHERE job_no = '".$job_no."' AND type='timber' ";
$check_result = mySQL_query($check_query);
$check_num = mysql_num_rows($check_result);
if ($check_num>0) {
	   $table_name = $_SESSION['db_prefix']."_cutting_bill_lines";  
}
else {
	$table_name = $_SESSION['db_prefix']."_cutting_bill_lines_done";
}
$timber_query = "SELECT * FROM $table_name WHERE bill_id = '".$bill_id."' AND type='timber' ";
$timber_result = mySQL_query($timber_query);
$timber_num = mysql_num_rows($timber_result);

$table_name = $_SESSION['db_prefix']."_cutting_bill_lines";
$check_query = "SELECT job_no FROM $table_name WHERE job_no = '".$job_no."' AND type='plate' ";
$check_result = mySQL_query($check_query);
$check_num = mysql_num_rows($check_result);
if ($check_num>0) {
	   $table_name = $_SESSION['db_prefix']."_cutting_bill_lines";  
}
else {
	$table_name = $_SESSION['db_prefix']."_cutting_bill_lines_done";
}
$plates_query = "SELECT * FROM $table_name WHERE bill_id = '".$bill_id."' AND type='plate' ";
$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, plate_area  FROM $table_name WHERE job_no  = '".$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");
$plate_area = mysql_result($length_result, 0, "plate_area");
?>
<!--style sheets-->
<link rel="stylesheet" type="text/css" href="../css/tooltip.css" />

<!--jscript-->
<script language="JavaScript" src="../jscript/tooltip.js"  type="text/javascript"></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 width="482"><label class="label_page_head">Cutting bill</label>&nbsp;&nbsp;&nbsp;<label class="label_page_head_2"><?php echo $name ?></label>
                &nbsp;&nbsp;&nbsp;
                </td>
              <td width="198"><label style="font-size:24px; color:#FB6808;"><?php echo "J".$job_no ?></label></td>
            </tr>
          </table>
        </div>
        <br />
        <br />
        <br />
        <div class="page_middle_frame">
          <?php 
		include ('../include/customer_block.php');
		echo '<br>';
		include ('../include/job_block.php')
		?>
        </div>
        <br />
        <p><br />
        </p></td>
      <!--center of tableMain--> 
    </tr>
    <tr>
      <td height="93"><!--center of tableMain-->
        
        <div class="page_middle_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, "bill_id");
					 $a1 = mysql_result($truss_result, $i, "truss_type");
					 $a2 = mysql_result($truss_result, $i, "quantity");
					 $a3 = mysql_result($truss_result, $i, "span");
					 //$a4 = mysql_result($truss_result, $i, "height");
					 $a5 = mysql_result($truss_result, $i, "press_points");
					 
				 ?>
            <tr>
              <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="page_middle_frame">
          <label class="label_std" style="font-size:18px">Timber class 5</label>
          <table width="588" id="table_details_display" cellpadding="0">
            <tr>
              <th width="156" align="left">Item</th>
              <th width="87" align="center">Qty</th>
              <th width="149" align="center">Total length</th>
              <th align="center">All total</th>
              <th width="74" align="center">&nbsp;</th>
              <th width="15" align="center">&nbsp;</th>
            </tr>
            <?php 
			 $sub_total=0;
				$sub_total2=0;
				$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_bill_lines";
								$check_query = "SELECT job_no FROM $table_name WHERE job_no = '".$job_no."' AND type='timber' ";
								$check_result = mySQL_query($check_query);
								$check_num = mysql_num_rows($check_result);
								if ($check_num>0) {
	   									$table_name = $_SESSION['db_prefix']."_cutting_bill_lines";  
								}
								else {
										$table_name = $_SESSION['db_prefix']."_cutting_bill_lines_done";
								} 
								$check_query = "SELECT quantity FROM $table_name WHERE store_item_id = '".$store_item_id."' AND job_no =  '".$job_no."' ";
								
								$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="6"><table width="489" id="table_details_display">
                  <tr>
                    <td colspan="4" align="left" valign="top"><label class="label_std"><?php echo $depth." x ".$width."&nbsp;(".$class.")" ?></label></td>
                  </tr>
                  <?php
				$j=0;
				$b6=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;
					 
					 if ( ($width==$width_sel) & ($depth==$depth_sel) & ($class==$class_sel) ) {
				 ?>
                  <tr>
                    <td width="164" align="left" valign="top"><?php echo $b4 ?> mm</td>
                    <td width="97" align="center" valign="top"><?php echo $b2 ?>
                      <?php $b6 = $b6+$b5;?></td>
                    <td width="116" align="center" valign="top"><?php echo number_format($b5 , 2, '.', ',')?>&nbsp;m</td>
                    <td width="92" align="right" valign="top">&nbsp;</td>
                  </tr>
                  <?php 
			 }
			 ?>
                  <?php
				$j++;
				 }
				 ?>
                  <tr>
                    <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>
                  </tr>
                </table>
                <?php
		}
		?></td>
            </tr>
            <?php
	  $n++;
					  }
	  ?>
            <tr>
              <td colspan="3" align="right" valign="top"><label class="label_std">Total:</label></td>
              <td width="91" align="right" valign="top"><!--<label class="label_std">estimated:&nbsp;&nbsp;<?php echo $s5_length ?>&nbsp;m</label>--><b><?php echo number_format($sub_total, 2, '.', ',') ?>&nbsp;m</b></td>
              <td colspan="2" >&nbsp;</td>
            </tr>
          </table>
          <label class="label_std" style="font-size:18px"><br>
            Timber class 7</label>
          <table width="588" id="table_details_display">
            <tr>
              <th width="175" align="left">Item</th>
              <th width="90" align="left">Qty</th>
              <th width="118" align="center">Total length</th>
              <th width="98" align="center">All total</th>
              <th width="12" align="center">&nbsp;</th>
              <th width="67" align="center">&nbsp;</th>
            </tr>
            <?php 
				  	$b6=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'  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_bill_lines";
								$check_query = "SELECT job_no FROM $table_name WHERE job_no = '".$job_no."' AND type='timber' ";
								$check_result = mySQL_query($check_query);
								$check_num = mysql_num_rows($check_result);
								if ($check_num>0) {
	   									$table_name = $_SESSION['db_prefix']."_cutting_bill_lines";  
								}
								else {
										$table_name = $_SESSION['db_prefix']."_cutting_bill_lines_done";
								} 
								$check_query = "SELECT quantity FROM $table_name WHERE store_item_id = '".$store_item_id."' AND job_no =  '".$job_no."' ";
								
								$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="6" align="left" valign="top"><label class="label_std"><?php echo $depth." x ".$width."&nbsp;(".$class.")" ?></label></td>
            </tr>
            <tr>
              <td colspan="6"><table width="494" id="table_details_display">
                  <?php
				$j=0;
				$b6=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;
					 
					 if ( ($width==$width_sel) & ($depth==$depth_sel) & ($class==$class_sel) ) {
				 ?>
                  <tr>
                    <td width="165" align="left" valign="top"><?php echo $b4 ?> mm</td>
                    <td width="96" align="center" valign="top"><?php echo $b2 ?>
                      <?php $b6 = $b6+$b5;?></td>
                    <td width="116" align="center" valign="top"><?php echo number_format($b5, 2, '.', ',') ?>&nbsp;m</td>
                    <td width="97" align="right" valign="top">&nbsp;</td>
                  </tr>
                  <?php 
			 }
				
			 ?>
                  <?php
				$j++;
				 }
				 ?>
                  <tr>
                    <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, '.', ',') ?>&nbsp;m
                      <?php $sub_total2=$sub_total2+$b6?></td>
                  </tr>
                </table>
                <?php
						}
						?></td>
            </tr>
            <?php
	  $n++;
					  }
	  ?>
            <tr>
              <td colspan="3" align="right"><b>Total:</b></td>
              <td align="right"><b><?php echo number_format($sub_total2, 2, '.', ',') ?>&nbsp;&nbsp;m</b></td>
              <td colspan="2" align="right">&nbsp;</td>
            </tr>
          </table>
          <br />
        </div>
        <br />
        <div class="page_middle_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, "bill_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>
        </div></td>
    </tr>
  </table>
  <!--end of tableMain--> 
  
</div>
<?php include ('../navigation/side_bar.php'); ?>

</body>
<?php } else {
$_SESSION['denied_return'] = "sales/sales_main.php";	
include ("../navigation/sales_sidebar.php");?>
<body onLoad="accessdenied(); return false">
</body>
<?php }
?>
</html>