<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<?php
include ("../navigation/header.php");
$page_name = "Production-costing";
include ("../navigation/access.php");
$today = date("Y-m-d");

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");	
$captured = mysql_result($job_result, 0, "captured");	
$compiled = mysql_result($job_result, 0, "compiled");	
$cutting_bill = mysql_result($job_result, 0, "cutting_bill");	
$supplied = mysql_result($job_result, 0, "supplied");	
$authorisation = mysql_result($job_result, 0, "authorisation");	
$produced = mysql_result($job_result, 0, "produced");	
$delivered = mysql_result($job_result, 0, "delivered");

if  ( ($captured=='yes') AND ($compiled=='yes') AND ($cutting_bill=='yes') AND ($supplied=='yes') AND ($authorisation=='yes') AND ($produced=='yes') AND ($delivered=='yes') ) {
	$completed ='yes';
}
else {
	$completed = 'no';
}


$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_name = $_SESSION['db_prefix']."_cutting_bill";
$cutting_query = "SELECT * FROM $table_name WHERE job_no = '".$job_no."' ";
$cutting_result = mySQL_query($cutting_query);
$cutting_num = mysql_num_rows($cutting_result);
if ($cutting_num!=0) {
		$bill_id = mysql_result($cutting_result, 0, "bill_id");
}
else {
	$bill_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";
		} 
$produced_press_point_total = 0;
$table_name = $_SESSION['db_prefix']."_production_bill_lines";
$produced_delivered_query = "SELECT truss_type, sum(quantity) AS quantity, press_points, press_charge FROM $table_name WHERE job_no = '".$job_no."' GROUP BY truss_type";
$produced_delivered_result = mySQL_query($produced_delivered_query);
$produced_delivered_num = mysql_num_rows($produced_delivered_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";
		} 
$timber0_query = "SELECT *, SUM(quantity) AS summed_quantity FROM $table_name WHERE type = 'timber' AND bill_id = '".$bill_id."' GROUP BY store_item_id";
$timber0_result = mySQL_query($timber0_query);
$timber0_num = mysql_num_rows($timber0_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";
		} 
$plate_query = "SELECT *, SUM(quantity) AS summed_quantity FROM $table_name WHERE type = 'plate' AND bill_id = '".$bill_id."' GROUP BY store_item_id";
$plate_result = mySQL_query($plate_query);
$plate_num = mysql_num_rows($plate_result);


$table_name = $_SESSION['db_prefix']."_captured_timber";
$check_query = "SELECT cap_timber_id FROM $table_name WHERE cap_timber_group = 'Trusses' AND cap_job = '".$job_no."' AND (cap_class = 'S5' OR cap_class = 'S7')  ";
$check_result = mySQL_query($check_query);
$check_num = mysql_num_rows($check_result);
if ($check_num>0) {
      $table_name = $_SESSION['db_prefix']."_captured_timber";
}
else { 
  $table_name = $_SESSION['db_prefix']."_captured_timber_done";
}
$timber_query = "SELECT * FROM $table_name WHERE cap_timber_group = 'Trusses' AND cap_job = '".$job_no."' AND cap_class = 'S5'  ";
$timber_result = mySQL_query($timber_query);
$timber_num = mysql_num_rows($timber_result);


$timber2_query = "SELECT * FROM $table_name WHERE cap_timber_group = 'Trusses' AND cap_job = '".$job_no."' AND cap_class = 'S7' ";
$timber2_result = mySQL_query($timber2_query);
$timber2_num = mysql_num_rows($timber2_result);


$quoted_timber_query = "SELECT cap_timber, cap_timber_length, cap_timber_price FROM $table_name WHERE cap_timber_name = 'Trusses' AND cap_job = '".$job_no."'  ";
$quoted_timber_result = mySQL_query($quoted_timber_query);
$quoted_timber_num = mysql_num_rows($quoted_timber_result);

$table_name = $_SESSION['db_prefix']."_captured_timber";
$check_query = "SELECT cap_timber_id FROM $table_name WHERE (cap_timber_group = 'Bracing'  OR cap_timber_group = 'Sundries') AND cap_job = '".$job_no."' AND cap_class = 'S5'  ";
$check_result = mySQL_query($check_query);
$check_num = mysql_num_rows($check_result);
if ($check_num>0) {
      $table_name = $_SESSION['db_prefix']."_captured_timber";
}
else { 
  $table_name = $_SESSION['db_prefix']."_captured_timber_done";
}

$quoted_sundries_timber_query = "SELECT cap_timber, cap_timber_length, cap_timber_price, auth_timber_length, auth_timber_price  FROM $table_name WHERE cap_timber_group = 'Sundries' AND cap_job = '".$job_no."'  ";
$quoted_sundries_timber_result = mySQL_query($quoted_sundries_timber_query);
$quoted_sundries_timber_num = mysql_num_rows($quoted_sundries_timber_result);


$quoted_bracing_timber_query = "SELECT cap_timber, cap_timber_length, cap_timber_price, auth_timber_length, auth_timber_price  FROM $table_name WHERE cap_timber_group = 'Bracing' AND cap_job = '".$job_no."'  "; 
$quoted_bracing_timber_result = mySQL_query($quoted_bracing_timber_query);
$quoted_bracing_timber_num = mysql_num_rows($quoted_bracing_timber_result);

$table_name = $_SESSION['db_prefix']."_captured_mitek";
$mitek_check_query = "SELECT  cap_mitek_id FROM $table_name WHERE cap_job = '".$job_no."'  ";
$mitek_check_result = mySQL_query($mitek_check_query);
$mitek_check_num = mysql_num_rows($mitek_check_result);
if ($mitek_check_num!=0) { 
$table_name = $_SESSION['db_prefix']."_captured_mitek";
}
else {
$table_name = $_SESSION['db_prefix']."_captured_mitek_done";
}
$mitek_query = "SELECT cap_item_id, SUM(cap_quantity) AS summed_cap_quantity, cap_price,  SUM(auth_quantity) AS summed_delivered, del_price FROM $table_name WHERE cap_job = '".$job_no."'  GROUP BY cap_item_id";
$mitek_result = mySQL_query($mitek_query);
$mitek_num = mysql_num_rows($mitek_result);


$table_name = $_SESSION['db_prefix']."_captured_sundries";
$additional_check_query = "SELECT  cap_sundries_id FROM $table_name WHERE cap_job = '".$job_no."'  ";
$additional_check_result = mySQL_query($additional_check_query);
$additional_check_num = mysql_num_rows($additional_check_result);
if ($additional_check_num!=0) { 
$table_name = $_SESSION['db_prefix']."_captured_sundries";
}
else {
$table_name = $_SESSION['db_prefix']."_captured_sundries_done";
}
$sundries_query = "SELECT cap_item_id, SUM(cap_quantity) AS summed_cap_quantity, cap_price, SUM(auth_quantity) AS summed_delivered, del_price  FROM $table_name WHERE cap_job = '".$job_no."' GROUP BY cap_item_id";
$sundries_result = mySQL_query($sundries_query);
$sundries_num = mysql_num_rows($sundries_result);


