JobEmployeeGrid.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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 : DynamicDataGrid<JobEmployee>, IJobControl
  15. {
  16. private CoreTable EmployeeQualifications;
  17. private CoreTable JobQualifications;
  18. private readonly Dictionary<Guid, Tuple<List<string>, List<string>>> warnings = new();
  19. public Job Job { get; set; }
  20. public JobPanelSettings Settings { get; set; }
  21. protected override void Init()
  22. {
  23. base.Init();
  24. HiddenColumns.Add(x => x.EmployeeLink.ID);
  25. HiddenColumns.Add(x => x.JobLink.ID);
  26. ActionColumns.Add(new DynamicImageColumn(QualificationImage) { Position = DynamicActionColumnPosition.Start, ToolTip = QualificationIssues});
  27. AddButton("Edit Employee", PRSDesktop.Resources.employee.AsBitmapImage(), EditEmployee);
  28. }
  29. protected override void DoReconfigure(FluentList<DynamicGridOption> options)
  30. {
  31. base.DoReconfigure(options);
  32. options
  33. .BeginUpdate()
  34. .Add(DynamicGridOption.MultiSelect)
  35. .Add(DynamicGridOption.SelectColumns)
  36. .EndUpdate();
  37. }
  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 FrameworkElement? QualificationIssues(DynamicActionColumn column, CoreRow? row)
  163. {
  164. var empid = row != null ? row.Get<JobEmployee, Guid>(x => x.EmployeeLink.ID) : Guid.Empty;
  165. if (warnings.ContainsKey(empid))
  166. {
  167. var summary = new List<string>();
  168. if (warnings[empid].Item1.Any())
  169. {
  170. summary.Add("Required Qualifications:");
  171. foreach (var warning in warnings[empid].Item1)
  172. summary.Add("- " + warning);
  173. summary.Add("");
  174. }
  175. if (warnings[empid].Item2.Any())
  176. {
  177. summary.Add("Other Qualifications:");
  178. foreach (var warning in warnings[empid].Item2)
  179. summary.Add("- " + warning);
  180. }
  181. if (summary.Any())
  182. return column.TextToolTip(string.Join("\n", summary));
  183. }
  184. return null;
  185. }
  186. private void CheckJobQualifications()
  187. {
  188. if (JobQualifications == null)
  189. {
  190. if (Job.ID != Guid.Empty)
  191. {
  192. JobQualifications = new Client<JobQualification>().Query(new Filter<JobQualification>(x => x.Job.ID).IsEqualTo(Job.ID));
  193. }
  194. else
  195. {
  196. JobQualifications = new CoreTable();
  197. JobQualifications.LoadColumns(typeof(JobQualification));
  198. }
  199. }
  200. }
  201. private void CheckEmployeeQualifications()
  202. {
  203. if (EmployeeQualifications == null)
  204. {
  205. var emps = new List<Guid>();
  206. foreach (var row in Data.Rows)
  207. {
  208. var emp = row.Get<JobEmployee, Guid>(x => x.EmployeeLink.ID);
  209. if (!emps.Contains(emp))
  210. emps.Add(emp);
  211. }
  212. if (emps.Any())
  213. {
  214. EmployeeQualifications =
  215. new Client<EmployeeQualification>().Query(new Filter<EmployeeQualification>(x => x.Employee.ID).InList(emps.ToArray()));
  216. }
  217. else
  218. {
  219. EmployeeQualifications = new CoreTable();
  220. EmployeeQualifications.LoadColumns(typeof(EmployeeQualification));
  221. }
  222. }
  223. }
  224. private bool QualificationsCheck(CoreRow arg)
  225. {
  226. return false;
  227. }
  228. private void Warn(Guid empid, string required, string optional)
  229. {
  230. if (!warnings.ContainsKey(empid))
  231. warnings[empid] = new Tuple<List<string>, List<string>>(new List<string>(), new List<string>());
  232. if (!string.IsNullOrWhiteSpace(required))
  233. warnings[empid].Item1.Add(required);
  234. if (!string.IsNullOrWhiteSpace(optional))
  235. warnings[empid].Item2.Add(optional);
  236. }
  237. protected override void Reload(Filters<JobEmployee> criteria, Columns<JobEmployee> columns, ref SortOrder<JobEmployee>? sort, Action<CoreTable?, Exception?> action)
  238. {
  239. EmployeeQualifications = null;
  240. JobQualifications = null;
  241. warnings.Clear();
  242. criteria.Add(new Filter<JobEmployee>(x => x.JobLink.ID).IsEqualTo(Job.ID));
  243. criteria.Add(new Filter<JobEmployee>(x => x.EmployeeLink.StartDate).IsEqualTo(DateTime.MinValue).Or(x => x.EmployeeLink.StartDate)
  244. .IsLessThanOrEqualTo(DateTime.Today));
  245. criteria.Add(new Filter<JobEmployee>(x => x.EmployeeLink.FinishDate).IsEqualTo(DateTime.MinValue).Or(x => x.EmployeeLink.FinishDate)
  246. .IsGreaterThanOrEqualTo(DateTime.Today));
  247. base.Reload(criteria, columns, ref sort, action);
  248. }
  249. protected override bool CanCreateItems()
  250. {
  251. return Job.ID != Guid.Empty;
  252. }
  253. protected override void DoAdd(bool OpenEditorOnDirectEdit = false)
  254. {
  255. var ids = ExtractValues(x => x.EmployeeLink.ID, Selection.All).ToArray();
  256. var msd = new MultiSelectDialog<Employee>(
  257. new Filter<Employee>(x => x.ID).NotInList(ids),
  258. new Columns<Employee>(x=>x.ID),
  259. true
  260. );
  261. if (msd.ShowDialog() == true)
  262. {
  263. List<JobEmployee> updates = new List<JobEmployee>();
  264. foreach (var row in msd.Data().Rows)
  265. {
  266. var jobemp = new JobEmployee();
  267. jobemp.JobLink.ID = Job.ID;
  268. jobemp.JobLink.Synchronise(Job);
  269. jobemp.EmployeeLink.ID = row.Get<Employee, Guid>(x => x.ID);
  270. updates.Add(jobemp);
  271. }
  272. using (new WaitCursor())
  273. {
  274. new Client<JobEmployee>().Save(updates, "Added By Job Employee Screen");
  275. Refresh(false,true);
  276. }
  277. }
  278. }
  279. }
  280. }