123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198 |
- 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<JobQualification, Job>, IJobControl
- {
- private CoreTable EmployeeQualifications;
- private CoreTable JobEmployees;
- private readonly Dictionary<Guid, List<string>> 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<string>();
- warnings[qualificationid].Add(message);
- }
- private BitmapImage QualificationImage(CoreRow arg)
- {
- BitmapImage result = null;
- CheckJobEmployees();
- CheckEmployeeQualifications();
- var qualid = arg != null ? (arg.EntityLinkID<JobQualification, QualificationLink>(x => x.Qualification) ?? Guid.Empty) : Guid.Empty;
- if (qualid != Guid.Empty)
- {
- if (arg.Get<JobQualification, bool>(x => x.Required))
- {
- foreach (var jerow in JobEmployees.Rows)
- {
- var empid = jerow.Get<JobEmployee, Guid>(x => x.EmployeeLink.ID);
- var erow = EmployeeQualifications.Rows.FirstOrDefault(r =>
- r.Get<EmployeeQualification, Guid>(c => c.Qualification.ID).Equals(qualid) &&
- r.Get<EmployeeQualification, Guid>(c => c.Employee.ID).Equals(empid));
- if (erow == null)
- {
- Warn(qualid, string.Format("{0} does not have this qualification", jerow.Get<JobEmployee, string>(x => x.EmployeeLink.Name)));
- result = PRSDesktop.Resources.disabled.AsBitmapImage();
- }
- else
- {
- var expiry = erow.Get<EmployeeQualification, DateTime>(x => x.Expiry);
- var permanent = erow.Get<EmployeeQualification, QualificationRenewal>(c => c.Qualification.Renewal) == QualificationRenewal.Permanent;
- if (!permanent && expiry < DateTime.Today)
- {
- Warn(qualid,
- string.Format("{0} expired on {1:dd MMM yy}", jerow.Get<JobEmployee, string>(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<JobEmployee, string>(x => x.EmployeeLink.Name), expiry));
- }
- else
- {
- Warn(qualid, string.Format("{0} is valid", jerow.Get<JobEmployee, string>(x => x.EmployeeLink.Name)));
- }
- }
- }
- if (result == null && JobEmployees.Rows.Any())
- result = PRSDesktop.Resources.tick.AsBitmapImage();
- }
- else
- {
- var erows = EmployeeQualifications.Rows.Where(r => r.Get<EmployeeQualification, Guid>(c => c.Qualification.ID).Equals(qualid));
- foreach (var erow in erows)
- {
- var expiry = erow.Get<EmployeeQualification, DateTime>(x => x.Expiry);
- var permanent = erow.Get<EmployeeQualification, QualificationRenewal>(c => c.Qualification.Renewal) == QualificationRenewal.Permanent;
- if (!permanent && expiry < DateTime.Today)
- {
- Warn(qualid,
- string.Format("{0} expired on {1:dd MMM yy}", erow.Get<EmployeeQualification, string>(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<EmployeeQualification, string>(x => x.Employee.Name), expiry));
- }
- else
- {
- Warn(qualid, string.Format("{0} is valid", erow.Get<EmployeeQualification, string>(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<JobEmployee>(x => x.JobLink.ID).IsEqualTo(JobID);
- filter.Ands.Add(new Filter<JobEmployee>(x => x.EmployeeLink.StartDate).IsEqualTo(DateTime.MinValue).Or(x => x.EmployeeLink.StartDate)
- .IsLessThanOrEqualTo(DateTime.Today));
- filter.Ands.Add(new Filter<JobEmployee>(x => x.EmployeeLink.FinishDate).IsEqualTo(DateTime.MinValue)
- .Or(x => x.EmployeeLink.FinishDate).IsGreaterThanOrEqualTo(DateTime.Today));
- JobEmployees = new Client<JobEmployee>().Query(filter);
- }
- else
- {
- JobEmployees = new CoreTable();
- JobEmployees.LoadColumns(typeof(JobEmployee));
- }
- }
- }
- private void CheckEmployeeQualifications()
- {
- if (EmployeeQualifications == null)
- {
- var emps = new List<Guid>();
- foreach (var row in JobEmployees.Rows)
- {
- var emp = row.Get<JobEmployee, Guid>(x => x.EmployeeLink.ID);
- if (!emps.Contains(emp))
- emps.Add(emp);
- }
- if (emps.Any())
- {
- EmployeeQualifications =
- new Client<EmployeeQualification>().Query(new Filter<EmployeeQualification>(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<JobQualification, Guid>(x => x.Qualification.ID);
- var messages = new List<string>();
- if (warnings.ContainsKey(qualid))
- {
- messages.Add(string.Format("{0} Summary:\n", arg.Get<JobQualification, string>(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<JobQualification> criteria, Columns<JobQualification> columns, ref SortOrder<JobQualification> sort,
- Action<CoreTable, Exception> action)
- {
- warnings.Clear();
- JobEmployees = null;
- EmployeeQualifications = null;
- base.Reload(criteria, columns, ref sort, action);
- }
- }
|