using System; using System.Collections.Generic; using System.Linq; using System.Windows; using System.Windows.Media.Imaging; using Comal.Classes; using InABox.Clients; using InABox.Core; using InABox.DynamicGrid; using InABox.WPF; namespace PRSDesktop; public class JobQualificationGrid : DynamicManyToManyDataGrid, IJobControl { private CoreTable EmployeeQualifications; private CoreTable JobEmployees; private readonly Dictionary> warnings = new(); public JobQualificationGrid() { Options.Add(DynamicGridOption.MultiSelect); ActionColumns.Add(new DynamicActionColumn(QualificationImage, CheckQualification) { Position = DynamicActionColumnPosition.Start }); } public Guid JobID { get => ID; set => ID = value; } private void Warn(Guid qualificationid, string message) { if (!warnings.ContainsKey(qualificationid)) warnings[qualificationid] = new List(); warnings[qualificationid].Add(message); } private BitmapImage QualificationImage(CoreRow arg) { BitmapImage result = null; CheckJobEmployees(); CheckEmployeeQualifications(); var qualid = arg != null ? (arg.EntityLinkID(x => x.Qualification) ?? Guid.Empty) : Guid.Empty; if (qualid != Guid.Empty) { if (arg.Get(x => x.Required)) { foreach (var jerow in JobEmployees.Rows) { var empid = jerow.Get(x => x.EmployeeLink.ID); var erow = EmployeeQualifications.Rows.FirstOrDefault(r => r.Get(c => c.Qualification.ID).Equals(qualid) && r.Get(c => c.Employee.ID).Equals(empid)); if (erow == null) { Warn(qualid, string.Format("{0} does not have this qualification", jerow.Get(x => x.EmployeeLink.Name))); result = PRSDesktop.Resources.disabled.AsBitmapImage(); } else { var expiry = erow.Get(x => x.Expiry); var permanent = erow.Get(c => c.Qualification.Renewal) == QualificationRenewal.Permanent; if (!permanent && expiry < DateTime.Today) { Warn(qualid, string.Format("{0} expired on {1:dd MMM yy}", jerow.Get(x => x.EmployeeLink.Name), expiry)); result = PRSDesktop.Resources.disabled.AsBitmapImage(); } else if (!permanent && expiry < DateTime.MaxValue.Date) { Warn(qualid, string.Format("{0} is valid until {1:dd MMM yy}", jerow.Get(x => x.EmployeeLink.Name), expiry)); } else { Warn(qualid, string.Format("{0} is valid", jerow.Get(x => x.EmployeeLink.Name))); } } } if (result == null && JobEmployees.Rows.Any()) result = PRSDesktop.Resources.tick.AsBitmapImage(); } else { var erows = EmployeeQualifications.Rows.Where(r => r.Get(c => c.Qualification.ID).Equals(qualid)); foreach (var erow in erows) { var expiry = erow.Get(x => x.Expiry); var permanent = erow.Get(c => c.Qualification.Renewal) == QualificationRenewal.Permanent; if (!permanent && expiry < DateTime.Today) { Warn(qualid, string.Format("{0} expired on {1:dd MMM yy}", erow.Get(x => x.Employee.Name), expiry)); if (result == null) result = PRSDesktop.Resources.warning.AsBitmapImage(); } else if (!permanent && expiry < DateTime.MaxValue.Date) { Warn(qualid, string.Format("{0} is valid until {1:dd MMM yy}", erow.Get(x => x.Employee.Name), expiry)); } else { Warn(qualid, string.Format("{0} is valid", erow.Get(x => x.Employee.Name))); } } if (result == null && erows.Any()) result = PRSDesktop.Resources.tick.AsBitmapImage(); } } return result; } private void CheckJobEmployees() { if (JobEmployees == null) { if (JobID != Guid.Empty) { var filter = new Filter(x => x.JobLink.ID).IsEqualTo(JobID); filter.Ands.Add(new Filter(x => x.EmployeeLink.StartDate).IsEqualTo(DateTime.MinValue).Or(x => x.EmployeeLink.StartDate) .IsLessThanOrEqualTo(DateTime.Today)); filter.Ands.Add(new Filter(x => x.EmployeeLink.FinishDate).IsEqualTo(DateTime.MinValue) .Or(x => x.EmployeeLink.FinishDate).IsGreaterThanOrEqualTo(DateTime.Today)); JobEmployees = new Client().Query(filter); } else { JobEmployees = new CoreTable(); JobEmployees.LoadColumns(typeof(JobEmployee)); } } } private void CheckEmployeeQualifications() { if (EmployeeQualifications == null) { var emps = new List(); foreach (var row in JobEmployees.Rows) { var emp = row.Get(x => x.EmployeeLink.ID); if (!emps.Contains(emp)) emps.Add(emp); } if (emps.Any()) { EmployeeQualifications = new Client().Query(new Filter(x => x.Employee.ID).InList(emps.ToArray())); } else { EmployeeQualifications = new CoreTable(); EmployeeQualifications.LoadColumns(typeof(EmployeeQualification)); } } } private bool CheckQualification(CoreRow arg) { if (arg == null) { MessageBox.Show("Please select a qualification to examine!"); return false; } var qualid = arg.Get(x => x.Qualification.ID); var messages = new List(); if (warnings.ContainsKey(qualid)) { messages.Add(string.Format("{0} Summary:\n", arg.Get(x => x.Qualification.Description))); foreach (var message in warnings[qualid]) messages.Add("- " + message); MessageBox.Show(string.Join("\n", messages)); } else { MessageBox.Show("No data to show!"); } return false; } protected override void Reload(Filters criteria, Columns columns, ref SortOrder sort, Action action) { warnings.Clear(); JobEmployees = null; EmployeeQualifications = null; base.Reload(criteria, columns, ref sort, action); } }