<!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-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");
 			}

?>

<?php

$table_name = $_SESSION['db_prefix']."_captured_jobs";
$cust_id_query = "SELECT cust_id, designer FROM $table_name WHERE job_no = '".$job_no."' ";
$cust_id_result = mySQL_query($cust_id_query);
$cust_id = mysql_result($cust_id_result, 0, "cust_id");
$designer = mysql_result($cust_id_result, 0, "designer");


$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,cap_quantity, auth_quantity FROM $table_name WHERE cap_job = '".$job_no."' ";
$mitek_result = mySQL_query($mitek_query);
$mitek_num = mysql_num_rows($mitek_result);



$table_name = $_SESSION['db_prefix']."_authorised_timber";
$bracing_check_query = "SELECT  auth_id FROM $table_name WHERE auth_timber_group = 'bracing' AND job_no = '".$job_no."'  ";
$bracing_check_result = mySQL_query($bracing_check_query);
$bracing_check_num = mysql_num_rows($bracing_check_result);
if ($bracing_check_num!=0) { 
$table_name = $_SESSION['db_prefix']."_authorised_timber";
}
else {
$table_name = $_SESSION['db_prefix']."_authorised_timber_done";
}
$bracing_query = "SELECT * FROM $table_name WHERE auth_timber_group = 'bracing' AND job_no = '".$job_no."' GROUP BY auth_timber_name  ";
$bracing_result = mySQL_query($bracing_query);
$bracing_num = mysql_num_rows($bracing_result);

$table_name = $_SESSION['db_prefix']."_authorised_timber";
$sundries_check_query = "SELECT  auth_id FROM $table_name WHERE auth_timber_group = 'sundries' AND job_no = '".$job_no."'  ";
$sundries_check_result = mySQL_query($sundries_check_query);
$sundries_check_num = mysql_num_rows($sundries_check_result);
if ($sundries_check_num!=0) { 
$table_name = $_SESSION['db_prefix']."_authorised_timber";
}
else {
$table_name = $_SESSION['db_prefix']."_authorised_timber_done";
}
$sundries_query = "SELECT * FROM $table_name WHERE auth_timber_group = 'sundries' AND job_no = '".$job_no."'   GROUP BY auth_timber_name  ";
$sundries_result = mySQL_query($sundries_query);
$sundries_num = mysql_num_rows($sundries_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";
}
$additional_query = "SELECT * FROM $table_name WHERE cap_job = '".$job_no."' ";
$additional_result = mySQL_query($additional_query);
$additional_num = mysql_num_rows($additional_result);

