<?php 
include('../include/session.php');

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

if (isset($_POST['mitek_button']) )
	   {
		 $sub_category= 'Mitek';
	   }
		 
if (isset($_POST['additional_button']) )
	  {
		$sub_category='Sundries';
	  }
	  
if (isset($_POST['bracing_button']) )
	  {
		$sub_category='Bracing';
	  }
	  
if (isset($_POST['sundries2_button']) )
	  {
		$sub_category='Sundries2';
	  }

$class5_num = $_POST['class5_num'];
$class7_num = $_POST['class7_num'];
$job_no = $_POST['job_no'];
$mitek_num = $_POST['mitek_num'];
$bracing_num = $_POST['bracing_num'];
$sundries_num = $_POST['sundries_num'];
$laminated_num = $_POST['laminated_num'];
$additional_num = $_POST['additional_num'];

$press_points = $_POST['press_points_edit'];
$plate_area = $_POST['plate_area_edit'];

$table_name = $_SESSION['db_prefix']."_captured_jobs";
$press_query = "UPDATE $table_name SET press_points = '".$press_points."', plate_area = '".$plate_area."' WHERE job_no = '".$job_no."' ";
execsql($press_query);

$j=0;
while ($j<$class5_num) {
	$edit_value = $_POST['class5_edit'.$j];
	
	$table_name = $_SESSION['db_prefix']."_captured_timber";
	$timber5_check_query = "SELECT cap_timber_id FROM $table_name WHERE cap_timber_group = 'Trusses' AND cap_job = '".$job_no."' AND cap_class = 'S5'  ";
	$timber5_check_result = mySQL_query($timber5_check_query);
	$timber5_check_num = mysql_num_rows($timber5_check_result);
	
	if ($timber5_check_num>0) {
		$table_nameS5 = $_SESSION['db_prefix']."_captured_timber";
	}
else {
	$table_nameS5 = $_SESSION['db_prefix']."_captured_timber_done";
}
	$check_query = "SELECT cap_timber_length FROM $table_nameS5 WHERE  cap_timber_name = 'Trusses' AND cap_timber = '".$_SESSION['s5_cap_timber'.$j]."' AND cap_job = '".$job_no."' ";
	$check_result = mySQL_query($check_query);
	$recorded_value = mysql_result($check_result, 0, "cap_timber_length");
	if ($recorded_value!=$edit_value) {
	       	$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 ('".$entryDate."' ,'".$_SESSION['username']."', '60', '".$job_no."' ,'".$_SESSION['s5_cap_timber'.$j]."','".$recorded_value."','".$edit_value."' )";
			$trackingResult = mySQL_query($trackingQuery);
	}
	
	

$bracing_query = "UPDATE $table_nameS5 SET cap_timber_length = '".$edit_value."', prereserved = '".$edit_value."' WHERE cap_timber_name = 'Trusses' AND cap_timber = '".$_SESSION['s5_cap_timber'.$j]."' AND cap_job = '".$job_no."' ";
	execsql($bracing_query);
	
	$j++;
}

