<!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-edit set complete";
include ("../navigation/access.php");

date_default_timezone_set('Africa/Gaborone');
$entry_date = date("Y-m-d H:i:s", time()); 

?>

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

<!--jscript-->
<script type="text/javascript" src="../css/modal.js"></script>
<script type="text/javascript" src="../css/dhtmlwindow.js"></script>
<script language="JavaScript" src="../jscript/cancel_confirmation.js"  type="text/javascript"></script> 
</head>

<?php 
if ($permission_num==1) {
	include ("../navigation/production_sidebar.php");
	
	$job_no = $_GET['job_no'];
	$edit = $_GET['edit'];
	if($edit==1) {
		
		$table_name = $_SESSION['db_prefix']."_captured_jobs";
		$complete_query = "UPDATE $table_name SET compiled = 'yes', cutting_bill = 'yes', supplied = 'yes', authorisation = 'yes', produced = 'yes', delivered = 'yes', del_date = '".$entry_date."' WHERE job_no = '".$job_no."' ";
		execsql($complete_query);
		
	$table_name = $_SESSION['db_prefix']."_captured_mitek";
	$mitek_query = "SELECT cap_mitek_id FROM $table_name WHERE  cap_job = '".$job_no."' ";
	$mitek_result = mySQL_query($mitek_query);
	$mitek_num = mysql_num_rows($mitek_result);
	
	$i=0;
	while ($i<$mitek_num) {
		$cap_mitek_id = mysql_result($mitek_result, $i, "cap_mitek_id");
		$cancel_mitek_query = "UPDATE $table_name SET not_delivered = auth_quantity-delivered WHERE cap_job = '".$job_no."' AND cap_mitek_id = '".$cap_mitek_id."' ";
		execsql($cancel_mitek_query);
		
		$table_name1 = $_SESSION['db_prefix']."_captured_mitek";
		$table_name2 = $_SESSION['db_prefix']."_captured_mitek_done";
		$copy_query = "INSERT INTO $table_name2 SELECT * FROM $table_name1 WHERE cap_job = '".$job_no."' AND cap_mitek_id = '".$cap_mitek_id."' ";
    	execsql($copy_query);
		
		$table_name = $_SESSION['db_prefix']."_captured_mitek";
		$delete_query = "DELETE FROM $table_name  WHERE cap_job = '".$job_no."' AND cap_mitek_id = '".$cap_mitek_id."'  ";
		execsql($delete_query);
					
		$i++;
	}
	
	$table_name2 = $_SESSION['db_prefix']."_captured_sundries";
	$sundries_query = "SELECT cap_sundries_id FROM $table_name2 WHERE  cap_job = '".$job_no."' ";
	$sundries_result = mySQL_query($sundries_query);
	$sundries_num = mysql_num_rows($sundries_result);
	$j=0;
	while ($j<$sundries_num) {
		$cap_sundries_id = mysql_result($sundries_result, $j, "cap_sundries_id");
		$cancel_sundries_query = "UPDATE $table_name2 SET not_delivered = auth_quantity-delivered WHERE cap_job = '".$job_no."' AND cap_sundries_id = '".$cap_sundries_id."' ";
		execsql($cancel_sundries_query);
		
		$table_name1 = $_SESSION['db_prefix']."_captured_sundries";
		$table_name2 = $_SESSION['db_prefix']."_captured_sundries_done";
		$copy_query = "INSERT INTO $table_name2 SELECT * FROM $table_name1 WHERE cap_job = '".$job_no."' AND cap_sundries_id = '".$cap_sundries_id."' ";
    	execsql($copy_query);
		
		$table_name = $_SESSION['db_prefix']."_captured_sundries";
		$delete_query = "DELETE FROM $table_name  WHERE cap_job = '".$job_no."' AND cap_sundries_id = '".$cap_sundries_id."'  ";
		execsql($delete_query);
						
		$j++;
	}
	
		
	$table_name1 = $_SESSION['db_prefix']."_cutting_bill_lines";
	$cutting_bill_query = "SELECT truss_type FROM $table_name1 WHERE  job_no = '".$job_no."' AND type='truss'";
	$cutting_bill_result = mySQL_query($cutting_bill_query);
	$cutting_bill_num = mysql_num_rows($cutting_bill_result);
	$j=0;
	while ($j<$cutting_bill_num) {
		$truss_type = mysql_result($cutting_bill_result, $j, "truss_type");
				
		$table_name1 = $_SESSION['db_prefix']."_cutting_bill_lines";
		$table_name2 = $_SESSION['db_prefix']."_cutting_bill_lines_done";
		$copy_query = "INSERT INTO $table_name2 SELECT * FROM $table_name1 WHERE job_no = '".$job_no."' AND truss_type = '".$truss_type."' ";
		execsql($copy_query);
    			
		$table_name = $_SESSION['db_prefix']."_cutting_bill_lines";
		$delete_query = "DELETE FROM $table_name  WHERE job_no = '".$job_no."' AND truss_type = '".$truss_type."' ";
		execsql($delete_query);
		
		$j++;
	}

	$table_name1 = $_SESSION['db_prefix']."_cutting_bill_lines";
	$cutting_bill_query = "SELECT store_item_id FROM $table_name1 WHERE  job_no = '".$job_no."' AND ( type='timber' OR type='plate')";
	$cutting_bill_result = mySQL_query($cutting_bill_query);
	$cutting_bill_num = mysql_num_rows($cutting_bill_result);
	$j=0;
	while ($j<$cutting_bill_num) {
		$store_item_id = mysql_result($cutting_bill_result, $j, "store_item_id");
		$table_name3 = $_SESSION['db_prefix']."_cutting_supplies_lines";
		$cutting_supplies_query = "SELECT store_item_id, quantity FROM $table_name3 WHERE  job_no = '".$job_no."' and store_item_id = '".$store_item_id."' ";
		$cutting_supplies_result = mySQL_query($cutting_supplies_query);
		$cutting_supplies_num = mysql_num_rows($cutting_supplies_result);
		if ($cutting_supplies_num > 0) {
			$supplied_quantity = mysql_result($cutting_supplies_result, 0, "quantity");
		}
		else {
			$supplied_quantity = 0;
		}
		$table_name1 = $_SESSION['db_prefix']."_cutting_bill_lines";
		$cancel_cutting_bill_query = "UPDATE $table_name1 SET not_delivered = quantity-'".$supplied_quantity."' WHERE job_no = '".$job_no."' AND store_item_id = '".$store_item_id."' ";
		execsql($cancel_cutting_bill_query);	
		
		$table_name1 = $_SESSION['db_prefix']."_cutting_bill_lines";
		$table_name2 = $_SESSION['db_prefix']."_cutting_bill_lines_done";
		$copy_query = "INSERT INTO $table_name2 SELECT * FROM $table_name1 WHERE job_no = '".$job_no."' AND store_item_id = '".$store_item_id."' ";
		execsql($copy_query);
    			
		$table_name = $_SESSION['db_prefix']."_cutting_bill_lines";
		$delete_query = "DELETE FROM $table_name  WHERE job_no = '".$job_no."' AND store_item_id = '".$store_item_id."' ";
		execsql($delete_query);
		
				
		
				
		$j++;
	}
	
	$table_name1 = $_SESSION['db_prefix']."_cutting_supplies_lines";
	$cutting_supplies_query = "SELECT store_item_id FROM $table_name1 WHERE  job_no = '".$job_no."' ";
	$cutting_supplies_result = mySQL_query($cutting_supplies_query);
	$cutting_supplies_num = mysql_num_rows($cutting_supplies_result);
	$j=0;
	while ($j<$cutting_bill_num) {
		$store_item_id = mysql_result($cutting_supplies_result, $j, "store_item_id");
				
		$table_name1 = $_SESSION['db_prefix']."_cutting_supplies_lines";
		$table_name2 = $_SESSION['db_prefix']."_cutting_supplies_lines_done";
		$copy_query = "INSERT INTO $table_name2 SELECT * FROM $table_name1 WHERE job_no = '".$job_no."' AND store_item_id = '".$store_item_id."' ";
    	execsql($copy_query);
		
		$table_name = $_SESSION['db_prefix']."_cutting_supplies_lines";
		$delete_query = "DELETE FROM $table_name  WHERE job_no = '".$job_no."' AND store_item_id = '".$store_item_id."'  ";
		execsql($delete_query);
		
		$j++;
	}
	
	
	
	
	$table_name3 = $_SESSION['db_prefix']."_captured_timber";
	$timber_query = "SELECT cap_timber_id FROM $table_name3 WHERE  cap_job = '".$job_no."' ";
	$timber_result = mySQL_query($timber_query);
	$timber_num = mysql_num_rows($timber_result);
	$m=0;
	while ($m<$timber_num) {
		$cap_timber_id = mysql_result($timber_result, $m, "cap_timber_id");
		$cancel_timber_query = "UPDATE $table_name3 SET prereserved = '0', reserved = '0'  WHERE cap_job = '".$job_no."' AND cap_timber_id = '".$cap_timber_id."' ";
		execsql($cancel_timber_query);	
		
		$table_name1 = $_SESSION['db_prefix']."_captured_timber";
		$table_name2 = $_SESSION['db_prefix']."_captured_timber_done";
		$copy_query = "INSERT INTO $table_name2 SELECT * FROM $table_name1 WHERE cap_job = '".$job_no."' AND cap_timber_id = '".$cap_timber_id."' ";
    	execsql($copy_query);
		
		$table_name = $_SESSION['db_prefix']."_captured_timber";
		$delete_query = "DELETE FROM $table_name  WHERE cap_job = '".$job_no."' AND cap_timber_id = '".$cap_timber_id."'  ";
		execsql($delete_query);				
		$m++;
	}
	
	$table_name2 = $_SESSION['db_prefix']."_authorised_timber";
	$authorised_query = "SELECT auth_id FROM $table_name2 WHERE  job_no = '".$job_no."' ";
	$authorised_result = mySQL_query($authorised_query);
	$authorised_num = mysql_num_rows($authorised_result);
	$j=0;
	while ($j<$authorised_num) {
		$auth_id = mysql_result($authorised_result, $j, "auth_id");
		$cancel_authorised_query = "UPDATE $table_name2 SET not_delivered = reserved-delivered WHERE job_no = '".$job_no."' AND auth_id = '".$auth_id."' ";
		
		execsql($cancel_authorised_query);	
		
		$table_name1 = $_SESSION['db_prefix']."_authorised_timber";
		$table_name2 = $_SESSION['db_prefix']."_authorised_timber_done";
		$copy_query = "INSERT INTO $table_name2 SELECT * FROM $table_name1 WHERE job_no = '".$job_no."' AND auth_id = '".$auth_id."' ";
    	execsql($copy_query);
		
		$table_name = $_SESSION['db_prefix']."_authorised_timber";
		$delete_query = "DELETE FROM $table_name  WHERE job_no = '".$job_no."' AND auth_id = '".$auth_id."'  ";
		execsql($delete_query);
				
		$j++;
	}
	
	$table_name = $_SESSION['db_prefix']."_tracking_production";
	$trackingQuery = "INSERT INTO $table_name (entry_date, user, action, details_1, details_2, details_3, details_4) VALUES ('".$entry_date."' ,'".$_SESSION['username']."', '89', '".$job_no."', '' ,'', '' )";
	$trackingResult = mySQL_query($trackingQuery);
	
		
	}
	
	
	$table_name = $_SESSION['db_prefix']."_captured_jobs";
	$job_query = "SELECT cust_id, captured, compiled, cutting_bill, supplied, authorisation, produced, delivered 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");
		
	$quote='no';
	
