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

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

//if (isset($_POST['truss_button']) )
//	   {
//		 $sub_category= 'Truss';
//	   }
//		 
//if (isset($_POST['t5_button']) )
//	  {
//		$sub_category='t5';
//	  }
//	  
//if (isset($_POST['t7_button']) )
//	  {
//		$sub_category='t7';
//	  }
//	  
//if (isset($_POST['plates_button']) )
//	  {
//		$sub_category='Plates';
//	  }


$job_no = $_POST['job_no'];
$truss_num = $_POST['truss_num'];
$t5_num = $_POST['t5_num'];
$t7_num = $_POST['t7_num'];
$plates_num = $_POST['plates_num'];
$bill_id = $_POST['bill_id'];

$i=0;
while ($i<$truss_num) {
	$truss_type = $_SESSION['edit_truss_type_'.$i];
	$edit_value = $_POST['truss_edit'.$i];
	$span_value = $_POST['truss_edit_span'.$i];
	$press_value = $_POST['truss_edit_press'.$i];
	
		
	if ($_SESSION['edit_truss_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']."', '79', '".$job_no."' ,'".$truss_type."','0','".$edit_value."' )";
			$trackingResult = mySQL_query($trackingQuery);
		
	$table_name = $_SESSION['db_prefix']."_cutting_bill_lines";
		$check_query = "SELECT job_no FROM $table_name WHERE job_no = '".$job_no."' AND type='truss' ";
		$check_result = mySQL_query($check_query);
		$check_num = mysql_num_rows($check_result);
		if ($check_num>0) {
	   		$table_name = $_SESSION['db_prefix']."_cutting_bill_lines";  
		}
		else {
			$table_name = $_SESSION['db_prefix']."_cutting_bill_lines_done";
		} 
	$truss_query = "INSERT INTO $table_name (job_no, bill_line_id, bill_id, type, truss_type, quantity, span, press_points ) VALUES ('".$job_no."', '0', '".$bill_id."', 'truss', '".$truss_type."', '".$edit_value."', '".$span_value."', '".$press_value."'  ) ";
		execsql($truss_query);
	}
	else
	{
		$table_name = $_SESSION['db_prefix']."_cutting_bill_lines";
		$check_query = "SELECT job_no FROM $table_name WHERE job_no = '".$job_no."' AND type='truss' ";
		$check_result = mySQL_query($check_query);
		$check_num = mysql_num_rows($check_result);
		if ($check_num>0) {
	   		$table_name = $_SESSION['db_prefix']."_cutting_bill_lines";  
		}
		else {
			$table_name = $_SESSION['db_prefix']."_cutting_bill_lines_done";
		} 
		$check_query = "SELECT quantity FROM $table_name WHERE truss_type = '".$truss_type."' AND job_no = '".$job_no."' ";
		$check_result = mySQL_query($check_query);
		$recorded_value = mysql_result($check_result, 0, "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']."', '78', '".$job_no."' ,'".$truss_type."','".$recorded_value."','".$edit_value."' )";
			$trackingResult = mySQL_query($trackingQuery);
	}
		
	$table_name = $_SESSION['db_prefix']."_cutting_bill_lines";
		$check_query = "SELECT job_no FROM $table_name WHERE job_no = '".$job_no."' AND type='truss' ";
		$check_result = mySQL_query($check_query);
		$check_num = mysql_num_rows($check_result);
		if ($check_num>0) {
	   		$table_name = $_SESSION['db_prefix']."_cutting_bill_lines";  
		}
		else {
			$table_name = $_SESSION['db_prefix']."_cutting_bill_lines_done";
		} 
	$truss_query = "UPDATE $table_name SET quantity = '".$edit_value."', span = '".$span_value."', press_points = '".$press_value."' WHERE truss_type = '".$truss_type."' AND job_no = '".$job_no."' ";
	execsql($truss_query);
	}
	$i++;
}