$j=0;
while ($j<$class7_num) {
	$edit_value = $_POST['class7_edit'.$j];
	
	$table_name = $_SESSION['db_prefix']."_captured_timber";
	$timber5_check_query = "SELECT cap_timber_id FROM $table_name WHERE cap_timber_group = 'Trusses' AND cap_job = '".$job_no."' AND cap_class = 'S7'  ";
	$timber5_check_result = mySQL_query($timber5_check_query);
	$timber5_check_num = mysql_num_rows($timber5_check_result);
	
	if ($timber5_check_num>0) {
		$table_nameS7 = $_SESSION['db_prefix']."_captured_timber";
	}
else {
	$table_nameS7 = $_SESSION['db_prefix']."_captured_timber_done";
}
	$check_query = "SELECT cap_timber_length FROM $table_nameS7 WHERE  cap_timber_name = 'Trusses' AND cap_timber = '".$_SESSION['s7_cap_timber'.$j]."' AND cap_job = '".$job_no."' ";
	$check_result = mySQL_query($check_query);
	$recorded_value = mysql_result($check_result, 0, "cap_timber_length");
	if ($recorded_value!=$edit_value) {
	       	$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 ('".$entryDate."' ,'".$_SESSION['username']."', '60', '".$job_no."' ,'".$_SESSION['s7_cap_timber'.$j]."','".$recorded_value."','".$edit_value."' )";
			$trackingResult = mySQL_query($trackingQuery);
	}
	
	$table_name = $_SESSION['db_prefix']."_captured_timber";
	$bracing_query = "UPDATE $table_nameS7 SET cap_timber_length = '".$edit_value."', prereserved = '".$edit_value."' WHERE cap_timber_name = 'Trusses' AND cap_timber = '".$_SESSION['s7_cap_timber'.$j]."' AND cap_job = '".$job_no."' ";
	execsql($bracing_query);
	
	$j++;
}



$i=0;
while ($i<$mitek_num) {
	$cap_item_id = $_POST['cap_item_id'.$i];
	$edit_value = $_POST['mitek_edit'.$i];
	
		
	if ($_SESSION['edit_mitek_line'.$i]=='0') {
			$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 ('".$entryDate."' ,'".$_SESSION['username']."', '62', '".$job_no."' ,'".$cap_item_id."','0','".$edit_value."' )";
			$trackingResult = mySQL_query($trackingQuery);
			
			$table_name = $_SESSION['db_prefix']."_store_items";
			$price_query = "SELECT price FROM $table_name WHERE store_item_id = '".$cap_item_id."'  ";
			$price_result = mySQL_query($price_query);
			$price = mysql_result($price_result, 0, "price");
		
			$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 = "INSERT INTO $table_name (cap_job, cap_item_id, cap_quantity, auth_quantity, cap_price ) VALUES ('".$job_no."', '".$cap_item_id."',  '".$edit_value."', '0',  '".$price."' ) ";
			execsql($mitek_query);
	
	}
	else {
		
		$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_name2 = $_SESSION['db_prefix']."_captured_mitek";
		}
		else {
			$table_name2 = $_SESSION['db_prefix']."_captured_mitek_done";
		}
		$check_query = "SELECT cap_quantity FROM $table_name2 WHERE cap_item_id = '".$cap_item_id."' AND cap_job = '".$job_no."' ";
		$check_result = mySQL_query($check_query);
		$recorded_value = mysql_result($check_result, 0, "cap_quantity");
		if ($recorded_value!=$edit_value) {
	       	$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 ('".$entryDate."' ,'".$_SESSION['username']."', '61', '".$job_no."' ,'".$cap_item_id."','".$recorded_value."','".$edit_value."' )";
			$trackingResult = mySQL_query($trackingQuery);
	}
				
		$mitek_query = "UPDATE $table_name2 SET cap_quantity = '".$edit_value."' WHERE cap_item_id = '".$cap_item_id."' AND cap_job = '".$job_no."' ";
		execsql($mitek_query);
	}
	$i++;
}