?>

<body>
<div class="main_frame">
<table id="tableMain" > <!--begin of tableMain-->
    <tr>
      <td width="830" height="125" valign="top" >
  <div class="page_head_frame">
    <table width="809" border="0" cellspacing="0" cellpadding="0">
      <tr>
        <td width="385"><label class="label_page_head">Job status edit</label></td>
        <td width="182" align="right"><label style="font-size:24px; color:#FB6808;"><?php echo "J".$job_no ?></label></td>
        <td width="113" align="right">&nbsp;</td>
      </tr>
    </table>
  </div>
  <div class="page_middle_frame">
         
      <br />
      <br />
    
    <?php 
	include('../include/customer_block.php');
	echo '<br />';
	include('../include/job_block.php');
	?>
  </div><br />
<br />

  <div class="page_middle_frame">
  <table width="616" id="table_details_display">
  <tr>
    <td width="191">Capture quote</td>
    <td width="66" 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 width="343" rowspan="6" align="center" valign="middle" style="border-bottom:hidden; border-right:hidden; border-top:hidden; background-color:transparent;"><input name="button" type="button" class="imagebutton" id="button"  onclick="cancel_confirmation(4, '../production/job_status_edit.php?job_no=<?php echo $job_no?>&edit=1');" value="Set job completed" /></td>
  </tr>
  <tr>
    <td>Cutting bill</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>
    </tr>
  <tr>
    <td>Issue direct materials</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>
    </tr>
  <tr>
    <td>Produced</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>
    </tr>
  <tr>
    <td>Authorisation</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>
    </tr>
  <tr>
    <td>Delivery</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>
  
  </td> 
      <!--center of tableMain-->
</tr>

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

</body>

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


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