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

	$d = date("d");
	$m = date("m");
	$y = date("Y");
	
	$result_value = $_POST['result_text'];
	$job_value = $_POST['job_value_text'];
	$captured_value = $_POST['captured_value_text'];
	
	$user_id = $_SESSION['username'];
	date_default_timezone_set('Africa/Gaborone');
	$date = date("Y-m-d");
    $entry_date = date("Y-m-d H:i:s",time()-3600);
	
	$s5_cost = 0;
	$i=0;
		while ($i<$_SESSION['list_1']) {
		   $s5_cost = $s5_cost + ($_SESSION['meter_price_'.$i] * $_SESSION['length_'.$i]);
		   $i++;
	}
		
	$s7_cost = 0;
	$j=0;
		while ($j<$_SESSION['list_2']) {
		   $s7_cost = $s7_cost + ($_SESSION['meter_price2_'.$j] * $_SESSION['length2_'.$j]);
		   $j++;
	}
	
	$timber_cost = $s5_cost + $s7_cost;
		
	
	$table_name = $_SESSION['db_prefix']."_presets";
	$plate_value_query = "SELECT preset_value FROM $table_name WHERE preset_name = 'Plate area' ";
	$plate_value_result = mySQL_query($plate_value_query);
	$plate_charge = mysql_result($plate_value_result, 0, "preset_value");
	
	$table_name = $_SESSION['db_prefix']."_presets";
	$press_value_query = "SELECT preset_value FROM $table_name WHERE preset_name = 'Press points' ";
	$press_value_result = mySQL_query($press_value_query);
	$press_charge = mysql_result($press_value_result, 0, "preset_value");

	$table_name = $_SESSION['db_prefix']."_captured_jobs";
	$job_query = "UPDATE $table_name SET del_date = '".$_SESSION['deliver_date']."', roof_cover = '".$_SESSION['roof_cover']."', roof_pitch = '".$_SESSION['roof_pitch']."', max_overhang = '".$_SESSION['overhang']."', roof_area = '".$_SESSION['roof_area']."', floor_area = '".$_SESSION['floor_area']."', S5_timber = '".$_SESSION['S5']."', S7_timber = '".$_SESSION['S7']."',  timber_cost = '".$timber_cost."', captured_value = '".$captured_value."', cap_result = '".$result_value."', plate_area = '".$_SESSION['plate_area']."', plate_charge = '".$plate_charge."', press_points = '".$_SESSION['press_points']."', press_charge = '".$press_charge."', compiled = 'yes' WHERE job_no = '".$_SESSION['job_no']."' ";
	execsql($job_query);
	
	//$table_name = $_SESSION['db_prefix']."_presets";
//	$preset_query = "SELECT preset_value FROM $table_name WHERE preset_name = 'Press points' ";
//	$preset_result = mySQL_query($preset_query);
//	$preset = mysql_result($preset_result, 0, "preset_value");
//	
//	$table_name = $_SESSION['db_prefix']."_cap_press_points";
//	$press_query = "INSERT INTO $table_name (job_no, press_points, press_price ) VALUES ('".$_SESSION['job_no']."', '".$_SESSION['press_points']."', '".$preset."' ) ";
//	execsql($press_query);
	
//CAPTURING S5 TIMBER FOR TRUSSES 	
	$i=0;
	while ($i<$_SESSION['list_1']) {
	//if ($_SESSION['length_'.$i]!='') {
			$table_name = $_SESSION['db_prefix']."_captured_timber";
			$timber_query = "INSERT INTO $table_name (cap_job, cap_timber_group, cap_timber_name, cap_timber, cap_timber_price, cap_timber_length, auth_timber_length, prereserved, reserved, cap_depth, cap_width, cap_class ) VALUES ('".$_SESSION['job_no']."', 'Trusses', 'Trusses', '".$_SESSION['type_'.$i]."', '".$_SESSION['meter_price_'.$i]."', '".$_SESSION['length_'.$i]."', '0', '".$_SESSION['length_'.$i]."', '0', '".$_SESSION['timber_depth_'.$i]."', '".$_SESSION['timber_width_'.$i]."', '".$_SESSION['timber_class_'.$i]."') ";
			//echo '<br>'.$timber_query;
			execsql($timber_query);	
	//}
	$i++;
	}
	
	
//CAPTURING S7 TIMBER FOR TRUSSES	
	$j=0;
	while ($j<$_SESSION['list_2']) {
	//if ($_SESSION['length2_'.$j]!='') {
			$table_name = $_SESSION['db_prefix']."_captured_timber";
			$timber2_query = "INSERT INTO $table_name (cap_job, cap_timber_group, cap_timber_name, cap_timber, cap_timber_price, cap_timber_length, auth_timber_length, prereserved, reserved, cap_depth, cap_width, cap_class ) VALUES ('".$_SESSION['job_no']."', 'Trusses', 'Trusses', '".$_SESSION['type2_'.$j]."', '".$_SESSION['meter_price2_'.$j]."', '".$_SESSION['length2_'.$j]."', '0', '".$_SESSION['length2_'.$j]."', '0', '".$_SESSION['timber2_depth_'.$j]."', '".$_SESSION['timber2_width_'.$j]."', '".$_SESSION['timber2_class_'.$j]."') ";
			execsql($timber2_query);
				
	//}
	$j++;
	}


