<!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" />


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

<!--jscript-->

<script language="JavaScript" src="../jscript/confirm_exit.js"  type="text/javascript"></script>
<script language="JavaScript" src="../jscript/cancel_confirmation.js"  type="text/javascript"></script> 
<script language="JavaScript" src="../jscript/show_hide_layers.js"  type="text/javascript"></script>
<script language="JavaScript" src="../jscript/numbers_only.js"  type="text/javascript"></script> 
<script language="JavaScript" src="../jscript/alert_message.js"  type="text/javascript"></script>   

<script type="text/javascript">
<!--
function validate_required(input,alerttxt,maxvalue)
{
with (input)
  {
  if (value>maxvalue)
    {
    alertMessage(1, 'deliverJobWizbase.php?p=2&s=', 'The entered amount exceeds the outstanding authorised quantity, the entry will be cleared.<br><br>Please enter a valid amount.');
	input.value = "";
	return false;
	}
  else
    {
    return true;
    }
  }
}
</script>

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

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

$job_no = $_GET['job_no'];
//$_SESSION['edit_start'] = $_GET['edit'];

//$sub_category=$_GET['sub_category'];
$quote='no';


$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']."_customer";
$name_query = "SELECT comp_name FROM $table_name WHERE cust_id = '".$cust_id."' ";
$name_result = mySQL_query($name_query);
$name = mysql_result($name_result, 0, "comp_name");

$table_name = $_SESSION['db_prefix']."_captured_mitek";
$mitek_query = "SELECT cap_mitek_id, cap_item_id,cap_quantity, auth_quantity, delivered, de_authorised, notes_text FROM $table_name WHERE cap_job = '".$job_no."' AND (auth_quantity-delivered-de_authorised) > 0";
$mitek_result = mySQL_query($mitek_query);
$mitek_num = mysql_num_rows($mitek_result);

$table_name = $_SESSION['db_prefix']."_authorised_timber";
$table_name2 = $_SESSION['db_prefix']."_authorised_timber_edit_temp";
$bracing_query = "SELECT auth_timber_name FROM $table_name WHERE auth_timber_group = 'bracing' AND job_no = '".$job_no."' AND store_item_id <> 0 AND (reserved-delivered-de_authorised) > 0 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";
$table_name2 = $_SESSION['db_prefix']."_authorised_timber_edit_temp";
$sundries_query = "SELECT auth_timber_name FROM $table_name WHERE auth_timber_group = 'sundries' AND job_no = '".$job_no."' AND store_item_id <> 0 AND (reserved-delivered-de_authorised) > 0 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_name2 = $_SESSION['db_prefix']."_captured_sundries_edit_temp";
$additional_query = "SELECT * FROM $table_name WHERE cap_job = '".$job_no."'  AND (auth_quantity-delivered-de_authorised) > 0";

$additional_result = mySQL_query($additional_query);
$additional_num = mysql_num_rows($additional_result);
?>
<?php include ('../include/wait.php'); ?>

</head>

<body onload="MM_showHideLayers('wait','','hide')" >

<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="472"><label class="label_page_head">Job de-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')
		?>
