c969-project/C969Project/Forms/ReportsForm.cs
2025-06-26 21:10:19 -05:00

172 lines
5.2 KiB
C#

using C969Project.Data;
using C969Project.Data.Models;
namespace C969Project;
public partial class ReportsForm : Form
{
private string[] _months =
[
"January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"
];
private string[] _appointmentTypes =
[
"Scrum", "Presentation", "Other"
];
public ReportsForm()
{
InitializeComponent();
ApptTypeByMonth_GenerateReport();
refreshButton1.Click += (sender, args) => { ApptTypeByMonth_GenerateReport(); };
UserSchedule_UpdateUsers();
userComboBox.SelectedIndexChanged += (sender, args) => { UserSchedule_GenerateReport(); };
userComboBox.SelectedIndex = -1;
refreshButton2.Click += (sender, args) =>
{
UserSchedule_UpdateUsers();
UserSchedule_GenerateReport();
};
CustomersByCity_GenerateReport();
refreshButton3.Click += (sender, args) => { CustomersByCity_GenerateReport(); };
}
#region Apptointments types by Month
private void ApptTypeByMonth_GenerateReport()
{
apptByMonthDataGrid.DataSource = null;
List<AppointmentTypeByMonth> appointmentTypes = new();
var appointments = DatabaseHelper.RetrieveAppointments();
foreach (var month in _months)
{
foreach (var type in _appointmentTypes)
{
int count = appointments.Count(a => a.Start.ToString("MMMM") == month && a.AppointmentType == type);
appointmentTypes.Add(new AppointmentTypeByMonth(month, type, count));
}
}
apptByMonthDataGrid.DataSource = appointmentTypes;
}
private class AppointmentTypeByMonth
{
public string Month { get; set; }
public string AppointmentType { get; set; }
public int Count { get; set; }
public AppointmentTypeByMonth(string month, string appointmentType, int count)
{
Month = month;
AppointmentType = appointmentType;
Count = count;
}
}
#endregion
#region User Schedule
private void UserSchedule_GenerateReport()
{
userScheduleDataGrid.DataSource = null;
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)
{
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
#region Number of Users by City
private void CustomersByCity_GenerateReport()
{
customersByCityDataGrid.DataSource = null;
var customers = DatabaseHelper.RetrieveCustomers();
var cityCounts = customers
.Select(c =>
{
var addr = DatabaseHelper.RetrieveAddress(c.AddressId);
var city = addr != null ? DatabaseHelper.RetrieveCity(addr.CityId) : null;
return city?.CityName;
})
.Where(cityName => !string.IsNullOrEmpty(cityName))
.GroupBy(cityName => cityName)
.Select(g => new CustomersByCity(g.Key, g.Count()))
.ToList();
customersByCityDataGrid.DataSource = cityCounts;
}
private class CustomersByCity
{
public string? City { get; set; }
public int Count { get; set; }
public CustomersByCity(string? city, int count)
{
City = city;
Count = count;
}
}
#endregion
}