JobEmployeeGrid.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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. using InABox.Wpf;
  13. using System.Threading;
  14. namespace PRSDesktop
  15. {
  16. internal class JobEmployeeGrid : DynamicDataGrid<JobEmployee>, IMasterDetailControl<Job,JobEmployee>
  17. {
  18. private CoreTable EmployeeQualifications;
  19. private CoreTable JobQualifications;
  20. private readonly Dictionary<Guid, Tuple<List<string>, List<string>>> warnings = new();
  21. public Job? Master { get; set; }
  22. public Filter<JobEmployee> MasterDetailFilter => (Master?.ID ?? Guid.Empty) != Guid.Empty
  23. ? new Filter<JobEmployee>(x => x.JobLink.ID).IsEqualTo(Master)
  24. : new Filter<JobEmployee>().None();
  25. protected override void Init()
  26. {
  27. base.Init();
  28. HiddenColumns.Add(x => x.EmployeeLink.ID);
  29. HiddenColumns.Add(x => x.JobLink.ID);
  30. ActionColumns.Add(new DynamicImageColumn(QualificationImage) { Position = DynamicActionColumnPosition.Start, ToolTip = QualificationIssues});
  31. AddButton("Edit Employee", PRSDesktop.Resources.employee.AsBitmapImage(), EditEmployee);
  32. }
  33. protected override void DoReconfigure(DynamicGridOptions options)
  34. {
  35. base.DoReconfigure(options);
  36. options.MultiSelect = true;
  37. options.SelectColumns = true;
  38. }
  39. private bool EditEmployee(Button arg1, CoreRow[] rows)
  40. {
  41. if (rows?.Length == 1)
  42. {
  43. var emps = new Client<Employee>().Query(
  44. new Filter<Employee>(x => x.ID).IsEqualTo(rows.First().Get<JobEmployee, Guid>(x => x.EmployeeLink.ID)));
  45. if (emps.Rows.Count == 1)
  46. {
  47. var emp = emps.Rows.First().ToObject<Employee>();
  48. var grid = new EmployeeGrid();
  49. return grid.EditItems(new[] { emp });
  50. }
  51. MessageBox.Show("Unable to Load Employee Card");
  52. }
  53. else
  54. {
  55. MessageBox.Show("Please select a single employee to edit!");
  56. }
  57. return false;
  58. }
  59. private BitmapImage QualificationImage(CoreRow arg)
  60. {
  61. BitmapImage result = null;
  62. CheckEmployeeQualifications();
  63. CheckJobQualifications();
  64. var empid = arg != null ? arg.Get<JobEmployee, Guid>(x => x.EmployeeLink.ID) : Guid.Empty;
  65. var erows = EmployeeQualifications.Rows.Where(r => r.Get<EmployeeQualification, Guid>(c => c.Employee.ID).Equals(empid));
  66. var CheckedQualifications = new List<Guid>();
  67. // Check Job Requirements
  68. var jrows = JobQualifications.Rows.Where(r => r.Get<JobQualification, bool>(x => x.Required).Equals(true));
  69. foreach (var jrow in jrows)
  70. {
  71. var qualificationid = jrow.Get<JobQualification, Guid>(x => x.Qualification.ID);
  72. var erow = erows.FirstOrDefault(r =>
  73. r.Get<EmployeeQualification, Guid>(c => c.Employee.ID).Equals(empid) &&
  74. r.Get<EmployeeQualification, Guid>(c => c.Qualification.ID).Equals(qualificationid));
  75. if (erow == null)
  76. {
  77. Warn(empid,
  78. string.Format("{0} is missing",
  79. jrow.Get<JobQualification, string>(c => c.Qualification.Description)
  80. ),
  81. ""
  82. );
  83. result = PRSDesktop.Resources.disabled.AsBitmapImage();
  84. }
  85. else
  86. {
  87. var expiry = erow.Get<EmployeeQualification, DateTime>(c => c.Expiry);
  88. var permanent = erow.Get<EmployeeQualification, QualificationRenewal>(c => c.Qualification.Renewal) == QualificationRenewal.Permanent;
  89. if (!permanent && expiry < DateTime.Today)
  90. {
  91. Warn(empid,
  92. string.Format("{0} expired on {1: dd MMM yy}",
  93. jrow.Get<JobQualification, string>(c => c.Qualification.Description),
  94. expiry
  95. ),
  96. ""
  97. );
  98. result = PRSDesktop.Resources.disabled.AsBitmapImage();
  99. }
  100. else if (!permanent && expiry < DateTime.MaxValue.Date)
  101. {
  102. Warn(empid,
  103. string.Format("{0} is valid until {1:dd MMM yy}",
  104. jrow.Get<JobQualification, string>(c => c.Qualification.Description),
  105. erow.Get<EmployeeQualification, DateTime>(c => c.Expiry)
  106. ),
  107. ""
  108. );
  109. }
  110. else
  111. {
  112. Warn(empid,
  113. string.Format("{0} is valid",
  114. jrow.Get<JobQualification, string>(c => c.Qualification.Description)
  115. ),
  116. ""
  117. );
  118. }
  119. }
  120. CheckedQualifications.Add(erow.Get<EmployeeQualification, Guid>(x => x.ID));
  121. }
  122. // Check Qualification Expiry
  123. foreach (var erow in erows)
  124. if (!CheckedQualifications.Contains(erow.Get<EmployeeQualification, Guid>(x => x.ID)))
  125. {
  126. var expiry = erow.Get<EmployeeQualification, DateTime>(x => x.Expiry);
  127. var permanent = erow.Get<EmployeeQualification, QualificationRenewal>(c => c.Qualification.Renewal) == QualificationRenewal.Permanent;
  128. if (!permanent && expiry < DateTime.Today)
  129. {
  130. Warn(empid,
  131. "",
  132. string.Format("{0} has expired",
  133. erow.Get<EmployeeQualification, string>(c => c.Qualification.Description)
  134. )
  135. );
  136. if (result == null)
  137. result = PRSDesktop.Resources.warning.AsBitmapImage();
  138. }
  139. else if (!permanent && expiry < DateTime.MaxValue.Date)
  140. {
  141. Warn(empid,
  142. "",
  143. string.Format("{0} is valid until {1:dd MMM yy}",
  144. erow.Get<JobQualification, string>(c => c.Qualification.Description),
  145. expiry
  146. )
  147. );
  148. }
  149. else
  150. {
  151. Warn(empid,
  152. "",
  153. string.Format("{0} is valid",
  154. erow.Get<JobQualification, string>(c => c.Qualification.Description)
  155. )
  156. );
  157. }
  158. }
  159. if (result == null && erows.Any())
  160. result = PRSDesktop.Resources.tick.AsBitmapImage();
  161. return result;
  162. }
  163. private FrameworkElement? QualificationIssues(DynamicActionColumn column, CoreRow? row)
  164. {
  165. var empid = row != null ? row.Get<JobEmployee, Guid>(x => x.EmployeeLink.ID) : Guid.Empty;
  166. if (warnings.ContainsKey(empid))
  167. {
  168. var summary = new List<string>();
  169. if (warnings[empid].Item1.Any())
  170. {
  171. summary.Add("Required Qualifications:");
  172. foreach (var warning in warnings[empid].Item1)
  173. summary.Add("- " + warning);
  174. summary.Add("");
  175. }
  176. if (warnings[empid].Item2.Any())
  177. {
  178. summary.Add("Other Qualifications:");
  179. foreach (var warning in warnings[empid].Item2)
  180. summary.Add("- " + warning);
  181. }
  182. if (summary.Any())
  183. return column.TextToolTip(string.Join("\n", summary));
  184. }
  185. return null;
  186. }
  187. private void CheckJobQualifications()
  188. {
  189. if (JobQualifications == null)
  190. {
  191. if ((Master?.ID ?? Guid.Empty) != Guid.Empty)
  192. {
  193. JobQualifications = new Client<JobQualification>().Query(new Filter<JobQualification>(x => x.Job.ID).IsEqualTo(Master.ID));
  194. }
  195. else
  196. {
  197. JobQualifications = new CoreTable();
  198. JobQualifications.LoadColumns(typeof(JobQualification));
  199. }
  200. }
  201. }
  202. private void CheckEmployeeQualifications()
  203. {
  204. if (EmployeeQualifications == null)
  205. {
  206. var emps = new List<Guid>();
  207. foreach (var row in Data.Rows)
  208. {
  209. var emp = row.Get<JobEmployee, Guid>(x => x.EmployeeLink.ID);
  210. if (!emps.Contains(emp))
  211. emps.Add(emp);
  212. }
  213. if (emps.Any())
  214. {
  215. EmployeeQualifications =
  216. new Client<EmployeeQualification>().Query(new Filter<EmployeeQualification>(x => x.Employee.ID).InList(emps.ToArray()));
  217. }
  218. else
  219. {
  220. EmployeeQualifications = new CoreTable();
  221. EmployeeQualifications.LoadColumns(typeof(EmployeeQualification));
  222. }
  223. }
  224. }
  225. private bool QualificationsCheck(CoreRow arg)
  226. {
  227. return false;
  228. }
  229. private void Warn(Guid empid, string required, string optional)
  230. {
  231. if (!warnings.ContainsKey(empid))
  232. warnings[empid] = new Tuple<List<string>, List<string>>(new List<string>(), new List<string>());
  233. if (!string.IsNullOrWhiteSpace(required))
  234. warnings[empid].Item1.Add(required);
  235. if (!string.IsNullOrWhiteSpace(optional))
  236. warnings[empid].Item2.Add(optional);
  237. }
  238. protected override void Reload(
  239. Filters<JobEmployee> criteria, Columns<JobEmployee> columns, ref SortOrder<JobEmployee>? sort,
  240. CancellationToken token, Action<CoreTable?, Exception?> action)
  241. {
  242. EmployeeQualifications = null;
  243. JobQualifications = null;
  244. warnings.Clear();
  245. criteria.Add(
  246. MasterDetailFilter
  247. .And(new Filter<JobEmployee>(x => x.EmployeeLink.StartDate).IsEqualTo(DateTime.MinValue)
  248. .Or(x => x.EmployeeLink.StartDate).IsLessThanOrEqualTo(DateTime.Today))
  249. .And(new Filter<JobEmployee>(x => x.EmployeeLink.FinishDate).IsEqualTo(DateTime.MinValue)
  250. .Or(x => x.EmployeeLink.FinishDate).IsGreaterThanOrEqualTo(DateTime.Today))
  251. );
  252. base.Reload(criteria, columns, ref sort, token, action);
  253. }
  254. protected override bool CanCreateItems()
  255. {
  256. return base.CanCreateItems() && (Master?.ID ?? Guid.Empty) != Guid.Empty;
  257. }
  258. protected override void DoAdd(bool OpenEditorOnDirectEdit = false)
  259. {
  260. var ids = ExtractValues(x => x.EmployeeLink.ID, Selection.All).ToArray();
  261. var msd = new MultiSelectDialog<Employee>(
  262. new Filter<Employee>(x => x.ID).NotInList(ids),
  263. Columns.None<Employee>().Add(x=>x.ID),
  264. true
  265. );
  266. if (msd.ShowDialog() == true)
  267. {
  268. List<JobEmployee> updates = new List<JobEmployee>();
  269. foreach (var row in msd.Data().Rows)
  270. {
  271. var jobemp = new JobEmployee();
  272. jobemp.JobLink.ID = Master?.ID ?? Guid.Empty;
  273. jobemp.JobLink.Synchronise(Master ?? new Job());
  274. jobemp.EmployeeLink.ID = row.Get<Employee, Guid>(x => x.ID);
  275. updates.Add(jobemp);
  276. }
  277. using (new WaitCursor())
  278. {
  279. new Client<JobEmployee>().Save(updates, "Added By Job Employee Screen");
  280. Refresh(false,true);
  281. }
  282. }
  283. }
  284. }
  285. }