<?php
$table_name = $_SESSION['db_prefix']."_cutting_bill";
$cutting_query = "SELECT * FROM $table_name WHERE job_no = '".$job_no."' ";
$cutting_result = mySQL_query($cutting_query);
$cutting_num = mysql_num_rows($cutting_result);
if ($cutting_num!=0) {
		$bill_id = mysql_result($cutting_result, 0, "bill_id");
}
else {
	$bill_id='';
}

/* =====   truss charges */
$table_name = $_SESSION['db_prefix']."_captured_jobs";
$quote_press_points_J_query = "SELECT press_points, press_charge FROM $table_name WHERE job_no = '".$job_no."' ";
$quote_press_points_J_result = mySQL_query($quote_press_points_J_query);
$quote_press_points_J = mysql_result($quote_press_points_J_result, 0, "press_points");
$quote_press_charge = mysql_result($quote_press_points_J_result, 0, "press_charge");
$quote_press_point_total = $quote_press_points_J * $quote_press_charge; 

$table_name = $_SESSION['db_prefix']."_cutting_bill_lines_done";


$produced_press_point_total = 0;
$table_name = $_SESSION['db_prefix']."_production_bill_lines";
$produced_delivered_J_query = "SELECT delivered, press_points, press_charge FROM $table_name WHERE job_no = '".$job_no."' ";
$produced_delivered_J_result = mySQL_query($produced_delivered_J_query);
$produced_delivered_J_num = mysql_num_rows($produced_delivered_J_result);

$i=0;
while ($i<$produced_delivered_J_num) {
	$produced_delivered = mysql_result($produced_delivered_J_result, $i, "delivered");
	$produced_press_points = mysql_result($produced_delivered_J_result, $i, "press_points");
	$produced_press_charge = mysql_result($produced_delivered_J_result, $i, "press_charge");
	$produced_press_point_sub_total = $produced_delivered * $produced_press_points * $produced_press_charge;
	$produced_press_point_total = $produced_press_point_total + $produced_press_point_sub_total;
	$i++;
}

$table_name = $_SESSION['db_prefix']."_captured_timber_done";
$quoted_timber_J_query = "SELECT * FROM $table_name WHERE cap_timber_group = 'Trusses' AND cap_job = '".$job_no."'  ";
$quoted_timber_J_result = mySQL_query($quoted_timber_J_query);
$quoted_timber_J_num = mysql_num_rows($quoted_timber_J_result);



$truss_timber_J_query = "SELECT cap_timber, cap_timber_length, auth_timber_length, cap_timber_price, auth_timber_price FROM $table_name WHERE cap_timber_name = 'Trusses' AND cap_job = '".$job_no."'  ";
$truss_timber_J_result = mySQL_query($truss_timber_J_query);
$truss_timber_J_num = mysql_num_rows($truss_timber_J_result);

$quoted_truss_timber_total=0;
$i=0;
while ($i<$quoted_timber_J_num) {
	$quoted_truss_timber_length = mysql_result($truss_timber_J_result, $i, "cap_timber_length");
	$quoted_truss_timber_price = mysql_result($truss_timber_J_result, $i, "cap_timber_price");
	$quoted_truss_timber_charge = $quoted_truss_timber_length * $quoted_truss_timber_price;	
	$quoted_truss_timber_total = $quoted_truss_timber_total + $quoted_truss_timber_charge;
	$i++;
}

$table_name = $_SESSION['db_prefix']."_cutting_bill_lines_done";
$cutting_truss_J_query = "SELECT quantity, price FROM $table_name WHERE type = 'timber' AND bill_id = '".$bill_id."' GROUP BY store_item_id";
$cutting_truss_J_result = mySQL_query($cutting_truss_J_query);
$cutting_truss_J_num = mysql_num_rows($cutting_truss_J_result);