$j=0;
while ($j<$bracing_num) {
	$bracing_name = $_POST['bracing_name'.$j];
	$edit_value = $_POST['bracing_edit'.$j]; 
	
	$table_name = $_SESSION['db_prefix']."_list_bracing";
	$name_query = "SELECT item_name, width, depth, class FROM $table_name WHERE item_name = '".$bracing_name."' ";
	$name_result = mySQL_query($name_query);
	$bracing_name = mysql_result($name_result, 0, "item_name");
	$bracing_width = mysql_result($name_result, 0, "width");
	$bracing_depth = mysql_result($name_result, 0, "depth");
	$bracing_class = mysql_result($name_result, 0, "class");
	$bracing_timber = $bracing_depth.'x'.$bracing_width.' ('.$bracing_class.')';
	
	if ($_SESSION['edit_bracing_line'.$j]=='0') {
		  	$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 ('".$entryDate."' ,'".$_SESSION['username']."', '64', '".$job_no."' ,'".$bracing_name."','0','".$edit_value."' )";
			$trackingResult = mySQL_query($trackingQuery);
			
			$bracing_type = $bracing_depth.'x'.$bracing_width.' ('.$bracing_class.')'; 
			$table_name = $_SESSION['db_prefix']."_timber_meter_price";
			$price_query = "SELECT price FROM $table_name WHERE timber = '".$bracing_type."' ";
			$price_result = mySQL_query($price_query);
			$price = mysql_result($price_result, 0, "price");
		
		    $table_name = $_SESSION['db_prefix']."_captured_timber";
			$bracing_check_query = "SELECT cap_timber_id FROM $table_name WHERE cap_job = '".$job_no."' AND cap_timber_group = 'Bracing' ";
			$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']."_captured_timber";
			}
			else {
					$table_name = $_SESSION['db_prefix']."_captured_timber_done";
			}
			$bracing_query = "INSERT INTO $table_name (cap_timber_group, cap_timber_name, cap_timber, cap_timber_length, auth_timber_length, prereserved, reserved, cap_job, cap_class, cap_width, cap_depth, cap_timber_price ) VALUES ('bracing', '".$bracing_name."', '".$bracing_timber."', '".$edit_value."', '0', '".$edit_value."', '0', '".$job_no."', '".$bracing_class."', '".$bracing_width."', '".$bracing_depth."', '".$price."') ";
			execsql($bracing_query);
	}
	else {
		    $table_name = $_SESSION['db_prefix']."_captured_timber";
			$bracing_check_query = "SELECT cap_timber_id FROM $table_name WHERE cap_job = '".$job_no."' AND cap_timber_group = 'Bracing' ";
			$bracing_check_result = mySQL_query($bracing_check_query);
			$bracing_check_num = mysql_num_rows($bracing_check_result);
		
			if ($bracing_check_num>0) {
					$table_name2 = $_SESSION['db_prefix']."_captured_timber";
			}
			else {
					$table_name2 = $_SESSION['db_prefix']."_captured_timber_done";
			}
		
		
		$check_query = "SELECT cap_timber_length FROM $table_name2 WHERE cap_timber_name = '".$bracing_name."' AND cap_job = '".$job_no."' ";
	execsql($check_query);
		$check_result = mySQL_query($check_query);
		$recorded_value = mysql_result($check_result, 0, "cap_timber_length");
		if ($recorded_value!=$edit_value) {
	       	$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 ('".$entryDate."' ,'".$_SESSION['username']."', '63', '".$job_no."' ,'".$bracing_name."','".$recorded_value."','".$edit_value."' )";
			$trackingResult = mySQL_query($trackingQuery);
	}
		
	
	$bracing_query = "UPDATE $table_name2 SET cap_timber_length = '".$edit_value."', prereserved = '".$edit_value."' WHERE cap_timber_name = '".$bracing_name."' AND cap_job = '".$job_no."' ";
	execsql($bracing_query);
	}
	$j++;
}

