using System; using System.Collections.Generic; using System.Linq; using System.Windows; using System.Windows.Controls; using System.Windows.Media.Imaging; using Comal.Classes; using InABox.Clients; using InABox.Core; using InABox.DynamicGrid; using InABox.WPF; namespace PRSDesktop { internal class JobEmployeeGrid : DynamicManyToManyDataGrid, IJobControl { private CoreTable EmployeeQualifications; private CoreTable JobQualifications; private readonly Dictionary, List>> warnings = new(); public JobEmployeeGrid() { Options.Add(DynamicGridOption.MultiSelect); HiddenColumns.Add(x => x.EmployeeLink.ID); HiddenColumns.Add(x => x.JobLink.ID); ActionColumns.Add(new DynamicImageColumn(QualificationImage, QualificationsCheck) { Position = DynamicActionColumnPosition.Start }); AddButton("Edit Employee", PRSDesktop.Resources.employee.AsBitmapImage(), EditEmployee); } public Guid ParentID { get => ID; set => ID = value; } public JobPanelSettings Settings { get; set; } private bool EditEmployee(Button arg1, CoreRow[] arg2) { if (arg2 != null && arg2.Length == 1) { var emps = new Client().Query( new Filter(x => x.ID).IsEqualTo(arg2.First().Get(x => x.EmployeeLink.ID))); if (emps.Rows.Count == 1) { var emp = emps.Rows.First().ToObject(); var grid = new EmployeeGrid(); return grid.EditItems(new[] { emp }); } MessageBox.Show("Unable to Load Employee Card"); } else { MessageBox.Show("Please select a single employee to edit!"); } return false; } private BitmapImage QualificationImage(CoreRow arg) { BitmapImage result = null; CheckEmployeeQualifications(); CheckJobQualifications(); var empid = arg != null ? arg.Get(x => x.EmployeeLink.ID) : Guid.Empty; var erows = EmployeeQualifications.Rows.Where(r => r.Get(c => c.Employee.ID).Equals(empid)); var CheckedQualifications = new List(); // Check Job Requirements var jrows = JobQualifications.Rows.Where(r => r.Get(x => x.Required).Equals(true)); foreach (var jrow in jrows) { var qualificationid = jrow.Get(x => x.Qualification.ID); var erow = erows.FirstOrDefault(r => r.Get(c => c.Employee.ID).Equals(empid) && r.Get(c => c.Qualification.ID).Equals(qualificationid)); if (erow == null) { Warn(empid, string.Format("{0} is missing", jrow.Get(c => c.Qualification.Description) ), "" ); result = PRSDesktop.Resources.disabled.AsBitmapImage(); } else { var expiry = erow.Get(c => c.Expiry); var permanent = erow.Get(c => c.Qualification.Renewal) == QualificationRenewal.Permanent; if (!permanent && expiry < DateTime.Today) { Warn(empid, string.Format("{0} expired on {1: dd MMM yy}", jrow.Get(c => c.Qualification.Description), expiry ), "" ); result = PRSDesktop.Resources.disabled.AsBitmapImage(); } else if (!permanent && expiry < DateTime.MaxValue.Date) { Warn(empid, string.Format("{0} is valid until {1:dd MMM yy}", jrow.Get(c => c.Qualification.Description), erow.Get(c => c.Expiry) ), "" ); } else { Warn(empid, string.Format("{0} is valid", jrow.Get(c => c.Qualification.Description) ), "" ); } } CheckedQualifications.Add(erow.Get(x => x.ID)); } // Check Qualification Expiry foreach (var erow in erows) if (!CheckedQualifications.Contains(erow.Get(x => x.ID))) { var expiry = erow.Get(x => x.Expiry); var permanent = erow.Get(c => c.Qualification.Renewal) == QualificationRenewal.Permanent; if (!permanent && expiry < DateTime.Today) { Warn(empid, "", string.Format("{0} has expired", erow.Get(c => c.Qualification.Description) ) ); if (result == null) result = PRSDesktop.Resources.warning.AsBitmapImage(); } else if (!permanent && expiry < DateTime.MaxValue.Date) { Warn(empid, "", string.Format("{0} is valid until {1:dd MMM yy}", erow.Get(c => c.Qualification.Description), expiry ) ); } else { Warn(empid, "", string.Format("{0} is valid", erow.Get(c => c.Qualification.Description) ) ); } } if (result == null && erows.Any()) result = PRSDesktop.Resources.tick.AsBitmapImage(); return result; } private void CheckJobQualifications() { if (JobQualifications == null) { if (ParentID != Guid.Empty) { JobQualifications = new Client().Query(new Filter(x => x.Job.ID).IsEqualTo(ParentID)); } else { JobQualifications = new CoreTable(); JobQualifications.LoadColumns(typeof(JobQualification)); } } } private void CheckEmployeeQualifications() { if (EmployeeQualifications == null) { var emps = new List(); foreach (var row in Data.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 QualificationsCheck(CoreRow arg) { var empid = arg != null ? arg.Get(x => x.EmployeeLink.ID) : Guid.Empty; if (warnings.ContainsKey(empid)) { var summary = new List(); if (warnings[empid].Item1.Any()) { summary.Add("Required Qualifications:"); foreach (var warning in warnings[empid].Item1) summary.Add("- " + warning); summary.Add(""); } if (warnings[empid].Item2.Any()) { summary.Add("Other Qualifications:"); foreach (var warning in warnings[empid].Item2) summary.Add("- " + warning); } if (summary.Any()) MessageBox.Show(string.Join("\n", summary)); else MessageBox.Show("No job-specific qualifications found for " + arg.Get(x => x.EmployeeLink.Name)); } else { MessageBox.Show("No job-specific qualifications found for " + arg.Get(x => x.EmployeeLink.Name)); } return false; } private void Warn(Guid empid, string required, string optional) { if (!warnings.ContainsKey(empid)) warnings[empid] = new Tuple, List>(new List(), new List()); if (!string.IsNullOrWhiteSpace(required)) warnings[empid].Item1.Add(required); if (!string.IsNullOrWhiteSpace(optional)) warnings[empid].Item2.Add(optional); } protected override void Reload(Filters criteria, Columns columns, ref SortOrder sort, Action action) { EmployeeQualifications = null; JobQualifications = null; warnings.Clear(); criteria.Add(new Filter(x => x.JobLink.ID).IsEqualTo(ParentID)); criteria.Add(new Filter(x => x.EmployeeLink.StartDate).IsEqualTo(DateTime.MinValue).Or(x => x.EmployeeLink.StartDate) .IsLessThanOrEqualTo(DateTime.Today)); criteria.Add(new Filter(x => x.EmployeeLink.FinishDate).IsEqualTo(DateTime.MinValue).Or(x => x.EmployeeLink.FinishDate) .IsGreaterThanOrEqualTo(DateTime.Today)); base.Reload(criteria, columns, ref sort, action); } } }