123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274 |
- 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<JobEmployee, Job>, IJobControl
- {
- private CoreTable EmployeeQualifications;
- private CoreTable JobQualifications;
- private readonly Dictionary<Guid, Tuple<List<string>, List<string>>> 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<Employee>().Query(
- new Filter<Employee>(x => x.ID).IsEqualTo(arg2.First().Get<JobEmployee, Guid>(x => x.EmployeeLink.ID)));
- if (emps.Rows.Count == 1)
- {
- var emp = emps.Rows.First().ToObject<Employee>();
- 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<JobEmployee, Guid>(x => x.EmployeeLink.ID) : Guid.Empty;
- var erows = EmployeeQualifications.Rows.Where(r => r.Get<EmployeeQualification, Guid>(c => c.Employee.ID).Equals(empid));
- var CheckedQualifications = new List<Guid>();
- // Check Job Requirements
- var jrows = JobQualifications.Rows.Where(r => r.Get<JobQualification, bool>(x => x.Required).Equals(true));
- foreach (var jrow in jrows)
- {
- var qualificationid = jrow.Get<JobQualification, Guid>(x => x.Qualification.ID);
- var erow = erows.FirstOrDefault(r =>
- r.Get<EmployeeQualification, Guid>(c => c.Employee.ID).Equals(empid) &&
- r.Get<EmployeeQualification, Guid>(c => c.Qualification.ID).Equals(qualificationid));
- if (erow == null)
- {
- Warn(empid,
- string.Format("{0} is missing",
- jrow.Get<JobQualification, string>(c => c.Qualification.Description)
- ),
- ""
- );
- result = PRSDesktop.Resources.disabled.AsBitmapImage();
- }
- else
- {
- var expiry = erow.Get<EmployeeQualification, DateTime>(c => c.Expiry);
- var permanent = erow.Get<EmployeeQualification, QualificationRenewal>(c => c.Qualification.Renewal) == QualificationRenewal.Permanent;
- if (!permanent && expiry < DateTime.Today)
- {
- Warn(empid,
- string.Format("{0} expired on {1: dd MMM yy}",
- jrow.Get<JobQualification, string>(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<JobQualification, string>(c => c.Qualification.Description),
- erow.Get<EmployeeQualification, DateTime>(c => c.Expiry)
- ),
- ""
- );
- }
- else
- {
- Warn(empid,
- string.Format("{0} is valid",
- jrow.Get<JobQualification, string>(c => c.Qualification.Description)
- ),
- ""
- );
- }
- }
- CheckedQualifications.Add(erow.Get<EmployeeQualification, Guid>(x => x.ID));
- }
- // Check Qualification Expiry
- foreach (var erow in erows)
- if (!CheckedQualifications.Contains(erow.Get<EmployeeQualification, Guid>(x => x.ID)))
- {
- 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(empid,
- "",
- string.Format("{0} has expired",
- erow.Get<EmployeeQualification, string>(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<JobQualification, string>(c => c.Qualification.Description),
- expiry
- )
- );
- }
- else
- {
- Warn(empid,
- "",
- string.Format("{0} is valid",
- erow.Get<JobQualification, string>(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<JobQualification>().Query(new Filter<JobQualification>(x => x.Job.ID).IsEqualTo(ParentID));
- }
- else
- {
- JobQualifications = new CoreTable();
- JobQualifications.LoadColumns(typeof(JobQualification));
- }
- }
- }
- private void CheckEmployeeQualifications()
- {
- if (EmployeeQualifications == null)
- {
- var emps = new List<Guid>();
- foreach (var row in Data.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 QualificationsCheck(CoreRow arg)
- {
- var empid = arg != null ? arg.Get<JobEmployee, Guid>(x => x.EmployeeLink.ID) : Guid.Empty;
- if (warnings.ContainsKey(empid))
- {
- var summary = new List<string>();
- 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<JobEmployee, string>(x => x.EmployeeLink.Name));
- }
- else
- {
- MessageBox.Show("No job-specific qualifications found for " + arg.Get<JobEmployee, string>(x => x.EmployeeLink.Name));
- }
- return false;
- }
- private void Warn(Guid empid, string required, string optional)
- {
- if (!warnings.ContainsKey(empid))
- warnings[empid] = new Tuple<List<string>, List<string>>(new List<string>(), new List<string>());
- if (!string.IsNullOrWhiteSpace(required))
- warnings[empid].Item1.Add(required);
- if (!string.IsNullOrWhiteSpace(optional))
- warnings[empid].Item2.Add(optional);
- }
- protected override void Reload(Filters<JobEmployee> criteria, Columns<JobEmployee> columns, ref SortOrder<JobEmployee> sort,
- Action<CoreTable, Exception> action)
- {
- EmployeeQualifications = null;
- JobQualifications = null;
- warnings.Clear();
- criteria.Add(new Filter<JobEmployee>(x => x.JobLink.ID).IsEqualTo(ParentID));
- criteria.Add(new Filter<JobEmployee>(x => x.EmployeeLink.StartDate).IsEqualTo(DateTime.MinValue).Or(x => x.EmployeeLink.StartDate)
- .IsLessThanOrEqualTo(DateTime.Today));
- criteria.Add(new Filter<JobEmployee>(x => x.EmployeeLink.FinishDate).IsEqualTo(DateTime.MinValue).Or(x => x.EmployeeLink.FinishDate)
- .IsGreaterThanOrEqualTo(DateTime.Today));
- base.Reload(criteria, columns, ref sort, action);
- }
- }
- }
|