$cutting_truss_timber_total=0;
$i=0;
while ($i<$cutting_truss_J_num) {
	$cutting_truss_timber_quantity = mysql_result($cutting_truss_J_result, $i, "quantity");
	$cutting_truss_timber_price = mysql_result($cutting_truss_J_result, $i, "price");
	$cutting_truss_timber_charge = $cutting_truss_timber_quantity * $cutting_truss_timber_price;	
	$cutting_truss_timber_total = $cutting_truss_timber_total + $cutting_truss_timber_charge;
	$i++;
}
/* =====   end truss timber charges */

/* =====   begin truss plate charges */
$table_name = $_SESSION['db_prefix']."_captured_jobs";
$quote_plates_J_query = "SELECT plate_area, plate_charge FROM $table_name WHERE job_no = '".$job_no."' ";
$quote_plates_J_result = mySQL_query($quote_plates_J_query);
$quote_plates_area = mysql_result($quote_plates_J_result, 0, "plate_area");
$quote_plates_charge = mysql_result($quote_plates_J_result, 0, "plate_charge");
$quote_plates_charge_total = $quote_plates_area * $quote_plates_charge; 

$table_name = $_SESSION['db_prefix']."_cutting_bill_lines_done";
$cutting_plate_J_query = "SELECT quantity, price FROM $table_name WHERE type = 'plate' AND bill_id = '".$bill_id."' GROUP BY store_item_id";
$cutting_plate_J_result = mySQL_query($cutting_plate_J_query);
$cutting_plate_J_num = mysql_num_rows($cutting_plate_J_result);

$cutting_plate_total=0;
$i=0;
while ($i<$cutting_plate_J_num) {
	$cutting_plate_quantity = mysql_result($cutting_plate_J_result, $i, "quantity");
	$cutting_plate_price = mysql_result($cutting_plate_J_result, $i, "price");
	$cutting_plate_charge = $cutting_plate_quantity * $cutting_plate_price;	
	$cutting_plate_total = $cutting_plate_total + $cutting_plate_charge;
	$i++;
}


/* =====   end truss plate charges */

/* =====   begin mitec products */
$table_name = $_SESSION['db_prefix']."_captured_mitek_done";
$mitek_J_query = "SELECT cap_item_id, cap_quantity, cap_price,  auth_quantity, del_price FROM $table_name WHERE cap_job = '".$job_no."'  GROUP BY cap_item_id";
$mitek_J_result = mySQL_query($mitek_J_query);
$mitek_J_num = mysql_num_rows($mitek_J_result);

$cap_mitek_total=0;
$auth_mitek_total=0;
$i=0;
while ($i<$mitek_J_num) {
	$cap_quantity = mysql_result($mitek_J_result, $i, "cap_quantity");
	$cap_price = mysql_result($mitek_J_result, $i, "cap_price");
	$auth_quantity = mysql_result($mitek_J_result, $i, "auth_quantity");
	$auth_price = mysql_result($mitek_J_result, $i, "del_price");

	$cap_total = $cap_quantity * $cap_price;
	$cap_mitek_total = $cap_mitek_total + $cap_total;
	$auth_total = $auth_quantity * $auth_price;
	$auth_mitek_total = $auth_mitek_total + $auth_total;
	
	$i++;
}


$table_name = $_SESSION['db_prefix']."_captured_timber_done";
$bracing_timber_J_query = "SELECT cap_timber, cap_timber_length, cap_timber_price, auth_timber_length, auth_timber_price  FROM $table_name WHERE cap_timber_group = 'Bracing' AND cap_job = '".$job_no."'  "; 
$bracing_timber_J_result = mySQL_query($bracing_timber_J_query);
$bracing_timber_J_num = mysql_num_rows($bracing_timber_J_result);

$cap_bracing_total=0;
$auth_bracing_total=0;
$i=0;
while ($i<$bracing_timber_J_num) {
	$cap_timber_length = mysql_result($bracing_timber_J_result, $i, "cap_timber_length");
	$cap_timber_price = mysql_result($bracing_timber_J_result, $i, "cap_timber_price");
	$auth_timber_length = mysql_result($bracing_timber_J_result, $i, "auth_timber_length");
	$auth_timber_price = mysql_result($bracing_timber_J_result, $i, "auth_timber_price");

	$cap_total = $cap_timber_length * $cap_timber_price;
	$cap_bracing_total = $cap_bracing_total + $cap_total;
	$auth_total = $auth_timber_length * $auth_timber_price;
	$auth_bracing_total = $auth_bracing_total + $auth_total;
	
	$i++;
}

