<?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."' ";
$_result = $con->query($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 = $con->query($check_query);
	$check_num = mysqli_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."' )";
	$alert_result = $con->query($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."' ";
		$alert_result = $con->query($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."' ";
		$delete_result = $con->query($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 = $con->query($check_query);
	$check_num = mysqli_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."' )";
	$alert_result = $con->query($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."' ";
		$alert_result = $con->query($alert_query);
	}
}
	else {
		$table_name = $_SESSION['db_prefix']."_alerts";
		$delete_query = "DELETE FROM $table_name WHERE alert_name = 'Warning' AND user_name = '".$user_name."' ";
		$delete_result = $con->query($delete_query);
	}
// end warning

// begin production margin
if ((isset($_GET['production_margin_alert_checkbox'])) OR (isset($_GET['production_margin_email_checkbox']))) {
	
	if (isset($_GET['production_margin_alert_checkbox'])) { $alert = 'yes';} else { $alert = 'no'; }
	if (isset($_GET['production_margin_email_checkbox'])) { $email = 'yes';} else { $email = 'no'; }
	$production_margin = $_GET['production_margin_text'];
	
	$table_name = $_SESSION['db_prefix']."_alerts";
	$check_query = "SELECT alert_name  FROM $table_name WHERE alert_name = 'Production margin' AND user_name = '".$user_name."' ";
	$check_result = $con->query($check_query);
	$check_num = mysqli_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 ('Production margin', '".$production_margin."', '".$alert."', '".$email."', '". $user_name."' )";
	$alert_result = $con->query($alert_query);
	
	}
	else {
		$table_name = $_SESSION['db_prefix']."_alerts";
		$alert_query = "UPDATE $table_name SET condition_1 = '".$production_margin."', send_alert = '".$alert."', send_email = '".$email."' WHERE alert_name = 'Production margin' AND user_name = '".$user_name."' ";
		$alert_result = $con->query($alert_query);
	}
}
	else {
		$table_name = $_SESSION['db_prefix']."_alerts";
		$delete_query = "DELETE FROM $table_name WHERE alert_name = 'Production margin' AND user_name = '".$user_name."' ";
		$delete_result = $con->query($delete_query);
	}
// end production margin


// begin packing margin
if ((isset($_GET['packing_margin_alert_checkbox'])) OR (isset($_GET['packing_margin_email_checkbox']))) {
	
	if (isset($_GET['packing_margin_alert_checkbox'])) { $alert = 'yes';} else { $alert = 'no'; }
	if (isset($_GET['packing_margin_email_checkbox'])) { $email = 'yes';} else { $email = 'no'; }
	$packing_margin = $_GET['packing_margin_text'];
	
	$table_name = $_SESSION['db_prefix']."_alerts";
	$check_query = "SELECT alert_name  FROM $table_name WHERE alert_name = 'Packing margin' AND user_name = '".$user_name."' ";
	$check_result = $con->query($check_query);
	$check_num = mysqli_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 ('packing margin', '".$packing_margin."', '".$alert."', '".$email."', '". $user_name."' )";
	$alert_result = $con->query($alert_query);
	
	}
	else {
		$table_name = $_SESSION['db_prefix']."_alerts";
		$alert_query = "UPDATE $table_name SET condition_1 = '".$packing_margin."', send_alert = '".$alert."', send_email = '".$email."' WHERE alert_name = 'Packing margin' AND user_name = '".$user_name."' ";
		$alert_result = $con->query($alert_query);
	}
}
	else {
		$table_name = $_SESSION['db_prefix']."_alerts";
		$delete_query = "DELETE FROM $table_name WHERE alert_name = 'Packing margin' AND user_name = '".$user_name."' ";
		$delete_result = $con->query($delete_query);
	}
// end packing margin

// begin order_quantity
if ((isset($_GET['order_alert_checkbox'])) OR (isset($_GET['order_email_checkbox']))) {
	
	if (isset($_GET['order_alert_checkbox'])) { $alert = 'yes';} else { $alert = 'no'; }
	if (isset($_GET['order_email_checkbox'])) { $email = 'yes';} else { $email = 'no'; }
	$quantity = $_GET['quantity_textbox'];
	
	$table_name = $_SESSION['db_prefix']."_alerts";
	$check_query = "SELECT alert_name  FROM $table_name WHERE alert_name = 'Order quantity' AND user_name = '".$user_name."' ";
	$check_result = $con->query($check_query);
	$check_num = mysqli_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 ('Order quantity', '".$quantity."', '".$alert."', '".$email."', '". $user_name."' )";
	$alert_result = $con->query($alert_query);
	
	}
	else {
		$table_name = $_SESSION['db_prefix']."_alerts";
		$alert_query = "UPDATE $table_name SET condition_1 = '".$quantity."', send_alert = '".$alert."', send_email = '".$email."' WHERE alert_name = 'Order quantity' AND user_name = '".$user_name."' ";
		$alert_result = $con->query($alert_query);
	}
}
	else {
		$table_name = $_SESSION['db_prefix']."_alerts";
		$delete_query = "DELETE FROM $table_name WHERE alert_name = 'Order quantity' AND user_name = '".$user_name."' ";
		$delete_result = $con->query($delete_query);
	}