$j=0;
while ($j<$t5_num) {
	
	$store_item_id = $_SESSION['edit_t5_store_item_id_'.$j];
	$bracing_original_qty = $_POST['bracing_original_qty'.$j];
	$bracing_edit_value = $_POST['t5_edit'.$j];
	$cap_timber_bracing = $_POST['cap_timber_bracing'.$j];
	$_SESSION['edit_t5_quantity_'.$j] = $bracing_edit_value;
		
	$table_name = $_SESSION['db_prefix']."_store_items";
	$length_query = "SELECT length FROM $table_name WHERE store_item_id = '".$store_item_id."'  ";
	$length_result = mySQL_query($length_query);
	$length = mysql_result($length_result, 0, "length");
	
	$table_name = $_SESSION['db_prefix']."_cutting_bill_lines";
		$check_query = "SELECT job_no FROM $table_name WHERE job_no = '".$job_no."' AND type='timber' ";
		$check_result = mySQL_query($check_query);
		$check_num = mysql_num_rows($check_result);
		if ($check_num>0) {
	   		$table_name = $_SESSION['db_prefix']."_cutting_bill_lines";  
		}
		else {
			$table_name = $_SESSION['db_prefix']."_cutting_bill_lines_done";
		} 
	
	if ($_SESSION['edit_t5_line'.$j]=='0') {
	
	$table_name2 = $_SESSION['db_prefix']."_tracking_production";
	$trackingQuery = "INSERT INTO $table_name2 (entry_date, user, action, details_1, details_2, details_3, details_4) VALUES ('".$entryDate."' ,'".$_SESSION['username']."', '75', '".$job_no."' ,'".$store_item_id."','0','".$bracing_edit_value."' )";
			$trackingResult = mySQL_query($trackingQuery);	
	
	$table_name = $_SESSION['db_prefix']."_cutting_bill_lines";
		$check_query = "SELECT job_no FROM $table_name WHERE job_no = '".$job_no."' AND type='timber' ";
		$check_result = mySQL_query($check_query);
		$check_num = mysql_num_rows($check_result);
		if ($check_num>0) {
	   		$table_name = $_SESSION['db_prefix']."_cutting_bill_lines";  
		}
		else {
			$table_name = $_SESSION['db_prefix']."_cutting_bill_lines_done";
		} 
	$t5_query = "INSERT INTO $table_name (job_no, bill_line_id, bill_id, type, store_item_id, quantity ) VALUES ('".$job_no."', '0', '".$bill_id."', 'timber', '".$store_item_id."', '".$bracing_edit_value."' ) ";
		execsql($t5_query);	
	$bracing_original_qty=0;	
	}
	
	
	$table_name = $_SESSION['db_prefix']."_cutting_bill_lines";
		$check_query = "SELECT job_no FROM $table_name WHERE job_no = '".$job_no."' AND type='timber' ";
		$check_result = mySQL_query($check_query);
		$check_num = mysql_num_rows($check_result);
		if ($check_num>0) {
	   		$table_name = $_SESSION['db_prefix']."_cutting_bill_lines";  
		}
		else {
			$table_name = $_SESSION['db_prefix']."_cutting_bill_lines_done";
		} 
		$check_query = "SELECT quantity FROM $table_name WHERE store_item_id = '".$store_item_id."' AND job_no = '".$job_no."' ";
		$check_result = mySQL_query($check_query);
		$recorded_value = mysql_result($check_result, 0, "quantity");
		if ($recorded_value!=$bracing_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']."', '74', '".$job_no."' ,'".$store_item_id."','".$recorded_value."','".$bracing_edit_value."' )";
			$trackingResult = mySQL_query($trackingQuery);
	}
	
	
	$original_reserved = $bracing_original_qty *($length/1000);
	$new_reserved = $bracing_edit_value *($length/1000);
	//echo '<br>original'.$original_qty.'  '.$original_reserved.'  new '.$bracing_edit_value.'  '.$new_reserved;
		
	$table_name = $_SESSION['db_prefix']."_captured_timber";
	$captured_query = "UPDATE $table_name SET reserved = (reserved + ('".$new_reserved."'-'".$original_reserved."')) WHERE cap_timber_name = 'Trusses' AND cap_timber = '".$cap_timber_bracing."' AND cap_job = '".$job_no."' ";
	//echo '<br>'.$captured_query;
	execsql($captured_query);
	//echo '<br>'.$captured_query;
	
	$table_name = $_SESSION['db_prefix']."_cutting_bill_lines";
		$check_query = "SELECT job_no FROM $table_name WHERE job_no = '".$job_no."' AND type='timber' ";
		$check_result = mySQL_query($check_query);
		$check_num = mysql_num_rows($check_result);
		if ($check_num>0) {
	   		$table_name = $_SESSION['db_prefix']."_cutting_bill_lines";  
		}
		else {
			$table_name = $_SESSION['db_prefix']."_cutting_bill_lines_done";
		} 
	$t5_query = "UPDATE $table_name SET quantity = '".$bracing_edit_value."' WHERE store_item_id = '".$store_item_id."' AND job_no = '".$job_no."' ";
	execsql($t5_query);	
	
	$j++;
}

