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

date_default_timezone_set('Africa/Khartoum');
$entryDate = date("Y-m-d H:i:s",time()-3600); 

if (isset($_POST['mitek_button']) )
	   {
		 $sub_category= 'Mitek';
	   }
		 
if (isset($_POST['additional_button']) )
	  {
		$sub_category='Sundries';
	  }
	  
if (isset($_POST['bracing_button']) )
	  {
		$sub_category='Bracing';
	  }
	  
if (isset($_POST['sundries2_button']) )
	  {
		$sub_category='Sundries2';
	  }


$job_no = $_POST['job_no'];
$mitek_num = $_POST['mitek_num'];
$bracing_num = $_POST['bracing_num'];
$sundries_num = $_POST['sundries_num'];
$additional_num = $_POST['additional_num'];
$complete = $_POST['complete_alert'];
$complete_mitek = $_POST['complete_mitek'];
$complete_bracing = $_POST['complete_bracing'];
$complete_sundries = $_POST['complete_sundries'];
$complete_additional = $_POST['complete_additional'];

$saved_reserved=0;

$i=0;
while ($i<$mitek_num) {
	
	$cap_item_id = $_POST['cap_item_id'.$i];
	$edit_value = $_POST['mitek_edit'.$i];

	if ($_SESSION['edit_mitek_line'.$i]!='0') {
					
			$table_name = $_SESSION['db_prefix']."_store_items";
			$price_query = "SELECT  price FROM $table_name WHERE store_item_id = '".$cap_item_id."' ";
			$price_result = mySQL_query($price_query);
			$del_price = mysql_result($price_result, 0, "price");
					
			if ($complete_additional==0) {
			  $table_name = $_SESSION['db_prefix']."_captured_mitek";
			}
			else {
				$table_name = $_SESSION['db_prefix']."_captured_mitek_done";
			}
			
			$saved_query = "SELECT  auth_quantity  FROM $table_name WHERE cap_job = '".$job_no."' AND cap_item_id = '".$cap_item_id."' ";
			$saved_result = mySQL_query($saved_query);
			$saved_auth_quantity = mysql_result($saved_result, 0, "auth_quantity");
			
			$update_query = "UPDATE $table_name SET auth_quantity = '".$edit_value."' WHERE cap_job = '".$job_no."' AND cap_item_id = '".$cap_item_id."' ";
			execsql($update_query);
			
			if ($saved_auth_quantity!=$edit_value) {
			$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 ('".$entryDate."' ,'".$_SESSION['username']."', '65', '".$job_no."' ,'".$cap_item_id."','".$saved_auth_quantity."','".$edit_value."' )";
			$trackingResult = mySQL_query($trackingQuery);
			}
	
	}
	else {
		$table_name = $_SESSION['db_prefix']."_captured_mitek_edit_temp";
		$check_query = "SELECT auth_quantity FROM $table_name WHERE cap_item_id = '".$cap_item_id."' AND cap_job = '".$job_no."' ";
			
	    $table_name = $_SESSION['db_prefix']."_store_items";
		$price_query = "SELECT  price FROM $table_name WHERE store_item_id = '".$cap_item_id."' ";
		$price_result = mySQL_query($price_query);
		$del_price = mysql_result($price_result, 0, "price");
		
		if ($complete_additional==0) {
			  $table_name = $_SESSION['db_prefix']."_captured_mitek";
			}
			else {
				$table_name = $_SESSION['db_prefix']."_captured_mitek_done";
			}
		//$additional_query = "UPDATE $table_name SET auth_quantity = '".$edit_value."', del_price = '".$del_price."'  WHERE cap_item_id = '".$cap_item_id."' AND cap_job = '".$job_no."' ";
		$additional_query = "INSERT INTO $table_name (cap_job, cap_job_type, cap_item_id, auth_quantity, authorised, del_price) VALUES ('".$job_no."', 'Project', '".$cap_item_id."', '".$edit_value."', 'yes', '".$del_price."') ";
		execsql($additional_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 ('".$entryDate."' ,'".$_SESSION['username']."', '66', '".$job_no."' ,'".$cap_item_id."','','".$edit_value."' )";
			$trackingResult = mySQL_query($trackingQuery);
	}
	$i++;
}

$j=0;
while ($j<$bracing_num) {
	$bracing_sub_num = $_POST['bracing_sub_num'.$j];
	$bracing_name = $_POST['bracing_name'.$j];
	$k=0;
	
	$table_name = $_SESSION['db_prefix']."_captured_timber";
	$zero_query = "UPDATE $table_name SET auth_timber_length = '0', reserved = '0' WHERE cap_timber_name = '".$bracing_name."' AND cap_job = '".$job_no."' ";
	execsql($zero_query);
	$tot_mlength=0;
	while ($k<$bracing_sub_num) {
	$bracing_store_item_id = $_POST['bracing_store_item_id'.$j.$k];
	$edit_value = $_POST['bracing_edit'.$j.$k];
	
	$table_name = $_SESSION['db_prefix']."_store_items";
	$length_query = "SELECT length, width, depth, class FROM $table_name WHERE store_item_id = '".$bracing_store_item_id."'  ";
	$length_result = mySQL_query($length_query);
	$length = mysql_result($length_result, 0, "length");
	$width = mysql_result($length_result, 0, "width");
	$depth = mysql_result($length_result, 0, "depth");
	$class = mysql_result($length_result, 0, "class");
	$cap_timber = $depth.'x'.$width.' ('.$class.')';
	
	$mlength = ($length * $edit_value)/1000;
	
	$table_name = $_SESSION['db_prefix']."_timber_meter_price";
	$mprice_query = "SELECT price FROM $table_name WHERE timber = '".$cap_timber."' ";
	$mprice_result = mySQL_query($mprice_query);
	$mprice = mysql_result($mprice_result, 0, "price");
	
	if ($_SESSION['edit_bracing_line'.$j.$k]=='0') {
		  		
		  if ($complete_bracing==0) {
			  $table_name = $_SESSION['db_prefix']."_authorised_timber";
			  $table_name2 = $_SESSION['db_prefix']."_captured_timber";
			}
			else {
				$table_name = $_SESSION['db_prefix']."_authorised_timber_done";
				$table_name2 = $_SESSION['db_prefix']."_captured_timber_done";
			}
			
						
			$bracing_query = "INSERT INTO $table_name (auth_timber_group, auth_timber_name, store_item_id, reserved, delivered, job_no ) VALUES ('bracing', '".$bracing_name."', '".$bracing_store_item_id."', '".$edit_value."', '0', '".$job_no."') ";
			execsql($bracing_query);
			
			$bracing_length_query = "INSERT INTO $table_name2 (cap_timber_group, cap_timber_name, cap_timber, auth_timber_length, reserved, cap_job, auth_timber_price ) VALUES ('bracing', '".$bracing_name."', '".$cap_timber."', '".$mlength."', '".$mlength."', '".$job_no."', '".$mprice."' ) ";
			execsql($bracing_length_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 ('".$entryDate."' ,'".$_SESSION['username']."', '68', '".$job_no."' ,'".$bracing_name."','".$saved_reserved."','".$edit_value."' )";
		  $trackingResult = mySQL_query($trackingQuery);
			}
	else {
		if ($complete_bracing==0) {
			  $table_name = $_SESSION['db_prefix']."_authorised_timber";
			}
			else {
				$table_name = $_SESSION['db_prefix']."_authorised_timber_done";
			}
			
		$check_query = "SELECT reserved FROM $table_name WHERE auth_timber_name = '".$bracing_name."' AND job_no = '".$job_no."' AND store_item_id = '".$bracing_store_item_id."' ";
		execsql($check_query);
		$check_result = mySQL_query($check_query);
		$recorded_value = mysql_result($check_result, 0, "reserved");
		if ($recorded_value!=$edit_value) {
	       	$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 ('".$entryDate."' ,'".$_SESSION['username']."', '67', '".$job_no."' ,'".$bracing_name."','".$recorded_value."','".$edit_value."' )";
			$trackingResult = mySQL_query($trackingQuery);
	}
	
	$new_reserved = $edit_value *($length/1000);
		
	if ($complete_bracing==0) {
			  $table_name = $_SESSION['db_prefix']."_captured_timber";
			}
			else {
				$table_name = $_SESSION['db_prefix']."_captured_timber_done";
			}
	$reserved_query = "SELECT reserved FROM $table_name WHERE cap_timber_name = '".$bracing_name."' AND cap_job = '".$job_no."' ";
	$reserved_result = mySQL_query($reserved_query);
	$reserved_num = mysql_num_rows($reserved_result);
	if ($reserved_num>0) {
	      $reserved = mysql_result($reserved_result, 0, "reserved");
	}
	else {
		$reserved=0;
	}
		
	$reserved = $reserved + $new_reserved;
		
	$tot_mlength = $tot_mlength + $mlength;
		
		
	if ($complete_bracing==0) {
			  $table_name = $_SESSION['db_prefix']."_authorised_timber";
			  $table_name2 = $_SESSION['db_prefix']."_captured_timber";
			  
			}
			else {
				$table_name = $_SESSION['db_prefix']."_authorised_timber_done";
				$table_name2 = $_SESSION['db_prefix']."_captured_timber_done";
				
			}
	$bracing_query = "UPDATE $table_name SET reserved = '".$edit_value."'  WHERE auth_timber_name = '".$bracing_name."' AND job_no = '".$job_no."' AND store_item_id = '".$bracing_store_item_id."' ";
	execsql($bracing_query);
	
	$bracing_length_query = "UPDATE $table_name2 SET auth_timber_length = '".$tot_mlength."' WHERE cap_timber_name = '".$bracing_name."' AND cap_job = '".$job_no."' ";
	execsql($bracing_length_query);
	}
	
	
	$k++;
	}
	$j++;
}

$j=0;
while ($j<$sundries_num) {
	$sundries_sub_num = $_POST['sundries_sub_num'.$j];
	$sundries_name = $_POST['sundries_name'.$j];
	$k=0;
	
	$table_name = $_SESSION['db_prefix']."_captured_timber";
	$zero_query = "UPDATE $table_name SET auth_timber_length = '0', reserved = '0' WHERE cap_timber_name = '".$sundries_name."' AND cap_job = '".$job_no."' ";
	execsql($zero_query);
	$tot_mlength=0;
	while ($k<$sundries_sub_num) {
	$sundries_store_item_id = $_POST['sundries_store_item_id'.$j.$k];
	$edit_value = $_POST['sundries_edit'.$j.$k];
	
		
	$table_name = $_SESSION['db_prefix']."_store_items";
	$length_query = "SELECT length, width, depth, class FROM $table_name WHERE store_item_id = '".$sundries_store_item_id."'  ";
	$length_result = mySQL_query($length_query);
	$length = mysql_result($length_result, 0, "length");
	$width = mysql_result($length_result, 0, "width");
	$depth = mysql_result($length_result, 0, "depth");
	$class = mysql_result($length_result, 0, "class");
	$cap_timber = $depth.'x'.$width.' ('.$class.')';
	
	$mlength = ($length * $edit_value)/1000;
	
	$table_name = $_SESSION['db_prefix']."_timber_meter_price";
	$mprice_query = "SELECT price FROM $table_name WHERE timber = '".$cap_timber."' ";
	$mprice_result = mySQL_query($mprice_query);
	$mprice = mysql_result($mprice_result, 0, "price");
	
	if ($_SESSION['edit_sundries_line'.$j.$k]=='0') {
		$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 ('".$entryDate."' ,'".$_SESSION['username']."', '68', '".$job_no."' ,'".$sundries_name."','0','".$edit_value."' )";
			$trackingResult = mySQL_query($trackingQuery);
		
		  if ($complete_sundries==0) {
			  $table_name = $_SESSION['db_prefix']."_authorised_timber";
			  $table_name2 = $_SESSION['db_prefix']."_captured_timber";
			}
			else {
				$table_name = $_SESSION['db_prefix']."_authorised_timber_done";
				$table_name2 = $_SESSION['db_prefix']."_captured_timber_done";
			}
			
			$sundries_query = "INSERT INTO $table_name (auth_timber_group, auth_timber_name, store_item_id, reserved, delivered, job_no ) VALUES ('sundries', '".$sundries_name."', '".$sundries_store_item_id."', '".$edit_value."', '0', '".$job_no."') ";
			execsql($sundries_query);
			
			$sundries_length_query = "INSERT INTO $table_name2 (cap_timber_group, cap_timber_name, cap_timber, auth_timber_length, reserved, cap_job, auth_timber_price ) VALUES ('sundries', '".$sundries_name."', '".$cap_timber."', '".$mlength."', '".$mlength."', '".$job_no."', '".$mprice."' ) ";
			execsql($sundries_length_query);
			
	}
	else {
	if ($complete_sundries==0) {
			  $table_name = $_SESSION['db_prefix']."_authorised_timber";
			}
			else {
				$table_name = $_SESSION['db_prefix']."_authorised_timber_done";
			}
		$check_query = "SELECT reserved FROM $table_name WHERE auth_timber_name = '".$sundries_name."' AND job_no = '".$job_no."' AND store_item_id = '".$sundries_store_item_id."' ";
		execsql($check_query);
		$check_result = mySQL_query($check_query);
		$recorded_value = mysql_result($check_result, 0, "reserved");
		if ($recorded_value!=$edit_value) {
	       	$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 ('".$entryDate."' ,'".$_SESSION['username']."', '67', '".$job_no."' ,'".$sundries_name."','".$recorded_value."','".$edit_value."' )";
			$trackingResult = mySQL_query($trackingQuery);
	}
	
	$new_reserved = $edit_value *($length/1000);
	
	if ($complete_sundries==0) {
			  $table_name = $_SESSION['db_prefix']."_captured_timber";
			}
			else {
				$table_name = $_SESSION['db_prefix']."_captured_timber_done";
			}
	$reserved_query = "SELECT reserved FROM $table_name WHERE cap_timber_name = '".$sundries_name."' AND cap_job = '".$job_no."' ";
	$reserved_result = mySQL_query($reserved_query);
	$reserved_num = mysql_num_rows($reserved_result);
	if ($reserved_num>0) {
	      $reserved = mysql_result($reserved_result, 0, "reserved");
	}
	else {
		$reserved=0;
	}
		
	$reserved = $reserved + $new_reserved;
	
	$tot_mlength = $tot_mlength + $mlength;
		
	if ($complete_sundries==0) {
			  $table_name = $_SESSION['db_prefix']."_authorised_timber";
			  $table_name2 = $_SESSION['db_prefix']."_captured_timber";
			}
			else {
				$table_name = $_SESSION['db_prefix']."_authorised_timber_done";
				$table_name2 = $_SESSION['db_prefix']."_captured_timber_done";
			}		
			
	$sundries_query = "UPDATE $table_name SET reserved = '".$edit_value."' WHERE auth_timber_name = '".$sundries_name."' AND job_no = '".$job_no."' AND store_item_id = '".$sundries_store_item_id."' ";
	execsql($sundries_query);
	
	$sundries_length_query = "UPDATE $table_name2 SET auth_timber_length = '".$tot_mlength."' WHERE cap_timber_name = '".$sundries_name."' AND cap_job = '".$job_no."' ";
	execsql($sundries_length_query);
	}
	$k++;
	}
	$j++;
}

$m=0;

while ($m<$additional_num) {
	$cap_item_id = $_POST['additional_item'.$m];
	$edit_value = $_POST['additional_edit'.$m];

	if ($_SESSION['edit_additional_line'.$m]!='0') {
					
			$table_name = $_SESSION['db_prefix']."_store_items";
			$price_query = "SELECT  price FROM $table_name WHERE store_item_id = '".$cap_item_id."' ";
			$price_result = mySQL_query($price_query);
			$del_price = mysql_result($price_result, 0, "price");
					
			if ($complete_additional==0) {
			  $table_name = $_SESSION['db_prefix']."_captured_sundries";
			}
			else {
				$table_name = $_SESSION['db_prefix']."_captured_sundries_done";
			}
			
			$saved_query = "SELECT  auth_quantity  FROM $table_name WHERE cap_job = '".$job_no."' AND cap_item_id = '".$cap_item_id."' ";
			$saved_result = mySQL_query($saved_query);
			$saved_auth_quantity = mysql_result($saved_result, 0, "auth_quantity");
			
			$update_query = "UPDATE $table_name SET auth_quantity = '".$edit_value."' WHERE cap_job = '".$job_no."' AND cap_item_id = '".$cap_item_id."' ";
			execsql($update_query);
			
			if ($saved_auth_quantity!=$edit_value) {
			$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 ('".$entryDate."' ,'".$_SESSION['username']."', '65', '".$job_no."' ,'".$cap_item_id."','".$saved_auth_quantity."','".$edit_value."' )";
			$trackingResult = mySQL_query($trackingQuery);
			}
	
	}
	else {
		$table_name = $_SESSION['db_prefix']."_captured_sundries_edit_temp";
		$check_query = "SELECT auth_quantity FROM $table_name WHERE cap_item_id = '".$cap_item_id."' AND cap_job = '".$job_no."' ";
			
	    $table_name = $_SESSION['db_prefix']."_store_items";
		$price_query = "SELECT  price FROM $table_name WHERE store_item_id = '".$cap_item_id."' ";
		$price_result = mySQL_query($price_query);
		$del_price = mysql_result($price_result, 0, "price");
		
		if ($complete_additional==0) {
			  $table_name = $_SESSION['db_prefix']."_captured_sundries";
			}
			else {
				$table_name = $_SESSION['db_prefix']."_captured_sundries_done";
			}
		//$additional_query = "UPDATE $table_name SET auth_quantity = '".$edit_value."', del_price = '".$del_price."'  WHERE cap_item_id = '".$cap_item_id."' AND cap_job = '".$job_no."' ";
		$additional_query = "INSERT INTO $table_name (cap_job, cap_job_type, cap_item_id, auth_quantity, authorised, del_price) VALUES ('".$job_no."', 'Project', '".$cap_item_id."', '".$edit_value."', 'yes', '".$del_price."') ";
		execsql($additional_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 ('".$entryDate."' ,'".$_SESSION['username']."', '66', '".$job_no."' ,'".$cap_item_id."','".$recorded_value."','".$edit_value."' )";
			$trackingResult = mySQL_query($trackingQuery);
	}
	$m++;
}

$_SESSION['edit_start']=0;

if (isset($_POST['save_close_button']) )
	   { 
	    $table_name = $_SESSION['db_prefix']."_captured_mitek_edit_temp";
		$delete_query = "DELETE FROM $table_name WHERE cap_job='".$job_no."' AND user = '".$_SESSION['username']."' "; 
		execsql($delete_query);
		
		$table_name = $_SESSION['db_prefix']."_authorised_timber_edit_temp";
		$delete_query = "DELETE FROM $table_name WHERE job_no='".$job_no."' AND user = '".$_SESSION['username']."' ";
		execsql($delete_query);

		$table_name = $_SESSION['db_prefix']."_captured_sundries_edit_temp";
		$delete_query = "DELETE FROM $table_name WHERE cap_job='".$job_no."' AND user = '".$_SESSION['username']."' "; 
		execsql($delete_query);
	   
	   
	   ?>
       <script>
	     window.location="../production/production_main.php";
		</script>
        <?php		 
	   }
	          
       if (isset($_POST['save_button']) )
	   {  ?>
       <script>
	     window.location="authorisation_edit.php?job_no=<?php echo $job_no?>&screen=0&edit=0&sub_category=; needToConfirm = false;"
	   </script>
        <?php		 
	   }
	   ?>

