JobQualificationGrid.cs 8.0 KB

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