$j=0;
while ($j<$sundries_num) {
	$sundries_name = $_POST['sundries_name'.$j];
	$edit_value = $_POST['sundries_edit'.$j];
	
	$table_name = $_SESSION['db_prefix']."_list_sundries";
	$name_query = "SELECT item_name, width, depth, class FROM $table_name WHERE item_name = '".$sundries_name."' ";
	$name_result = mySQL_query($name_query);
	$sundries_name = mysql_result($name_result, 0, "item_name");
	$sundries_width = mysql_result($name_result, 0, "width");
	$sundries_depth = mysql_result($name_result, 0, "depth");
	$sundries_class = mysql_result($name_result, 0, "class");
	$sundries_timber = $sundries_depth.'x'.$sundries_width.' ('.$sundries_class.')';
	
	if ($_SESSION['edit_sundries_line'.$j]=='0') {
		  $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 ('".$entryDate."' ,'".$_SESSION['username']."', '64', '".$job_no."' ,'".$sundries_name."','0','".$edit_value."' )";
			$trackingResult = mySQL_query($trackingQuery);
			
			$sundries_type = $sundries_depth.'x'.$sundries_width.' ('.$sundries_class.')';
			$table_name = $_SESSION['db_prefix']."_timber_meter_price";
			$price_query = "SELECT price FROM $table_name WHERE timber = '".$sundries_type."' ";
			$price_result = mySQL_query($price_query);
			$price = mysql_result($price_result, 0, "price");
			
		    $table_name = $_SESSION['db_prefix']."_captured_timber";
			$sundries_check_query = "SELECT cap_timber_id FROM $table_name WHERE cap_job = '".$job_no."' AND cap_timber_group = 'Sundries' ";
			$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']."_captured_timber";
			}
			else {
					$table_name = $_SESSION['db_prefix']."_captured_timber_done";
			}		
		
		  $table_name = $_SESSION['db_prefix']."_captured_timber";
			$sundries_query = "INSERT INTO $table_name (cap_timber_group, cap_timber_name, cap_timber, cap_timber_length, auth_timber_length, prereserved, reserved, cap_job, cap_class, cap_width, cap_depth, cap_timber_price ) VALUES ('sundries', '".$sundries_name."',  '".$sundries_timber."',  '".$edit_value."', '0', '".$edit_value."', '0', '".$job_no."', '".$sundries_class."', '".$sundries_width."', '".$sundries_depth."', '".$price."') ";
			execsql($sundries_query);
	}
	else {
		    $table_name = $_SESSION['db_prefix']."_captured_timber";
			$sundries_check_query = "SELECT cap_timber_id FROM $table_name WHERE cap_job = '".$job_no."' AND cap_timber_group = 'Sundries' ";
			$sundries_check_result = mySQL_query($sundries_check_query);
			$sundries_check_num = mysql_num_rows($sundries_check_result);
		
			if ($sundries_check_num>0) {
					$table_name2 = $_SESSION['db_prefix']."_captured_timber";
			}
			else {
					$table_name2 = $_SESSION['db_prefix']."_captured_timber_done";
			}	
		
		
		$check_query = "SELECT cap_timber_length FROM $table_name2 WHERE cap_timber_name = '".$sundries_name."' AND cap_job = '".$job_no."' ";
	execsql($bracing_query);
		$check_result = mySQL_query($check_query);
		$recorded_value = mysql_result($check_result, 0, "cap_timber_length");
		if ($recorded_value!=$edit_value) {
	       	$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 ('".$entryDate."' ,'".$_SESSION['username']."', '63', '".$job_no."' ,'".$sundries_name."','".$recorded_value."','".$edit_value."' )";
			$trackingResult = mySQL_query($trackingQuery);
	}
		
	
	$sundries_query = "UPDATE $table_name2 SET cap_timber_length = '".$edit_value."', prereserved = '".$edit_value."' WHERE cap_timber_name = '".$sundries_name."' AND cap_job = '".$job_no."'  ";
	
	execsql($sundries_query);
	}
	$j++;
}


