User schedules report done

This commit is contained in:
Spudnut2000 2025-06-26 20:20:36 -05:00
parent 0ecd299273
commit 1764749ecc
4 changed files with 168 additions and 29 deletions

View File

@ -40,6 +40,62 @@ public static class DatabaseHelper
return null; return null;
} }
public static List<User> RetrieveUsers()
{
using MySqlConnection connection = new MySqlConnection(AppSettings.GetSetting("ConnectionStrings", "DefaultConnection"));
try
{
connection.Open();
}
catch (MySqlException e)
{
MessageBox.Show($"Database connection error: {e.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return new List<User>();
}
List<User> users = new List<User>();
using MySqlCommand command = new MySqlCommand("SELECT * FROM client_schedule.user", connection);
using MySqlDataReader reader = command.ExecuteReader();
while (reader.Read())
{
var user = new User(reader.GetInt32("userId"), reader.GetString("userName"));
users.Add(user);
}
return users;
}
public static User? RetrieveUser(int id)
{
using MySqlConnection connection = new MySqlConnection(AppSettings.GetSetting("ConnectionStrings", "DefaultConnection"));
try
{
connection.Open();
}
catch (MySqlException e)
{
MessageBox.Show($"Database connection error: {e.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return null;
}
string query = "SELECT * FROM client_schedule.user WHERE userId = @userId";
using MySqlCommand command = new MySqlCommand(query, connection);
command.Parameters.AddWithValue("@userId", id);
using MySqlDataReader reader = command.ExecuteReader();
User? user = null;
while (reader.Read())
{
user = new User(reader.GetInt32("userId"), reader.GetString("userName"));
}
return user ?? null;
}
#region Customers #region Customers
public static List<Customer> RetrieveCustomers() public static List<Customer> RetrieveCustomers()

View File

@ -10,4 +10,9 @@ public class User
UserId = id; UserId = id;
Username = username; Username = username;
} }
public override string ToString()
{
return Username;
}
} }

View File