</div><br />
<br />
<br />
<br />
<br />
<br />
<form name="form1" id="form1" action="de_authorisation_edit_save.php" method="POST">
   <div class="page_middle_frame">
 <table width="675" id="table_details_display" > 
              <tr>
                <td align="left" valign="top"><label class="label_std">Mitek products:</label></td>
                <td colspan="2" align="center" valign="top"><label class="label_std">Outstanding Authorised</label></td>
                <td colspan="2" align="center" valign="top"><label class="label_std">de-Authorise </label>
                                  <input name="edit_job_no" type="hidden" value="<?php echo $job_no?>" /></td>
                <td align="center" valign="top"><label class="label_std"><label class="label_std">Reason to de-Authorise </label></label></td>
              </tr>
              <?php 
				  $i=0;
				  while ($i<$mitek_num) { 
					    $store_item_id = mysql_result($mitek_result, $i, "cap_item_id");										
						$quantity =( mysql_result($mitek_result, $i, "auth_quantity")-mysql_result($mitek_result, $i, "delivered")-mysql_result($mitek_result, $i, "de_authorised"));
						
						$table_name = $_SESSION['db_prefix']."_store_items";
						$item_name_query = "SELECT item_name, unit FROM $table_name WHERE store_item_id = '".$store_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="228"><?php echo $item_name ?><input name="cap_item_id<?php echo $i?>" id="cap_item_id<?php echo $i?>" type="hidden" value="<?php echo $store_item_id ?>" /></td>
               <td width="65" style="border-right:hidden"><div align="right"><?php echo $quantity ?></div></td>
               <td width="43">&nbsp;&nbsp;<?php echo $unit?></td>
               <td width="69" align="right"><input name="mitek_edit<?php echo $i?>" type="text" class="text60" id="mitek_edit<?php echo $i?>"  onfocus = "this.select();" style="text-align:right;" onKeyUp="validate_required(mitek_edit<?php echo $i?>, 'The entered amount exceeds the outstanding authorised quantity, the entry will be cleared. ' +'\n'+ 'Please enter a valid amount.', <?php echo $quantity ?>)" onkeypress="return numbersonly(this, event)" autocomplete="off"/></td>
               <td width="32" align="center"><?php echo $unit?></td>
               <td width="210" align="center"><label for="reason_text"></label>
                <input name="reason_text<?php echo $i?>" type="text" class="text200" id="reason_text<?php echo $i?>" maxlength="50"  /></td>
             </tr>
              <?php
						
			  $i++;
             }
			 ?>
             <input name="mitek_num" type="hidden" value="<?php echo $i;?>" />
            </table>
   </div>
          
          
  <div class="page_middle_frame">
    <table width="676" align="left" id="table_details_display" >
      <tr style="border-bottom:double">
        <td><label class="label_std">Bracing</label></td>
        <td width="41" align="center"><label class="label_std">Item</label></td>
        <td width="114" align="center"><label class="label_std">Outstanding Authorised</label></td>
        <td colspan="2" align="center"><label class="label_std">de-Authorise </label>
          <input name="edit_job_no2" type="hidden" value="<?php echo $job_no?>" /></td>
        <td align="center"><label class="label_std">Reason to de-Authorise </label>
          </label></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']."_authorised_timber";
				
					$bracing_sub_query = "SELECT * FROM $table_name WHERE  auth_timber_name = '".$bracing_name."'  AND job_no = '".$job_no."' AND store_item_id>0  ";
					$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")-mysql_result($bracing_sub_result, $k, "delivered")-mysql_result($bracing_sub_result, $k, "de_authorised");
				 
				 	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");
					}
						
					
			 ?>
              
        
           
      <tr>
        <td width="183"><?php if ($temp_name!=$bracing_name) { echo $bracing_name;} ?><?php $temp_name=$bracing_name; ?>
          
          <input name="bracing_name<?php echo $j?>" type="hidden" id="bracing_name<?php echo $j?>" value="<?php echo $bracing_name?>" />
                    
          <input type="hidden" name="bracing_store_item_id<?php echo $j?>" id="bracing_store_item_id<?php echo $j?>" value="<?php echo $store_item_id?>" /></td>
        <td align="left" ><?php echo $length;?></td>
        <td align="center"><?php  if($length!=0) { echo $quantity.' no';}?></td>
        <td width="69" align="right"><input name="bracing_edit<?php echo $j?>" type="text" class="text60" id="bracing_edit<?php echo $j?>"  onfocus = "this.select();"  style="text-align:right;" onKeyUp="validate_required(bracing_edit<?php echo $j?>, 'The entered amount exceeds the outstanding authorised quantity, the entry will be cleared. ' +'\n'+ 'Please enter a valid amount.', <?php echo $quantity ?>)" onkeypress="return numbersonly(this, event)" autocomplete="off"/></td>
        <td width="27" align="center">no</td>
        <td width="214" align="center"><input name="bracing_reason_text<?php echo $j?>" type="text" class="text200" id="bracing_reason_text<?php echo $j?>" maxlength="50"  /></td>
      </tr>
      <?php 
			 $k++;
			 }
			 $j++;
			 }
			 ?>
      <input name="bracing_num" type="hidden" value="<?php echo $j;?>" />
    </table>
   <p><br />