$j=0;
while ($j<$laminated_num) {
	$laminated_name = $_POST['laminated_name'.$j];
	$edit_value = $_POST['laminated_edit'.$j];
	
	$table_name = $_SESSION['db_prefix']."_store_items";
	$name_query = "SELECT item_name, width, depth, class FROM $table_name WHERE item_name = '".$laminated_name."' ";
	$name_result = mySQL_query($name_query);
	$laminated_name = mysql_result($name_result, 0, "item_name");
	$laminated_width = mysql_result($name_result, 0, "width");
	$laminated_depth = mysql_result($name_result, 0, "depth");
	$laminated_class = mysql_result($name_result, 0, "class");
	$laminated_timber = $laminated_depth.'x'.$laminated_width.' ('.$laminated_class.')';
	//echo '<br>*****'.$_SESSION['edit_laminated_line'.$j];
	if ($_SESSION['edit_laminated_line'.$j]=='0') {
		  $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 ('".$entryDate."' ,'".$_SESSION['username']."', '64', '".$job_no."' ,'".$laminated_name."','0','".$edit_value."' )";
			$trackingResult = mySQL_query($trackingQuery);
			
			$laminated_type = $laminated_depth.' x '.$laminated_width.' ('.$laminated_class.')  - ~';
			$table_name = $_SESSION['db_prefix']."_store_items";
			$price_query = "SELECT price FROM $table_name WHERE item_name = '".$laminated_type."' ";
			$price_result = mySQL_query($price_query);
			$price = mysql_result($price_result, 0, "price");
			
		    $table_name = $_SESSION['db_prefix']."_captured_timber";
			$laminated_check_query = "SELECT cap_timber_id FROM $table_name WHERE cap_job = '".$job_no."' AND cap_timber_group = 'Laminated' ";
			$laminated_check_result = mySQL_query($laminated_check_query);
			$laminated_check_num = mysql_num_rows($laminated_check_result);
		
			if ($laminated_check_num!=0) {
					$table_name = $_SESSION['db_prefix']."_captured_timber";
			}
			else {
					$table_name = $_SESSION['db_prefix']."_captured_timber_done";
			}		
		
		  $table_name = $_SESSION['db_prefix']."_captured_timber";
			$laminated_query = "INSERT INTO $table_name (cap_timber_group, cap_timber_name, cap_timber, cap_timber_length, auth_timber_length, prereserved, reserved, cap_job, cap_class, cap_width, cap_depth, cap_timber_price ) VALUES ('laminated', '".$laminated_name."',  '".$laminated_timber."',  '".$edit_value."', '0', '".$edit_value."', '0', '".$job_no."', '".$laminated_class."', '".$laminated_width."', '".$laminated_depth."', '".$price."') ";
			execsql($laminated_query);
	}
	else {
		    $table_name = $_SESSION['db_prefix']."_captured_timber";
			$laminated_check_query = "SELECT cap_timber_id FROM $table_name WHERE cap_job = '".$job_no."' AND cap_timber_group = 'Laminated' ";
			$laminated_check_result = mySQL_query($laminated_check_query);
			$laminated_check_num = mysql_num_rows($laminated_check_result);
			if ($laminated_check_num!=0) {
					$table_name2 = $_SESSION['db_prefix']."_captured_timber";
			}
			else {
					$table_name2 = $_SESSION['db_prefix']."_captured_timber_done";
			}	
		
		
		$check_query = "SELECT cap_timber_length FROM $table_name2 WHERE cap_timber_name = '".$laminated_name."' AND cap_job = '".$job_no."' ";
		execsql($check_query);
		$check_result = mySQL_query($check_query);
		$recorded_value = mysql_result($check_result, 0, "cap_timber_length");
		if ($recorded_value!=$edit_value) {
	       	$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 ('".$entryDate."' ,'".$_SESSION['username']."', '63', '".$job_no."' ,'".$laminated_name."','".$recorded_value."','".$edit_value."' )";
			$trackingResult = mySQL_query($trackingQuery);
	}
		
	
	$laminated_query = "UPDATE $table_name2 SET cap_timber_length = '".$edit_value."', prereserved = '".$edit_value."' WHERE cap_timber_name = '".$laminated_name."' AND cap_job = '".$job_no."'  ";
	
	execsql($laminated_query);
	}
	$j++;
}