@ -36,14 +36,15 @@ partial class ReportsForm
refreshButton1 = new System.Windows.Forms.Button(); refreshButton1 = new System.Windows.Forms.Button();
apptByMonthDataGrid = new System.Windows.Forms.DataGridView(); apptByMonthDataGrid = new System.Windows.Forms.DataGridView();
userSchedulesTabPage = new System.Windows.Forms.TabPage(); userSchedulesTabPage = new System.Windows.Forms.TabPage();
customersByCountryTabPage = new System.Windows.Forms.TabPage(); userScheduleDataGrid = new System.Windows.Forms.DataGridView();
scheduleDataGrid = new System.Windows.Forms.DataGridView(); userComboBox = new System.Windows.Forms.ComboBox();
refreshButton2 = new System.Windows.Forms.Button(); refreshButton2 = new System.Windows.Forms.Button();
customersByCountryTabPage = new System.Windows.Forms.TabPage();
tabControl1.SuspendLayout(); tabControl1.SuspendLayout();
apptByMothTabPage.SuspendLayout(); apptByMothTabPage.SuspendLayout();
((System.ComponentModel.ISupportInitialize)apptByMonthDataGrid).BeginInit(); ((System.ComponentModel.ISupportInitialize)apptByMonthDataGrid).BeginInit();
userSchedulesTabPage.SuspendLayout(); userSchedulesTabPage.SuspendLayout();
((System.ComponentModel.ISupportInitialize)scheduleDataGrid).BeginInit(); ((System.ComponentModel.ISupportInitialize)userScheduleDataGrid).BeginInit();
SuspendLayout(); SuspendLayout();
// //
// tabControl1 // tabControl1
@ -88,8 +89,9 @@ partial class ReportsForm
// //
// userSchedulesTabPage // userSchedulesTabPage
// //
userSchedulesTabPage.Controls.Add(userScheduleDataGrid);
userSchedulesTabPage.Controls.Add(userComboBox);
userSchedulesTabPage.Controls.Add(refreshButton2); userSchedulesTabPage.Controls.Add(refreshButton2);
userSchedulesTabPage.Controls.Add(scheduleDataGrid);
userSchedulesTabPage.Location = new System.Drawing.Point(4, 24); userSchedulesTabPage.Location = new System.Drawing.Point(4, 24);
userSchedulesTabPage.Name = "userSchedulesTabPage"; userSchedulesTabPage.Name = "userSchedulesTabPage";
userSchedulesTabPage.Padding = new System.Windows.Forms.Padding(3); userSchedulesTabPage.Padding = new System.Windows.Forms.Padding(3);
@ -98,6 +100,30 @@ partial class ReportsForm
userSchedulesTabPage.Text = "User Schedules"; userSchedulesTabPage.Text = "User Schedules";
userSchedulesTabPage.UseVisualStyleBackColor = true; userSchedulesTabPage.UseVisualStyleBackColor = true;
// //
// userScheduleDataGrid
//
userScheduleDataGrid.Location = new System.Drawing.Point(6, 35);
userScheduleDataGrid.Name = "userScheduleDataGrid";
userScheduleDataGrid.Size = new System.Drawing.Size(756, 357);
userScheduleDataGrid.TabIndex = 5;
//
// userComboBox
//
userComboBox.FormattingEnabled = true;
userComboBox.Location = new System.Drawing.Point(6, 6);
userComboBox.Name = "userComboBox";
userComboBox.Size = new System.Drawing.Size(660, 23);
userComboBox.TabIndex = 4;
//
// refreshButton2
//
refreshButton2.Location = new System.Drawing.Point(672, 6);
refreshButton2.Name = "refreshButton2";
refreshButton2.Size = new System.Drawing.Size(90, 23);
refreshButton2.TabIndex = 3;
refreshButton2.Text = "Refresh";
refreshButton2.UseVisualStyleBackColor = true;
//
// customersByCountryTabPage // customersByCountryTabPage
// //
customersByCountryTabPage.Location = new System.Drawing.Point(4, 24); customersByCountryTabPage.Location = new System.Drawing.Point(4, 24);
@ -107,23 +133,6 @@ partial class ReportsForm
customersByCountryTabPage.Text = "Customers By Country"; customersByCountryTabPage.Text = "Customers By Country";
customersByCountryTabPage.UseVisualStyleBackColor = true; customersByCountryTabPage.UseVisualStyleBackColor = true;
// //
// scheduleDataGrid
//
scheduleDataGrid.Location = new System.Drawing.Point(6, 6);
scheduleDataGrid.Name = "scheduleDataGrid";
scheduleDataGrid.ReadOnly = true;
scheduleDataGrid.Size = new System.Drawing.Size(756, 357);
scheduleDataGrid.TabIndex = 2;
//
// refreshButton2
//
refreshButton2.Location = new System.Drawing.Point(672, 369);
refreshButton2.Name = "refreshButton2";
refreshButton2.Size = new System.Drawing.Size(90, 23);
refreshButton2.TabIndex = 3;
refreshButton2.Text = "Refresh";
refreshButton2.UseVisualStyleBackColor = true;
//
// ReportsForm // ReportsForm
// //
AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F);
@ -135,11 +144,14 @@ partial class ReportsForm
apptByMothTabPage.ResumeLayout(false); apptByMothTabPage.ResumeLayout(false);
((System.ComponentModel.ISupportInitialize)apptByMonthDataGrid).EndInit(); ((System.ComponentModel.ISupportInitialize)apptByMonthDataGrid).EndInit();
userSchedulesTabPage.ResumeLayout(false); userSchedulesTabPage.ResumeLayout(false);
((System.ComponentModel.ISupportInitialize)scheduleDataGrid).EndInit(); ((System.ComponentModel.ISupportInitialize)userScheduleDataGrid).EndInit();
ResumeLayout(false); ResumeLayout(false);
} }
private System.Windows.Forms.DataGridView scheduleDataGrid; private System.Windows.Forms.DataGridView userScheduleDataGrid;
private System.Windows.Forms.ComboBox userComboBox;
private System.Windows.Forms.Button refreshButton2; private System.Windows.Forms.Button refreshButton2;
private System.Windows.Forms.Button refreshButton1; private System.Windows.Forms.Button refreshButton1;

