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

//$job_no=1504031;

$table_name = "tlh_captured_jobs";
$plate_prog_query = "SELECT press_points, press_charge, plate_area, plate_charge, value FROM $table_name WHERE job_no = '".$job_no."' ";
$plate_prog_result = mySQL_query($plate_prog_query);
$job_value = mysql_result($plate_prog_result, 0, "value");
$press_points = mysql_result($plate_prog_result, 0, "press_points");
$press_charge = mysql_result($plate_prog_result, 0, "press_charge");
$plate_area = mysql_result($plate_prog_result, 0, "plate_area");
$plate_charge = mysql_result($plate_prog_result, 0, "plate_charge");

$press_total = $press_points * $press_charge;
$plate_total = $plate_area * $plate_charge;

//echo '<br>*****press total: '.$press_total;
//echo '<br>*****plate total: '.$plate_total;


$table_name = "tlh_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 = "tlh_captured_timber";
	}
else {
	$table_nameS5 = "tlh_captured_timber_done";
}
$timber_prog_query = "SELECT cap_timber_length, cap_timber_price FROM $table_nameS5 WHERE cap_timber_group = 'Trusses' AND cap_job = '".$job_no."' AND cap_class = 'S5'  ";
$timber_prog_result = mySQL_query($timber_prog_query);
$timber_prog_num = mysql_num_rows($timber_prog_result);


$table_name = "tlh_captured_timber";
$timber7_check_query = "SELECT cap_timber_id FROM $table_name WHERE cap_timber_group = 'Trusses' AND cap_job = '".$job_no."' AND cap_class = 'S7'  ";
$timber7_check_result = mySQL_query($timber7_check_query);
$timber7_check_num = mysql_num_rows($timber7_check_result);
		
if ($timber7_check_num>0) {
		$table_nameS7 = "tlh_captured_timber";
	}
else {
	$table_nameS7 = "tlh_captured_timber_done";
}
$timber2_query = "SELECT cap_timber_length, cap_timber_price FROM $table_nameS7 WHERE cap_timber_group = 'Trusses' and cap_job = '".$job_no."' AND cap_class = 'S7' ";
$timber2_result = mySQL_query($timber2_query);
$timber2_num = mysql_num_rows($timber2_result);



$table_name = "tlh_captured_mitek";
$mitek_prog_check_query = "SELECT cap_mitek_id FROM $table_name WHERE cap_job = '".$job_no."'  ";
$mitek_prog_check_result = mySQL_query($mitek_prog_check_query);
$mitek_prog_check_num = mysql_num_rows($mitek_prog_check_result);

if ($mitek_prog_check_num>0) {
$table_name = "tlh_captured_mitek";
}
else {
$table_name = "tlh_captured_mitek_done";
}
$mitek_prog_query = "SELECT cap_quantity, cap_price FROM $table_name WHERE cap_job = '".$job_no."' ";
$mitek_prog_result = mySQL_query($mitek_prog_query);
$mitek_prog_num = mysql_num_rows($mitek_prog_result);

$table_name = "tlh_captured_timber";
$bracing_prog_check_query = "SELECT cap_timber_id FROM $table_name WHERE cap_job = '".$job_no."' AND cap_timber_group = 'Bracing' ";
$bracing_prog_check_result = mySQL_query($bracing_prog_check_query);
$bracing_prog_check_num = mysql_num_rows($bracing_prog_check_result);
		
if ($bracing_prog_check_num>0) {
		$table_name = "tlh_captured_timber";
	}
else {
	$table_name = "tlh_captured_timber_done";
}
$bracing_prog_query = "SELECT cap_timber_length, cap_timber_price FROM $table_name WHERE cap_timber_group = 'Bracing' AND cap_job = '".$job_no."' GROUP BY cap_timber_name ";
$bracing_prog_result = mySQL_query($bracing_prog_query);
$bracing_prog_num = mysql_num_rows($bracing_prog_result);

$table_name = "tlh_captured_timber";
$sundries_prog_check_query = "SELECT cap_timber_id FROM $table_name WHERE cap_job = '".$job_no."' AND cap_timber_group = 'sundries' ";
$sundries_prog_check_result = mySQL_query($sundries_prog_check_query);
$sundries_prog_check_num = mysql_num_rows($sundries_prog_check_result);
		
