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

$email = $_GET['email_text'];
$access_id = $_GET['access_id'];
$user_name = $_GET['user_name'];

$table_name = $_SESSION['db_prefix']."_users";
$email_query = "UPDATE $table_name SET email = '".$email."' WHERE access_id = '".$access_id."' ";
execsql($email_query);

// begin late
if ((isset($_GET['late_alert_checkbox'])) OR (isset($_GET['late_email_checkbox']))) {
	
	if (isset($_GET['late_alert_checkbox'])) { $alert = 'yes';} else { $alert = 'no'; }
	if (isset($_GET['late_email_checkbox'])) { $email = 'yes';} else { $email = 'no'; }
	$late_days = $_GET['late_textbox'];
	
	$table_name = $_SESSION['db_prefix']."_alerts";
	$check_query = "SELECT alert_name  FROM $table_name WHERE alert_name = 'Late coming' AND user_name = '".$user_name."' ";
	$check_result = mySQL_query($check_query);
	$check_num = mysql_num_rows($check_result);
	
	if($check_num==0) {
	
	$table_name = $_SESSION['db_prefix']."_alerts";
	$alert_query = "INSERT INTO $table_name (alert_name, condition_1, send_alert, send_email, user_name ) VALUES ('Late coming', '".$late_days."', '".$alert."', '".$email."', '". $user_name."' )";
	execsql($alert_query);
	
	}
	else {
		$table_name = $_SESSION['db_prefix']."_alerts";
		$alert_query = "UPDATE $table_name SET condition_1 = '".$late_days."', send_alert = '".$alert."', send_email = '".$email."' WHERE alert_name = 'Late coming' AND user_name = '".$user_name."' ";
		execsql($alert_query);
	}
}
	else {
		$table_name = $_SESSION['db_prefix']."_alerts";
		$delete_query = "DELETE FROM $table_name WHERE alert_name = 'Late coming' AND user_name = '".$user_name."' ";
		execsql($delete_query);
	}
// end late

// begin warning
if ((isset($_GET['warning_alert_checkbox'])) OR (isset($_GET['warning_email_checkbox']))) {
	
	if (isset($_GET['warning_alert_checkbox'])) { $alert = 'yes';} else { $alert = 'no'; }
	if (isset($_GET['warning_email_checkbox'])) { $email = 'yes';} else { $email = 'no'; }
	
	
	$table_name = $_SESSION['db_prefix']."_alerts";
	$check_query = "SELECT alert_name  FROM $table_name WHERE alert_name = 'Warning' AND user_name = '".$user_name."' ";
	$check_result = mySQL_query($check_query);
	$check_num = mysql_num_rows($check_result);
	
	if($check_num==0) {
	
	$table_name = $_SESSION['db_prefix']."_alerts";
	$alert_query = "INSERT INTO $table_name (alert_name, condition_1, send_alert, send_email, user_name ) VALUES ('Warning', '', '".$alert."', '".$email."', '". $user_name."' )";
	execsql($alert_query);
	
	}
	else {
		$table_name = $_SESSION['db_prefix']."_alerts";
		$alert_query = "UPDATE $table_name SET send_alert = '".$alert."', send_email = '".$email."' WHERE alert_name = 'Warning' AND user_name = '".$user_name."' ";
		execsql($alert_query);
	}
}
	else {
		$table_name = $_SESSION['db_prefix']."_alerts";
		$delete_query = "DELETE FROM $table_name WHERE alert_name = 'Warning' AND user_name = '".$user_name."' ";
		execsql($delete_query);
	}
// end warning