$table_name = $_SESSION['db_prefix']."_del_timber";
$timber3_query = "SELECT store_item_id, SUM(delivered) AS summed_delivered, price FROM $table_name WHERE type = 'Bracing' and job_no = '".$job_no."' GROUP BY store_item_id";
$timber3_result = mySQL_query($timber3_query);
$timber3_num = mysql_num_rows($timber3_result);

$table_name = $_SESSION['db_prefix']."_del_timber";
$timber4_query = "SELECT store_item_id, SUM(delivered) AS summed_delivered, price FROM $table_name WHERE type = 'Sundries' and job_no = '".$job_no."' GROUP BY store_item_id";
$timber4_result = mySQL_query($timber4_query);
$timber4_num = mysql_num_rows($timber4_result);

$table_name6 = $_SESSION['db_prefix']."_variations";
$variations_query = "SELECT * FROM $table_name6 WHERE job_no = '".$job_no."' ";
$variations_result = mySQL_query($variations_query);
$variations_num = mysql_num_rows($variations_result);


$load=$_GET['load'];
if ($load!=0) {
	  $q_total=$_GET['q_total'];
	  $a_total=$_GET['a_total'];
	  
}
else {
	$q_total='0.00';
	$a_total='0.00';
	$d_total='0.00';
}
?>
<!--style sheets-->
<link rel="stylesheet" type="text/css" href="../css/tooltip.css" />


<!--jscript-->
<script language="JavaScript" src="../jscript/tooltip.js"  type="text/javascript"></script> 
<script type="text/javascript" src="../jscript/tablesort.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 width="452"><label class="label_page_head">Job costing</label>
                &nbsp;&nbsp;&nbsp;<label class="label_page_head_2"><?php echo $name; ?></label>
                </td>
              <td width="228"><label style="font-size:24px; color:#FB6808;"><?php echo "J".$job_no ?></label></td>
            </tr>
          </table>
        </div>
        <br />
        <br /></td>
      <!--center of tableMain--> 
    </tr>
    <tr>
      <td height="93"><!--center of tableMain-->
        
        <div class="page_middle_frame">
          
            <?php 
		include ('../include/customer_block.php');
		echo '<br>';
		include ('../include/job_block.php')
		?><br />
<br />

        <table width="648"  id="table_details_display">
  <tr>
    <td width="299">Cost to date: <?php echo date("l, d-m-Y", strtotime($today)) ?></td>
    <td width="116" align="center">quoted</td>
    <td width="116" align="center">delivered</td>
    <td width="127" align="center">balance</td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td align="right"><?php echo number_format($q_total, 2, '.', ',')?></td>
    <td align="right"><?php echo number_format($a_total, 2, '.', ',')?></td>
    <td align="right"><?php $d_total = $q_total - $a_total ?><?php if ($d_total>0) { echo '<font color=red>+'.number_format($d_total, 2, '.', ','); } else { echo number_format($d_total, 2, '.', ','); } ?></td>
  </tr>
</table>
     
        </div>
        </td>
    </tr>
    <tr>
      <td><div class="page_middle_frame">
       <?php if ($completed=='no') { echo 'This job is in progress'; } else { echo 'This job is completed'; } ?>
      <table width="650" border="0">
  <tr>
    <td align="center">capture quote</td>
    <td align="center">cutting bill</td>
    <td align="center">direct materials</td>
    <td align="center">produced</td>
    <td align="center">authorisation</td>
    <td align="center">delivered</td>
  </tr>
  <tr>
    <td align="center"><?php if ($compiled=='yes') { ?><img src="../images/round_green.png" width="16" height="16" /><?php } else { ?><img src="../images/round_red.png" width="16" height="16" /><?php } ?></td>
    <td align="center"><?php if ($cutting_bill=='yes') { ?><img src="../images/round_green.png" width="16" height="16" /><?php } else { ?><img src="../images/round_red.png" width="16" height="16" /><?php } ?></td>
    <td align="center"><?php if ($supplied=='yes') { ?><img src="../images/round_green.png" width="16" height="16" /><?php } else { ?><img src="../images/round_red.png" width="16" height="16" /><?php } ?></td>
    <td align="center"><?php if ($produced=='yes') { ?><img src="../images/round_green.png" width="16" height="16" /><?php } else { ?><img src="../images/round_red.png" width="16" height="16" /><?php } ?></td>
    <td align="center"><?php if ($authorisation=='yes') { ?><img src="../images/round_green.png" width="16" height="16" /><?php } else { ?><img src="../images/round_red.png" width="16" height="16" /><?php } ?></td>
    <td align="center"><?php if ($delivered=='yes') { ?><img src="../images/round_green.png" width="16" height="16" /><?php } else { ?><img src="../images/round_red.png" width="16" height="16" /><?php } ?></td>
  </tr>
