JobEmployeeGrid.cs 11 KB

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