?>
<!--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; min-height:550px;">
  <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="472"><label class="label_page_head">Job Authorisation</label>&nbsp;&nbsp;&nbsp;<label class="label_page_head_2"><?php echo $name; ?></label>
              </td>
            <td width="208"><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 />
  </div>
  <div class="page_middle_frame">
 <table width="646" id="table_details_display" >
              <tr>
                <td align="left" valign="top"><label class="label_std">Mitek products:</label></td>
                <td colspan="2" align="left" valign="top">Quoted</td>
                <td colspan="2" align="left" valign="top">Authorised</td>
              </tr>
               <?php 
				  $i=0;
				   while ($i<$mitek_num) { 
					    $item_id = mysql_result($mitek_result, $i, "cap_item_id");
						//$_SESSION['quantity_'.$i] = mysql_result($mitek_result, $i, "cap_quantity");
						$mitek_quantity = mysql_result($mitek_result, $i, "cap_quantity");
						$mitek_auth_quantity = mysql_result($mitek_result, $i, "auth_quantity");
						$table_name = $_SESSION['db_prefix']."_store_items";
						$item_name_query = "SELECT item_name, unit FROM $table_name WHERE store_item_id = '".$item_id."' ";
						$item_name_result = mySQL_query($item_name_query);
						$item_name = mysql_result($item_name_result, 0, "item_name"); 
						$unit = mysql_result($item_name_result, 0, "unit"); 	
				 ?>
             <tr>
               <td width="351"><?php echo $item_name ?></td>
               <td width="46" style="border-right:hidden"><?php echo $mitek_quantity ?></td>
               <td width="36">&nbsp;<?php echo $unit?></td>
               <td width="91" align="right" style="border-right:hidden"><?php echo $mitek_auth_quantity ?></td>
               <td width="98">&nbsp;&nbsp;<?php echo $unit?></td>
             </tr>
              <?php
						
			  $i++;
             }
             ?>
        </table></div>
  <div class="page_middle_frame">
    <table width="646" align="left" id="table_details_display" >
      <tr style="border-bottom:double">
        <td><label class="label_std">Bracing</label></td>
        <td width="66">Item</td>
        <td width="86">Quoted</td>
        <td colspan="2">Authorised </td>
        <td>Total </td>
      </tr>
      <?php 
			 $j=0;
			 $temp_name = '';
			 $tot_length=0;
			 while ($j<$bracing_num) {
				 $bracing_name = mysql_result($bracing_result, $j, "auth_timber_name");
				 
				    $table_name = $_SESSION['db_prefix']."_captured_timber";
					$reserved_query = "SELECT SUM(cap_timber_length) AS quoted_reserved FROM $table_name WHERE cap_timber_name = '".$bracing_name."' AND cap_job = '".$job_no."' GROUP BY cap_timber_name, cap_job ";
					$reserved_result = mySQL_query($reserved_query);
					$reserved_num = mysql_num_rows($reserved_result);
					if ($reserved_num!=0) {
					$quoted_reserved = mysql_result($reserved_result, 0, "quoted_reserved");
					}
					else {
						$quoted_reserved = '0';
					}
						
				 		 
				 	$table_name = $_SESSION['db_prefix']."_authorised_timber";
					$bracing_check_query = "SELECT  auth_id FROM $table_name WHERE auth_timber_group = 'bracing' AND job_no = '".$job_no."'  ";
					$bracing_check_result = mySQL_query($bracing_check_query);
					$bracing_check_num = mysql_num_rows($bracing_check_result);
					if ($bracing_check_num!=0) { 
					$table_name = $_SESSION['db_prefix']."_authorised_timber";
					}
					else {
					$table_name = $_SESSION['db_prefix']."_authorised_timber_done";
					}
					$bracing_sub_query = "SELECT * FROM $table_name WHERE  auth_timber_name = '".$bracing_name."' AND reserved>0 AND job_no = '".$job_no."' ";
					$bracing_sub_result = mySQL_query($bracing_sub_query);
					$bracing_sub_num = mysql_num_rows($bracing_sub_result);
					$k=0;
					$temp_name = '';
					$tot_length=0;
					while ($k<$bracing_sub_num) {
					$store_item_id = mysql_result($bracing_sub_result, $k, "store_item_id");
				 	$quantity = mysql_result($bracing_sub_result, $k, "reserved");
				 
				 	if ($store_item_id!='0') {
					$table_name = $_SESSION['db_prefix']."_store_items";
					$item_query = "SELECT item_name, length FROM $table_name WHERE store_item_id = '".$store_item_id."' ";
					$item_result = mySQL_query($item_query);
					$item_name = mysql_result($item_result, 0, "item_name");
					$length = mysql_result($item_result, 0, "length");
					$sub_tot_length = $length*$quantity;
					$tot_length = $tot_length + $sub_tot_length;
					$_SESSION['bracing_qty_'.$j]=$tot_length;
					$_SESSION['bracing_name2'.$j]=$bracing_name; 
					}
					else {
						$item_name = '';
						$length=0;
						$tot_length = 0;
						$_SESSION['bracing_qty_'.$j]=$tot_length;
						$_SESSION['bracing_name2'.$j]=$bracing_name;
					}
						
					
			 ?>
      <tr>
        <td width="280"><?php if ($temp_name!=$bracing_name) echo $bracing_name; ?>
          <?php $temp_name=$bracing_name;?></td>
        <td align="left" ><?php echo $length;?></td>
        <td align="left" >&nbsp;</td>
        <td width="56" style="border-right:hidden"><?php  if($length!=0) { echo $quantity.' no';}?></td>
        <td width="56" align="right"><?php echo number_format($length/1000, 2, '.', ',').'  m'; ?></td>
        <td width="74" align="right"><?php echo number_format($sub_tot_length/1000, 2, '.', ',') ?></td>
      </tr>
      <?php if ( $k+1==$bracing_sub_num) {?>
      <tr <?php if ( $k+1==$bracing_sub_num) {?>style="border-bottom:double;"<?php } ?>>
        <td>&nbsp;</td>
        <td align="left" >&nbsp;</td>
        <td align="right" ><?php echo number_format($quoted_reserved, 2, '.', ',').'&nbsp;m'; ?></td>
        <td style="border-right:hidden">&nbsp;</td>
        <td align="right">&nbsp;</td>
        <td align="right"><?php if ( $k+1==$bracing_sub_num) { echo number_format($tot_length/1000, 2, '.', ',').'&nbsp;m';}?></td>
      </tr>
      <?php
	   }
			 $k++;
					}
			 $j++;
			 }
			 $_SESSION['list_6b']=$j;
			 ?>
    </table>
  </div>
  <div class="page_middle_frame">
    <table width="646" align="left" id="table_details_display" >
      <tr style="border-bottom:double">
        <td><label class="label_std">Sundries</label></td>
        <td width="66">Item</td>
        <td width="86">Quoted</td>
        <td colspan="2">Authorised </td>
        <td>Total </td>
      </tr>
      <?php 
			 $j=0;
			 $temp_name = '';
			 $tot_length=0;
			 while ($j<$sundries_num) {
				 $sundries_name = mysql_result($sundries_result, $j, "auth_timber_name");
				 
				    $table_name = $_SESSION['db_prefix']."_captured_timber";
					$reserved_query = "SELECT SUM(cap_timber_length) AS quoted_reserved FROM $table_name WHERE cap_timber_name = '".$sundries_name."' AND cap_job = '".$job_no."' GROUP BY cap_timber_name, cap_job ";
					$reserved_result = mySQL_query($reserved_query);
					$reserved_num = mysql_num_rows($reserved_result);
					if ($reserved_num!=0) {
					$quoted_reserved = mysql_result($reserved_result, 0, "quoted_reserved");
					}
					else {
						$quoted_reserved='0';
					}
				 
				 	$table_name = $_SESSION['db_prefix']."_authorised_timber";
					$sundries_check_query = "SELECT  auth_id FROM $table_name WHERE auth_timber_group = 'sundries' AND job_no = '".$job_no."'  ";
					$sundries_check_result = mySQL_query($sundries_check_query);
					$sundries_check_num = mysql_num_rows($sundries_check_result);
					if ($sundries_check_num!=0) { 
					$table_name = $_SESSION['db_prefix']."_authorised_timber";
					}
					else {
					$table_name = $_SESSION['db_prefix']."_authorised_timber_done";
					}
					$sundries_sub_query = "SELECT * FROM $table_name WHERE  auth_timber_name = '".$sundries_name."' AND reserved>0 AND job_no = '".$job_no."' ";
					$sundries_sub_result = mySQL_query($sundries_sub_query);
					$sundries_sub_num = mysql_num_rows($sundries_sub_result);
					$k=0;
					$temp_name = '';
					$tot_length=0;
					while ($k<$sundries_sub_num) {
					$store_item_id = mysql_result($sundries_sub_result, $k, "store_item_id");
				 	$quantity = mysql_result($sundries_sub_result, $k, "reserved");
				 
				 	if ($store_item_id!='0') {
					$table_name = $_SESSION['db_prefix']."_store_items";
					$item_query = "SELECT item_name, length FROM $table_name WHERE store_item_id = '".$store_item_id."' ";
					$item_result = mySQL_query($item_query);
					$item_name = mysql_result($item_result, 0, "item_name");
					$length = mysql_result($item_result, 0, "length");
					$sub_tot_length = $length*$quantity;
					$tot_length = $tot_length + $sub_tot_length;
					$_SESSION['sundries_qty_'.$j]=$tot_length;
					$_SESSION['sundries_name2'.$j]=$sundries_name; 
					}
					else {
						$item_name = '';
						$length=0;
						$tot_length = 0;
						$_SESSION['sundries_qty_'.$j]=$tot_length;
						$_SESSION['sundries_name2'.$j]=$sundries_name;
					}
						
					
			 ?>
      <tr>
        <td width="280"><?php if ($temp_name!=$sundries_name) echo $sundries_name; ?>
          <?php $temp_name=$sundries_name;?></td>
        <td align="left" ><?php echo $length;?></td>
        <td align="left" >&nbsp;</td>
        <td width="56" style="border-right:hidden"><?php  if($length!=0) { echo $quantity.' no';}?></td>
        <td width="56" align="right"><?php echo number_format($length/1000, 2, '.', ',').'  m'; ?></td>
        <td width="74" align="right"><?php echo number_format($sub_tot_length/1000, 2, '.', ',') ?></td>
      </tr>
      <?php if ( $k+1==$sundries_sub_num) {?>
      <tr <?php if ( $k+1==$sundries_sub_num) {?>style="border-bottom:double;"<?php } ?>>
        <td>&nbsp;</td>
        <td align="left" >&nbsp;</td>
        <td align="right" ><?php echo number_format($quoted_reserved, 2, '.', ',').'&nbsp;m'; ?></td>
        <td style="border-right:hidden">&nbsp;</td>
        <td align="right">&nbsp;</td>
        <td align="right"><?php if ( $k+1==$sundries_sub_num) { echo number_format($tot_length/1000, 2, '.', ',').'&nbsp;m';}?></td>
      </tr>
      <?php
	   }
			 $k++;
					}
			 $j++;
			 }
			 $_SESSION['list_6b']=$j;
			 ?>
    </table>
  </div>
  <div class="page_middle_frame">

 <table width="646" id="table_details_display" > 
             <tr>
               <td><label class="label_std">Additional Sundries</label></td>
               <td colspan="2">Quoted</td>
               <td colspan="2">Authorised</td>
             </tr>
             <?php 
				  $m=0;
				  while ($m<$additional_num) { 
					    $additional_item_id = mysql_result($additional_result, $m, "cap_item_id");
						$_additional_quantity = mysql_result($additional_result, $m, "cap_quantity");
						$_additional_auth_quantity = mysql_result($additional_result, $m, "auth_quantity");
						$table_name = $_SESSION['db_prefix']."_store_items";
						$item_name_query = "SELECT item_name, unit FROM $table_name WHERE store_item_id = '".$additional_item_id. "' ";
						$item_name_result = mySQL_query($item_name_query);
						$item_name_num = mysql_num_rows($item_name_result);
						if ($item_name_num!=0) {
						$item_name = mysql_result($item_name_result, 0, "item_name"); 
						$unit = mysql_result($item_name_result, 0, "unit"); 
						}
				 ?>
             <tr>
               <td width="351"><?php echo $item_name ?></td>
               <td width="46" style="border-right:hidden" ><?php echo $_additional_quantity?></td>
               <td width="26"><?php echo $unit ?></td>
               <td width="102" align="right" style="border-right:hidden"><?php echo $_additional_auth_quantity ?></td>
               <td width="97"><?php echo $unit ?></td>
             </tr>
              <?php
					
			  $m++;
             }
				
             ?>
    </table>
  </div>
  </td>
</tr>

</table>  <!--end of tableMain-->
  
</div>

<?php include ('../navigation/side_bar.php'); ?>



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