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 : DynamicDataGrid, IJobControl { private CoreTable EmployeeQualifications; private CoreTable JobEmployees; private readonly Dictionary> warnings = new(); public JobQualificationGrid() { ActionColumns.Add(new DynamicImageColumn(QualificationImage, CheckQualification) { Position = DynamicActionColumnPosition.Start }); } protected override void DoReconfigure(FluentList options) { base.DoReconfigure(options); options .BeginUpdate() .Add(DynamicGridOption.MultiSelect) .Add(DynamicGridOption.SelectColumns) .EndUpdate(); } public Job Job { get; set; } public JobPanelSettings Settings { get; set; } 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 (Job.ID != Guid.Empty) { var filter = new Filter(x => x.JobLink.ID).IsEqualTo(Job.ID); 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; criteria.Add(new Filter(x => x.Job.ID).IsEqualTo(Job.ID)); base.Reload(criteria, columns, ref sort, action); } protected override bool CanCreateItems() { return Job.ID != Guid.Empty; } protected override void DoAdd(bool OpenEditorOnDirectEdit = false) { var ids = ExtractValues(x => x.Qualification.ID, Selection.All).ToArray(); var msd = new MultiSelectDialog( new Filter(x => x.ID).NotInList(ids), new Columns(x=>x.ID), true); if (msd.ShowDialog() == true) { List updates = new List(); foreach (var row in msd.Data().Rows) { var jobqual = new JobQualification(); jobqual.Job.ID = Job.ID; jobqual.Job.Synchronise(Job); jobqual.Qualification.ID = row.Get(x => x.ID); updates.Add(jobqual); } using (new WaitCursor()) { new Client().Save(updates, "Added By Job Qualification Screen"); Refresh(false,true); } } } protected override void CustomiseEditor(JobQualification[] items, DynamicGridColumn column, BaseEditor editor) { if (String.Equals(column.ColumnName, "Job.ID")) editor.Editable = Editable.Hidden; base.CustomiseEditor(items, column, editor); } }