JobRequisitionGrid.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Threading.Tasks;
  5. using System.Windows;
  6. using System.Windows.Controls;
  7. using com.healthmarketscience.jackcess;
  8. using Comal.Classes;
  9. using InABox.Clients;
  10. using InABox.Core;
  11. using InABox.DynamicGrid;
  12. using InABox.WPF;
  13. using NPOI.SS.Formula.Functions;
  14. namespace PRSDesktop
  15. {
  16. internal class JobRequisitionGrid : DynamicDataGrid<JobRequisition>, IJobControl
  17. {
  18. public Job Job { get; set; }
  19. public JobPanelSettings Settings { get; set; }
  20. private readonly Button _approve;
  21. private readonly Button _createtask;
  22. public JobRequisitionGrid()
  23. {
  24. HiddenColumns.Add(x => x.ID);
  25. HiddenColumns.Add(x => x.Job.ID);
  26. HiddenColumns.Add(x => x.Approved);
  27. _approve = AddButton("Approve", null, ApproveClick);
  28. _createtask = AddButton("Create Task", null, CreateTask);
  29. }
  30. protected override void DoReconfigure(FluentList<DynamicGridOption> options)
  31. {
  32. base.DoReconfigure(options);
  33. options.AddRange(
  34. DynamicGridOption.SelectColumns,
  35. DynamicGridOption.FilterRows
  36. );
  37. }
  38. private bool ApproveClick(Button button, CoreRow[] rows)
  39. {
  40. if (rows == null || !rows.Any())
  41. {
  42. MessageBox.Show("Please select a row first!");
  43. return false;
  44. }
  45. var requi = rows[0].ToObject<JobRequisition>();
  46. requi.Approved = requi.Approved.IsEmpty() ? DateTime.Now : DateTime.MinValue;
  47. UpdateRow<DateTime?>(rows[0], "Approved", requi.Approved.IsEmpty() ? null : requi.Approved, true);
  48. new Client<JobRequisition>().Save(requi, requi.Approved.IsEmpty() ? "Cleared Approval" : "Marked as Approved");
  49. UpdateButton(_approve, null, !requi.Approved.IsEmpty() ? "Unapprove" : "Approve");
  50. _createtask.IsEnabled = !requi.Approved.IsEmpty();
  51. return false;
  52. }
  53. protected override void SelectItems(CoreRow[] rows)
  54. {
  55. base.SelectItems(rows);
  56. if (rows?.Length == 1)
  57. {
  58. _approve.Visibility = Security.IsAllowed<CanApproveJobRequisitions>()
  59. ? Visibility.Visible
  60. : Visibility.Collapsed;
  61. UpdateButton(_approve, null, !rows[0].Get<JobRequisition, DateTime>(c => c.Approved).IsEmpty() ? "Unapprove" : "Approve");
  62. _createtask.Visibility = Visibility.Visible;
  63. _createtask.IsEnabled = !rows[0].Get<JobRequisition, DateTime>(c => c.Approved).IsEmpty();
  64. }
  65. else
  66. {
  67. _approve.Visibility = Visibility.Collapsed;
  68. _createtask.Visibility = Visibility.Collapsed;
  69. }
  70. }
  71. private bool CreateTask(System.Windows.Controls.Button btn, CoreRow[] rows)
  72. {
  73. if (!rows.Any())
  74. {
  75. MessageBox.Show("Please select at least one row to add to Task!");
  76. return false;
  77. }
  78. Progress.Show("Working");
  79. JobRequisition jobrequi = rows.FirstOrDefault().ToObject<JobRequisition>();
  80. Kanban kanban = new Kanban();
  81. kanban.ManagerLink.ID = Guid.Parse(new Client<Employee>().Query(new Filter<Employee>(x => x.UserLink.UserID).IsEqualTo(ClientFactory.UserID), new Columns<Employee>(x => x.ID)).Rows.FirstOrDefault().Values[0].ToString());
  82. string jobnumber = new Client<Job>().Query(new Filter<Job>(x => x.ID).IsEqualTo(Job.ID), new Columns<Job>(x => x.JobNumber)).Rows.FirstOrDefault().Values[0].ToString();
  83. kanban.JobLink.ID = Job.ID;
  84. kanban.JobLink.Synchronise(Job);
  85. kanban.Title = "New Job Requisition Raised for Ordering (Job " + jobnumber + " (" + jobrequi.Number + ")" + ")";
  86. kanban.Category = "Open";
  87. kanban.Description = "Requisition number: " + jobnumber + " (" + jobrequi.Number + "):" + System.Environment.NewLine;
  88. CoreTable table = new Client<JobRequisitionItem>().Query
  89. (
  90. new Filter<JobRequisitionItem>(x => x.Requisition.ID).IsEqualTo(jobrequi.ID),
  91. new Columns<JobRequisitionItem>(x => x.ID)
  92. );
  93. if (table.Rows.Any())
  94. {
  95. kanban.Description = kanban.Description + "Requisition contains " + table.Rows.Count + " items.";
  96. }
  97. Progress.Close();
  98. var page = new TaskGrid();
  99. page.MyID = App.EmployeeID;
  100. page.OnAfterSave += (form, items) =>
  101. {
  102. KanbanAfterSave(form, items.Cast<Kanban>().ToArray());
  103. };
  104. return page.EditItems(new[] { kanban });
  105. }
  106. private void KanbanAfterSave(IDynamicEditorForm form, Kanban[] items)
  107. {
  108. Progress.Show("Saving documents");
  109. JobRequisition jobrequi = SelectedRows.FirstOrDefault().ToObject<JobRequisition>();
  110. CoreTable table = new Client<JobRequisitionDocument>().Query(new Filter<JobRequisitionDocument>(x => x.EntityLink.ID).IsEqualTo(jobrequi.ID),
  111. new Columns<JobRequisitionDocument>(x => x.DocumentLink.ID, x => x.DocumentLink.FileName));
  112. if (table.Rows.Any())
  113. {
  114. List<KanbanDocument> docs = new List<KanbanDocument>();
  115. foreach (CoreRow row in table.Rows)
  116. {
  117. KanbanDocument doc = new KanbanDocument();
  118. doc.DocumentLink.ID = Guid.Parse(row.Values[0].ToString());
  119. if (row.Values[1] != null)
  120. doc.DocumentLink.FileName = row.Values[1].ToString();
  121. doc.EntityLink.ID = items[0].ID;
  122. docs.Add(doc);
  123. }
  124. new Client<KanbanDocument>().Save(docs, "Created From Job Requi Screen");
  125. }
  126. Progress.Close();
  127. }
  128. protected override void Reload(Filters<JobRequisition> criteria, Columns<JobRequisition> columns, ref SortOrder<JobRequisition>? sort,
  129. Action<CoreTable?, Exception?> action)
  130. {
  131. if (!Security.IsAllowed<CanApproveJobRequisitions>())
  132. criteria.Add(new Filter<JobRequisition>(x => x.Approved).IsNotEqualTo(DateTime.MinValue));
  133. criteria.Add(new Filter<JobRequisition>(x => x.Job.ID).IsEqualTo(Job.ID));
  134. base.Reload(criteria, columns, ref sort, action);
  135. }
  136. protected override bool CanCreateItems()
  137. {
  138. return Job.ID != Guid.Empty;
  139. }
  140. protected override JobRequisition CreateItem()
  141. {
  142. var result = base.CreateItem();
  143. result.Job.ID = Job.ID;
  144. result.Job.Synchronise(Job);
  145. return result;
  146. }
  147. }
  148. }