</table>

      </div>
      <div class="page_middle_frame">
          <div align="left">
            <label class="label_std">Trusses:</label>
          </div>
          <table width="676" id="table_details_display">
           <thead>
            <tr>
              <th width="209">Truss type </th>
              <th width="116">Qty</th>
              <th width="71">Quoted FT</th>
              <th width="56">Quoted charge</th>
              <th width="56" align="center">FT</th>
              <th width="71">Charge</th>
              <th width="65">&nbsp;</th>
             </tr>
            </thead>
            <?php 
	$table_name = $_SESSION['db_prefix']."_captured_jobs";
	$press_points_query = "SELECT press_points, press_charge FROM $table_name WHERE job_no = '".$job_no."' ";
	$press_points_result = mySQL_query($press_points_query);
	$press_points = mysql_result($press_points_result, 0, "press_points");
	$press_charge = mysql_result($press_points_result, 0, "press_charge");
	$quote_total0 = $press_points * $press_charge;   
	?>
	     <tr>
              <td align="left" valign="top" >&nbsp;</td>
              <td align="left" valign="top">&nbsp;</td>
              <td align="center" valign="top"><?php echo $press_points ?></td>
              <td align="right" valign="top" style="background-color:#FFE9D2"><?php if ($quote_total0>0) { echo number_format($quote_total0, 2, '.', ','); } ?></td>
              <td align="left" valign="top">&nbsp;</td>
              <td align="left" valign="top">&nbsp;</td>
              <td align="right" valign="top">&nbsp;</td>
              
            </tr>
	     
              <?php 
	$sub_total0=0.00;
	$i=0;
	$sub_total0=0.00;
	$press_points_total = 0;
	$a4a_tot='';
	$difference0 = 0;
	if ($produced_delivered_num!=0) {
	while ($i<$produced_delivered_num) {
		$a1 = mysql_result($produced_delivered_result, $i, "truss_type");
	    $a2 = mysql_result($produced_delivered_result, $i, "press_charge");
		$a3 = mysql_result($produced_delivered_result, $i, "quantity");
		$a4 = mysql_result($produced_delivered_result, $i, "press_points");
		$a5 = $a3*$a4*$a2;
		$sub_total0 = $sub_total0+$a5;	
		$press_points_total = $press_points_total + $a4;
			
		
			 
		$difference0 = $sub_total0 - $quote_total0;
	?>
            <tr>
              <td align="left" valign="top"><?php echo $a1 ?></td>
              <td align="center" valign="top"><?php echo $a3 ?>&nbsp;&nbsp;&nbsp;&nbsp;(pp: <?php echo $a4 ?>)<?php $a4a = $a3*$a4?></td>
              <td align="left" valign="top">&nbsp;</td>
              <td align="left" valign="top">&nbsp;</td>
              <td align="center" valign="top"><?php echo $a4a ?><?php $a4a_tot = $a4a_tot + $a4a?></td>
              <td align="right" valign="top" style="background-color:#FFE9D2"><?php echo number_format($a5, 2, '.', ',') ?></td>
              <td align="left" valign="top">&nbsp;</td>
            </tr>
            <?php
	$i++;
	}
	}
	else {
		?>
        <tr>
	       <td colspan="7" align="left" valign="top" >Trusses not yet produced.</td>
	   </tr>
     <?php
	}
	?>
   <tr>
              <td align="left" valign="top">Section result: <?php if (($quote_total0!=0) && ($sub_total0!=0)) { echo number_format((((($quote_total0-$sub_total0)/$quote_total0)*100)), 2, '.', ','); } ?> %</td>
              <td align="left" valign="top">&nbsp;</td>
              <td align="left" valign="top">&nbsp;</td>
              <td align="right" valign="top" style="background-color:#FFBF80"><?php echo number_format($quote_total0, 2, '.', ',') ?></td>
              <td align="center" valign="top"><?php echo $a4a_tot ?>&nbsp;</td>
              <td align="right" valign="top" style="background-color:#FFBF80"><?php echo number_format($sub_total0, 2, '.', ',')?></td>
              <td align="right" valign="top" style="background-color:#FFBF80"><?php if ($difference0>0) { echo '<font color=red>+'.number_format($difference0, 2, '.', ','); } else {  echo number_format($difference0, 2, '.', ','); }  ?></td>
            </tr>
          </table><br />

          <table width="676" id="table_details_display" border="1" class="sortable-onload-0r no-arrow">
            <thead>
            <tr>
              <th width="209" class="sortable-text" style="background-color:#F0F0F0;">Timber </th>
              <th width="56" align="center" style="background-color:#F0F0F0;">Qty</th>
              <th width="56" align="center" style="background-color:#F0F0F0;">Price</th>
              <th width="71" align="center" style="background-color:#F0F0F0;">Total</th>
              <th width="56" align="center" style="background-color:#F0F0F0;"> Qty</th>
              <th width="56" align="center" style="background-color:#F0F0F0;">Price</th>
              <th width="71" align="center" style="background-color:#F0F0F0;">Total</th>
              <th width="65" align="center" style="background-color:#F0F0F0;">&nbsp;</th>
             </tr>
            </thead>
              <?php 
	$j=0;
	$quote_total1 = 0;		  
	while ($j<$quoted_timber_num) {
		$cap_timber = mysql_result($quoted_timber_result, $j, "cap_timber");
		$cap_timber_length = mysql_result($quoted_timber_result, $j, "cap_timber_length");
		$cap_timber_price = mysql_result($quoted_timber_result, $j, "cap_timber_price");
		$quoted_price = $cap_timber_length * $cap_timber_price;	
		$quote_total1 = $quote_total1 + $quoted_price;
		if ($cap_timber_length>0) {	  
	?>
	     <tr>
              <td align="left" valign="top" ><?php echo $cap_timber ?></td>
              <td align="right" valign="top"><?php if ($cap_timber_length>0) {echo $cap_timber_length;} ?></td>
              <td align="right" valign="top"><?php if ($cap_timber_price>0) {echo $cap_timber_price; } ?></td>
              <td align="right" valign="top" style="background-color:#FFE9D2"><?php if ($quoted_price>0) {echo number_format($quoted_price, 2, '.', ','); } ?></td>
              <td align="left" valign="top">&nbsp;</td>
              <td align="left" valign="top">&nbsp;</td>
              <td align="right" valign="top">&nbsp;</td>
              <td align="left" valign="top">&nbsp;</td>
            </tr>
    <?php	
		}
	$j++;
	}
			  
	$i=0;
	$a4='0.00';
	$sub_total1='0.00';
	$a4_tot='';
	while ($i<$timber0_num) {
		$a1 = mysql_result($timber0_result, $i, "store_item_id");
				$table_name = $_SESSION['db_prefix']."_store_items";
				$name_query = "SELECT item_name, width, depth, class FROM $table_name WHERE store_item_id = '".$a1."'  ";
				$name_result = mySQL_query($name_query);
				$a1a = mysql_result($name_result, 0, "item_name");
				$width = mysql_result($name_result, 0, "width");
				$depth = mysql_result($name_result, 0, "depth");
				$class = mysql_result($name_result, 0, "class");
		
		$a2 = mysql_result($timber0_result, $i, "summed_quantity");
		$a3 = mysql_result($timber0_result, $i, "price");
		$a4 = $a2*$a3;
		$sub_total1 = $sub_total1+$a4;	
		
	?>
            <tr>
              <td align="left" valign="top" ><?php echo $a1a;?></td>
              <td align="center" valign="top">&nbsp;</td>
              <td align="right" valign="top">&nbsp;</td>
              <td align="right" valign="top" >&nbsp;</td>
              <td align="center" valign="top"><?php if ($a2>0) {echo $a2;}?></td>
              <td align="right" valign="top"><?php if ($a3>0) {echo number_format($a3, 2, '.', ',');} ?></td>
              <td align="right" valign="top" style="background-color:#FFE9D2"><?php if ($a4>0) {echo number_format($a4, 2, '.', ',');} $a4_tot=$a4_tot+$a4; ?></td>
              <td align="left" valign="top">&nbsp;</td>
            </tr>
            
            <?php
	$i++;
	}
	?>
            <?php
	if ($quoted_timber_num==0) {
             ?>
			 <tr>
              <td width="209">&nbsp;</td>
              <td width="56">&nbsp;</td>
              <td width="56">&nbsp;</td>
              <td width="71">&nbsp;</td>
              <td width="56" align="center" >&nbsp;</td>
              <td width="56" >&nbsp;</td>
              <td width="71" align="right" >&nbsp;</td>
              <td width="65" >&nbsp;</td>
            </tr>
            <?php
			 }
	$difference1 = $a4_tot - $quote_total1;
	?>
    <tfoot>
            <tr>
              <td align="left" valign="top">Section result: <?php if (($quote_total1!=0) && ($a4_tot!=0)) { echo number_format((((($quote_total1-$a4_tot)/$quote_total1)*100)), 2, '.', ','); } ?> %</td>
              <td align="left" valign="top">&nbsp;</td>
              <td align="left" valign="top">&nbsp;</td>
              <td align="right" valign="top" style="background-color:#FFBF80"><?php if ($quote_total1>0) {echo number_format($quote_total1, 2, '.', ',');} ?>&nbsp;</td>
              <td align="left" valign="top">&nbsp;</td>
              <td align="left" valign="top">&nbsp;</td>
              <td align="right" valign="top" style="background-color:#FFBF80"> <?php if ($a4_tot>0) {echo number_format($a4_tot, 2, '.', ',');} $sub_total1 = $a4_tot; ?></td>
              <td align="right" valign="top" style="background-color:#FFBF80">
                <?php if ($difference1>0) { echo '<font color=red>+'.number_format($difference1, 2, '.', ','); } else { echo number_format($difference1, 2, '.', ','); } ?>
              </td>
            </tr>
      </tfoot>
          </table>
          <br />