//sales
if ((isset($_GET['quotation_alert_checkbox'])) OR (isset($_GET['quotation_email_checkbox']))) {
	
	if (isset($_GET['quotation_alert_checkbox'])) { $alert = 'yes';} else { $alert = 'no'; }
	if (isset($_GET['quotation_email_checkbox'])) { $email = 'yes';} else { $email = 'no'; }
	$condition_1 = $_GET['quotation_select'];
	
	$table_name = $_SESSION['db_prefix']."_alerts";
	$check_query = "SELECT alert_name  FROM $table_name WHERE alert_name = 'quotation' AND user_name = '".$user_name."' ";
	$check_result = mySQL_query($check_query);
	$check_num = mysql_num_rows($check_result);
	
	if($check_num==0) {
			
	$table_name = $_SESSION['db_prefix']."_alerts";
	$alert_query = "INSERT INTO $table_name (alert_name, condition_1, send_alert, send_email, user_name ) VALUES ('quotation', '".$condition_1."', '".$alert."', '".$email."', '". $user_name."' )";
	execsql($alert_query);
	
	}
	else {
		$table_name = $_SESSION['db_prefix']."_alerts";
		$alert_query = "UPDATE $table_name SET condition_1 = '".$condition_1."', send_alert = '".$alert."', send_email = '".$email."' WHERE alert_name = 'quotation' AND user_name = '".$user_name."' ";
		execsql($alert_query);
	}
}
	else {
		$table_name = $_SESSION['db_prefix']."_alerts";
		$delete_query = "DELETE FROM $table_name WHERE alert_name = 'quotation' AND user_name = '".$user_name."' ";
		execsql($delete_query);
	}


if ((isset($_GET['receive_order_alert_checkbox'])) OR (isset($_GET['receive_order_email_checkbox']))) {
	
	if (isset($_GET['receive_order_alert_checkbox'])) { $alert = 'yes';} else { $alert = 'no'; }
	if (isset($_GET['receive_order_email_checkbox'])) { $email = 'yes';} else { $email = 'no'; }
	$condition_1 = $_GET['receive_order_select'];
	
	$table_name = $_SESSION['db_prefix']."_alerts";
	$check_query = "SELECT alert_name  FROM $table_name WHERE alert_name = 'receive order' AND user_name = '".$user_name."' ";
	$check_result = mySQL_query($check_query);
	$check_num = mysql_num_rows($check_result);
	
	if($check_num==0) {
			
	$table_name = $_SESSION['db_prefix']."_alerts";
	$alert_query = "INSERT INTO $table_name (alert_name, condition_1, send_alert, send_email, user_name ) VALUES ('receive order', '".$condition_1."', '".$alert."', '".$email."', '". $user_name."' )";
	execsql($alert_query);
	
	}
	else {
		$table_name = $_SESSION['db_prefix']."_alerts";
		$alert_query = "UPDATE $table_name SET condition_1 = '".$condition_1."', send_alert = '".$alert."', send_email = '".$email."' WHERE alert_name = 'receive order' AND user_name = '".$user_name."' ";
		execsql($alert_query);
	}
}
	else {
		$table_name = $_SESSION['db_prefix']."_alerts";
		$delete_query = "DELETE FROM $table_name WHERE alert_name = 'receive order' AND user_name = '".$user_name."' ";
		execsql($delete_query);
	}
	
	

if ((isset($_GET['order_value_alert_checkbox'])) OR (isset($_GET['order_value_email_checkbox']))) {
	
	if (isset($_GET['order_value_alert_checkbox'])) { $alert = 'yes';} else { $alert = 'no'; }
	if (isset($_GET['order_value_email_checkbox'])) { $email = 'yes';} else { $email = 'no'; }
	
	
	$table_name = $_SESSION['db_prefix']."_alerts";
	$check_query = "SELECT alert_name  FROM $table_name WHERE alert_name = 'order value' AND user_name = '".$user_name."' ";
	$check_result = mySQL_query($check_query);
	$check_num = mysql_num_rows($check_result);
	
	if($check_num==0) {
			
	$table_name = $_SESSION['db_prefix']."_alerts";
	$alert_query = "INSERT INTO $table_name (alert_name, send_alert, send_email, user_name ) VALUES ('order value',  '".$alert."', '".$email."', '". $user_name."' )";
	execsql($alert_query);
	
	}
	else {
		$table_name = $_SESSION['db_prefix']."_alerts";
		$alert_query = "UPDATE $table_name SET send_alert = '".$alert."', send_email = '".$email."' WHERE alert_name = 'order value' AND user_name = '".$user_name."' ";
		execsql($alert_query);
	}
}
	else {
		$table_name = $_SESSION['db_prefix']."_alerts";
		$delete_query = "DELETE FROM $table_name WHERE alert_name = 'order value' AND user_name = '".$user_name."' ";
		execsql($delete_query);
	}