//CAPTURING MITEK PRODUCTS	
	$table_name7 = $_SESSION['db_prefix']."_store_items";
	$k=0;
	while ($k<$_SESSION['list_3']) {
	if ($_SESSION['quantity_'.$k]!='') {
			$table_name2 = $_SESSION['db_prefix']."_captured_mitek";
			$mitek_query = "INSERT INTO $table_name2 (cap_job, cap_job_type, cap_item_id, cap_quantity, cap_price) VALUES ('".$_SESSION['job_no']."', 'Project', '".$_SESSION['item_id_'.$k]."', '".$_SESSION['quantity_'.$k]."', '".$_SESSION['mitek_price'.$k]."' )";
			execsql($mitek_query);
			
		$reserved_query = "UPDATE $table_name7 SET prereserved = prereserved+".$_SESSION['quantity_'.$k]." WHERE store_item_id = '".$_SESSION['item_id_'.$k]."' ";
		execsql($reserved_query);
		
	}	
		
	$k++;
	}

//CAPTURING PLATES	
	$table_name = $_SESSION['db_prefix']."_captured_plates";
			$plates_query = "INSERT INTO $table_name (cap_job, cap_area ) VALUES ('".$_SESSION['job_no']."', '".$_SESSION['plate']."') ";
			execsql($plates_query);
	


//CAPTURING BRACING TIMBER	
	$j=0;
	while ($j<$_SESSION['list_5a']) {
		if ($_SESSION['bracing_qty_'.$j]!=0) {
			$table_name = $_SESSION['db_prefix']."_captured_timber";
			$timber_query = "INSERT INTO $table_name (cap_job, cap_timber_group,  cap_timber_name, cap_timber, cap_timber_price, cap_timber_length, auth_timber_length, prereserved, reserved, cap_depth, cap_width, cap_class ) VALUES ('".$_SESSION['job_no']."', 'Bracing', '".$_SESSION['bracing_name_'.$j]."', '".$_SESSION['bracing_type_'.$j]."', '".$_SESSION['bracing_meter_price_'.$j]."', '".$_SESSION['bracing_qty_'.$j]."' , '0', '".$_SESSION['bracing_qty_'.$j]."' , '0', '".$_SESSION['bracing_depth_'.$j]."', '".$_SESSION['bracing_width_'.$j]."', '".$_SESSION['bracing_class_'.$j]."') ";
			//echo '<br>'.$timber_query;
			execsql($timber_query);	
		}
			
	$j++;
	}
	

