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