View File

@ -1,4 +1,5 @@
using C969Project.Data; using C969Project.Data;
using C969Project.Data.Models;
namespace C969Project; namespace C969Project;
@ -21,6 +22,15 @@ public partial class ReportsForm : Form
ApptTypeByMonth_GenerateReport(); ApptTypeByMonth_GenerateReport();
refreshButton1.Click += (sender, args) => { ApptTypeByMonth_GenerateReport(); }; refreshButton1.Click += (sender, args) => { ApptTypeByMonth_GenerateReport(); };
UserSchedule_UpdateUsers();
userComboBox.SelectedIndexChanged += (sender, args) => { UserSchedule_GenerateReport(); };
userComboBox.SelectedIndex = -1; // Reset selection
refreshButton2.Click += (sender, args) =>
{
UserSchedule_UpdateUsers();
UserSchedule_GenerateReport();
};
} }
#region Apptointments types by Month #region Apptointments types by Month
@ -42,11 +52,8 @@ public partial class ReportsForm : Form
apptByMonthDataGrid.DataSource = appointmentTypes; apptByMonthDataGrid.DataSource = appointmentTypes;
} }
#endregion
private class AppointmentTypeByMonth
class AppointmentTypeByMonth
{ {
public string Month { get; set; } public string Month { get; set; }
public string AppointmentType { get; set; } public string AppointmentType { get; set; }
@ -58,10 +65,69 @@ public partial class ReportsForm : Form
AppointmentType = appointmentType; AppointmentType = appointmentType;
Count = count; Count = count;
} }
}
#endregion
#region User Schedule
private void UserSchedule_GenerateReport()
{
userScheduleDataGrid.DataSource = null;
public override string ToString() User? selectedUser = (User?)userComboBox.SelectedItem;
List<UserSchedule> userSchedules = new();
var appointments = DatabaseHelper.RetrieveAppointments();
var usersAppointments = appointments
.FindAll(a => a.UserId == selectedUser.UserId)
.ToList();
if (usersAppointments.Count == 0)
{ {
return $"{Month} - {AppointmentType}: {Count}"; MessageBox.Show("No appointments found for the selected user.", "No Appointments", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
foreach (var appointment in usersAppointments)
{
userSchedules.Add(new UserSchedule(
selectedUser.Username,
appointment.Title,
appointment.AppointmentType,
appointment.Start.ToLocalTime().ToString("MM/dd/yyyy hh:mm ") + TimeZoneInfo.Local.Id,
appointment.End.ToLocalTime().ToString("MM/dd/yyyy hh:mm ") + TimeZoneInfo.Local.Id
));
}
userScheduleDataGrid.DataSource = userSchedules;
}
private void UserSchedule_UpdateUsers()
{
userComboBox.DataSource = null;
userComboBox.DataSource = DatabaseHelper.RetrieveUsers();
userComboBox.SelectedIndex = -1;
}
private class UserSchedule
{
public string Username { get; set; }
public string AppointmentName { get; set; }
public string AppointmentType { get; set; }
public string StartDate { get; set; }
public string EndDate { get; set; }
public UserSchedule(string username, string appointmentName, string appointmentType, string startDate, string endDate)
{
Username = username;
AppointmentName = appointmentType;
AppointmentType = appointmentType;
StartDate = startDate;
EndDate = endDate;
} }
} }
#endregion
} }