$j=0;
while ($j<$t7_num) {
	$store_item_id = $_SESSION['edit_t7_store_item_id_'.$j];
	$sundries_original_qty = $_POST['sundries_original_qty'.$j];
	$sundries_edit_value = $_POST['t7_edit'.$j];
	$cap_timber_sundries = $_POST['cap_timber_sundries'.$j];
	$_SESSION['edit_t7_quantity_'.$j] = $sundries_edit_value;
	
	$table_name = $_SESSION['db_prefix']."_store_items";
	$length_query = "SELECT length FROM $table_name WHERE store_item_id = '".$store_item_id."'  ";
	$length_result = mySQL_query($length_query);
	$length = mysql_result($length_result, 0, "length");
	
	$table_name = $_SESSION['db_prefix']."_cutting_bill_lines";
		$check_query = "SELECT job_no FROM $table_name WHERE job_no = '".$job_no."' AND type='timber' ";
		$check_result = mySQL_query($check_query);
		$check_num = mysql_num_rows($check_result);
		if ($check_num>0) {
	   		$table_name = $_SESSION['db_prefix']."_cutting_bill_lines";  
		}
		else {
			$table_name = $_SESSION['db_prefix']."_cutting_bill_lines_done";
		} 
		
	if ($_SESSION['edit_t7_line'.$j]=='0') {
		 $table_name2 = $_SESSION['db_prefix']."_tracking_production";
	$trackingQuery = "INSERT INTO $table_name2 (entry_date, user, action, details_1, details_2, details_3, details_4) VALUES ('".$entryDate."' ,'".$_SESSION['username']."', '75', '".$job_no."' ,'".$store_item_id."','0','".$sundries_edit_value."' )";
			$trackingResult = mySQL_query($trackingQuery);
		
	
	$t7_query = "INSERT INTO $table_name (job_no, bill_line_id, bill_id, type, store_item_id, quantity ) VALUES ('".$job_no."', '0', '".$bill_id."', 'timber', '".$store_item_id."', '".$sundries_edit_value."' ) ";
		execsql($t7_query);	
	$sundries_original_qty=0;
	}
	
		$check_query = "SELECT quantity FROM $table_name WHERE store_item_id = '".$store_item_id."' AND job_no = '".$job_no."' ";
		$check_result = mySQL_query($check_query);
		$recorded_value = mysql_result($check_result, 0, "quantity");
		if ($recorded_value!=$sundries_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']."', '74', '".$job_no."' ,'".$store_item_id."','".$recorded_value."','".$sundries_edit_value."' )";
			$trackingResult = mySQL_query($trackingQuery);
	}
		
	$original_reserved = $sundries_original_qty *($length/1000);
	$new_reserved = $sundries_edit_value *($length/1000);
	
	$table_name = $_SESSION['db_prefix']."_captured_timber";
	$captured_query = "UPDATE $table_name SET reserved = (reserved + ('".$new_reserved."'-'".$original_reserved."')) WHERE cap_timber_name = 'Trusses' AND cap_timber = '".$cap_timber_sundries."' AND cap_job = '".$job_no."' ";
	execsql($captured_query);
	//echo '<br>'.$captured_query;
		
	$table_name = $_SESSION['db_prefix']."_cutting_bill_lines";
		$check_query = "SELECT job_no FROM $table_name WHERE job_no = '".$job_no."' AND type='timber' ";
		$check_result = mySQL_query($check_query);
		$check_num = mysql_num_rows($check_result);
		if ($check_num>0) {
	   		$table_name = $_SESSION['db_prefix']."_cutting_bill_lines";  
		}
		else {
			$table_name = $_SESSION['db_prefix']."_cutting_bill_lines_done";
		} 
	$t7_query = "UPDATE $table_name SET quantity = '".$sundries_edit_value."' WHERE store_item_id = '".$store_item_id."' AND job_no = '".$job_no."' ";
	execsql($t7_query);	
	
	$j++;
}


