<head>
<?php

$job_no = $_SESSION['job_no'];
$quote='';
$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_name = $_SESSION['db_prefix']."_captured_jobs";
$job_query = "SELECT *  FROM $table_name WHERE job_no = '".$_SESSION['job_no']."' ";
$job_result = mySQL_query($job_query);
$designer=mysql_result($job_result, 0, "designer");
$quote_note=mysql_result($job_result, 0, "quote_note");

$table_name = $_SESSION['db_prefix']."_captured_mitek";
$table_name2 = $_SESSION['db_prefix']."_captured_mitek_temp";
$mitek_query = "SELECT cap_item_id, cap_quantity FROM $table_name  WHERE cap_job = '".$_SESSION['job_no']."' UNION ALL  SELECT cap_item_id, cap_quantity FROM $table_name2  WHERE cap_job = '".$_SESSION['job_no']."' AND user =  '".$_SESSION['username']."' ";
$mitek_result = mySQL_query($mitek_query);
$mitek_num = mysql_num_rows($mitek_result);

$table_name = $_SESSION['db_prefix']."_authorised_timber_temp";
$bracing_query = "SELECT * FROM $table_name WHERE auth_timber_group = 'bracing' AND job_no = '".$_SESSION['job_no']."'  AND user =  '".$_SESSION['username']."' 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_temp";
$sundries_query = "SELECT * FROM $table_name WHERE auth_timber_group = 'sundries' AND job_no = '".$_SESSION['job_no']."'  AND user =  '".$_SESSION['username']."'  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";
$table_name3 = $_SESSION['db_prefix']."_captured_sundries_temp";
$additional_query = "SELECT * FROM $table_name WHERE cap_job = '".$_SESSION['job_no']."' UNION ALL SELECT * FROM $table_name3 WHERE cap_job = '".$_SESSION['job_no']."'  AND user =  '".$_SESSION['username']."' ";
$additional_result = mySQL_query($additional_query);
$additional_num = mysql_num_rows($additional_result);
?>

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

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


<link href="../css/main.css" rel="stylesheet" type="text/css" />