/* =====  end bracing timber  */

/* =====  begin sundries timber  */
$table_name = $_SESSION['db_prefix']."_captured_timber_done";
$sundries_timber_J_query = "SELECT cap_timber, cap_timber_length, cap_timber_price, auth_timber_length, auth_timber_price  FROM $table_name WHERE cap_timber_group = 'Sundries' AND cap_job = '".$job_no."'  "; 
$sundries_timber_J_result = mySQL_query($sundries_timber_J_query);
$sundries_timber_J_num = mysql_num_rows($sundries_timber_J_result);

$cap_sundries_total=0;
$auth_sundries_total=0;
$i=0;
while ($i<$sundries_timber_J_num) {
	$cap_timber_length = mysql_result($sundries_timber_J_result, $i, "cap_timber_length");
	$cap_timber_price = mysql_result($sundries_timber_J_result, $i, "cap_timber_price");
	$auth_timber_length = mysql_result($sundries_timber_J_result, $i, "auth_timber_length");
	$auth_timber_price = mysql_result($sundries_timber_J_result, $i, "auth_timber_price");

	$cap_total = $cap_timber_length * $cap_timber_price;
	$cap_sundries_total = $cap_sundries_total + $cap_total;
	$auth_total = $auth_timber_length * $auth_timber_price;
	$auth_sundries_total = $auth_sundries_total + $auth_total;
	
	$i++;
}

/* =====  end sundries timber  */


/* =====   begin additional sundries products */
$table_name = $_SESSION['db_prefix']."_captured_sundries_done";
$add_sundries_J_query = "SELECT cap_item_id, cap_quantity, cap_price,  auth_quantity, del_price FROM $table_name WHERE cap_job = '".$job_no."'  GROUP BY cap_item_id";
$add_sundries_J_result = mySQL_query($add_sundries_J_query);
$add_sundries_J_num = mysql_num_rows($add_sundries_J_result);

$cap_add_sundries_total=0;
$auth_add_sundries_total=0;
$i=0;
while ($i<$add_sundries_J_num) {
	$cap_quantity = mysql_result($add_sundries_J_result, $i, "cap_quantity");
	$cap_price = mysql_result($add_sundries_J_result, $i, "cap_price");
	$auth_quantity = mysql_result($add_sundries_J_result, $i, "auth_quantity");
	$auth_price = mysql_result($add_sundries_J_result, $i, "del_price");

	$cap_total = $cap_quantity * $cap_price;
	$cap_add_sundries_total = $cap_add_sundries_total + $cap_total;
	$auth_total = $auth_quantity * $auth_price;
	$auth_add_sundries_total = $auth_add_sundries_total + $auth_total;
	
	$i++;
}

/* =====  end additional sundries products  */

/* =====  begin variations  */

$table_name6 = $_SESSION['db_prefix']."_variations";
$variations_J_query = "SELECT delivered, price FROM $table_name6 WHERE job_no = '".$job_no."' ";
$variations_J_result = mySQL_query($variations_J_query);
$variations_J_num = mysql_num_rows($variations_J_result);

$variations_total = 0;
$i = 0;
while ($i<$variations_J_num) {
	$var_quantity = mysql_result($variations_J_result, $i, "delivered");
	$var_price = mysql_result($variations_J_result, $i, "price");
	
	$var_total = $var_quantity * $var_price;
	$variations_total = $variations_total + $var_total;
	$i++;
}

/* =====  end variations  */

$final_value = $produced_press_point_total + $cutting_truss_timber_total + $cutting_plate_total + $auth_mitek_total + $auth_bracing_total + $auth_sundries_total + $auth_add_sundries_total + $variations_total;

$final_result = $job_value - $final_value;






$final_result_perc = ($final_result/$job_value) * 100;

?>