<table width="676" id="table_details_display" border="1" class="sortable-onload-0r no-arrow">
            <thead>
            <tr>
              <th width="209" class="sortable-text" style="background-color:#F0F0F0;">Plates</th>
              <th width="56" align="center" style="background-color:#F0F0F0;">m<sup>2</sup></th>
              <th width="56" align="center" style="background-color:#F0F0F0;">Cost/m<sup>2</sup></th>
              <th width="71" align="center" style="background-color:#F0F0F0;">Total</th>
              <th width="56" align="center" style="background-color:#F0F0F0;"> Qty</th>
              <th width="56" align="center" style="background-color:#F0F0F0;">Price</th>
              <th width="71" align="center" style="background-color:#F0F0F0;">Total</th>
              <th width="65" align="center" style="background-color:#F0F0F0;">&nbsp;</th>
             </tr>
            </thead>
              <?php
	$quote_total1a='0.00'; 
	$table_name = $_SESSION['db_prefix']."_captured_jobs";
	$cap_plate_query = "SELECT plate_area, plate_charge FROM $table_name WHERE job_no = '".$job_no."' ";
	$cap_plate_result = mySQL_query($cap_plate_query);
	$cap_plate_area = mysql_result($cap_plate_result, 0, "plate_area");
	$cap_plate_charge = mysql_result($cap_plate_result, 0, "plate_charge");
	$quote_total1a = $cap_plate_area * $cap_plate_charge;
		  
	?>
	     <tr>
              <td align="left" valign="top" >&nbsp;</td>
              <td align="right" valign="top"><?php if ($cap_plate_area>0) {echo number_format($cap_plate_area, 6, '.', ',');} ?>&nbsp;</td>
              <td align="right" valign="top"><?php if ($cap_plate_charge>0) {echo number_format($cap_plate_charge, 2, '.', ',');} ?></td>
              <td align="right" valign="top" style="background-color:#FFE9D2"><?php if ($quote_total1a>0) {echo number_format($quote_total1a, 2, '.', ',');}?></td>
              <td align="left" valign="top">&nbsp;</td>
              <td align="left" valign="top">&nbsp;</td>
              <td align="right" valign="top">&nbsp;</td>
              <td align="left" valign="top">&nbsp;</td>
            </tr>
    <?php		  
				  
	$i=0;
	$a4='0.00';
	$sub_total1a='0.00';
	$a4_tot='';
	while ($i<$plate_num) {
		$a1 = mysql_result($plate_result, $i, "store_item_id");
				$table_name = $_SESSION['db_prefix']."_store_items";
				$name_query = "SELECT item_name, width, depth, class FROM $table_name WHERE store_item_id = '".$a1."'  ";
				$name_result = mySQL_query($name_query);
				$a1a = mysql_result($name_result, 0, "item_name");
		
				
		$a2 = mysql_result($plate_result, $i, "summed_quantity");
		$a3 = mysql_result($plate_result, $i, "price");
		
				
		$a4 = $a2*$a3;
		$a4_total = $a2*$a3;
		$sub_total1a = $sub_total1a+$a4;	
	?>
            <tr>
              <td align="left" valign="top" ><?php echo $a1a;?></td>
              <td align="center" valign="top">&nbsp;</td>
              <td align="right" valign="top">&nbsp;</td>
              <td align="right" valign="top" >&nbsp;</td>
              <td align="center" valign="top"><?php if ($a2>0) {echo $a2;} ?></td>
              <td align="right" valign="top"><?php if ($a3>0) {echo number_format($a3, 2, '.', ',');} ?></td>
              <td align="right" valign="top" style="background-color:#FFE9D2"><?php if ($a4>0) {echo number_format($a4, 2, '.', ',');} $a4_tot=$a4_tot+$a4;?></td>
              <td align="left" valign="top">&nbsp;</td>
            </tr>
            
            <?php
	$i++;
	}
	?>
    <?php
	if ($plate_num==0) {
             ?>
			 <tr>
              <td width="209">&nbsp;</td>
              <td width="56">&nbsp;</td>
              <td width="56">&nbsp;</td>
              <td width="71">&nbsp;</td>
              <td width="56" align="center" >&nbsp;</td>
              <td width="56" >&nbsp;</td>
              <td width="71" align="right" >&nbsp;</td>
              <td width="65" >&nbsp;</td>
            </tr>
            <?php
			 }
	$difference1 = $a4_tot - $quote_total1a;
	?>
    <tfoot>
            <tr>
              <td align="left" valign="top">Section result: <?php if (($quote_total1a!=0) && ($a4_tot!=0)) { echo number_format((((($quote_total1a-$a4_tot)/$quote_total1a)*100)), 2, '.', ','); } ?> %</td>
              <td align="left" valign="top">&nbsp;</td>
              <td align="left" valign="top">&nbsp;</td>
              <td align="right" valign="top" style="background-color:#FFBF80"><?php if ($quote_total1a>0) {echo number_format($quote_total1a, 2, '.', ',');}?></td>
              <td align="left" valign="top">&nbsp;</td>
              <td align="left" valign="top">&nbsp;</td>
              <td align="right" valign="top" style="background-color:#FFBF80"><?php if ($a4_tot>0) {echo number_format($a4_tot, 2, '.', ',');} $sub_total1a = $a4_tot; ?></td>
              <td align="right" valign="top" style="background-color:#FFBF80"><?php if ($difference1>0) { echo '<font color=red>+'.number_format($difference1, 2, '.', ','); } else { echo number_format($difference1, 2, '.', ','); } ?>
              </td>
            </tr>
      </tfoot>
          </table>
      </div></td>
    </tr>
    <tr>
      <td height="220"><div class="page_middle_frame">
        <div align="left"></div>
          <table width="676" id="table_details_display" border="1">
           <thead>
            <tr>
              <th colspan="2">eCo products</th>
              <th colspan="3" align="center">Quoted</th>
              <th colspan="3" align="center">Authorised</th>
              <th align="left">&nbsp;</th>
            </tr>
            <tr>
              <th>Item</th>
              <th align="center"><label class="label_std">Unit</label></th>
              <th align="center">Qty</th>
              <th align="center">Price</th>
              <th align="center">Total</th>
              <th align="center"><label class="label_std">Qty</label></th>
              <th align="center"><label class="label_std">Price</label></th>
              <th align="center"><label class="label_std">Total</label></th>
              <th align="left">&nbsp;</th>
            </tr>
           </thead> 
            <?php 
	$i=0;
	$cap_total = '0.00';
	$quote_total2 = '0.00';
	$total2 ='0.00';
	$sub_total2='0.00';
	while ($i<$mitek_num) {
		$cap_item_id = mysql_result($mitek_result, $i, "cap_item_id");
			$table_name4 = $_SESSION['db_prefix']."_store_items";
			$item_query = "SELECT item_name, unit FROM $table_name4 WHERE store_item_id = '".$cap_item_id."' ";
			$item_result = mySQL_query($item_query);
			$item_name = mysql_result($item_result, 0, "item_name");
			$unit = mysql_result($item_result, 0, "unit");
		$cap_quantity = mysql_result($mitek_result, $i, "summed_cap_quantity");
		
		$cap_price = mysql_result($mitek_result, $i, "cap_price");
		$delivered = mysql_result($mitek_result, $i, "summed_delivered");
		
		
		$price = mysql_result($mitek_result, $i, "del_price");
		$cap_total = $cap_quantity * $cap_price;
		$quote_total2 = $quote_total2 + $cap_total;
		$total2 = $price * $delivered;
		$sub_total2 = $sub_total2 + $total2;
		$difference2a = $total2 - $cap_total;
	?>
            <tr>
              <td width="177"><?php echo $item_name ?></td>
              <td width="28" align="left"><?php echo $unit ?></td>
              <td width="50" align="center"><?php if ($cap_quantity>0) {echo $cap_quantity;} ?></td>
              <td width="54" align="right"><?php if ($cap_price>0) {echo $cap_price;} ?></td>
              <td width="69" align="right" style="background-color:#FFE9D2"><?php if ($cap_total>0) {echo $cap_total;} ?></td>
              <td width="54" align="center"><?php echo $delivered; ?></td>
              <td width="54" align="right"><?php echo number_format($price, 2, '.', ',') ?></td>
              <td width="69" align="right" style="background-color:#FFE9D2"><?php echo number_format($total2, 2, '.', ',') ?></td>
              <td width="63" align="right"><?php  if ($difference2a>0) { echo '<font color=red>+'.number_format($difference2a, 2, '.', ','); } else { echo number_format($difference2a, 2, '.', ','); } ?></td>
            </tr>
            <?php
	$i++;
	}
	if ($mitek_num==0) {
             ?>
			 <tr>
              <td width="177">&nbsp;</td>
              <td width="28">&nbsp;</td>
              <td width="50">&nbsp;</td>
              <td width="54">&nbsp;</td>
              <td width="69">&nbsp;</td>
              <td width="54" align="center" >&nbsp;</td>
              <td width="54" >&nbsp;</td>
              <td width="69" align="right" >&nbsp;</td>
              <td width="63" >&nbsp;</td>
            </tr>
            <?php
			 }
	$difference2 = $sub_total2 - $quote_total2;
	
	?>
            <tr>
              <td>Section result: <?php if (($quote_total2!=0) && ($sub_total2!=0)) { echo number_format((((($quote_total2-$sub_total2)/$quote_total2)*100)), 2, '.', ','); } ?> %</td>
              <td align="left">&nbsp;</td>
              <td align="left">&nbsp;</td>
              <td align="left">&nbsp;</td>
              <td align="right" style="background-color:#FFBF80"><?php if ($quote_total2>0) {echo $quote_total2 ;} ?></td>
              <td align="left">&nbsp;</td>
              <td align="left">&nbsp;</td>
              <td align="right" style="background-color:#FFBF80"><?php echo number_format($sub_total2, 2, '.', ','); ?></td>
              <td align="right" style="background-color:#FFBF80"><?php if ($difference2>0) { echo '<font color=red>+'.number_format($difference2, 2, '.', ','); } else { echo number_format($difference2, 2, '.', ','); } ?>
              </td>
            </tr>
          </table>
        </div>
        <div class="page_middle_frame">
          <div align="left"></div>
          <table width="676" id="table_details_display" border='1'>
          <thead>
            <tr>
              <th colspan="2"><label class="label_std">Bracing</label>              </th>
              <th colspan="3" align="center">Quoted</th>
              <th colspan="3" align="center">Authorised</th>
              <th align="left">&nbsp;</th>
            </tr>
            <tr>
              <th>Item</th>
              <th align="center"><label class="label_std">Unit</label></th>
              <th align="center">Qty</th>
              <th align="center">Price</th>
              <th align="center">Total</th>
              <th align="center"><label class="label_std">Qty</label></th>
              <th align="center"><label class="label_std">Price</label></th>
              <th align="center"><label class="label_std">Total</label></th>
              <th align="right">&nbsp;</th>
            </tr>
           </thead> 
            <?php
			
          $j=0;
		  $quote_total3 = '0.00';
		  $auth_total3 = '0.00';		  
		while ($j<$quoted_bracing_timber_num) {
		$cap_timber = mysql_result($quoted_bracing_timber_result, $j, "cap_timber");
		$cap_timber_length = mysql_result($quoted_bracing_timber_result, $j, "cap_timber_length");
		$auth_timber_length = mysql_result($quoted_bracing_timber_result, $j, "auth_timber_length");
		$cap_timber_price = mysql_result($quoted_bracing_timber_result, $j, "cap_timber_price");
		$auth_timber_price = mysql_result($quoted_bracing_timber_result, $j, "auth_timber_price");
		$quoted_price = $cap_timber_length * $cap_timber_price;	
		$quote_total3 = $quote_total3 + $quoted_price;
		$auth_price = $auth_timber_length * $auth_timber_price;	
		$auth_total3 = $auth_total3 + $auth_price;
		$difference3a = $auth_price - $quoted_price;
		?>	  
          <tr>
              <td width="177"><?php echo $cap_timber ?></td>
              <td width="28">&nbsp;m</td>
              <td width="50" align="center"><?php if ($cap_timber_length>0) {echo number_format($cap_timber_length, 2, '.', ',');} ?></td>
              <td width="54" align="right"><?php if ($cap_timber_price>0) {echo $cap_timber_price;} ?></td>
              <td width="69" align="right" style="background-color:#FFE9D2"><?php if ($quoted_price>0) {echo number_format($quoted_price, 2, '.', ',');} ?></td>
              <td width="54" align="center" ><?php echo number_format($auth_timber_length, 2, '.', ','); ?></td>
              <td width="54" align="right" ><?php echo $auth_timber_price;  ?></td>
              <td width="69" align="right" style="background-color:#FFE9D2"><?php echo number_format($auth_price, 2, '.', ','); ?></td>
              <td width="63" align="right" ><?php  if ($difference3a>0) { echo '<font color=red>+'.number_format($difference3a, 2, '.', ','); } else { echo number_format($difference3a, 2, '.', ','); } ?></td>
            </tr>
            <?php
			 $j++;
			 }
			 
			 //$j=0;
