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

	$d = date("d");
	$m = date("m");
	$y = date("Y");
	
	$user_id = $_SESSION['username'];
	date_default_timezone_set('Africa/Khartoum');
	$date = date("Y-m-d");
    $entry_date = date("Y-m-d H:i:s",time()-3600); 

	//MITEK PRODUCTS: SET AUTHORISED AND RESERVED
			
	$k=0;
	while ($k<$_SESSION['list_3']) {
		
	$table_name2 = $_SESSION['db_prefix']."_store_items";	
	$price_query = "SELECT price FROM $table_name2 WHERE store_item_id = '".$_SESSION['item_id'.$k]."' ";
	$price_result = mySQL_query($price_query);
	$price = mysql_result($price_result, 0, "price");	
		
	if ($_SESSION['mitek_line'.$k]!='0') {
	$table_name = $_SESSION['db_prefix']."_captured_mitek";			
	$mitek_query = "UPDATE $table_name SET auth_quantity = '".$_SESSION['quantity_'.$k]."', authorised='yes', delivered='0', del_price = '".$price."' WHERE cap_mitek_id = '".$_SESSION['mitek_line'.$k]."' ";
	execsql($mitek_query);
	}
	else {
		$table_name = $_SESSION['db_prefix']."_captured_mitek";	
		$mitek_query = "INSERT INTO $table_name (cap_job, cap_item_id, cap_quantity, auth_quantity, authorised, delivered, del_price ) VALUES ('".$_SESSION['job_no']."', '".$_SESSION['item_id'.$k]."', '0', '".$_SESSION['quantity_'.$k]."', 'yes', '0', '".$price."' ) ";
	
		execsql($mitek_query);
		
	$table_name = $_SESSION['db_prefix']."_captured_mitek";		
	$id_query = "SELECT cap_mitek_id FROM $table_name ORDER BY cap_mitek_id DESC LIMIT 1  ";
	$id_result = mySQL_query($id_query);
	$_SESSION['mitek_line'.$k] = mysql_result($id_result, 0, "cap_mitek_id");	
		
	}
	
	$table_name = $_SESSION['db_prefix']."_captured_mitek";	
	$item_query = "SELECT cap_item_id, cap_quantity, auth_quantity FROM $table_name WHERE cap_mitek_id = '".$_SESSION['mitek_line'.$k]."' ";
	$item_result = mySQL_query($item_query);
	$item_id = mysql_result($item_result, 0, "cap_item_id");
	$cap_quantity = mysql_result($item_result, 0, "cap_quantity");
	$auth_quantity = mysql_result($item_result, 0, "auth_quantity");
	
	$table_name2 = $_SESSION['db_prefix']."_store_items";	
	$reserve_query = "UPDATE $table_name2 SET reserved=reserved+".$auth_quantity."  WHERE store_item_id = '".$item_id."' ";
	execsql($reserve_query);
		
	$k++;
	}
	
	//TIMBER: SET AUTHORISED AND RESERVED FOR TIMBERS	
	
	//BRACING
	$table_name10 = $_SESSION['db_prefix']."_captured_timber";
	$bracing_sub_query = "SELECT * FROM $table_name10 WHERE  cap_timber_group = 'Bracing' AND cap_job = '".$_SESSION['job_no']."' ORDER BY cap_timber_name ";
	//echo '<br>'.$bracing_sub_query;
	//echo $bracing_sub_query;
	$bracing_sub_result = mySQL_query($bracing_sub_query);
	$bracing_sub_num = mysql_num_rows($bracing_sub_result);
	$j=0;
	while ($j<$bracing_sub_num) {
		$bracing_name = mysql_result($bracing_sub_result, $j, "cap_timber_name");
		
		$cap_depth = mysql_result($bracing_sub_result, $j, "cap_depth");
		$cap_width = mysql_result($bracing_sub_result, $j, "cap_width");
		$cap_class = mysql_result($bracing_sub_result, $j, "cap_class");
		
		$timber_size = $cap_depth.'x'.$cap_width.' ('.$cap_class.')';
		
		$table_name = $_SESSION['db_prefix']."_timber_meter_price";
		$meter_price_query = "SELECT price FROM $table_name WHERE timber = '".$timber_size."'  ";
		$meter_price_result = mySQL_query($meter_price_query);
		$bracing_meter_price = mysql_result($meter_price_result, 0, "price");
		
		//if ($bracing_name==$_SESSION['bracing_name2'.$j]) {
		  $table_name10 = $_SESSION['db_prefix']."_captured_timber";	
	      $make_zero_query = "UPDATE $table_name10 SET auth_timber_length = '".$_SESSION['bracing_qty_'.$j]."', prereserved = '0', reserved ='".$_SESSION['bracing_qty_'.$j]."', delivered='0', auth_timber_price = '".$bracing_meter_price."' WHERE cap_job= '".$_SESSION['job_no']."' AND cap_timber_name = '".$bracing_name."' ";
    	//echo '<br>'.$make_zero_query;
		execsql($make_zero_query);
		//echo '<br>'.$make_zero_query;
 		//}
		$j++;
	}
	

	//SUNDRIES
	$table_name10 = $_SESSION['db_prefix']."_captured_timber";	
	$sundries_sub_query = "SELECT * FROM $table_name10 WHERE  cap_timber_group = 'Sundries' AND cap_job = '".$_SESSION['job_no']."'ORDER BY cap_timber_name ";
	//echo $sundries_sub_query;
	$sundries_sub_result = mySQL_query($sundries_sub_query);
	$sundries_sub_num = mysql_num_rows($sundries_sub_result);
	$j=0;
	while ($j<$sundries_sub_num) {
		$sundries_name = mysql_result($sundries_sub_result, $j, "cap_timber_name");
		
		$cap_depth = mysql_result($sundries_sub_result, $j, "cap_depth");
		$cap_width = mysql_result($sundries_sub_result, $j, "cap_width");
		$cap_class = mysql_result($sundries_sub_result, $j, "cap_class");
		
		$timber_size = $cap_depth.'x'.$cap_width.' ('.$cap_class.')';
		
		$table_name = $_SESSION['db_prefix']."_timber_meter_price";
		$meter_price_query = "SELECT price FROM $table_name WHERE timber = '".$timber_size."'  ";
		$meter_price_result = mySQL_query($meter_price_query);
		$sundries_meter_price = mysql_result($meter_price_result, 0, "price");
		
		
		//if ($sundries_name==$_SESSION['sundries_name2'.$j]) {
		  $table_name10 = $_SESSION['db_prefix']."_captured_timber";
	      $make_zero_query = "UPDATE $table_name10 SET auth_timber_length = '".$_SESSION['sundries_qty_'.$j]."', prereserved = '0', reserved ='".$_SESSION['sundries_qty_'.$j]."', delivered='0', auth_timber_price = '".$sundries_meter_price."' WHERE cap_job= '".$_SESSION['job_no']."' AND cap_timber_name = '".$sundries_name."' ";
    	//echo '<br>'.$make_zero_query;
		execsql($make_zero_query);
		//echo '<br>'.$make_zero_query;
 		//}
		$j++;
	}
		
	//ADDITIONAL SUNDRIES: SET AUTHORISED AND RESERVED
	$table_name7 = $_SESSION['db_prefix']."_captured_sundries";	
	$n=0;
	while ($n<$_SESSION['list_6']) {
	$table_name2 = $_SESSION['db_prefix']."_store_items";		
	$price_query = "SELECT price FROM $table_name2 WHERE store_item_id = '".$_SESSION['additional_item_id'.$n]."' ";
	$price_result = mySQL_query($price_query);
	$price = mysql_result($price_result, 0, "price");	
		
	if ($_SESSION['additional_line'.$n]!='0') {	
	        
			$additional_query = "UPDATE $table_name7 SET auth_quantity = '".$_SESSION['additional_quantity_'.$n]."', authorised='yes', delivered='0', del_price = '".$price."'  WHERE cap_sundries_id = '".$_SESSION['additional_line'.$n]."' ";
	execsql($additional_query);
		}
	else {
		$additional_query = "INSERT INTO $table_name7 (cap_job, cap_item_id, cap_quantity, auth_quantity, authorised, delivered, del_price ) VALUES ('".$_SESSION['job_no']."', '".$_SESSION['additional_item_id'.$n]."', '0', '".$_SESSION['additional_quantity_'.$n]."', 'yes', '0', '".$price."') ";
		execsql($additional_query);
		
	$id_query = "SELECT cap_sundries_id FROM $table_name7 ORDER BY cap_sundries_id DESC LIMIT 1  ";
	$id_result = mySQL_query($id_query);
	$_SESSION['additional_line'.$n] = mysql_result($id_result, 0, "cap_sundries_id");	
		
	}
	
			
		$item_query = "SELECT cap_item_id, cap_quantity, auth_quantity FROM $table_name7 WHERE cap_sundries_id = '".$_SESSION['additional_line'.$n]."' ";
		$item_result = mySQL_query($item_query);
		$item_id2 = mysql_result($item_result, 0, "cap_item_id");
		$cap_quantity2 = mysql_result($item_result, 0, "cap_quantity");
		$auth_quantity2 = mysql_result($item_result, 0, "auth_quantity");
	
		//UPDATE RESERVED IN STORE ITEMS TABLE
		//$reserve2_query = "UPDATE $table_name8 SET  reserved=reserved+".$auth_quantity2."  WHERE store_item_id = '".$item_id2."' ";
//		execsql($reserve2_query);
		//echo '<br>'.$reserve2_query;
	
		
		
		$n++;
	}
	
	$table_name20 = $_SESSION['db_prefix']."_authorised_timber_temp";
	$table_name21 = $_SESSION['db_prefix']."_authorised_timber";
	
	$copy_query="INSERT INTO $table_name21 (auth_timber_group, auth_timber_name, store_item_id, reserved, delivered, job_no)
                    SELECT auth_timber_group, auth_timber_name, store_item_id, reserved, delivered, job_no FROM $table_name20 WHERE job_no = '".$_SESSION['job_no']."' AND user =  '".$_SESSION['username']."' ";
	execsql($copy_query);
	

	//echo $copy_query;				
	
	$table_name = $_SESSION['db_prefix']."_captured_jobs";
	$authorisation_query = "UPDATE $table_name SET authorisation = 'yes' WHERE job_no = '".$_SESSION['job_no']."' ";
	execsql($authorisation_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']."', '72', '".$_SESSION['job_no']."' ,'','','' )";
