| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250 |
- 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;
- using InABox.Wpf;
- namespace PRSDesktop;
- public class JobQualificationGrid : DynamicDataGrid<JobQualification>, IMasterDetailControl<Job,JobQualification>
- {
- private CoreTable EmployeeQualifications;
- private CoreTable JobEmployees;
- private readonly Dictionary<Guid, List<string>> warnings = new();
-
- public Job? Master { get; set; }
- public Filter<JobQualification> MasterDetailFilter => (Master?.ID ?? Guid.Empty) != Guid.Empty
- ? new Filter<JobQualification>(x => x.Job.ID).IsEqualTo(Master.ID)
- : new Filter<JobQualification>().None();
- public JobQualificationGrid()
- {
- ActionColumns.Add(new DynamicImageColumn(QualificationImage, CheckQualification) { Position = DynamicActionColumnPosition.Start });
- }
-
-
- protected override void DoReconfigure(FluentList<DynamicGridOption> options)
- {
- base.DoReconfigure(options);
- options
- .BeginUpdate()
- .Add(DynamicGridOption.MultiSelect)
- .Add(DynamicGridOption.SelectColumns)
- .EndUpdate();
- }
-
- 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 ((Master?.ID ?? Guid.Empty) != Guid.Empty)
- {
- var filter = new Filter<JobEmployee>(x => x.JobLink.ID).IsEqualTo(Master?.ID ?? Guid.Empty);
- 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;
- criteria.Add(MasterDetailFilter);
- base.Reload(criteria, columns, ref sort, action);
- }
-
- protected override bool CanCreateItems()
- {
- return (Master?.ID ?? Guid.Empty) != Guid.Empty;
- }
-
- protected override void DoAdd(bool OpenEditorOnDirectEdit = false)
- {
- var ids = ExtractValues(x => x.Qualification.ID, Selection.All).ToArray();
- var msd = new MultiSelectDialog<Qualification>(
- new Filter<Qualification>(x => x.ID).NotInList(ids),
- new Columns<Qualification>(x=>x.ID),
- true);
- if (msd.ShowDialog() == true)
- {
- List<JobQualification> updates = new List<JobQualification>();
- foreach (var row in msd.Data().Rows)
- {
- var jobqual = new JobQualification();
- jobqual.Job.ID = Master?.ID ?? Guid.Empty;
- jobqual.Job.Synchronise(Master ?? new Job());
- jobqual.Qualification.ID = row.Get<Qualification, Guid>(x => x.ID);
- updates.Add(jobqual);
- }
- using (new WaitCursor())
- {
- new Client<JobQualification>().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);
- }
- }
|