if ($sundries_prog_check_num>0) {
		$table_name = "tlh_captured_timber";
	}
else {
	$table_name = "tlh_captured_timber_done";
}
$sundries_prog_query = "SELECT cap_timber_length, cap_timber_price FROM $table_name WHERE cap_timber_group = 'sundries' AND cap_job = '".$job_no."' GROUP BY cap_timber_name ";
$sundries_prog_result = mySQL_query($sundries_prog_query);
$sundries_prog_num = mysql_num_rows($sundries_prog_result);





$table_name = "tlh_captured_sundries";
$additional_prog_check_query = "SELECT cap_sundries_id FROM $table_name WHERE cap_job = '".$job_no."'  ";
$additional_prog_check_result = mySQL_query($additional_prog_check_query);
$additional_prog_check_num = mysql_num_rows($additional_prog_check_result); 	
if ($additional_prog_check_num>0) {
		$table_name = "tlh_captured_sundries";
	}
else {
	$table_name = "tlh_captured_sundries_done";
}
$additional_prog_query = "SELECT cap_quantity, cap_price FROM $table_name WHERE cap_job = '".$job_no."' ";
$additional_prog_result = mySQL_query($additional_prog_query);
$additional_prog_num = mysql_num_rows($additional_prog_result);



$timber_total=0;
$i=0;
while ($i<$timber_prog_num) { 
			$quantity = mysql_result($timber_prog_result, $i, "cap_timber_length");
			$price = mysql_result($timber_prog_result, $i, "cap_timber_price");
			
			$total = $quantity * $price;
			$timber_total = $timber_total + $total;
$i++;	
}
//echo '<br>*****timber total: '.$timber_total;

$timber2_total=0;
$i=0;
while ($i<$timber2_num) { 
			$quantity = mysql_result($timber2_result, $i, "cap_timber_length");
			$price = mysql_result($timber2_result, $i, "cap_timber_price");
			
			$total = $quantity * $price;
			$timber2_total = $timber2_total + $total;
$i++;	
}
//echo '<br>*****timber2 total: '.$timber2_total;
				

$mitek_total=0;
$i=0;
while ($i<$mitek_prog_num) { 
			$quantity = mysql_result($mitek_prog_result, $i, "cap_quantity");
			$price = mysql_result($mitek_prog_result, $i, "cap_price");
			
			$total = $quantity * $price;
			$mitek_total = $mitek_total + $total;
$i++;	
}
//echo '<br>*****mitek total: '.$mitek_total;

$bracing_total=0;
$i=0;
while ($i<$bracing_prog_num) { 
			$quantity = mysql_result($bracing_prog_result, $i, "cap_timber_length");
			$price = mysql_result($bracing_prog_result, $i, "cap_timber_price");
			
			$total = $quantity * $price;
			$bracing_total = $bracing_total + $total;
$i++;	
}
//echo '<br>*****bracing total: '.$bracing_total;

$sundries_total=0;
$i=0;
while ($i<$sundries_prog_num) { 
			$quantity = mysql_result($sundries_prog_result, $i, "cap_timber_length");
			$price = mysql_result($sundries_prog_result, $i, "cap_timber_price");
			
			$total = $quantity * $price;
			$sundries_total = $sundries_total + $total;
$i++;	
}
//echo '<br>*****sundries total: '.$sundries_total;

$additional_total=0;
$i=0;
while ($i<$additional_prog_num) { 
			$quantity = mysql_result($additional_prog_result, $i, "cap_quantity");
			$price = mysql_result($additional_prog_result, $i, "cap_price");
			
			$total = $quantity * $price;
			$additional_total = $additional_total + $total;
$i++;	
}
//echo '<br>*****additional total: '.$additional_total;

$captured_total= $press_total + $plate_total + $timber_total + $timber2_total + $mitek_total + $bracing_total + $sundries_total + $additional_total;

//echo '<br>*****'.$captured_total;
//echo '<br>*****'.$job_value;
if ($job_value>0) {
$prog_result = (($job_value-$captured_total)/$job_value)*100;
}
else {
	$prog_result = 0;
}
//echo '<br>*****'.$result;



//echo '<br>*****'.'end prog_calculate.php';


?>