// end order quantity


// begin min stock direct materials
if ((isset($_GET['min_direct_materials_alert_checkbox'])) OR (isset($_GET['min_direct_materials_email_checkbox']))) {
	
	if (isset($_GET['min_direct_materials_alert_checkbox'])) { $alert = 'yes';} else { $alert = 'no'; }
	if (isset($_GET['min_direct_materials_email_checkbox'])) { $email = 'yes';} else { $email = 'no'; }
	
	
	$table_name = $_SESSION['db_prefix']."_alerts";
	$check_query = "SELECT alert_name  FROM $table_name WHERE alert_name = 'Min direct materials' AND user_name = '".$user_name."' ";
	$check_result = $con->query($check_query);
	$check_num = mysqli_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 ('Min direct materials', '', '".$alert."', '".$email."', '". $user_name."' )";
	$alert_result = $con->query($alert_query);
	
	}
	else {
		$table_name = $_SESSION['db_prefix']."_alerts";
		$alert_query = "UPDATE $table_name SET send_alert = '".$alert."', send_email = '".$email."' WHERE alert_name = 'Min direct materials' AND user_name = '".$user_name."' ";
		$alert_result = $con->query($alert_query);
	}
}
	else {
		$table_name = $_SESSION['db_prefix']."_alerts";
		$delete_query = "DELETE FROM $table_name WHERE alert_name = 'Min direct materials' AND user_name = '".$user_name."' ";
		$delete_result = $con->query($delete_query);
	}
// end min stock direct materials

// begin min stock sales products
if ((isset($_GET['min_sales_products_alert_checkbox'])) OR (isset($_GET['min_sales_products_email_checkbox']))) {
	
	if (isset($_GET['min_sales_products_alert_checkbox'])) { $alert = 'yes';} else { $alert = 'no'; }
	if (isset($_GET['min_sales_products_email_checkbox'])) { $email = 'yes';} else { $email = 'no'; }
	
	
	$table_name = $_SESSION['db_prefix']."_alerts";
	$check_query = "SELECT alert_name  FROM $table_name WHERE alert_name = 'Min sales product' AND user_name = '".$user_name."' ";
	$check_result = $con->query($check_query);
	$check_num = mysqli_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 ('Min sales product', '', '".$alert."', '".$email."', '". $user_name."' )";
	$alert_result = $con->query($alert_query);
	
	}
	else {
		$table_name = $_SESSION['db_prefix']."_alerts";
		$alert_query = "UPDATE $table_name SET send_alert = '".$alert."', send_email = '".$email."' WHERE alert_name = 'Min sales product' AND user_name = '".$user_name."' ";
		$alert_result = $con->query($alert_query);
	}
}
	else {
		$table_name = $_SESSION['db_prefix']."_alerts";
		$delete_query = "DELETE FROM $table_name WHERE alert_name = 'Min sales product' AND user_name = '".$user_name."' ";
		$delete_result = $con->query($delete_query);
	}
// end min stock sales products

// begin min stock
if ((isset($_GET['min_stock_alert_checkbox'])) OR (isset($_GET['min_stock_email_checkbox']))) {
	
	if (isset($_GET['min_stock_alert_checkbox'])) { $alert = 'yes';} else { $alert = 'no'; }
	if (isset($_GET['min_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 = 'Min stock' AND user_name = '".$user_name."' ";
	$check_result = $con->query($check_query);
	$check_num = mysqli_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 ('Min stock', '', '".$alert."', '".$email."', '". $user_name."' )";
	$alert_result = $con->query($alert_query);
	
	}
	else {
		$table_name = $_SESSION['db_prefix']."_alerts";
		$alert_query = "UPDATE $table_name SET send_alert = '".$alert."', send_email = '".$email."' WHERE alert_name = 'Min stock' AND user_name = '".$user_name."' ";
		$alert_result = $con->query($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."' ";
		$delete_result = $con->query($delete_query);
	}
// end min stock

// 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 = $con->query($check_query);
	$check_num = mysqli_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."' )";
	$alert_result = $con->query($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."' ";
		$alert_result = $con->query($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."' ";
		$delete_result = $con->query($delete_query);
	}
// end stock count




	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		 
	   }
	   
?>