$m=0;
while ($m<$additional_num) {
	$cap_item_id = $_POST['additional_item'.$m];
	$edit_value = $_POST['additional_edit'.$m];
		
	if ($_SESSION['edit_additional_line'.$m]=='0') {
			$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 ('".$entryDate."' ,'".$_SESSION['username']."', '62', '".$job_no."' ,'".$cap_item_id."','0','".$edit_value."' )";
			$trackingResult = mySQL_query($trackingQuery);
			
			$table_name = $_SESSION['db_prefix']."_store_items";
			$price_query = "SELECT price FROM $table_name WHERE store_item_id = '".$cap_item_id."'  ";
			$price_result = mySQL_query($price_query);
			$price = mysql_result($price_result, 0, "price");
			
			$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 = "INSERT INTO $table_name (cap_job, cap_item_id, cap_quantity, auth_quantity, cap_price ) VALUES ('".$job_no."', '".$cap_item_id."',  '".$edit_value."', '0',  '".$price."'  ) ";
			execsql($additional_query);
	
	}
	else {
		$table_name = $_SESSION['db_prefix']."_captured_sundries";
			$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_name2 = $_SESSION['db_prefix']."_captured_sundries";
			}
			else {
					$table_name2 = $_SESSION['db_prefix']."_captured_sundries_done";
			}
		
		$table_name = $_SESSION['db_prefix']."_captured_sundries";
		$check_query = "SELECT cap_quantity FROM $table_name2 WHERE cap_item_id = '".$cap_item_id."' AND cap_job = '".$job_no."' ";
		$check_result = mySQL_query($check_query);
		$recorded_value = mysql_result($check_result, 0, "cap_quantity");
		if ($recorded_value!=$edit_value) {
	       	$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 ('".$entryDate."' ,'".$_SESSION['username']."', '61', '".$job_no."' ,'".$cap_item_id."','".$recorded_value."','".$edit_value."' )";
			$trackingResult = mySQL_query($trackingQuery);
	}
		
		
		$additional_query = "UPDATE $table_name2 SET cap_quantity = '".$edit_value."' WHERE cap_item_id = '".$cap_item_id."' AND cap_job = '".$job_no."' ";
		execsql($additional_query);
	}
	$m++;
}

$table_name = $_SESSION['db_prefix']."_captured_jobs";
$plate_query = "SELECT value, (plate_area*plate_charge) AS summed_plate, (press_points*press_charge) AS summed_press FROM $table_name WHERE job_no = '".$job_no."' ";
$plate_result = mySQL_query($plate_query);
$plate_num = mysql_num_rows($plate_result);
$job_value = mysql_result($plate_result, 0, "value");
$plate_total = mysql_result($plate_result, 0, "summed_plate");
$press_total = mysql_result($plate_result, 0, "summed_press");


$table_name = $_SESSION['db_prefix']."_captured_timber";
$trusses_check_query = "SELECT cap_timber_id FROM $table_name WHERE cap_job = '".$job_no."' AND cap_timber_group = 'trusses' ";
$trusses_check_result = mySQL_query($trusses_check_query);
$trusses_check_num = mysql_num_rows($trusses_check_result);

if ($trusses_check_num>0) {
$table_name = $_SESSION['db_prefix']."_captured_timber";
}
else {
$table_name = $_SESSION['db_prefix']."_captured_timber_done";
}
$trusses_query = "SELECT SUM(cap_timber_length*cap_timber_price) AS summed_trusses_total FROM $table_name WHERE cap_job = '".$job_no."' AND cap_timber_group = 'trusses' ";
$trusses_result = mySQL_query($trusses_query);
$trusses_num = mysql_num_rows($trusses_result);
$trusses_total = mysql_result($trusses_result, 0, "summed_trusses_total");


