<head>
<script type="text/javascript" src="../jscript/jquery.min.js"></script>
<link rel="stylesheet" type="text/css" href="../css/wizard.css" />
<link href="../css/main.css" rel="stylesheet" type="text/css">

<?php
$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";
$mitek_query = "SELECT cap_item_id,cap_quantity FROM $table_name WHERE cap_job = '".$_SESSION['job_no']."' ";
$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']."' GROUP BY auth_timber_name  ";
//$bracing_result = mySQL_query($bracing_query);
//$bracing_num = mysql_num_rows($bracing_result);
$table_name10 = $_SESSION['db_prefix']."_captured_timber";
$bracing_query = "SELECT * FROM $table_name10 WHERE  cap_timber_group = 'Bracing' AND cap_job = '".$_SESSION['job_no']."' ORDER BY cap_timber_name ";
	echo $bracing_query;
	$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']."'   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_query = "SELECT * FROM $table_name WHERE cap_job = '".$_SESSION['job_no']."' ";
$additional_result = mySQL_query($additional_query);
$additional_num = mysql_num_rows($additional_result);
?>

</head>
<?php include('../include/wait.php') ?>
<link href="../css/wait.css" rel="stylesheet" type="text/css">
<body onLoad="MM_showHideLayers('wait','','hide')" onFocus="MM_showHideLayers('wait','','hide');"  onBlur="MM_showHideLayers('wait','','hide');">