//production
if ((isset($_GET['captured_quote_alert_checkbox'])) OR (isset($_GET['captured_quote_email_checkbox']))) {
	if (isset($_GET['captured_quote_alert_checkbox'])) { $alert = 'yes';} else { $alert = 'no'; }
	if (isset($_GET['captured_quote_email_checkbox'])) { $email = 'yes';} else { $email = 'no'; }
	$condition_1 = $_GET['captured_quote_select'];
	
	$table_name = $_SESSION['db_prefix']."_alerts";
	$check_query = "SELECT alert_name  FROM $table_name WHERE alert_name = 'captured_quote' AND user_name = '".$user_name."' ";
	$check_result = mySQL_query($check_query);
	$check_num = mysql_num_rows($check_result);
	
	if($check_num==0) {
			
	$table_name = $_SESSION['db_prefix']."_alerts";
	$alert_query = "INSERT INTO $table_name (alert_name, condition_1, send_alert, send_email, user_name ) VALUES ('captured_quote', '".$condition_1."', '".$alert."', '".$email."', '". $user_name."' )";
	execsql($alert_query);
	
	}
	else {
		$table_name = $_SESSION['db_prefix']."_alerts";
		$alert_query = "UPDATE $table_name SET condition_1 = '".$condition_1."', send_alert = '".$alert."', send_email = '".$email."' WHERE alert_name = 'captured_quote' AND user_name = '".$user_name."' ";
		execsql($alert_query);
	}
}
	else {
		$table_name = $_SESSION['db_prefix']."_alerts";
		$delete_query = "DELETE FROM $table_name WHERE alert_name = 'captured_quote' AND user_name = '".$user_name."' ";
		execsql($delete_query);
	}

if ((isset($_GET['cutting_timber_alert_checkbox'])) OR (isset($_GET['cutting_timber_email_checkbox']))) {
	if (isset($_GET['cutting_timber_alert_checkbox'])) { $alert = 'yes';} else { $alert = 'no'; }
	if (isset($_GET['cutting_timber_email_checkbox'])) { $email = 'yes';} else { $email = 'no'; }
	$condition_1 = $_GET['cutting_timber_select'];
	
	$table_name = $_SESSION['db_prefix']."_alerts";
	$check_query = "SELECT alert_name  FROM $table_name WHERE alert_name = 'cutting_timber' AND user_name = '".$user_name."' ";
	$check_result = mySQL_query($check_query);
	$check_num = mysql_num_rows($check_result);
	
	if($check_num==0) {
			
	$table_name = $_SESSION['db_prefix']."_alerts";
	$alert_query = "INSERT INTO $table_name (alert_name, condition_1, send_alert, send_email, user_name ) VALUES ('cutting_timber', '".$condition_1."', '".$alert."', '".$email."', '". $user_name."' )";
	execsql($alert_query);
	
	}
	else {
		$table_name = $_SESSION['db_prefix']."_alerts";
		$alert_query = "UPDATE $table_name SET condition_1 = '".$condition_1."', send_alert = '".$alert."', send_email = '".$email."' WHERE alert_name = 'cutting_timber' AND user_name = '".$user_name."' ";
		execsql($alert_query);
	}
}
	else {
		$table_name = $_SESSION['db_prefix']."_alerts";
		$delete_query = "DELETE FROM $table_name WHERE alert_name = 'cutting_timber' AND user_name = '".$user_name."' ";
		execsql($delete_query);
	}