$trackingResult = mySQL_query($trackingQuery); 
	
	$job_no=$_SESSION['job_no'];
	
	 $_SESSION['job_no']="";
	 $_SESSION['screen']=0;
	 $_SESSION['start2']=0;
	 $_SESSION['start5']=0;
	 $_SESSION['cat']='';
	 $_SESSION['name']='';
	
	$i=0;
	 while ($i<25) {
		 $_SESSION['length_'.$i]="";
		 $_SESSION['length2_'.$i]="";
		 $_SESSION['type'.$i]="";
		 $_SESSION['type2'.$i]="";
		 $_SESSION['quantity_'.$i]="";
		 $_SESSION['mitek_line'.$i]="";
		 $i++;
	 }
	
	$n=10;
	while ($n<25) {
	$m=10;
	while ($m<30) {
		$_SESSION['check'.$n.$m] = 'no';
		$_SESSION['check2_'.$n.$m] = 'no';
		$_SESSION['bracing_qty'.$n.$m]='';
		$_SESSION['sundries_qty'.$n.$m]='';
		$_SESSION['item_id'.$n.$m]='';
		$_SESSION['item2_id'.$n.$m]='';
		$_SESSION['bracing_qty'.$n.$m]='';
		$_SESSION['sundries_qty'.$n.$m]='';
		//echo '<br>bracing'.$n.$m;
		//echo '<br>sundries'.$n.$m;		
		$m++;
	}
	$n++;
	}

	 $m=0;
	 while ($m<25) {
		 $_SESSION['additional_item_id'.$m]="";
		 $_SESSION['additional_quantity_'.$m]="";
		 $_SESSION['additional_line'.$m]="";
		 $m++;
	 } 
	 
	$table_name = $_SESSION['db_prefix']."_authorised_timber_temp";
	$delete_query = "DELETE FROM $table_name WHERE user =  '".$_SESSION['username']."' "; 
	execsql($delete_query);	
	
	$table_name = $_SESSION['db_prefix']."_captured_mitek_temp";
	$delete_query = "DELETE FROM $table_name WHERE cap_job = '".$_SESSION['job_no']."'"; 
	execsql($delete_query);	
	
	$table_name = $_SESSION['db_prefix']."_captured_sundries_temp";
	$delete_query = "DELETE FROM $table_name WHERE cap_job = '".$_SESSION['job_no']."' " ; 
	execsql($delete_query);	 
	
	
	$_SESSION['list_5a']=0;
	$_SESSION['list_5b']=0;
	$_SESSION['list_6a']=0;
	$_SESSION['list_6b']=0;
	 
	
?>

<script language="javascript" type="text/javascript">
        
	    window.location="../production/authorisation.php?job_no=<?php echo $job_no ?>";
        
       </script>