<div class="page_middle_frame">
<form id="feedbackform" method="get" action="authorisationWiz7.php" >
<div class="wizard_frame">
<?php 
		include ('../include/customer_block.php');
		include ('../include/job_block.php')
		?></div>
   <div class="wizard_frame">
 <table width="646" id="table_details_display" >
              <tr>
                <td ><label class="label_std">Mitek products:</label></td>
                <td colspan="2" >Quoted</td>
                <td colspan="2" >Authorised</td>
              </tr>
               <?php 
				  $i=0;
				  $_SESSION['list_3'] = $mitek_num;
				  while ($i<$mitek_num) { 
					    
					    $_SESSION['item_id'.$i] = 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");
						$table_name = $_SESSION['db_prefix']."_store_items";
						$item_name_query = "SELECT item_name, unit FROM $table_name WHERE store_item_id = '".$_SESSION['item_id'.$i]."' ";
						$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="350"><?php echo $item_name ?>
               <input type="hidden" name="item_id_<?php echo $i?>" id="item_id_<?php echo $i?>" value="<?php echo $_SESSION['item_id'.$i] ?>"></td>
               <td width="46" align="right" style="border-right:hidden"><?php echo $mitek_quantity ?></td>
               <td width="36">&nbsp;&nbsp;<?php echo $unit?></td>
               <td width="92" align="right" style="border-right:hidden"><?php echo $_SESSION['quantity_'.$i]?></td>
               <td width="98" align="left">&nbsp;&nbsp;<?php echo $unit?>&nbsp;</td>
             </tr>
              <?php
						
			  $i++;
             }
             ?>
             </table></div>
  <div class="wizard_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 = '".$_SESSION['job_no']."' GROUP BY cap_timber_name, cap_job ";
					$reserved_result = mySQL_query($reserved_query);
					$reserved_num = mysql_num_rows($reserved_result);
					$quoted_reserved = mysql_result($reserved_result, 0, "quoted_reserved");
				 
				 
				 	$table_name = $_SESSION['db_prefix']."_authorised_timber_temp";
					$bracing_sub_query = "SELECT * FROM $table_name WHERE  auth_timber_name = '".$bracing_name."' AND (reserved>0 OR store_item_id = 0) AND user =  '".$_SESSION['username']."'";
					$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/1000;
					$_SESSION['bracing_name2'.$j]=$bracing_name; 
					}
					else {
						$item_name = '';
						$length=0;
						//$tot_length = 0;
						$_SESSION['bracing_qty_'.$j]=$tot_length/1000;
						$_SESSION['bracing_name2'.$j]=$bracing_name;
						$sub_tot_length = '';
						//$tot_length = $tot_length + $sub_tot_length;
					}
						
					
			 ?>
       <tr>
         <td width="280"><?php if ($temp_name!=$bracing_name) echo $bracing_name; ?>
           <?php $temp_name=$bracing_name;?>
           </td>
         <td align="left" ><?php if ($length!=0) { echo $length; }?></td>
         <td align="left" >&nbsp;</td>
         <td width="56" style="border-right:hidden"><?php if ($length!=0) { if($length!=0) { echo $quantity.' no';} }?></td>
         <td width="56" align="right"><?php if ($length!=0) { echo number_format($length/1000, 2, '.', ',').'  m'; } ?></td>
         <td width="74" align="right"><?php if ($length!=0) { 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="wizard_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 = '".$_SESSION['job_no']."' GROUP BY cap_timber_name, cap_job ";
					$reserved_result = mySQL_query($reserved_query);
					$reserved_num = mysql_num_rows($reserved_result);
					$quoted_reserved = mysql_result($reserved_result, 0, "quoted_reserved");
				 
				 
				 	$table_name = $_SESSION['db_prefix']."_authorised_timber_temp";
					$sundries_sub_query = "SELECT * FROM $table_name WHERE  auth_timber_name = '".$sundries_name."' AND (reserved>0 OR store_item_id = 0)  AND user =  '".$_SESSION['username']."'";
					$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/1000;
					$_SESSION['sundries_name2'.$j]=$sundries_name; 
					}
					else {
						$item_name = '';
						$length=0;
						//$tot_length = 0;
						$_SESSION['sundries_qty_'.$j]=$tot_length/1000;
						$_SESSION['sundries_name2'.$j]=$sundries_name;
						$sub_tot_length = '';
						
											
					}
						
					
			 ?>
      <tr>
        <td width="280"><?php if ($temp_name!=$sundries_name) echo $sundries_name; ?>
          <?php $temp_name=$sundries_name;?></td>
        <td align="left" ><?php if ($length!=0) { echo $length; } ?></td>
        <td align="left" >&nbsp;</td>
        <td width="56" style="border-right:hidden"><?php if ($length!=0) {  if($length!=0) { echo $quantity.' no';} }?></td>
        <td width="56" align="right"><?php if ($length!=0) {  echo number_format($length/1000, 2, '.', ',').'  m'; } ?></td>
        <td width="74" align="right"><?php if ($length!=0) {  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="wizard_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;
				  $_SESSION['list_6'] = $additional_num;
				  while ($m<$additional_num) { 
					    $_SESSION['additional_item_id'.$m] = mysql_result($additional_result, $m, "cap_item_id");
						//$_SESSION['additional_quantity_'.$m] = mysql_result($additional_result, $m, "cap_quantity");
						$_additional_quantity = mysql_result($additional_result, $m, "cap_quantity");
						$table_name = $_SESSION['db_prefix']."_store_items";
						$item_name_query = "SELECT item_name, unit FROM $table_name WHERE store_item_id = '".$_SESSION['additional_item_id'.$m]."' ";
						$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="350"><?php echo $item_name ?><?php echo $_SESSION['additional_item_id'.$m] ?>
                 <input type="hidden" name="additional_item_id_<?php echo $m?>" id="additional_item_id_<?php echo $m?>" value="<?php echo $_SESSION['additional_item_id'.$m] ?>">
               </td>
               <td width="56" align="right" style="border-right:hidden" ><?php echo $_additional_quantity?></td>
               <td width="26"><?php echo $unit ?></td>
               <td width="92" align="right" style="border-right:hidden"><?php echo $_SESSION['additional_quantity_'.$m]?></td>
               <td width="98"><?php echo $unit ?></td>
             </tr>
              <?php
					
			  $m++;
             }
             ?>
    </table>
  </div>
              <br />
<br />

  <div class="wizard_frame">
              <label class="label_std">If you want to make changes use the 'Prev' button to go back. <br />
              To finish the wizard click 'Finish'.</label>
  </div>
       
              <table width="675" height="50" align="center" id="table_button_bar">
                <tr>
                  <td width="117"><input type="button" name="lastprev_button" class="imagebutton" value="Prev" onClick="needToConfirm = false; MM_showHideLayers('wait','','show'); MM_goToURL('parent','authorisationWizbase.php?s=&p=5&screen=0');return document.MM_returnValue" /></td>
                  <td width="115">
                    <input name="s" type="hidden" id="s" value="6" />
                    <input name="p" type="hidden" id="p" value="6" />
                  </td>
                  <td width="99"><?php  echo memory_get_usage()/1024.0 . " kb \n"; ?></td>
                  <td width="125" align="right"><input name="cancelbutton" type="button" class="imagebutton" id="cancelbutton" onClick="needToConfirm = false; cancel_confirmation(1, '../production/production_main.php');" value="Cancel" /></td>
                  <td width="110" align="right"><input  name="finish_button" type="submit" class="imagebutton" id="finish_button" value="Finish" onClick="needToConfirm = false; MM_showHideLayers('wait','','show'); "  /></td>
                </tr>
              </table>
              
</form>
</div>
</body>