JobEmployeeGrid.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Windows;
  5. using System.Windows.Controls;
  6. using System.Windows.Media.Imaging;
  7. using Comal.Classes;
  8. using InABox.Clients;
  9. using InABox.Core;
  10. using InABox.DynamicGrid;
  11. using InABox.WPF;
  12. namespace PRSDesktop
  13. {
  14. internal class JobEmployeeGrid : DynamicManyToManyDataGrid<JobEmployee, Job>, IJobControl
  15. {
  16. private CoreTable EmployeeQualifications;
  17. private CoreTable JobQualifications;
  18. private readonly Dictionary<Guid, Tuple<List<string>, List<string>>> warnings = new();
  19. public JobEmployeeGrid()
  20. {
  21. Options.Add(DynamicGridOption.MultiSelect);
  22. HiddenColumns.Add(x => x.EmployeeLink.ID);
  23. HiddenColumns.Add(x => x.JobLink.ID);
  24. ActionColumns.Add(new DynamicActionColumn(QualificationImage, QualificationsCheck) { Position = DynamicActionColumnPosition.Start });
  25. AddButton("Edit Employee", PRSDesktop.Resources.employee.AsBitmapImage(), EditEmployee);
  26. }
  27. public Guid JobID
  28. {
  29. get => ID;
  30. set => ID = value;
  31. }
  32. private bool EditEmployee(Button arg1, CoreRow[] arg2)
  33. {
  34. if (arg2 != null && arg2.Length == 1)
  35. {
  36. var emps = new Client<Employee>().Query(
  37. new Filter<Employee>(x => x.ID).IsEqualTo(arg2.First().Get<JobEmployee, Guid>(x => x.EmployeeLink.ID)));
  38. if (emps.Rows.Count == 1)
  39. {
  40. var emp = emps.Rows.First().ToObject<Employee>();
  41. var grid = new EmployeeGrid();
  42. return grid.EditItems(new[] { emp });
  43. }
  44. MessageBox.Show("Unable to Load Employee Card");
  45. }
  46. else
  47. {
  48. MessageBox.Show("Please select a single employee to edit!");
  49. }
  50. return false;
  51. }
  52. private BitmapImage QualificationImage(CoreRow arg)
  53. {
  54. BitmapImage result = null;
  55. CheckEmployeeQualifications();
  56. CheckJobQualifications();
  57. var empid = arg != null ? arg.Get<JobEmployee, Guid>(x => x.EmployeeLink.ID) : Guid.Empty;
  58. var erows = EmployeeQualifications.Rows.Where(r => r.Get<EmployeeQualification, Guid>(c => c.Employee.ID).Equals(empid));
  59. var CheckedQualifications = new List<Guid>();
  60. // Check Job Requirements
  61. var jrows = JobQualifications.Rows.Where(r => r.Get<JobQualification, bool>(x => x.Required).Equals(true));
  62. foreach (var jrow in jrows)
  63. {
  64. var qualificationid = jrow.Get<JobQualification, Guid>(x => x.Qualification.ID);
  65. var erow = erows.FirstOrDefault(r =>
  66. r.Get<EmployeeQualification, Guid>(c => c.Employee.ID).Equals(empid) &&
  67. r.Get<EmployeeQualification, Guid>(c => c.Qualification.ID).Equals(qualificationid));
  68. if (erow == null)
  69. {
  70. Warn(empid,
  71. string.Format("{0} is missing",
  72. jrow.Get<JobQualification, string>(c => c.Qualification.Description)
  73. ),
  74. ""
  75. );
  76. result = PRSDesktop.Resources.disabled.AsBitmapImage();
  77. }
  78. else
  79. {
  80. var expiry = erow.Get<EmployeeQualification, DateTime>(c => c.Expiry);
  81. var permanent = erow.Get<EmployeeQualification, QualificationRenewal>(c => c.Qualification.Renewal) == QualificationRenewal.Permanent;
  82. if (!permanent && expiry < DateTime.Today)
  83. {
  84. Warn(empid,
  85. string.Format("{0} expired on {1: dd MMM yy}",
  86. jrow.Get<JobQualification, string>(c => c.Qualification.Description),
  87. expiry
  88. ),
  89. ""
  90. );
  91. result = PRSDesktop.Resources.disabled.AsBitmapImage();
  92. }
  93. else if (!permanent && expiry < DateTime.MaxValue.Date)
  94. {
  95. Warn(empid,
  96. string.Format("{0} is valid until {1:dd MMM yy}",
  97. jrow.Get<JobQualification, string>(c => c.Qualification.Description),
  98. erow.Get<EmployeeQualification, DateTime>(c => c.Expiry)
  99. ),
  100. ""
  101. );
  102. }
  103. else
  104. {
  105. Warn(empid,
  106. string.Format("{0} is valid",
  107. jrow.Get<JobQualification, string>(c => c.Qualification.Description)
  108. ),
  109. ""
  110. );
  111. }
  112. }
  113. CheckedQualifications.Add(erow.Get<EmployeeQualification, Guid>(x => x.ID));
  114. }
  115. // Check Qualification Expiry
  116. foreach (var erow in erows)
  117. if (!CheckedQualifications.Contains(erow.Get<EmployeeQualification, Guid>(x => x.ID)))
  118. {
  119. var expiry = erow.Get<EmployeeQualification, DateTime>(x => x.Expiry);
  120. var permanent = erow.Get<EmployeeQualification, QualificationRenewal>(c => c.Qualification.Renewal) == QualificationRenewal.Permanent;
  121. if (!permanent && expiry < DateTime.Today)
  122. {
  123. Warn(empid,
  124. "",
  125. string.Format("{0} has expired",
  126. erow.Get<EmployeeQualification, string>(c => c.Qualification.Description)
  127. )
  128. );
  129. if (result == null)
  130. result = PRSDesktop.Resources.warning.AsBitmapImage();
  131. }
  132. else if (!permanent && expiry < DateTime.MaxValue.Date)
  133. {
  134. Warn(empid,
  135. "",
  136. string.Format("{0} is valid until {1:dd MMM yy}",
  137. erow.Get<JobQualification, string>(c => c.Qualification.Description),
  138. expiry
  139. )
  140. );
  141. }
  142. else
  143. {
  144. Warn(empid,
  145. "",
  146. string.Format("{0} is valid",
  147. erow.Get<JobQualification, string>(c => c.Qualification.Description)
  148. )
  149. );
  150. }
  151. }
  152. if (result == null && erows.Any())
  153. result = PRSDesktop.Resources.tick.AsBitmapImage();
  154. return result;
  155. }
  156. private void CheckJobQualifications()
  157. {
  158. if (JobQualifications == null)
  159. {
  160. if (JobID != Guid.Empty)
  161. {
  162. JobQualifications = new Client<JobQualification>().Query(new Filter<JobQualification>(x => x.Job.ID).IsEqualTo(JobID));
  163. }
  164. else
  165. {
  166. JobQualifications = new CoreTable();
  167. JobQualifications.LoadColumns(typeof(JobQualification));
  168. }
  169. }
  170. }
  171. private void CheckEmployeeQualifications()
  172. {
  173. if (EmployeeQualifications == null)
  174. {
  175. var emps = new List<Guid>();
  176. foreach (var row in Data.Rows)
  177. {
  178. var emp = row.Get<JobEmployee, Guid>(x => x.EmployeeLink.ID);
  179. if (!emps.Contains(emp))
  180. emps.Add(emp);
  181. }
  182. if (emps.Any())
  183. {
  184. EmployeeQualifications =
  185. new Client<EmployeeQualification>().Query(new Filter<EmployeeQualification>(x => x.Employee.ID).InList(emps.ToArray()));
  186. }
  187. else
  188. {
  189. EmployeeQualifications = new CoreTable();
  190. EmployeeQualifications.LoadColumns(typeof(EmployeeQualification));
  191. }
  192. }
  193. }
  194. private bool QualificationsCheck(CoreRow arg)
  195. {
  196. var empid = arg != null ? arg.Get<JobEmployee, Guid>(x => x.EmployeeLink.ID) : Guid.Empty;
  197. if (warnings.ContainsKey(empid))
  198. {
  199. var summary = new List<string>();
  200. if (warnings[empid].Item1.Any())
  201. {
  202. summary.Add("Required Qualifications:");
  203. foreach (var warning in warnings[empid].Item1)
  204. summary.Add("- " + warning);
  205. summary.Add("");
  206. }
  207. if (warnings[empid].Item2.Any())
  208. {
  209. summary.Add("Other Qualifications:");
  210. foreach (var warning in warnings[empid].Item2)
  211. summary.Add("- " + warning);
  212. }
  213. if (summary.Any())
  214. MessageBox.Show(string.Join("\n", summary));
  215. else
  216. MessageBox.Show("No job-specific qualifications found for " + arg.Get<JobEmployee, string>(x => x.EmployeeLink.Name));
  217. }
  218. else
  219. {
  220. MessageBox.Show("No job-specific qualifications found for " + arg.Get<JobEmployee, string>(x => x.EmployeeLink.Name));
  221. }
  222. return false;
  223. }
  224. private void Warn(Guid empid, string required, string optional)
  225. {
  226. if (!warnings.ContainsKey(empid))
  227. warnings[empid] = new Tuple<List<string>, List<string>>(new List<string>(), new List<string>());
  228. if (!string.IsNullOrWhiteSpace(required))
  229. warnings[empid].Item1.Add(required);
  230. if (!string.IsNullOrWhiteSpace(optional))
  231. warnings[empid].Item2.Add(optional);
  232. }
  233. protected override void Reload(Filters<JobEmployee> criteria, Columns<JobEmployee> columns, ref SortOrder<JobEmployee> sort,
  234. Action<CoreTable, Exception> action)
  235. {
  236. EmployeeQualifications = null;
  237. JobQualifications = null;
  238. warnings.Clear();
  239. criteria.Add(new Filter<JobEmployee>(x => x.JobLink.ID).IsEqualTo(JobID));
  240. criteria.Add(new Filter<JobEmployee>(x => x.EmployeeLink.StartDate).IsEqualTo(DateTime.MinValue).Or(x => x.EmployeeLink.StartDate)
  241. .IsLessThanOrEqualTo(DateTime.Today));
  242. criteria.Add(new Filter<JobEmployee>(x => x.EmployeeLink.FinishDate).IsEqualTo(DateTime.MinValue).Or(x => x.EmployeeLink.FinishDate)
  243. .IsGreaterThanOrEqualTo(DateTime.Today));
  244. base.Reload(criteria, columns, ref sort, action);
  245. }
  246. }
  247. }