//CAPTURING SUNDRIES TIMBER
	$k=0;
	while ($k<$_SESSION['list_5b']) {
		if ($_SESSION['sundries_qty_'.$k]!=0) {
			$table_name = $_SESSION['db_prefix']."_captured_timber";
			$timber_query = "INSERT INTO $table_name (cap_job, cap_timber_group, cap_timber_name, cap_timber, cap_timber_price, cap_timber_length, auth_timber_length, prereserved, reserved, cap_depth, cap_width, cap_class ) VALUES ('".$_SESSION['job_no']."', 'Sundries', '".$_SESSION['sundries_name_'.$k]."', '".$_SESSION['sundries_type_'.$k]."', '".$_SESSION['sundries_meter_price_'.$k]."', '".$_SESSION['sundries_qty_'.$k]."', '0', '".$_SESSION['sundries_qty_'.$k]."', '0', '".$_SESSION['sundries_depth_'.$k]."', '".$_SESSION['sundries_width_'.$k]."', '".$_SESSION['sundries_class_'.$k]."') ";
			//echo '<br>'.$timber_query;
			execsql($timber_query);			
		}
	$k++;
	}
		
	$m=0;
	while ($m<$_SESSION['list_6']) {
		if ($_SESSION['additional_quantity_'.$m]!=0) {			
			$table_name3 = $_SESSION['db_prefix']."_captured_sundries";
			$add_1_query = "INSERT INTO $table_name3 (cap_job, cap_job_type, cap_item_id, cap_quantity, cap_price) VALUES ('".$_SESSION['job_no']."', 'Project', '".$_SESSION['additional_item_id_'.$m]."', '".$_SESSION['additional_quantity_'.$m]."', '".$_SESSION['sundries_price'.$m]."' )";
			execsql($add_1_query);
			
			$table_name = $_SESSION['db_prefix']."_store_items";
			$reserved2_query = "UPDATE $table_name SET prereserved = prereserved+".$_SESSION['additional_quantity_'.$m]." WHERE store_item_id = '".$_SESSION['additional_item_id_'.$m]."' ";
			execsql($reserved2_query);
			
			}
			$m++;
		}
			
			
			$table_name = $_SESSION['db_prefix']."_quotations";
			$quote_query = "SELECT seller, designer, company_contact, contact_phone, project_no, project_place, project_description FROM $table_name WHERE quote_no = '".$_SESSION['job_no']."' ";
			$quote_result = mySQL_query($quote_query);
			$b1=mysql_result($quote_result, 0, "seller");
			$b2=mysql_result($quote_result, 0, "designer");
			$b3=mysql_result($quote_result, 0, "company_contact");
			$b4=mysql_result($quote_result, 0, "contact_phone");
			$b5=mysql_result($quote_result, 0, "project_no")." ";
			$b6=mysql_result($quote_result, 0, "project_place");
			$b7=mysql_result($quote_result, 0, "project_description");
			
			
			
			$table_name = $_SESSION['db_prefix']."_production_jobs";
			$production_query = "INSERT INTO $table_name (job_no, cust_id, date, project_no, project_place, project_description, designer, roof_cover, roof_pitch, max_overhang, roof_area, floor_area, details, trusses, timber ) VALUES ('".$_SESSION['job_no']."', '".$_SESSION['cust_id']."', '".$date."', '".$b5."', '".$b6."', '".$b7."',  '".$b2."', '".$_SESSION['roof_cover']."', '".$_SESSION['roof_pitch']."', '".$_SESSION['overhang']."', '".$_SESSION['roof_area']."', '".$_SESSION['floor_area']."', 'no', 'no', 'no') ";
			execsql($production_query);
			
			$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 ('".$entry_date."' ,'".$_SESSION['username']."', '69', '".$_SESSION['job_no']."', '".$_SESSION['cust_id']."', '','' )";
			$trackingResult = mySQL_query($trackingQuery);
			

include('../include/captured_job_alert.php');
				 
	
	 $_SESSION['name']="";
	 
	 $_SESSION['roof_cover']="";
	 $_SESSION['roof_pitch']="";
	 $_SESSION['overhang']="";
	 $_SESSION['roof_area']="";
	 $_SESSION['floor_area']="";
	 
	 
	 $a=0;
	 while($a<$_SESSION['list_1']) {
		  $_SESSION['type_'.$a]="";
		  $_SESSION['timber_depth_'.$a]="";
		  $_SESSION['timber_width_'.$a]="";
		  $_SESSION['timber_class_'.$a]="";
		  $_SESSION['length_qty_'.$a]="";
		  $a++;
	 }
	 

	 $b=0;
	 while($b<$_SESSION['list_2']) {
		  $_SESSION['type2_'.$b]="";
		  $_SESSION['timber2_depth_'.$b]="";
		  $_SESSION['timber2_width_'.$b]="";
		  $_SESSION['timber2e_class_'.$b]="";
		  $_SESSION['length2_qty_'.$b]="";
		  $b++;
	 }
	 
	 
	 $i=0;
	 while ($i<$_SESSION['list_3']) {
		 $_SESSION['length_'.$i]="";
		 $_SESSION['length2_'.$i]="";
		 $_SESSION['type'.$i]="";
		 $_SESSION['type2'.$i]="";
		 $_SESSION['quantity_'.$i]="";
		 $i++;
	 }
	 
	 
	 $j=0;
	 while($j<$_SESSION['list_5a']) {
		  $_SESSION['bracing_name_'.$j]="";
		  $_SESSION['bracing_depth_'.$j]="";
		  $_SESSION['bracing_width_'.$j]="";
		  $_SESSION['bracing_class_'.$j]="";
		  $_SESSION['bracing_qty_'.$j]="";
		  $j++;
	 }
	 
	 $k=0;
	 while($k<$_SESSION['list_5b']) {
		  $_SESSION['sundries_name_'.$k]="";
		  $_SESSION['sundries_depth_'.$k]="";
		  $_SESSION['sundries_width_'.$k]="";
		  $_SESSION['sundries_class_'.$k]="";
		  $_SESSION['sundries_qty_'.$k]="";
		  $k++;
	 }

	 
		 
	 while ($m<$_SESSION['list_6']) {
		 $_SESSION['additional_item_id'.$m]="";
		 $_SESSION['additional_quantity_'.$m]="";
		 $m++;
	 } 
	 
	 $_SESSION['length_tot_1']=0;
	 $_SESSION['length_tot_2']=0;
	 $_SESSION['plate']=""; 
	
?>

<script language="javascript" type="text/javascript">
        
	      window.location="../production/captured_job_1.php?job_no=<?php echo $_SESSION['job_no'] ?>";
        
       </script>




