<head>

<?php
include ("../navigation/header.php");
$page_name = "Production-edit cutting bill";
include ("../navigation/access.php");

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

$job_no = $_GET['job_no'];
$screen = $_GET['screen'];
$_SESSION['edit_start'] = $_GET['edit'];
$sub_category = $_GET['sub_category'];
$quote='no';
$complete_alert=0;

$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_num = mysql_num_rows($bill_id_result);
if ($bill_id_num!=0) {
$bill_id = mysql_result($bill_id_result, 0, "bill_id"); }
else {
	$bill_id=0;
}


if ($_SESSION['edit_start']==0) {
	$table_name = $_SESSION['db_prefix']."_cutting_bill_lines_edit_temp";
	$delete_query = "DELETE FROM $table_name WHERE  bill_id = '". $bill_id."' AND user =  '".$_SESSION['username']."'  "; 
	execsql($delete_query);
}

$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";
			$complete_alert=1;
		} 
$table_name2 = $_SESSION['db_prefix']."_cutting_bill_lines_edit_temp";
$truss_query = "SELECT * FROM $table_name WHERE bill_id = '".$bill_id."' AND type='truss' UNION ALL SELECT * FROM $table_name2 WHERE bill_id = '".$bill_id."' AND type='truss' AND user =  '".$_SESSION['username']."'";
$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";
			$complete_alert=1;
		} 
$table_name2 = $_SESSION['db_prefix']."_cutting_bill_lines_edit_temp";
$table_name3 = $_SESSION['db_prefix']."_store_items";
$timber_query = "SELECT $table_name.* FROM $table_name, $table_name3 WHERE bill_id = '".$bill_id."' AND TYPE='timber' AND $table_name.store_item_id = $table_name3.store_item_id AND class='S5' UNION SELECT $table_name2.* FROM $table_name2, $table_name3 WHERE bill_id = '".$bill_id."' AND TYPE='timber' AND $table_name2.store_item_id = $table_name3.store_item_id AND class='S5'AND  user =  '".$_SESSION['username']."'";
$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='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";
			$complete_alert=1;
		}

$table_name2 = $_SESSION['db_prefix']."_cutting_bill_lines_edit_temp";
$table_name3 = $_SESSION['db_prefix']."_store_items";
$timber7_query = "SELECT $table_name.* FROM $table_name, $table_name3 WHERE bill_id = '".$bill_id."' AND TYPE='timber' AND $table_name.store_item_id = $table_name3.store_item_id AND class='S7' UNION SELECT $table_name2.* FROM $table_name2, $table_name3 WHERE bill_id = '".$bill_id."' AND TYPE='timber' AND $table_name2.store_item_id = $table_name3.store_item_id AND class='S7' AND user =  '".$_SESSION['username']."'";
$timber7_result = mySQL_query($timber7_query);
$timber7_num = mysql_num_rows($timber7_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";
			$complete_alert=1;
		} 
$table_name2 = $_SESSION['db_prefix']."_cutting_bill_lines_edit_temp";
$plates_query = "SELECT * FROM $table_name WHERE bill_id = '".$bill_id."' AND type='plate' UNION SELECT * FROM $table_name2 WHERE bill_id = '".$bill_id."' AND type='plate' 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, 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");

if ($sub_category=='Truss') {
	$file_open = '../edit_windows/trusses_edit_add.php?job_no='.$job_no.'&bill_id='.$bill_id.'&sub_category=Trusses';
}

if ($sub_category=='t5')  {
	$file_open = '../edit_windows/timber_add.php?bill_id='.$bill_id.'&job_no='.$job_no.'&class=S5';
}

if ($sub_category=='t7')  {
	$file_open = '../edit_windows/timber_add.php?bill_id='.$bill_id.'&job_no='.$job_no.'&class=S7';
}

if ($sub_category=='Plates')  {
	$file_open = '../edit_windows/plates_add.php?bill_id='.$bill_id.'&job_no='.$job_no;
}