//			 $total3 ='';
//			 $sub_total3 = '0.00';
//			 while ($j<$timber3_num) {
//				 $store_item_id = mysql_result($timber3_result, $j, "store_item_id");
//					$table_name4 = $_SESSION['db_prefix']."_store_items";
//					$item_query = "SELECT item_name, unit FROM $table_name4 WHERE store_item_id = '".$store_item_id."' ";
//					$item_result = mySQL_query($item_query);
//					$item_name = mysql_result($item_result, 0, "item_name");
//					$unit = mysql_result($item_result, 0, "unit");
//				$delivered = mysql_result($timber3_result, $j, "summed_delivered");
//				$price = mysql_result($timber3_result, $j, "price");
//				$total3 = $price * $delivered;
//				$sub_total3 = $sub_total3 + $total3;
//			 ?>
<!--            <tr>
//              <td width="177"><?php echo $item_name ?></td>
//              <td width="28">&nbsp;no </td>
//              <td width="50">&nbsp;</td>
//              <td width="54">&nbsp;</td>
//              <td width="69">&nbsp;</td>
//              <td width="54" align="center" ><?php if ($delivered>0) {echo $delivered;} ?></td>
//              <td width="54" align="right" ><?php if ($price>0) {echo number_format($price, 2, '.', ',');} ?></td>
//              <td width="69" align="right" style="background-color:#FFE9D2"><?php if ($total3>0) {echo number_format($total3, 2, '.', ',');} ?></td>
//              <td width="63" >&nbsp;</td>
//            </tr>
//     -->       <?php
//			 $j++;
//			 }
			 
             if ($timber3_num==0) {
             ?>
			 <tr>
              <td width="177">&nbsp;</td>
              <td width="28">&nbsp;</td>
              <td width="50">&nbsp;</td>
              <td width="54">&nbsp;</td>
              <td width="69">&nbsp;</td>
              <td width="54" align="center" >&nbsp;</td>
              <td width="54" >&nbsp;</td>
              <td width="69" align="right" >&nbsp;</td>
              <td width="63" >&nbsp;</td>
            </tr>
            <?php
			 }
			 $difference3 = $auth_total3 - $quote_total3;
			 ?>
            <tr>
              <td colspan="4">Section result: <?php if (($quote_total3!=0) && ($auth_total3!=0)) { echo number_format((((($quote_total3-$auth_total3)/$quote_total3)*100)), 2, '.', ','); } ?> %</td>
              <td align="right" style="background-color:#FFBF80"><?php if ($quote_total3>0) {echo number_format($quote_total3, 2, '.', ',') ;}?></td>
              <td >&nbsp;</td>
              <td >&nbsp;</td>
              <td align="right" style="background-color:#FFBF80"><?php if ($auth_total3>0) {echo number_format($auth_total3, 2, '.', ',') ;}?></td>
              <td align="right" style="background-color:#FFBF80"><?php if ($difference3>0) { echo '<font color=red>+'.number_format($difference3, 2, '.', ','); } else { echo number_format($difference3, 2, '.', ','); } ?>
              </td>
            </tr>
          </table>
        </div>
        <div class="page_middle_frame">
          <div align="left"></div>
          <table width="676" id="table_details_display" border="1">
           <thead>
            <tr>
              <th colspan="2"><label class="label_std">Sundry timbers</label>              </th>
              <th colspan="3" align="center">Quoted</th>
              <th colspan="3" align="center">Authorised</th>
              <th align="left">&nbsp;</th>
            </tr>
            <tr>
              <th>Item</th>
              <th align="center"><label class="label_std">Unit</label></th>
              <th align="center">Qty</th>
              <th align="center">Price</th>
              <th align="center">Total</th>
              <th align="center"><label class="label_std">Qty</label></th>
              <th align="center"><label class="label_std">Price</label></th>
              <th align="center"><label class="label_std">Total</label></th>
              <th align="left">&nbsp;</th>
            </tr>
           </thead> 
          <?php
          $j=0;
		  $quote_total4 = '0.00';
		  $auth_total4 = '0.00';	
		  
		while ($j<$quoted_sundries_timber_num) {
		$cap_timber = mysql_result($quoted_sundries_timber_result, $j, "cap_timber");
		$cap_timber_length = mysql_result($quoted_sundries_timber_result, $j, "cap_timber_length");
		$auth_timber_length = mysql_result($quoted_sundries_timber_result, $j, "auth_timber_length");
		$cap_timber_price = mysql_result($quoted_sundries_timber_result, $j, "cap_timber_price");
		$auth_timber_price = mysql_result($quoted_sundries_timber_result, $j, "auth_timber_price");
		$quoted_price = $cap_timber_length * $cap_timber_price;	
		$quote_total4 = $quote_total4 + $quoted_price;
		$auth_price = $auth_timber_length * $auth_timber_price;	
		$auth_total4 = $auth_total4 + $auth_price;
		$difference4a = $auth_price - $quoted_price;
		?>	  
          <tr>
              <td width="177"><?php echo $cap_timber ?></td>
              <td width="28">&nbsp;m</td>
              <td width="50" align="center"><?php if ($cap_timber_length>0) {echo number_format($cap_timber_length, 2, '.', ',');} ?></td>
              <td width="54" align="right"><?php if ($cap_timber_price>0) {echo $cap_timber_price ;}?></td>
              <td width="69" align="right" style="background-color:#FFE9D2"><?php if ($quoted_price>0) {echo number_format($quoted_price, 2, '.', ',');} ?></td>
              <td width="54" align="center" ><?php echo number_format($auth_timber_length, 2, '.', ','); ?></td>
              <td width="54" align="right" ><?php echo $auth_timber_price ;?></td>
              <td width="69" align="right" style="background-color:#FFE9D2"><?php echo number_format($auth_price, 2, '.', ','); ?></td>
              <td width="63" align="right" ><?php  if ($difference4a>0) { echo '<font color=red>+'.number_format($difference4a, 2, '.', ','); } else { echo number_format($difference4a, 2, '.', ','); } ?></td>
            </tr>
            <?php
			 $j++;
			 }
			
				
			 $k=0;
			 $total4='0.00';
			 $sub_total4 = '0.00';
			 while ($k<$timber4_num) {
				 $store_item_id = mysql_result($timber4_result, $k, "store_item_id");
					$table_name4 = $_SESSION['db_prefix']."_store_items";
					$item_query = "SELECT item_name, unit FROM $table_name4 WHERE store_item_id = '".$store_item_id."' ";
					$item_result = mySQL_query($item_query);
					$item_name = mysql_result($item_result, 0, "item_name");
					$unit = mysql_result($item_result, 0, "unit");
				$delivered = mysql_result($timber4_result, $k, "summed_delivered");
				$price = mysql_result($timber4_result, $k, "price");
				$total4 = $price * $delivered;
				$sub_total4 = $sub_total4 + $total4;
//			 ?>
            <!--<tr>
//              <td width="177"><?php echo $item_name ?></td>
//              <td width="28">&nbsp;no</td>
//              <td width="50">&nbsp;</td>
//              <td width="54">&nbsp;</td>
//              <td width="69">&nbsp;</td>
//              <td width="54" align="center" ><?php if ($delivered>0) {echo $delivered;} ?></td>
//              <td width="54" align="right" ><?php if ($price>0) {echo number_format($price, 2, '.', ',');} ?></td>
//              <td width="69" align="right" style="background-color:#FFE9D2"><?php if ($total4>0) {echo number_format($total4, 2, '.', ',');} ?></td>
//              <td width="63" >&nbsp;</td>
//            </tr>-->
           <?php
//				
			 $k++;
			 }
			 if ($timber4_num==0) {
             ?>
			 <tr>
              <td width="177">&nbsp;</td>
              <td width="28">&nbsp;</td>
              <td width="50">&nbsp;</td>
              <td width="54">&nbsp;</td>
              <td width="69">&nbsp;</td>
              <td width="54" align="center" >&nbsp;</td>
              <td width="54" >&nbsp;</td>
              <td width="69" align="right" >&nbsp;</td>
              <td width="63" >&nbsp;</td>
            </tr>
            <?php
			 }
			 $difference4 = $auth_total4 - $quote_total4;
			 ?>
            <tr>
              <td>Section result: <?php if (($quote_total4!=0) && ($auth_total4!=0)) { echo number_format((((($quote_total4-$auth_total4)/$quote_total4)*100)), 2, '.', ',') ; } ?> %</td>
              <td>&nbsp;</td>
              <td>&nbsp;</td>
              <td>&nbsp;</td>
              <td align="right" style="background-color:#FFBF80"><?php if ($quote_total4>0) {echo number_format($quote_total4, 2, '.', ',') ;} ?></td>
              <td >&nbsp;</td>
              <td >&nbsp;</td>
              <td align="right" style="background-color:#FFBF80"><?php if ($auth_total4>0) {echo number_format($auth_total4, 2, '.', ',') ;} ?></td>
              <td align="right" style="background-color:#FFBF80"><?php if ($difference4>0) { echo '<font color=red>+'.number_format($difference4, 2, '.', ','); } else { echo number_format($difference4, 2, '.', ','); } ?>
              </td>
            </tr>
          </table>
        </div>
        <div class="page_middle_frame">
          <div align="left">
            
          </div>
          <table width="676" id="table_details_display" border="1">
           <thead>
            <tr>
              <th colspan="2"><label class="label_std">Additional sundries</label></th>
              <th colspan="3" align="center">Quoted</th>
              <th colspan="3" align="center">Authorised</th>
              <th align="left">&nbsp;</th>
            </tr>
            <tr>
              <th>Item</th>
              <th align="center"><label class="label_std">Unit</label></th>
              <th align="center">Qty</th>
              <th align="center">Price</th>
              <th align="center">Total</th>
              <th align="center"><label class="label_std">Qty</label></th>
              <th align="center"><label class="label_std">Price</label></th>
              <th align="center"><label class="label_std">Total</label></th>
              <th align="left">&nbsp;</th>
            </tr>
           </thead> 
            <?php 
			 $total5='0.00';
			 $sub_total5 = '0.00';
			 $quote_total5 = '0.00';
				  $m=0;
				   while ($m<$sundries_num) { 
					    $additional_item_id = mysql_result($sundries_result, $m, "cap_item_id");
						$table_name = $_SESSION['db_prefix']."_store_items";
							$name_query = "SELECT item_name, unit FROM $table_name WHERE store_item_id = '".$additional_item_id."' ";
							$name_result = mySQL_query($name_query);
							$name_num = mysql_num_rows($name_result);
							$item_name = mysql_result($name_result, 0, "item_name");
						$cap_quantity	= mysql_result($sundries_result, $m, "summed_cap_quantity");
						$cap_price	= mysql_result($sundries_result, $m, "cap_price");
						$delivered = mysql_result($sundries_result, $m, "summed_delivered");
						$del_price = mysql_result($sundries_result, $m, "del_price");
						$cap_total = $cap_quantity * $cap_price;
						$quote_total5 = $quote_total5 + $cap_total;
						$total5 = $del_price * $delivered;
						$sub_total5 = $sub_total5 + $total5;
						$difference5a = $total5 - $cap_total;
						
				 ?>
            <tr>
              <td width="177"><?php echo $item_name ?>
                <input type="hidden" name="additional_item_id_<?php echo $m?>" id="additional_item_id_<?php echo $m?>" value="<?php echo $additional_item_id ?>"></td>
              <td width="28"><?php echo $unit ?></td>
              <td width="50" align="center"><?php if ($cap_quantity>0) {echo $cap_quantity;} ?></td>
              <td width="54" align="right"><?php if ($cap_price>0) {echo $cap_price;} ?></td>
              <td width="69" align="right" style="background-color:#FFE9D2"><?php if ($cap_total>0) {echo number_format($cap_total, 2, '.', ',');} ?>&nbsp;</td>
              <td width="54" align="center" ><?php echo $delivered; ?></td>
              <td width="54" align="right" ><?php echo number_format($del_price, 2, '.', ',') ; ?></td>
              <td width="69" align="right" style="background-color:#FFE9D2"><?php echo number_format($total5, 2, '.', ','); ?></td>
              <td width="63" align="right" ><?php  if ($difference5a>0) { echo '<font color=red>+'.number_format($difference5a, 2, '.', ','); } else { echo number_format($difference5a, 2, '.', ','); }?></td>
            </tr>
            <?php
			  $m++;
             }
			 if ($sundries_num==0) {
             ?>
			 <tr>
              <td width="177">&nbsp;</td>
              <td width="28">&nbsp;</td>
              <td width="50">&nbsp;</td>
              <td width="54">&nbsp;</td>
              <td width="69">&nbsp;</td>
              <td width="54" align="center" >&nbsp;</td>
              <td width="54" >&nbsp;</td>
              <td width="69" align="right" >&nbsp;</td>
              <td width="63" >&nbsp;</td>
            </tr>
            <?php
			 }
			 $difference5 = $sub_total5 - $quote_total5;
             ?>
            <tr>
              <td>Section result: <?php if (($quote_total5!=0) && ($sub_total5!=0)) { echo number_format((((($quote_total5-$sub_total5)/$quote_total5)*100)), 2, '.', ','); } ?> %</td>
              <td>&nbsp;</td>
              <td>&nbsp;</td>
              <td>&nbsp;</td>
              <td align="right" style="background-color:#FFBF80"><?php if ($quote_total5>0) {echo number_format($quote_total5, 2, '.', ',') ;} ?></td>
              <td >&nbsp;</td>
              <td >&nbsp;</td>
              <td align="right" style="background-color:#FFBF80"><?php if ($sub_total5>0) {echo number_format($sub_total5, 2, '.', ',') ;} ?></td>
              <td align="right" style="background-color:#FFBF80"><?php if ($difference5>0) { echo '<font color=red>+'.number_format($difference5, 2, '.', ','); } else { echo number_format($difference5, 2, '.', ','); } ?>
              </td>
            </tr>
          </table>
        </div>
        <div class="page_middle_frame">
          <div align="left">
            
          </div>
          <table width="676" id="table_details_display" border="1">
           <thead>
            <tr>
              <th colspan="2"><label class="label_std">Project  total: </label></th>
              <th colspan="2" align="center">Quoted</th>
              <th colspan="2" align="center">Delivered</th>
              <th width="63" align="left">&nbsp;</th>
            </tr>
            
           </thead> 
            <?php 
				  $quote_total = $quote_total0 + $quote_total1 + $quote_total1a + $quote_total2 + $quote_total3 + $quote_total4 + $quote_total5 ;	
				  $all_total = $sub_total0 + $sub_total1 + $sub_total1a + $sub_total2 + $auth_total3 + $auth_total4 + $sub_total5 ;
				  $difference_total = $all_total - $quote_total;
				  
				  
				  if ($sub_total0==0) {$count_total0 = $quote_total0;} else {$count_total0 = $sub_total0; }
				  if ($sub_total1==0) {$count_total1 = $quote_total1;} else {$count_total1 = $sub_total1; }
				  if ($sub_total1a==0) {$count_total1a = $quote_total1a;} else {$count_total1a = $sub_total1a; }
				  if ($sub_total2==0) {$count_total2 = $quote_total2;} else {$count_total2 = $sub_total2; }
				  if ($auth_total3==0) {$count_total3 = $quote_total3;} else {$count_total3 = $auth_total3; }
				  if ($auth_total4==0) {$count_total4 = $quote_total4;} else {$count_total4 = $auth_total4; }
				  if ($sub_total5==0) {$count_total5 = $quote_total5;} else {$count_total5 = $sub_total5; }
				  				  
				  $count_total = $count_total0 + $count_total1 + $count_total1a + $count_total2 + $count_total3 + $count_total4 + $count_total5 ;
				  
				 
				 ?>
            <tr>
              <td width="76">&nbsp;</td>
              <td width="125">&nbsp;</td>
              <td width="114" align="right">&nbsp;</td>
              <td width="69" align="right"  style="background-color:#FFBF80"><?php if ($quote_total>0) {echo number_format($quote_total, 2, '.', ',');} ?></td>
              <td width="114" align="right">&nbsp;</td>
              <td width="69" align="right" style="background-color:#FFBF80"><?php if ($all_total>0) {echo number_format($all_total, 2, '.', ',');} ?></td>
              <td width="63" align="right" style="background-color:#FFBF80"><?php if ($difference_total>0) { echo '<font color=red>+'.number_format($difference_total, 2, '.', ','); } else { echo number_format($difference_total, 2, '.', ','); } ?>
              </span></td>
            </tr>
          </table>
        </div>
        <div class="page_middle_frame">
          <table width="676" id="table_details_display" border="1">
           <thead>
            <tr>
              <th colspan="2"><label class="label_std">Variations </label></th>
              <th width="185" align="center">&nbsp;&nbsp;</th>
              <th colspan="3" align="center">De-authorised/Delivered</th>
              <th width="63" align="left">&nbsp;</th>
            </tr>
            <tr>
              <th>Item</th>
              <th align="center"><label class="label_std">Unit</label></th>
              <th align="center">Reason&nbsp;&nbsp;</th>
              <th align="center"><label class="label_std">Qty</label></th>
              <th align="center"><label class="label_std">Price</label></th>
              <th align="center"><label class="label_std">Total</label></th>
              <th align="left">&nbsp;</th>
             
            </tr>
           </thead> 
            <?php 
			      $total6='0.00';
				  $sub_total6='0.00';
				  
			
       		
			 $p=0;
				  while ($p<$variations_num) { 
					    $store_item_id = mysql_result($variations_result, $p, "store_item_id");
						$table_name = $_SESSION['db_prefix']."_store_items";
							$name_query = "SELECT item_name, unit FROM $table_name WHERE store_item_id = '".$store_item_id."' ";
							$name_result = mySQL_query($name_query);
							$name_num = mysql_num_rows($name_result);
							$item_name = mysql_result($name_result, 0, "item_name");
							$unit = mysql_result($name_result, 0, "unit");
							$delivered = mysql_result($variations_result, $p, "delivered");
						
						$price = mysql_result($variations_result, $p, "price");
						$reason_text = mysql_result($variations_result, $p, "notes_text");
						$total6 = $price * $delivered;
						$sub_total6 = $sub_total6 + $total6;
						
				 ?>
            <tr>
              <td width="177"><?php echo $item_name ?>&nbsp;</td>
              <td width="28"><?php echo $unit ?>&nbsp;</td>
              <td>&nbsp;&nbsp;&nbsp;<?php echo $reason_text ?></td>
              <td width="54" align="right" ><?php echo $delivered ?></td>
              <td width="54" align="right" ><?php echo number_format($price, 2, '.', ',') ?></td>
              <td width="69" align="right" style="background-color:#FFE9D2"><?php echo number_format($total6, 2, '.', ',') ?></td>
              <td width="63" align="right" >&nbsp;</td>
            </tr>
            <?php
			  $p++;
             }
             ?>
            <tr>
              <td>&nbsp;</td>
              <td>&nbsp;</td>
              <td>&nbsp;</td>
              <td align="right" >&nbsp;</td>
              <td align="right" >&nbsp;</td>
              <td align="right" style="background-color:#FFBF80"><?php echo number_format($sub_total6, 2, '.', ',') ?></td>
              <td align="right" >&nbsp;</td>
            </tr>
          </table>
        </div>
        <div class="page_middle_frame">
          <div align="left">
            <label class="label_std">All total: </label>
          </div>
          <table width="676" id="table_details_display">
            <?php 
				  $final_total = $all_total + $sub_total6 ;
				 ?>
            <tr>
              <td width="191">&nbsp;</td>
              <td width="329">&nbsp;</td>
              <td width="71" align="right" style="background-color:#FFBF80"><?php if ($final_total>0) {echo number_format($final_total, 2, '.', ',');} ?></td>
              <td width="65" align="right" >&nbsp;</td>
            </tr>
          </table>
        </div>
        <div class="page_middle_frame">
          <div align="left">
            <label class="label_std">Result: </label>
          </div>
          <table width="676" id="table_details_display">
            <?php 
				  $final_total = $all_total + $sub_total6 ;
				 ?>
            <tr>
              <td width="191" valign="middle"><?php if ($completed=='yes') { ?>
              <img src="../images/round_green.png" width="16" height="16" />&nbsp;Job completed. <?php } else { ?>
              <img src="../images/round_red.png" width="16" height="16" /> Job not completed yet. <?php } ?> </td>
              <td width="163" valign="middle">Job value:</td>
              <td width="164" valign="middle"><?php if ($completed=='yes') { echo 'Final job cost:'; } else { echo 'Estimated job cost'; } ?></td>
              <td width="71" valign="middle" ><?php if ($completed=='yes') { echo 'Final result:'; } else { echo 'Estimated result'; } ?></td>
              <td width="65" align="right" valign="middle" >&nbsp;</td>
            </tr>
            <tr>
              <td>&nbsp;</td>
              <td align="right"><?php echo number_format($job_value, 2, '.', ',') ?>&nbsp;</td>
              <td align="right">
                <?php if ($final_total>0) {echo number_format($final_total, 2, '.', ',');} ?>
              </td>
              <td align="right" style="background-color:#FFBF80"><?php echo number_format($job_value-$final_total, 2, '.', ',') ?></td>
              <td align="right" ><?php $percentage = (($job_value-$final_total)/$job_value)*100; echo number_format($percentage, 2, '.', ',') ?> %</td>
            </tr>
          </table>
        </div></td>
    </tr>
  </table>
  <!--end of tableMain--> 
  
</div>d
<?php include ('../navigation/side_bar.php'); ?>

<?php
if ($load==0) {
	?>
<script language="javascript" type="text/javascript">
          window.location="costing_job.php?job_no=<?php echo $job_no?>&load=1&q_total=<?php echo $quote_total ?>&a_total=<?php echo $final_total?>";
</script>
<?php
}
?>



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