$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 SUM(cap_quantity*cap_price) AS summed_mitek_total FROM $table_name WHERE cap_job = '".$job_no."' ";
$mitek_result = mySQL_query($mitek_query);
$mitek_num = mysql_num_rows($mitek_result);
$mitek_total = mysql_result($mitek_result, 0, "summed_mitek_total");


$table_name = $_SESSION['db_prefix']."_captured_timber";
$bracing_check_query = "SELECT cap_timber_id FROM $table_name WHERE cap_job = '".$job_no."' AND cap_timber_group = 'bracing' ";
$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']."_captured_timber";
}
else {
$table_name = $_SESSION['db_prefix']."_captured_timber_done";
}
$bracing_query = "SELECT SUM(cap_timber_length*cap_timber_price) AS summed_bracing_total FROM $table_name WHERE cap_job = '".$job_no."' AND cap_timber_group = 'bracing' ";
$bracing_result = mySQL_query($bracing_query);
$bracing_num = mysql_num_rows($bracing_result);
$bracing_total = mysql_result($bracing_result, 0, "summed_bracing_total");


$table_name = $_SESSION['db_prefix']."_captured_timber";
$sundries_check_query = "SELECT cap_timber_id FROM $table_name WHERE cap_job = '".$job_no."' AND cap_timber_group = 'sundries' ";
$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']."_captured_timber";
}
else {
$table_name = $_SESSION['db_prefix']."_captured_timber_done";
}
$sundries_query = "SELECT SUM(cap_timber_length*cap_timber_price) AS summed_sundries_total FROM $table_name WHERE cap_job = '".$job_no."' AND cap_timber_group = 'sundries' ";
$sundries_result = mySQL_query($sundries_query);
$sundries_num = mysql_num_rows($sundries_result);
$sundries_total = mysql_result($sundries_result, 0, "summed_sundries_total");


$table_name = $_SESSION['db_prefix']."_captured_sundries";
$add_sundries_check_query = "SELECT cap_sundries_id FROM $table_name WHERE cap_job = '".$job_no."'  ";
$add_sundries_check_result = mySQL_query($add_sundries_check_query);
$add_sundries_check_num = mysql_num_rows($add_sundries_check_result);

if ($add_sundries_check_num>0) {
$table_name = $_SESSION['db_prefix']."_captured_sundries";
}
else {
$table_name = $_SESSION['db_prefix']."_captured_sundries_done";
}
$add_sundries_query = "SELECT SUM(cap_quantity*cap_price) AS summed_add_sundries_total FROM $table_name WHERE cap_job = '".$job_no."' ";
$add_sundries_result = mySQL_query($add_sundries_query);
$add_sundries_num = mysql_num_rows($add_sundries_result);
$add_sundries_total = mysql_result($add_sundries_result, 0, "summed_add_sundries_total");


$captured_total=$trusses_total+$plate_total+$press_total+$mitek_total+$bracing_total+$sundries_total+$add_sundries_total;
$result = (($job_value-$captured_total)/$job_value)*100; 


$table_name = $_SESSION['db_prefix']."_captured_jobs";
$update_query = "UPDATE $table_name SET captured_value = '".$captured_total."', cap_result = '".$result."'  WHERE job_no = '".$job_no."' ";
execsql($update_query); 


//$table_name = $_SESSION['db_prefix']."_captured_timber_edit_temp";
//$delete_query = "DELETE FROM $table_name WHERE cap_job =  '".$job_no."'  "; 
//execsql($delete_query);   


$_SESSION['edit_start']=0;

	if (isset($_POST['save_close_button']) )
	   { ?>
       <script>
	 		 window.location="../production/production_main.php";
		</script>
        <?php		 
	   }
	   
	if (isset($_POST['save_button']) )
	   {  ?>
       <script>
	          window.location="captured_edit.php?job_no=<?php echo $job_no?>&save_message=0&screen=0&edit=0&sub_category=; needToConfirm = false;"
	   </script>
        <?php		 
	   }
	   ?>