<link href="../css/main.css" rel="stylesheet" type="text/css" />
<form id="feedbackform" method="get" action="authorisationWiz7.php" >
<div class="page_middle_frame">
          <table width="646" align="left" id="table_details_display" >
              <tr>
                <td width="136" align="left" valign="top">Customer:</td>
                <td colspan="3" align="left" valign="top"><?php echo $_SESSION['name'] ?></td>
              </tr>
             <tr>
                <td align="left" valign="top">Job  no:</td>
                <td width="160" align="left" valign="top"><?php echo "J".$_SESSION['job_no'] ?></td>
                <td width="147" align="left" valign="top">Designer:</td>
                <td width="183" align="left" valign="top"><?php echo $designer ?>&nbsp;</td>
            </tr>
             <tr>
               <td align="left" valign="top">Job note:</td>
               <td colspan="3" align="left" valign="top"><?php echo $quote_note?>&nbsp;</td>
             </tr>
    </table>
  </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">Captured</td>
                <td colspan="2" align="left" valign="top">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="260"><?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="82" style="border-right:hidden"><div align="right"><?php echo $mitek_quantity ?></div></td>
               <td width="80">&nbsp;&nbsp;<?php echo $unit?></td>
               <td width="102" style="border-right:hidden"><div align="right"><?php echo $_SESSION['quantity_'.$i]?></div></td>
               <td width="98" align="right">&nbsp;&nbsp;<?php echo $unit?>&nbsp;</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>Item</td>
               <td colspan="2">Authorised no/m</td>
               <td>total m</td>
             </tr>
            
            
             <?php 
			 $j=0;
			 $temp_name = '';
			 $tot_length=0;
			 while ($j<$bracing_num) {
				 $bracing_name = mysql_result($bracing_result, $j, "cap_timber_name");
				 echo '<br>'.$bracing_name;
				 
				 	$table_name = $_SESSION['db_prefix']."_captured_timber";
					$bracing_sub_query = "SELECT * FROM $table_name WHERE  cap_timber_name = '".$bracing_name."' AND cap_job = '".$_SESSION['job_no']."' ";
					echo $bracing_sub_query;
					$bracing_sub_result = mySQL_query($bracing_sub_query);
					$bracing_sub_num = mysql_num_rows($bracing_sub_result);
					echo $bracing_sub_num;
					if ($bracing_sub_num==0) {
						$store_item_id=0;
						echo '<br>NO STORE ITEM';
					}
					else {
					
					$k=0;
					$temp_name = '';
					$tot_length=0;
					echo 'bracing num:'.$bracing_num;
					while ($k<$bracing_num) {
															
				 	$store_item_id = mysql_result($bracing_sub_result, $k, "store_item_id");
				 	$quantity = mysql_result($bracing_sub_result, $k, "reserved");
					echo '<br>store_item'.$store_item_id;				
				     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")*$quantity)/1000;
					$tot_length = $tot_length + $length;
					$_SESSION['bracing_qty_'.$k]=$tot_length;
					$_SESSION['bracing_name2'.$k]=$bracing_name;
					
					 }
					 else {
					echo 'komt-ie?';
					$tot_length = '';
					$_SESSION['bracing_qty_'.$k]='';
					$_SESSION['bracing_name2'.$k]=$bracing_name;
					$k++;
					 }
					}
					
					
			 ?>
             <tr <?php if ( $k+1==$bracing_sub_num) {?>style="border-bottom:double;"<?php } ?>>
               <td width="257"><?php if ($temp_name!=$bracing_name) echo $bracing_name; ?><?php $temp_name=$bracing_name;?>
               <label for="textfield"></label></td>
               <td width="169" align="left" ><?php echo $item_name?></td>
               <td width="56" style="border-right:hidden"><?php echo $quantity?><span style="border-right:hidden">&nbsp;no</span></td>
               <td width="66" align="right"><?php echo number_format($length, 2, '.', ',') ?>&nbsp;m</td>
               <td width="74" align="right"><?php if ( $k+1==$bracing_sub_num) { echo number_format($tot_length, 2, '.', ',').'&nbsp;m';}?></td>
             </tr>
             <?php
			 $k++;
				}
				
			  
			 $j++;
			 }
			 $_SESSION['list_6a']=$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>Item</td>
        <td colspan="2">Authorised no/m</td>
        <td>total m</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']."_authorised_timber_temp";
					$sundries_sub_query = "SELECT * FROM $table_name WHERE  auth_timber_name = '".$sundries_name."' ";
					$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");
				 
				 	$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")*$quantity)/1000;
					$tot_length = $tot_length + $length;
					$_SESSION['sundries_qty_'.$j]=$tot_length;
					$_SESSION['sundries_name2'.$j]=$sundries_name; 
					
			 ?>
      <tr <?php if ( $k+1==$sundries_sub_num) {?>style="border-bottom:double;"<?php } ?>>
        <td width="257"><?php if ($temp_name!=$sundries_name) echo $sundries_name; ?>
          <?php $temp_name=$sundries_name;?>
          <label for="textfield2"></label></td>
        <td width="163" align="left" ><?php echo $item_name?></td>
        <td width="62" style="border-right:hidden"><?php echo $quantity?><span style="border-right:hidden">&nbsp;no</span></td>
        <td width="66" align="right"><?php echo number_format($length, 2, '.', ',') ?>&nbsp;m</td>
        <td width="74" align="right"><?php if ( $k+1==$sundries_sub_num) { echo number_format($tot_length, 2, '.', ',');}?></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">&nbsp;</td>
               <td colspan="2">&nbsp;</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="254"><?php echo $item_name ?>
                 <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="88" style="border-right:hidden" ><div align="right"><?php echo $_additional_quantity?></div></td>
               <td width="80"><?php echo $unit ?></td>
               <td width="102" style="border-right:hidden"><div align="right"><?php echo $_SESSION['additional_quantity_'.$m]?></div></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>
       </td>
           
    </tr>
          <tr>
            <td>
              <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');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">&nbsp;</td>
                  <td width="125" align="right"><input name="cancelbutton" type="button" class="imagebutton" id="cancelbutton" onClick="needToConfirm = false; confirmRefresh(); MM_showHideLayers('wait','','show');" 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>
            </td>
    </tr>
</table>
         
 
  
</form>
</div>
</body>