?>

<!--style sheets-->
<link rel="stylesheet" type="text/css" href="../css/main.css" />
<link rel="stylesheet" href="../css/dhtmlwindow.css" type="text/css" />
<link rel="stylesheet" href="../css/modal.css" type="text/css" />
<link rel="stylesheet" type="text/css" href="../css/tooltip.css" />
<link href="../css/wait.css" rel="stylesheet" type="text/css">

<!--jscript-->
<script type="text/javascript" src="../css/modal.js"></script>
<script type="text/javascript" src="../css/dhtmlwindow.js"></script>
<script language="JavaScript" src="../jscript/cancel_confirmation.js"  type="text/javascript"></script> 
<script language="JavaScript" src="../jscript/confirm_exit.js"  type="text/javascript"></script>
<script language="JavaScript" src="../jscript/show_hide_layers.js"  type="text/javascript"></script>
<script language="JavaScript" src="../jscript/tooltip.js"  type="text/javascript"></script> \
<script language="JavaScript" src="../jscript/alert_message.js"  type="text/javascript"></script>





<link href="../css/wait.css" rel="stylesheet" type="text/css">
<?php include('../include/wait.php') ?>


</head>



<body onLoad="MM_showHideLayers('wait','','hide'); <?php if ($screen==1) {  ?>needToConfirm = false; editwindow=dhtmlmodal.open('editBox', 'iframe', '<?php echo $file_open ?>', 'Add item', 'width=500px,height=200px,center=1,resize=0,scrolling=0');  editwindow.onclose=function(){return window}; return false;<?php } ?>; <?php if ($complete_alert==1) { ?> alertMessage(1, '', 'This job is completed.<br>Any additions and/or edits are not effecting the reserved stock figures, but do have effect on the final job costing.')<?php } ?>; MM_showHideLayers('wait','','hide') "  onFocus="MM_showHideLayers('wait','','hide');">