if ((isset($_GET['cutting_plate_alert_checkbox'])) OR (isset($_GET['cutting_plate_email_checkbox']))) {
	
	if (isset($_GET['cutting_plate_alert_checkbox'])) { $alert = 'yes';} else { $alert = 'no'; }
	if (isset($_GET['cutting_plate_email_checkbox'])) { $email = 'yes';} else { $email = 'no'; }
	$condition_1 = $_GET['cutting_plate_select'];
	
	$table_name = $_SESSION['db_prefix']."_alerts";
	$check_query = "SELECT alert_name  FROM $table_name WHERE alert_name = 'cutting_plate' AND user_name = '".$user_name."' ";
	$check_result = mySQL_query($check_query);
	$check_num = mysql_num_rows($check_result);
	
	if($check_num==0) {
			
	$table_name = $_SESSION['db_prefix']."_alerts";
	$alert_query = "INSERT INTO $table_name (alert_name, condition_1, send_alert, send_email, user_name ) VALUES ('cutting_plate', '".$condition_1."', '".$alert."', '".$email."', '". $user_name."' )";
	execsql($alert_query);
	
	}
	else {
		$table_name = $_SESSION['db_prefix']."_alerts";
		$alert_query = "UPDATE $table_name SET condition_1 = '".$condition_1."', send_alert = '".$alert."', send_email = '".$email."' WHERE alert_name = 'cutting_plate' AND user_name = '".$user_name."' ";
		execsql($alert_query);
	}
}
	else {
		$table_name = $_SESSION['db_prefix']."_alerts";
		$delete_query = "DELETE FROM $table_name WHERE alert_name = 'cutting_plate' AND user_name = '".$user_name."' ";
		execsql($delete_query);
	}
	
	
// begin stock count
if ((isset($_GET['stock_count_alert_checkbox'])) OR (isset($_GET['stock_count_email_checkbox']))) {
	
	if (isset($_GET['stock_count_alert_checkbox'])) { $alert = 'yes';} else { $alert = 'no'; }
	if (isset($_GET['stock_count_email_checkbox'])) { $email = 'yes';} else { $email = 'no'; }
	
	
	$table_name = $_SESSION['db_prefix']."_alerts";
	$check_query = "SELECT alert_name  FROM $table_name WHERE alert_name = 'Stock count' AND user_name = '".$user_name."' ";
	$check_result = mySQL_query($check_query);
	$check_num = mysql_num_rows($check_result);
	
	if($check_num==0) {
	
	$table_name = $_SESSION['db_prefix']."_alerts";
	$alert_query = "INSERT INTO $table_name (alert_name, condition_1, send_alert, send_email, user_name ) VALUES ('Stock count', '', '".$alert."', '".$email."', '". $user_name."' )";
	execsql($alert_query);
	
	}
	else {
		$table_name = $_SESSION['db_prefix']."_alerts";
		$alert_query = "UPDATE $table_name SET send_alert = '".$alert."', send_email = '".$email."' WHERE alert_name = 'Stock count' AND user_name = '".$user_name."' ";
		execsql($alert_query);
	}
}
	else {
		$table_name = $_SESSION['db_prefix']."_alerts";
		$delete_query = "DELETE FROM $table_name WHERE alert_name = 'Stock count' AND user_name = '".$user_name."' ";
		execsql($delete_query);
	}
// end stock count

