JobEmployeeGrid.cs 11 KB

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