<!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" />
<title>Untitled Document</title>
</head>

<body>
</body>
</html>

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

$table_name = $_SESSION['db_prefix']."_captured_jobs";
$job_query = "SELECT job_no  FROM $table_name WHERE captured_value IS NULL ";
$job_result = mySQL_query($job_query);
$job_num = mysql_num_rows($job_result);
$a=0;
while ($a<$job_num) {
	
$job_no = mysql_result($job_result, $a, "job_no");

//$job_no = '1412017';

$table_name = $_SESSION['db_prefix']."_captured_jobs";
	$press_points_query = "SELECT press_points, press_charge FROM $table_name WHERE job_no = '".$job_no."' ";
	$press_points_result = mySQL_query($press_points_query);
	$press_points = mysql_result($press_points_result, 0, "press_points");
	$press_charge = mysql_result($press_points_result, 0, "press_charge");
	$quote_total0 = $press_points * $press_charge;  
	
	
$table_name = $_SESSION['db_prefix']."_captured_timber_done";	
$quoted_timber_query = "SELECT cap_timber, cap_timber_length, cap_timber_price FROM $table_name WHERE cap_timber_name = 'Trusses' AND cap_job = '".$job_no."'  ";
$quoted_timber_result = mySQL_query($quoted_timber_query);
$quoted_timber_num = mysql_num_rows($quoted_timber_result);	
$j=0;
	$quote_total1 = 0;		  
	while ($j<$quoted_timber_num) {
		$cap_timber = mysql_result($quoted_timber_result, $j, "cap_timber");
		$cap_timber_length = mysql_result($quoted_timber_result, $j, "cap_timber_length");
		$cap_timber_price = mysql_result($quoted_timber_result, $j, "cap_timber_price");
		$quoted_price = $cap_timber_length * $cap_timber_price;	
		$quote_total1 = $quote_total1 + $quoted_price;
		$j++;
	}
	
	
	
$quote_total1a='0.00'; 
	$table_name = $_SESSION['db_prefix']."_captured_jobs";
	$cap_plate_query = "SELECT plate_area, plate_charge FROM $table_name WHERE job_no = '".$job_no."' ";
	$cap_plate_result = mySQL_query($cap_plate_query);
	$cap_plate_area = mysql_result($cap_plate_result, 0, "plate_area");
	$cap_plate_charge = mysql_result($cap_plate_result, 0, "plate_charge");
	$quote_total1a = $cap_plate_area * $cap_plate_charge;	
	
	
	
$table_name = $_SESSION['db_prefix']."_captured_mitek_done";
$mitek_query = "SELECT cap_item_id, SUM(cap_quantity) AS summed_cap_quantity, cap_price,  SUM(auth_quantity) AS summed_delivered, del_price FROM $table_name WHERE cap_job = '".$job_no."'  GROUP BY cap_item_id";
$mitek_result = mySQL_query($mitek_query);
$mitek_num = mysql_num_rows($mitek_result);	
	
$i=0;
	$cap_total = '0.00';
	$quote_total2 = '0.00';
	$total2 ='0.00';
	$sub_total2='0.00';
	while ($i<$mitek_num) {
		$cap_item_id = mysql_result($mitek_result, $i, "cap_item_id");
			$table_name4 = $_SESSION['db_prefix']."_store_items";
			$item_query = "SELECT item_name, unit FROM $table_name4 WHERE store_item_id = '".$cap_item_id."' ";
			$item_result = mySQL_query($item_query);
			$item_name = mysql_result($item_result, 0, "item_name");
			$unit = mysql_result($item_result, 0, "unit");
		$cap_quantity = mysql_result($mitek_result, $i, "summed_cap_quantity");
		$cap_price = mysql_result($mitek_result, $i, "cap_price");
		$delivered = mysql_result($mitek_result, $i, "summed_delivered");
		$price = mysql_result($mitek_result, $i, "del_price");
		$cap_total = $cap_quantity * $cap_price;
		$quote_total2 = $quote_total2 + $cap_total;
		$total2 = $price * $delivered;
		$sub_total2 = $sub_total2 + $total2;
		$difference2a = $total2 - $cap_total;
		$i++;
	}


$table_name = $_SESSION['db_prefix']."_captured_timber_done";
$quoted_bracing_timber_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."'  "; 
$quoted_bracing_timber_result = mySQL_query($quoted_bracing_timber_query);
$quoted_bracing_timber_num = mysql_num_rows($quoted_bracing_timber_result);

$j=0;
		  $quote_total3 = '0.00';
		  $auth_total3 = '0.00';		  
		while ($j<$quoted_bracing_timber_num) {
		$cap_timber = mysql_result($quoted_bracing_timber_result, $j, "cap_timber");
		$cap_timber_length = mysql_result($quoted_bracing_timber_result, $j, "cap_timber_length");
		$auth_timber_length = mysql_result($quoted_bracing_timber_result, $j, "auth_timber_length");
		$cap_timber_price = mysql_result($quoted_bracing_timber_result, $j, "cap_timber_price");
		$auth_timber_price = mysql_result($quoted_bracing_timber_result, $j, "auth_timber_price");
		$quoted_price = $cap_timber_length * $cap_timber_price;	
		$quote_total3 = $quote_total3 + $quoted_price;
		$auth_price = $auth_timber_length * $auth_timber_price;	
		$auth_total3 = $auth_total3 + $auth_price;
		$difference3a = $auth_price - $quoted_price;
		$j++;
		}


$table_name = $_SESSION['db_prefix']."_captured_timber_done";
$quoted_sundries_timber_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."'  ";
$quoted_sundries_timber_result = mySQL_query($quoted_sundries_timber_query);
$quoted_sundries_timber_num = mysql_num_rows($quoted_sundries_timber_result);

 $j=0;
		  $quote_total4 = '0.00';
		  $auth_total4 = '0.00';	
		  
		while ($j<$quoted_sundries_timber_num) {
		$cap_timber = mysql_result($quoted_sundries_timber_result, $j, "cap_timber");
		$cap_timber_length = mysql_result($quoted_sundries_timber_result, $j, "cap_timber_length");
		$auth_timber_length = mysql_result($quoted_sundries_timber_result, $j, "auth_timber_length");
		$cap_timber_price = mysql_result($quoted_sundries_timber_result, $j, "cap_timber_price");
		$auth_timber_price = mysql_result($quoted_sundries_timber_result, $j, "auth_timber_price");
		$quoted_price = $cap_timber_length * $cap_timber_price;	
		$quote_total4 = $quote_total4 + $quoted_price;
		$auth_price = $auth_timber_length * $auth_timber_price;	
		$auth_total4 = $auth_total4 + $auth_price;
		$difference4a = $auth_price - $quoted_price;
	  	$j++;
		}



$table_name = $_SESSION['db_prefix']."_captured_sundries_done";
$sundries_query = "SELECT cap_item_id, SUM(cap_quantity) AS summed_cap_quantity, cap_price, SUM(auth_quantity) AS summed_delivered, del_price  FROM $table_name WHERE cap_job = '".$job_no."' GROUP BY cap_item_id";
$sundries_result = mySQL_query($sundries_query);
$sundries_num = mysql_num_rows($sundries_result);
$m=0;
$total5='0.00';
			 $sub_total5 = '0.00';
			 $quote_total5 = '0.00';
				  $m=0;
				   while ($m<$sundries_num) { 
					    $additional_item_id = mysql_result($sundries_result, $m, "cap_item_id");
						$table_name = $_SESSION['db_prefix']."_store_items";
							$name_query = "SELECT item_name, unit FROM $table_name WHERE store_item_id = '".$additional_item_id."' ";
							$name_result = mySQL_query($name_query);
							$name_num = mysql_num_rows($name_result);
							$item_name = mysql_result($name_result, 0, "item_name");
						$cap_quantity	= mysql_result($sundries_result, $m, "summed_cap_quantity");
						$cap_price	= mysql_result($sundries_result, $m, "cap_price");
						$delivered = mysql_result($sundries_result, $m, "summed_delivered");
						$del_price = mysql_result($sundries_result, $m, "del_price");
						$cap_total = $cap_quantity * $cap_price;
						$quote_total5 = $quote_total5 + $cap_total;
						$total5 = $del_price * $delivered;
						$sub_total5 = $sub_total5 + $total5;
						$difference5a = $total5 - $cap_total;
						$m++;
				   }

$quote_total = $quote_total0 + $quote_total1 + $quote_total1a + $quote_total2 + $quote_total3 + $quote_total4 + $quote_total5 ;	

echo '<br>*****'.$quote_total;

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

$a++;
}
	
?>	
	