//if ((isset($_GET['order_stock_alert_checkbox'])) OR (isset($_GET['order_stock_email_checkbox']))) {
//	
//	if (isset($_GET['order_stock_alert_checkbox'])) { $alert = 'yes';} else { $alert = 'no'; }
//	if (isset($_GET['order_stock_email_checkbox'])) { $email = 'yes';} else { $email = 'no'; }
//		
//	$table_name = $_SESSION['db_prefix']."_alerts";
//	$check_query = "SELECT alert_name  FROM $table_name WHERE alert_name = 'order stock' AND user_name = '".$user_name."' ";
//	$check_result = mySQL_query($check_query);
//	$check_num = mysql_num_rows($check_result);
//	
//	if($check_num==0) {
//			
//	$table_name = $_SESSION['db_prefix']."_alerts";
//	$alert_query = "INSERT INTO $table_name (alert_name, send_alert, send_email, user_name ) VALUES ('order stock',  '".$alert."', '".$email."', '". $user_name."' )";
//	execsql($alert_query);
//	
//	}
//	else {
//		$table_name = $_SESSION['db_prefix']."_alerts";
//		$alert_query = "UPDATE $table_name SET send_alert = '".$alert."', send_email = '".$email."' WHERE alert_name = 'order stock' AND user_name = '".$user_name."' ";
//		execsql($alert_query);
//	}
//}
//	else {
//		$table_name = $_SESSION['db_prefix']."_alerts";
//		$delete_query = "DELETE FROM $table_name WHERE alert_name = 'order stock' AND user_name = '".$user_name."' ";
//		execsql($delete_query);
//	}

if (isset($_GET['min_stock_alert_checkbox'])) { $alert = 'yes';} else { $alert = 'no'; }
if (isset($_GET['min_stock_email_checkbox'])) { $email = 'yes';} else { $email = 'no'; }
	

if  ( (isset($_GET['min_stock_alert_checkbox'])) OR (isset($_GET['min_stock_email_checkbox'])) ){
	if (isset($_GET['timber_checkbox'])) { $condition_1 = 'Timber';} else { $condition_1 = 'no'; }
	if (isset($_GET['mitek_checkbox'])) { $condition_2 = 'Mitek';} else { $condition_2 = 'no'; }
	if (isset($_GET['sundries_checkbox'])) { $condition_3 = 'Sundries';} else { $condition_3 = 'no'; }
	if (isset($_GET['other_checkbox'])) { $condition_4 = 'Other';} else { $condition_4 = 'no'; }
	$condition_5 = '';
	
	$table_name = $_SESSION['db_prefix']."_alerts";
	$check_query = "SELECT alert_name  FROM $table_name WHERE alert_name = 'min stock' AND user_name = '".$user_name."' ";
	$check_result = mySQL_query($check_query);
	$check_num = mysql_num_rows($check_result);
	if($check_num==0) {
			
	$table_name = $_SESSION['db_prefix']."_alerts";
	$alert_query = "INSERT INTO $table_name (alert_name, condition_1, condition_2, condition_3, condition_4, condition_5, send_alert, send_email, user_name ) VALUES ('min stock', '".$condition_1."', '".$condition_2."', '".$condition_3."', '".$condition_4."', '".$condition_5."', '".$alert."', '".$email."', '". $user_name."' )";
	execsql($alert_query);
	}
	else {
		$table_name = $_SESSION['db_prefix']."_alerts";
		$alert_query = "UPDATE $table_name SET condition_1 = '".$condition_1."', condition_2 = '".$condition_2."', condition_3 = '".$condition_3."', condition_4 = '".$condition_4."', condition_5 = '".$condition_5."', send_alert = '".$alert."', send_email = '".$email."' WHERE alert_name = 'min stock' AND user_name = '".$user_name."' ";
		execsql($alert_query);
		}
}
	else {
		$table_name = $_SESSION['db_prefix']."_alerts";
		$delete_query = "DELETE FROM $table_name WHERE alert_name = 'min stock' AND user_name = '".$user_name."' ";
		execsql($delete_query);
		}
	



	if (isset($_GET['save_close_button']) )
	   { ?>
       <script>
	  window.location="user_list.php";
		</script>
        <?php		 
	   }
	          
       if (isset($_GET['save_button']) )
	   {  ?>
       <script>
	     window.location="alerts.php?set=1&user_select=<?php echo $access_id?>";
	   </script>
        <?php		 
	   }
	   
?>