<div class="main_frame" style="height:auto;">
<form name="form1" id="form1" action="cutting_bill_edit_save.php" method="POST" >
  <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">Edit Cutting bill</label>
              <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') ;  
	
		?>
          <input type="hidden" name="job_no" id="job_no" value="<?php echo $job_no?>">
        </div>
        <br />
        </td>
      <!--center of tableMain--> 
    </tr>

    <tr>
      <td height="93"><!--center of tableMain-->

   <div class="page_middle_frame">
 <table width="675" id="table_details_display" > 
          <label class="label_std" style="font-size:18px">Trusses</label>
            <tr>
              <th width="114" align="left">Truss Ref</th>
              <th width="110" align="center">Qty</th>
              <th width="113" align="center">Span</th>
              <th width="117" align="center">FT</th>
              <th width="180" align="center">Total
                FT</th>
            </tr>
            <?php
				$i=0;
				$sub_tot=0;
				$a6_total=0;
				while ($i<$truss_num){
					 $_SESSION['edit_truss_line'.$i] = mysql_result($truss_result, $i, "bill_line_id");
					 $_SESSION['edit_truss_type_'.$i] = mysql_result($truss_result, $i, "truss_type");
					 	
					 
					 if ($_SESSION['edit_start']==0) {
						$_SESSION['edit_truss_quantity_'.$i] = mysql_result($truss_result, $i, "quantity");
						$_SESSION['edit_truss_span_'.$i] = mysql_result($truss_result, $i, "span");
					    $_SESSION['edit_truss_press_'.$i] = mysql_result($truss_result, $i, "press_points");	
						if ($_SESSION['edit_truss_quantity_'.$i]=='') {
						       $_SESSION['edit_truss_quantity_'.$i] = mysql_result($truss_result, $i, "quantity");
							   $_SESSION['edit_truss_span_'.$i] = mysql_result($truss_result, $i, "span");
					 		   $_SESSION['edit_truss_press_'.$i] = mysql_result($truss_result, $i, "press_points");
						}
						if ($_SESSION['edit_truss_quantity_'.$i]=='0') {
						       $_SESSION['edit_truss_quantity_'.$i] = mysql_result($truss_result, $i, "quantity");
							   $_SESSION['edit_truss_span_'.$i] = mysql_result($truss_result, $i, "span");
					 		   $_SESSION['edit_truss_press_'.$i] = mysql_result($truss_result, $i, "press_points");
						}
						}
						
						if ($_SESSION['edit_truss_line'.$i]=='0') {
							   $_SESSION['edit_truss_quantity_'.$i] = mysql_result($truss_result, $i, "quantity");
							   $_SESSION['edit_truss_span_'.$i] = mysql_result($truss_result, $i, "span");
							   $_SESSION['edit_truss_press_'.$i] = mysql_result($truss_result, $i, "press_points");
						}
										 
					 
					 //$a4 = mysql_result($truss_result, $i, "height");
					 
					 
				 ?>
            <tr>
              <td align="left" valign="top"><?php echo $_SESSION['edit_truss_type_'.$i] ?></td>
              <td align="center" valign="top">
              <input name="truss_edit<?php echo $i?>" type="text" class="text60" id="truss_edit<?php echo $i?>" value="<?php echo $_SESSION['edit_truss_quantity_'.$i] ?>" onfocus = "this.select();" style="text-align:right; "/></td>
              <td align="center" valign="top"><input name="truss_edit_span<?php echo $i?>" type="text" class="text60" id="truss_edit_span<?php echo $i?>" value="<?php echo $_SESSION['edit_truss_span_'.$i] ?>" onfocus = "this.select();" style="text-align:right; "/></td>
              <td align="center" valign="top"><input name="truss_edit_press<?php echo $i?>" type="text" class="text60" id="truss_edit_press<?php echo $i?>" value="<?php echo $_SESSION['edit_truss_press_'.$i] ?>" onfocus = "this.select();" style="text-align:right; "/>                </td>
              <td align="center" valign="top"><?php $a6 = $_SESSION['edit_truss_quantity_'.$i]*$_SESSION['edit_truss_press_'.$i]; 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>
 <p>
   <input name="truss_button" type="submit" class="imagebutton" id="truss_button" value="Add truss" onClick="needToConfirm = false; this.form.action='cutting_bill_edit_temp_save.php'; "/>
   <input name="truss_num" id="truss_num" type="hidden" value="<?php echo $truss_num?>" />
    <input name="bill_id" id="bill_id" type="hidden" value="<?php echo $bill_id?>" />
 </p>
   </div>    
          
          
  <div class="page_middle_frame">
  <label class="label_std" style="font-size:18px">Timber class 5</label>
  <table width="676" align="left" id="table_details_display" >
    
    <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";
								}
								$table_name2 = $_SESSION['db_prefix']."_cutting_bill_lines_edit_temp";
								$check_query = "SELECT quantity FROM $table_name WHERE store_item_id = '".$store_item_id."' AND job_no =  '".$job_no."' UNION ALL SELECT quantity FROM $table_name2 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){
					 $_SESSION['edit_t5_line'.$j] = mysql_result($timber_result, $j, "bill_line_id");
					 $_SESSION['edit_t5_store_item_id_'.$j] = mysql_result($timber_result, $j, "store_item_id");
					 
					 if ($_SESSION['edit_start']==0) {
						$_SESSION['edit_t5_quantity_'.$j] = mysql_result($timber_result, $j, "quantity");	
						if ($_SESSION['edit_t5_quantity_'.$j]=='') {
						       $_SESSION['edit_t5_quantity_'.$j] = mysql_result($timber_result, $j, "quantity");
						}
						if ($_SESSION['edit_t5_quantity_'.$j]=='0') {
						       $_SESSION['edit_t5_quantity_'.$j] = mysql_result($timber_result, $j, "quantity");
						}
						}
						
						if ($_SESSION['edit_t5_line'.$j]==0) {
							  $_SESSION['edit_t5_quantity_'.$j] = 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 = '".$_SESSION['edit_t5_store_item_id_'.$j]."' ";
						$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*$_SESSION['edit_t5_quantity_'.$j])/1000;
					 
					 if ( ($width==$width_sel) & ($depth==$depth_sel) & ($class==$class_sel) ) {
				 ?>
                  <tr>
                    <td width="164" align="left" valign="top"><?php echo $b4 ?> mm 
                      <input name="cap_timber_bracing<?php echo $j?>" type="hidden" value="<?php echo $depth."x".$width." (".$class.")" ?>"></td>
                    <td width="97" align="center" valign="top"><input name="t5_edit<?php echo $j?>" type="text" class="text60" id="t5_edit<?php echo $j?>" value="<?php echo $_SESSION['edit_t5_quantity_'.$j] ?>" onfocus = "this.select();" style="text-align:right; "/><input name="bracing_original_qty<?php echo $j?>" type="hidden" value="<?php echo $_SESSION['edit_t5_quantity_'.$j] ?>">
                      <?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
		}
		?>
                <input name="t5_num" id="t5_num" type="hidden" value="<?php echo $timber_num?>" /></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>
    <p>&nbsp;      </p>
    
          <p>&nbsp;</p>
          <p>&nbsp;</p>
          <p>&nbsp;</p>
          <p>
            <input name="t5_button" type="submit" class="imagebutton" id="t5_button" value="Add class 5 timber" onClick="needToConfirm=false; this.form.action='cutting_bill_edit_temp_save.php'; "/>
            <label class="label_std" style="font-size:18px"><br>
            </label>
                   
        </div>
        <br />
      
  <div class="page_middle_frame">
  <label class="label_std" style="font-size:18px">Timber class 7</label>
  <table width="676" align="left" id="table_details_display" >
    
    <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='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_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";
								}
								$table_name2 = $_SESSION['db_prefix']."_cutting_bill_lines_edit_temp";
								$check_query = "SELECT quantity FROM $table_name WHERE store_item_id = '".$store_item_id."' AND job_no =  '".$job_no."' UNION ALL SELECT quantity FROM $table_name2 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<$timber7_num){
					
					 $_SESSION['edit_t7_line'.$j] = mysql_result($timber7_result, $j, "bill_line_id");
					 
					 $_SESSION['edit_t7_store_item_id_'.$j] = mysql_result($timber7_result, $j, "store_item_id");
					 
					 if ($_SESSION['edit_start']==0) {
						$_SESSION['edit_t7_quantity_'.$j] = mysql_result($timber7_result, $j, "quantity");	
						if ($_SESSION['edit_t7_quantity_'.$j]=='') {
						       $_SESSION['edit_t7_quantity_'.$j] = mysql_result($timber7_result, $j, "quantity");
						}
						if ($_SESSION['edit_t7_quantity_'.$j]=='0') {
						       $_SESSION['edit_t7_quantity_'.$j] = mysql_result($timber7_result, $j, "quantity");
						}
						}
						
						if ($_SESSION['edit_t7_line'.$j]==0) {
							  $_SESSION['edit_t7_quantity_'.$j] = mysql_result($timber7_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 = '".$_SESSION['edit_t7_store_item_id_'.$j]."' ";
						$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*$_SESSION['edit_t7_quantity_'.$j])/1000;
					 
					 if ( ($width==$width_sel) & ($depth==$depth_sel) & ($class==$class_sel) ) {
				 ?>
                  <tr>
                    <td width="164" align="left" valign="top"><?php echo $b4 ?> mm 
                      <input name="cap_timber_sundries<?php echo $j?>" type="hidden" value="<?php echo $depth."x".$width." (".$class.")" ?>"></td>
                    <td width="97" align="center" valign="top"><input name="t7_edit<?php echo $j?>" type="text" class="text60" id="t7_edit<?php echo $j?>" value="<?php echo $_SESSION['edit_t7_quantity_'.$j] ?>" onfocus = "this.select();" style="text-align:right; "/>
                      <input name="sundries_original_qty<?php echo $j?>" type="hidden" value="<?php echo $_SESSION['edit_t7_quantity_'.$j] ?>">
                      <?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
		}
		?>
                <input name="t7_num" id="t7_num" type="hidden" value="<?php echo $timber7_num?>" /></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 $s7_length ?>&nbsp;m</label>--><b><?php echo number_format($sub_total, 2, '.', ',') ?>&nbsp;m</b></td>
              <td colspan="2" >&nbsp;</td>
            </tr>
      </table>
    <p>&nbsp;      </p>
    
          <p>&nbsp;</p>
          <p>&nbsp;</p>
          <p>&nbsp;</p>
          <p>
            <input name="t7_button" type="submit" class="imagebutton" id="t7_button" value="Add class S7 timber" 
            onClick="needToConfirm=false; this.form.action='cutting_bill_edit_temp_save.php'; "/>
                              
        </div>        
          
  <div class="page_middle_frame">
    <label class="label_std" style="font-size:18px">Plates</label>
    <table width="676" align="left" 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){
					$_SESSION['edit_plates_line'.$k] = mysql_result($plates_result, $k, "bill_line_id");
					 $_SESSION['edit_plates_store_item_id'.$k] = mysql_result($plates_result, $k, "store_item_id");	
					 
					 if ($_SESSION['edit_start']==0) {
						$_SESSION['edit_plates_quantity_'.$k] = mysql_result($plates_result, $k, "quantity");	
						if ($_SESSION['edit_plates_quantity_'.$k]=='') {
						       $_SESSION['edit_plates_quantity_'.$k] = mysql_result($plates_result, $k, "quantity");
						}
						if ($_SESSION['edit_plates_quantity_'.$k]=='0') {
						       $_SESSION['edit_plates_quantity_'.$k] = mysql_result($plates_result, $k, "quantity");
						}
						}
						
						if ($_SESSION['edit_plates_line'.$k]=='0') {
							   $_SESSION['edit_plates_quantity_'.$k] = mysql_result($plates_result, $k, "quantity");
						}
					 $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 = '".$_SESSION['edit_plates_store_item_id'.$k]."' ";
						$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"><input name="plates_edit<?php echo $k?>" type="text" class="text60" id="plates_edit<?php echo $k?>" value="<?php echo $_SESSION['edit_plates_quantity_'.$k] ?>" onfocus = "this.select();" style="text-align:right; "/></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>--><input name="plates_num" id="plates_num" type="hidden" value="<?php echo $plates_num?>" /></td>
      </tr>
  </table>
    <p>
      <input name="plates_button" type="submit" class="imagebutton" id="plates_button" value="Add plates" 
      onClick="needToConfirm=false; this.form.action='cutting_bill_edit_temp_save.php';  "/>
    </p>
  </div>   
    
  <div class="page_middle_frame">
    <table width="645" border="0">
      <tr>
      <td width="20"></td>
      <td width="615" align="right"><input type="hidden" name="job_no" id="job_no" value="<?php echo $job_no ?>" />
        <input name="button2" type="button" class="imagebutton" id="button2" onClick="cancel_confirmation(3, '../production/production_main.php');" value="Close"/>        
        <input name="save_button"  id="save_button" type="submit" class="imagebutton" value="Save" onClick="needToConfirm = false; confirmRefresh(); MM_showHideLayers('wait','','show');"/>        <input name="save_close_button"  id="save_close_button" type="submit" class="imagebutton" value="Save &amp; Close" onClick="needToConfirm = false; confirmRefresh(); MM_showHideLayers('wait','','show');" /></td>
    </tr>
</table>
  </div>
  </td>
  </tr>
  </table>
</form>
  </div>
  <!--end of tableMain--> 
  
</body>
<?php include ('../navigation/side_bar.php'); ?>


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