<br />

  </div>
  <div class="page_middle_frame">
    <table width="676" align="left" id="table_details_display" >
      <tr style="border-bottom:double">
        <td><label class="label_std">Sundries</label></td>
        <td width="41"><label class="label_std">Item</label></td>
        <td width="112" align="center"><label class="label_std">Outstanding authorised</label></td>
        <td colspan="2" align="center"><label class="label_std">de-Authorise </label>
          <input name="edit_job_no3" type="hidden" value="<?php echo $job_no?>" /></td>
        <td width="211" align="center"><label class="label_std">Reason to de-Authorise </label>
          </label></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";
				 $table_name2 = $_SESSION['db_prefix']."_authorised_timber_edit_temp";
					$sundries_sub_query = "SELECT * FROM $table_name WHERE  auth_timber_name = '".$sundries_name."'  AND job_no = '".$job_no."' AND store_item_id>0   UNION ALL SELECT * FROM $table_name2 WHERE  auth_timber_name = '".$sundries_name."'  AND job_no = '".$job_no."' AND store_item_id>0 ";
					$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")-mysql_result($sundries_sub_result, $k, "delivered")-mysql_result($sundries_sub_result, $k, "de_authorised");
				 
				 	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;
					}
					 					
			 ?>
      
      <input name="sundries_sub_num<?php echo $j;?>" type="hidden" value="<?php echo $k+1;?>" />
      <tr>
        <td width="183"><?php if ($temp_name!=$sundries_name) { echo $sundries_name;} ?>
          <?php $temp_name=$sundries_name; ?>
          <input name="sundries_name<?php echo $j?>" type="hidden" id="sundries_name<?php echo $j?>" value="<?php echo $sundries_name?>" />
          <input type="hidden" name="sundries_store_item_id<?php echo $j?>" id="sundries_store_item_id<?php echo $j?>" value="<?php echo $store_item_id?>" /></td>
        <td align="left" ><?php echo $length;?></td>
        <td align="center" ><?php  if($length!=0) { echo $quantity.' no';}?></td>
        <td width="69" align="right"><input name="sundries_edit<?php echo $j?>" type="text" class="text60" id="sundries_edit<?php echo $j?>" onfocus = "this.select();"  style="text-align:right;" onKeyUp="validate_required(sundries_edit<?php echo $j?>, 'The entered amount exceeds the outstanding authorised quantity, the entry will be cleared. ' +'\n'+ 'Please enter a valid amount.', <?php echo $quantity ?>)" onkeypress="return numbersonly(this, event)" autocomplete="off"/></td>
        <td width="32" align="center">no</td>
        <td align="right"><input name="sundries_reason_text<?php echo $j?>" type="text" class="text200" id="sundries_reason_text<?php echo $j?>" maxlength="50"  /></td>
        </tr>
      <?php 
			 $k++;
			 }
			 $j++;
			 }
			 ?>
             <input name="sundries_num" type="hidden" value="<?php echo $j;?>" />
    </table>
    
  </div>
  <div class="page_middle_frame">

 <table width="682" id="table_details_display" >
   <tr>
     <td align="left" valign="top"><label class="label_std">Additional Sundries</label></td>
     <td colspan="2" align="center" valign="top"><label class="label_std">Outstanding authorised</label></td>
     <td colspan="2" align="center" valign="top"><label class="label_std">de- Authorised</label></td>
     <td align="center" valign="top"><label class="label_std">
       <label class="label_std">Reason to de-Authorise </label>
       </label></td>
   </tr> 
             <?php 
				  $m=0;
				  while ($m<$additional_num) {
					    $store_item_id = mysql_result($additional_result, $m, "cap_item_id"); 
					 	$_additional_quantity = ( mysql_result($additional_result, $m, "auth_quantity")-mysql_result($additional_result, $m, "delivered")-mysql_result($additional_result, $m, "de_authorised"));
						
						$table_name = $_SESSION['db_prefix']."_store_items";
						$item_name_query = "SELECT item_name, unit FROM $table_name WHERE store_item_id = '".$store_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="228"><?php echo $item_name ?>
                <input name="additional_item<?php echo $m?>" id="additional_item<?php echo $m?>" type="hidden" value="<?php echo $store_item_id?>" /></td>
               <td width="71" style="border-right:hidden" ><div align="right"><?php echo $_additional_quantity?></div></td>
               <td width="41"><?php echo $unit ?></td>
               <td width="71" align="right"><input name="additional_edit<?php echo $m?>" type="text" class="text60" id="additional_edit<?php echo $m?>"  onfocus = "this.select();"  style="text-align:right;" onKeyUp="validate_required(additional_edit<?php echo $m?>, 'The entered amount exceeds the outstanding authorised quantity, the entry will be cleared. ' +'\n'+ 'Please enter a valid amount.', <?php echo $_additional_quantity ?>)" onkeypress="return numbersonly(this, event)" autocomplete="off"/></td>
               <td width="26" align="center"><?php echo $unit ?></td>
               <td width="215" align="right"><input name="add_reason_text<?php echo $m?>" type="text" class="text200" id="add_reason_text<?php echo $m?>" maxlength="50"  /></td>
             </tr>
              <?php
					
			  $m++;
             }
			?>
            <input name="additional_num" type="hidden" value="<?php echo $m;?>" />
    </table>
  </div>
  <div class="page_middle_frame">
    <table width="645" border="0">
      <tr>
      <td width="20"></td>
      <td width="615" align="right"><input type="hidden" name="job_no" id="job_no" value="<?php echo $job_no ?>" />
        <input name="button2" type="button" class="imagebutton" id="button2" onclick="cancel_confirmation(3, '../production/production_main.php');" value="Close"/>
        <input name="save_button"  id="save_button" type="submit" class="imagebutton" value="Save " onclick="needToConfirm = false; confirmRefresh();" />
        <input name="save_close_button"  id="save_close_button" type="submit" class="imagebutton" value="Save &amp; Close" onclick="needToConfirm = false; confirmRefresh();" /></td>
    </tr>
</table>
  </div>
  </form>
  </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>