$j=0;
while ($j<$plates_num) {
	
	$store_item_id = $_SESSION['edit_plates_store_item_id'.$j];
	
	$edit_value = $_POST['plates_edit'.$j];
	$_SESSION['edit_plates_quantity_'.$j] = $edit_value;
	$table_name = $_SESSION['db_prefix']."_cutting_bill_lines";
		$check_query = "SELECT job_no FROM $table_name WHERE job_no = '".$job_no."' AND type='plate' ";
		$check_result = mySQL_query($check_query);
		$check_num = mysql_num_rows($check_result);
		if ($check_num>0) {
	   		$table_name = $_SESSION['db_prefix']."_cutting_bill_lines";  
		}
		else {
			$table_name = $_SESSION['db_prefix']."_cutting_bill_lines_done";
		} 
	if ($_SESSION['edit_plates_line'.$j]=='0') {
		
			$table_name2 = $_SESSION['db_prefix']."_tracking_production";
			$trackingQuery = "INSERT INTO $table_name2 (entry_date, user, action, details_1, details_2, details_3, details_4) VALUES ('".$entryDate."' ,'".$_SESSION['username']."', '77', '".$job_no."' ,'".$store_item_id."','0','".$edit_value."' )";
			$trackingResult = mySQL_query($trackingQuery);
		
			$plates_query = "INSERT INTO $table_name (job_no, bill_line_id, bill_id, type, store_item_id, quantity ) VALUES ('".$job_no."', '0', '".$bill_id."', 'plate', '".$store_item_id."', '".$edit_value."' ) ";
			execsql($plates_query);	
			$recorded_value=0;
	}
	
		
			$check_query = "SELECT quantity FROM $table_name WHERE store_item_id = '".$store_item_id."' AND job_no = '".$job_no."' ";
			
			$check_result = mySQL_query($check_query);
			$recorded_value = mysql_result($check_result, 0, "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']."', '76', '".$job_no."' ,'".$store_item_id."','".$recorded_value."','".$edit_value."' )";
			$trackingResult = mySQL_query($trackingQuery);
		
		
	$table_name = $_SESSION['db_prefix']."_cutting_bill_lines";
		$check_query = "SELECT job_no FROM $table_name WHERE job_no = '".$job_no."' AND type='plate' ";
		$check_result = mySQL_query($check_query);
		$check_num = mysql_num_rows($check_result);
		if ($check_num>0) {
	   		$table_name = $_SESSION['db_prefix']."_cutting_bill_lines";  
		}
		else {
			$table_name = $_SESSION['db_prefix']."_cutting_bill_lines_done";
		} 
	$plates_query = "UPDATE $table_name SET quantity = '".$edit_value."' WHERE store_item_id = '".$store_item_id."' AND job_no = '".$job_no."' ";
	execsql($plates_query);	
	}
	$j++;
}

$_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="cutting_bill_edit.php?job_no=<?php echo $job_no?>&screen=0&edit=0&sub_category=; needToConfirm = false;"
		</script>
        <?php		 
	   }
?>




