JobQualificationGrid.cs 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Windows;
  5. using System.Windows.Media.Imaging;
  6. using Comal.Classes;
  7. using InABox.Clients;
  8. using InABox.Core;
  9. using InABox.DynamicGrid;
  10. using InABox.WPF;
  11. using InABox.Wpf;
  12. using System.Threading;
  13. namespace PRSDesktop;
  14. public class JobQualificationGrid : DynamicDataGrid<JobQualification>, IMasterDetailControl<Job,JobQualification>
  15. {
  16. private CoreTable EmployeeQualifications;
  17. private CoreTable JobEmployees;
  18. private readonly Dictionary<Guid, List<string>> warnings = new();
  19. public Job? Master { get; set; }
  20. public Filter<JobQualification> MasterDetailFilter => (Master?.ID ?? Guid.Empty) != Guid.Empty
  21. ? new Filter<JobQualification>(x => x.Job.ID).IsEqualTo(Master.ID)
  22. : new Filter<JobQualification>().None();
  23. public JobQualificationGrid()
  24. {
  25. ActionColumns.Add(new DynamicImageColumn(QualificationImage, CheckQualification) { Position = DynamicActionColumnPosition.Start });
  26. }
  27. protected override void DoReconfigure(DynamicGridOptions options)
  28. {
  29. base.DoReconfigure(options);
  30. options.MultiSelect = true;
  31. options.SelectColumns = true;
  32. }
  33. private void Warn(Guid qualificationid, string message)
  34. {
  35. if (!warnings.ContainsKey(qualificationid))
  36. warnings[qualificationid] = new List<string>();
  37. warnings[qualificationid].Add(message);
  38. }
  39. private BitmapImage QualificationImage(CoreRow arg)
  40. {
  41. BitmapImage result = null;
  42. CheckJobEmployees();
  43. CheckEmployeeQualifications();
  44. var qualid = arg != null ? (arg.EntityLinkID<JobQualification, QualificationLink>(x => x.Qualification) ?? Guid.Empty) : Guid.Empty;
  45. if (qualid != Guid.Empty)
  46. {
  47. if (arg.Get<JobQualification, bool>(x => x.Required))
  48. {
  49. foreach (var jerow in JobEmployees.Rows)
  50. {
  51. var empid = jerow.Get<JobEmployee, Guid>(x => x.EmployeeLink.ID);
  52. var erow = EmployeeQualifications.Rows.FirstOrDefault(r =>
  53. r.Get<EmployeeQualification, Guid>(c => c.Qualification.ID).Equals(qualid) &&
  54. r.Get<EmployeeQualification, Guid>(c => c.Employee.ID).Equals(empid));
  55. if (erow == null)
  56. {
  57. Warn(qualid, string.Format("{0} does not have this qualification", jerow.Get<JobEmployee, string>(x => x.EmployeeLink.Name)));
  58. result = PRSDesktop.Resources.disabled.AsBitmapImage();
  59. }
  60. else
  61. {
  62. var expiry = erow.Get<EmployeeQualification, DateTime>(x => x.Expiry);
  63. var permanent = erow.Get<EmployeeQualification, QualificationRenewal>(c => c.Qualification.Renewal) == QualificationRenewal.Permanent;
  64. if (!permanent && expiry < DateTime.Today)
  65. {
  66. Warn(qualid,
  67. string.Format("{0} expired on {1:dd MMM yy}", jerow.Get<JobEmployee, string>(x => x.EmployeeLink.Name), expiry));
  68. result = PRSDesktop.Resources.disabled.AsBitmapImage();
  69. }
  70. else if (!permanent && expiry < DateTime.MaxValue.Date)
  71. {
  72. Warn(qualid,
  73. string.Format("{0} is valid until {1:dd MMM yy}", jerow.Get<JobEmployee, string>(x => x.EmployeeLink.Name), expiry));
  74. }
  75. else
  76. {
  77. Warn(qualid, string.Format("{0} is valid", jerow.Get<JobEmployee, string>(x => x.EmployeeLink.Name)));
  78. }
  79. }
  80. }
  81. if (result == null && JobEmployees.Rows.Any())
  82. result = PRSDesktop.Resources.tick.AsBitmapImage();
  83. }
  84. else
  85. {
  86. var erows = EmployeeQualifications.Rows.Where(r => r.Get<EmployeeQualification, Guid>(c => c.Qualification.ID).Equals(qualid));
  87. foreach (var erow in erows)
  88. {
  89. var expiry = erow.Get<EmployeeQualification, DateTime>(x => x.Expiry);
  90. var permanent = erow.Get<EmployeeQualification, QualificationRenewal>(c => c.Qualification.Renewal) == QualificationRenewal.Permanent;
  91. if (!permanent && expiry < DateTime.Today)
  92. {
  93. Warn(qualid,
  94. string.Format("{0} expired on {1:dd MMM yy}", erow.Get<EmployeeQualification, string>(x => x.Employee.Name), expiry));
  95. if (result == null)
  96. result = PRSDesktop.Resources.warning.AsBitmapImage();
  97. }
  98. else if (!permanent && expiry < DateTime.MaxValue.Date)
  99. {
  100. Warn(qualid,
  101. string.Format("{0} is valid until {1:dd MMM yy}", erow.Get<EmployeeQualification, string>(x => x.Employee.Name), expiry));
  102. }
  103. else
  104. {
  105. Warn(qualid, string.Format("{0} is valid", erow.Get<EmployeeQualification, string>(x => x.Employee.Name)));
  106. }
  107. }
  108. if (result == null && erows.Any())
  109. result = PRSDesktop.Resources.tick.AsBitmapImage();
  110. }
  111. }
  112. return result;
  113. }
  114. private void CheckJobEmployees()
  115. {
  116. if (JobEmployees == null)
  117. {
  118. if ((Master?.ID ?? Guid.Empty) != Guid.Empty)
  119. {
  120. var filter = new Filter<JobEmployee>(x => x.JobLink.ID).IsEqualTo(Master?.ID ?? Guid.Empty);
  121. filter.Ands.Add(new Filter<JobEmployee>(x => x.EmployeeLink.StartDate).IsEqualTo(DateTime.MinValue).Or(x => x.EmployeeLink.StartDate)
  122. .IsLessThanOrEqualTo(DateTime.Today));
  123. filter.Ands.Add(new Filter<JobEmployee>(x => x.EmployeeLink.FinishDate).IsEqualTo(DateTime.MinValue)
  124. .Or(x => x.EmployeeLink.FinishDate).IsGreaterThanOrEqualTo(DateTime.Today));
  125. JobEmployees = new Client<JobEmployee>().Query(filter);
  126. }
  127. else
  128. {
  129. JobEmployees = new CoreTable();
  130. JobEmployees.LoadColumns(typeof(JobEmployee));
  131. }
  132. }
  133. }
  134. private void CheckEmployeeQualifications()
  135. {
  136. if (EmployeeQualifications == null)
  137. {
  138. var emps = new List<Guid>();
  139. foreach (var row in JobEmployees.Rows)
  140. {
  141. var emp = row.Get<JobEmployee, Guid>(x => x.EmployeeLink.ID);
  142. if (!emps.Contains(emp))
  143. emps.Add(emp);
  144. }
  145. if (emps.Any())
  146. {
  147. EmployeeQualifications =
  148. new Client<EmployeeQualification>().Query(new Filter<EmployeeQualification>(x => x.Employee.ID).InList(emps.ToArray()));
  149. }
  150. else
  151. {
  152. EmployeeQualifications = new CoreTable();
  153. EmployeeQualifications.LoadColumns(typeof(EmployeeQualification));
  154. }
  155. }
  156. }
  157. private bool CheckQualification(CoreRow arg)
  158. {
  159. if (arg == null)
  160. {
  161. MessageBox.Show("Please select a qualification to examine!");
  162. return false;
  163. }
  164. var qualid = arg.Get<JobQualification, Guid>(x => x.Qualification.ID);
  165. var messages = new List<string>();
  166. if (warnings.ContainsKey(qualid))
  167. {
  168. messages.Add(string.Format("{0} Summary:\n", arg.Get<JobQualification, string>(x => x.Qualification.Description)));
  169. foreach (var message in warnings[qualid])
  170. messages.Add("- " + message);
  171. MessageBox.Show(string.Join("\n", messages));
  172. }
  173. else
  174. {
  175. MessageBox.Show("No data to show!");
  176. }
  177. return false;
  178. }
  179. protected override void Reload(
  180. Filters<JobQualification> criteria, Columns<JobQualification> columns, ref SortOrder<JobQualification>? sort,
  181. CancellationToken token, Action<CoreTable?, Exception?> action)
  182. {
  183. warnings.Clear();
  184. JobEmployees = null;
  185. EmployeeQualifications = null;
  186. criteria.Add(MasterDetailFilter);
  187. base.Reload(criteria, columns, ref sort, token, action);
  188. }
  189. protected override bool CanCreateItems()
  190. {
  191. return (Master?.ID ?? Guid.Empty) != Guid.Empty;
  192. }
  193. protected override void DoAdd(bool OpenEditorOnDirectEdit = false)
  194. {
  195. var ids = ExtractValues(x => x.Qualification.ID, Selection.All).ToArray();
  196. var msd = new MultiSelectDialog<Qualification>(
  197. new Filter<Qualification>(x => x.ID).NotInList(ids),
  198. Columns.None<Qualification>().Add(x=>x.ID),
  199. true);
  200. if (msd.ShowDialog() == true)
  201. {
  202. List<JobQualification> updates = new List<JobQualification>();
  203. foreach (var row in msd.Data().Rows)
  204. {
  205. var jobqual = new JobQualification();
  206. jobqual.Job.ID = Master?.ID ?? Guid.Empty;
  207. jobqual.Job.Synchronise(Master ?? new Job());
  208. jobqual.Qualification.ID = row.Get<Qualification, Guid>(x => x.ID);
  209. updates.Add(jobqual);
  210. }
  211. using (new WaitCursor())
  212. {
  213. new Client<JobQualification>().Save(updates, "Added By Job Qualification Screen");
  214. Refresh(false,true);
  215. }
  216. }
  217. }
  218. protected override void CustomiseEditor(JobQualification[] items, DynamicGridColumn column, BaseEditor editor)
  219. {
  220. if (String.Equals(column.ColumnName, "Job.ID"))
  221. editor.Editable = Editable.Hidden;
  222. base.CustomiseEditor(items, column, editor);
  223. }
  224. }