JobQualificationGrid.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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. namespace PRSDesktop;
  12. public class JobQualificationGrid : DynamicManyToManyDataGrid<JobQualification, Job>, IJobControl
  13. {
  14. private CoreTable EmployeeQualifications;
  15. private CoreTable JobEmployees;
  16. private readonly Dictionary<Guid, List<string>> warnings = new();
  17. public JobQualificationGrid()
  18. {
  19. Options.Add(DynamicGridOption.MultiSelect);
  20. ActionColumns.Add(new DynamicActionColumn(QualificationImage, CheckQualification) { Position = DynamicActionColumnPosition.Start });
  21. }
  22. public Guid JobID
  23. {
  24. get => ID;
  25. set => ID = value;
  26. }
  27. private void Warn(Guid qualificationid, string message)
  28. {
  29. if (!warnings.ContainsKey(qualificationid))
  30. warnings[qualificationid] = new List<string>();
  31. warnings[qualificationid].Add(message);
  32. }
  33. private BitmapImage QualificationImage(CoreRow arg)
  34. {
  35. BitmapImage result = null;
  36. CheckJobEmployees();
  37. CheckEmployeeQualifications();
  38. var qualid = arg != null ? (arg.EntityLinkID<JobQualification, QualificationLink>(x => x.Qualification) ?? Guid.Empty) : Guid.Empty;
  39. if (qualid != Guid.Empty)
  40. {
  41. if (arg.Get<JobQualification, bool>(x => x.Required))
  42. {
  43. foreach (var jerow in JobEmployees.Rows)
  44. {
  45. var empid = jerow.Get<JobEmployee, Guid>(x => x.EmployeeLink.ID);
  46. var erow = EmployeeQualifications.Rows.FirstOrDefault(r =>
  47. r.Get<EmployeeQualification, Guid>(c => c.Qualification.ID).Equals(qualid) &&
  48. r.Get<EmployeeQualification, Guid>(c => c.Employee.ID).Equals(empid));
  49. if (erow == null)
  50. {
  51. Warn(qualid, string.Format("{0} does not have this qualification", jerow.Get<JobEmployee, string>(x => x.EmployeeLink.Name)));
  52. result = PRSDesktop.Resources.disabled.AsBitmapImage();
  53. }
  54. else
  55. {
  56. var expiry = erow.Get<EmployeeQualification, DateTime>(x => x.Expiry);
  57. var permanent = erow.Get<EmployeeQualification, QualificationRenewal>(c => c.Qualification.Renewal) == QualificationRenewal.Permanent;
  58. if (!permanent && expiry < DateTime.Today)
  59. {
  60. Warn(qualid,
  61. string.Format("{0} expired on {1:dd MMM yy}", jerow.Get<JobEmployee, string>(x => x.EmployeeLink.Name), expiry));
  62. result = PRSDesktop.Resources.disabled.AsBitmapImage();
  63. }
  64. else if (!permanent && expiry < DateTime.MaxValue.Date)
  65. {
  66. Warn(qualid,
  67. string.Format("{0} is valid until {1:dd MMM yy}", jerow.Get<JobEmployee, string>(x => x.EmployeeLink.Name), expiry));
  68. }
  69. else
  70. {
  71. Warn(qualid, string.Format("{0} is valid", jerow.Get<JobEmployee, string>(x => x.EmployeeLink.Name)));
  72. }
  73. }
  74. }
  75. if (result == null && JobEmployees.Rows.Any())
  76. result = PRSDesktop.Resources.tick.AsBitmapImage();
  77. }
  78. else
  79. {
  80. var erows = EmployeeQualifications.Rows.Where(r => r.Get<EmployeeQualification, Guid>(c => c.Qualification.ID).Equals(qualid));
  81. foreach (var erow in erows)
  82. {
  83. var expiry = erow.Get<EmployeeQualification, DateTime>(x => x.Expiry);
  84. var permanent = erow.Get<EmployeeQualification, QualificationRenewal>(c => c.Qualification.Renewal) == QualificationRenewal.Permanent;
  85. if (!permanent && expiry < DateTime.Today)
  86. {
  87. Warn(qualid,
  88. string.Format("{0} expired on {1:dd MMM yy}", erow.Get<EmployeeQualification, string>(x => x.Employee.Name), expiry));
  89. if (result == null)
  90. result = PRSDesktop.Resources.warning.AsBitmapImage();
  91. }
  92. else if (!permanent && expiry < DateTime.MaxValue.Date)
  93. {
  94. Warn(qualid,
  95. string.Format("{0} is valid until {1:dd MMM yy}", erow.Get<EmployeeQualification, string>(x => x.Employee.Name), expiry));
  96. }
  97. else
  98. {
  99. Warn(qualid, string.Format("{0} is valid", erow.Get<EmployeeQualification, string>(x => x.Employee.Name)));
  100. }
  101. }
  102. if (result == null && erows.Any())
  103. result = PRSDesktop.Resources.tick.AsBitmapImage();
  104. }
  105. }
  106. return result;
  107. }
  108. private void CheckJobEmployees()
  109. {
  110. if (JobEmployees == null)
  111. {
  112. if (JobID != Guid.Empty)
  113. {
  114. var filter = new Filter<JobEmployee>(x => x.JobLink.ID).IsEqualTo(JobID);
  115. filter.Ands.Add(new Filter<JobEmployee>(x => x.EmployeeLink.StartDate).IsEqualTo(DateTime.MinValue).Or(x => x.EmployeeLink.StartDate)
  116. .IsLessThanOrEqualTo(DateTime.Today));
  117. filter.Ands.Add(new Filter<JobEmployee>(x => x.EmployeeLink.FinishDate).IsEqualTo(DateTime.MinValue)
  118. .Or(x => x.EmployeeLink.FinishDate).IsGreaterThanOrEqualTo(DateTime.Today));
  119. JobEmployees = new Client<JobEmployee>().Query(filter);
  120. }
  121. else
  122. {
  123. JobEmployees = new CoreTable();
  124. JobEmployees.LoadColumns(typeof(JobEmployee));
  125. }
  126. }
  127. }
  128. private void CheckEmployeeQualifications()
  129. {
  130. if (EmployeeQualifications == null)
  131. {
  132. var emps = new List<Guid>();
  133. foreach (var row in JobEmployees.Rows)
  134. {
  135. var emp = row.Get<JobEmployee, Guid>(x => x.EmployeeLink.ID);
  136. if (!emps.Contains(emp))
  137. emps.Add(emp);
  138. }
  139. if (emps.Any())
  140. {
  141. EmployeeQualifications =
  142. new Client<EmployeeQualification>().Query(new Filter<EmployeeQualification>(x => x.Employee.ID).InList(emps.ToArray()));
  143. }
  144. else
  145. {
  146. EmployeeQualifications = new CoreTable();
  147. EmployeeQualifications.LoadColumns(typeof(EmployeeQualification));
  148. }
  149. }
  150. }
  151. private bool CheckQualification(CoreRow arg)
  152. {
  153. if (arg == null)
  154. {
  155. MessageBox.Show("Please select a qualification to examine!");
  156. return false;
  157. }
  158. var qualid = arg.Get<JobQualification, Guid>(x => x.Qualification.ID);
  159. var messages = new List<string>();
  160. if (warnings.ContainsKey(qualid))
  161. {
  162. messages.Add(string.Format("{0} Summary:\n", arg.Get<JobQualification, string>(x => x.Qualification.Description)));
  163. foreach (var message in warnings[qualid])
  164. messages.Add("- " + message);
  165. MessageBox.Show(string.Join("\n", messages));
  166. }
  167. else
  168. {
  169. MessageBox.Show("No data to show!");
  170. }
  171. return false;
  172. }
  173. protected override void Reload(Filters<JobQualification> criteria, Columns<JobQualification> columns, ref SortOrder<JobQualification> sort,
  174. Action<CoreTable, Exception> action)
  175. {
  176. warnings.Clear();
  177. JobEmployees = null;
  178. EmployeeQualifications = null;
  179. base.Reload(criteria, columns, ref sort, action);
  180. }
  181. }