JobDocumentSetMilestoneTasks.cs 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading;
  6. using System.Windows;
  7. using System.Windows.Controls;
  8. using System.Windows.Media.Imaging;
  9. using Comal.Classes;
  10. using InABox.Clients;
  11. using InABox.Core;
  12. using InABox.DynamicGrid;
  13. using InABox.Wpf;
  14. using InABox.WPF;
  15. namespace PRSDesktop;
  16. public class JobDocumentSetMilestoneTasks : DynamicDataGrid<JobDocumentSetMileStoneKanban>
  17. {
  18. public Guid MileStoneID { get; set; }
  19. public Guid JobID { get; set; }
  20. public JobDocumentSetSettings Settings { get; set; }
  21. protected override void DoReconfigure(DynamicGridOptions options)
  22. {
  23. base.DoReconfigure(options);
  24. options.Clear();
  25. options.RecordCount = true;
  26. }
  27. protected override void Init()
  28. {
  29. base.Init();
  30. HiddenColumns.Add(x => x.Kanban.ID);
  31. HiddenColumns.Add(x => x.Kanban.Summary);
  32. HiddenColumns.Add(x => x.Entity.DocumentSet.Code);
  33. HiddenColumns.Add(x => x.Entity.Type.Code);
  34. HiddenColumns.Add(x => x.Entity.Revision);
  35. // HiddenColumns.Add(x => x.Kanban.Completed);
  36. // ActionColumns.Add(new DynamicImageColumn(StatusImage));
  37. ActionColumns.Add(new DynamicMenuColumn(TaskMenuOpening));
  38. AddButton("", InABox.Wpf.Resources.add.AsBitmapImage(), AddTask);
  39. AddButton("", InABox.Wpf.Resources.pencil.AsBitmapImage(), EditTask);
  40. AddButton("", InABox.Wpf.Resources.delete.AsBitmapImage(), DeleteTask, DynamicGridButtonPosition.Right);
  41. }
  42. private void TaskMenuOpening(DynamicMenuColumn column, CoreRow? row)
  43. {
  44. var status = column.AddItem("Set Status", PRSDesktop.Resources.kanban, null);
  45. column.AddItem("To Do", null, (r) => SetTaskStatus(r, KanbanStatus.Open,false), status);
  46. column.AddItem("In Progress", null, (r) => SetTaskStatus(r, KanbanStatus.InProgress,false), status);
  47. column.AddItem("Waiting", null, (r) => SetTaskStatus(r, KanbanStatus.Waiting,false), status);
  48. column.AddItem("Complete", null, (r) => SetTaskStatus(r, KanbanStatus.Complete,true), status);
  49. column.AddSeparator();
  50. column.AddItem("Email Task", PRSDesktop.Resources.email, EmailTask);
  51. }
  52. private void SetTaskStatus(CoreRow row, KanbanStatus category, bool complete)
  53. {
  54. using (new WaitCursor())
  55. {
  56. var kanbanID = row.Get<JobDocumentSetMileStoneKanban, Guid>(c => c.Kanban.ID);
  57. var kanban = new Client<Kanban>().Load(
  58. new Filter<Kanban>(x => x.ID).IsEqualTo(kanbanID)).FirstOrDefault();
  59. if (kanban == null)
  60. {
  61. MessageWindow.ShowError("Cannot Load Kanban", $"No Kanban with ID {kanbanID}");
  62. return;
  63. }
  64. if (category != kanban.Status)
  65. {
  66. kanban.Status = category;
  67. Client.Save(kanban, "Updated from Job Document Milestone List");
  68. DoChanged();
  69. Refresh(false,true);
  70. }
  71. }
  72. }
  73. private void EmailTask(CoreRow? row)
  74. {
  75. var files = new Client<Document>().Query(
  76. new Filter<Document>(x => x.ID).InQuery(new Filter<JobDocumentSetMileStoneFile>(x => x.EntityLink.ID).IsEqualTo(MileStoneID),
  77. x => x.DocumentLink.ID),
  78. Columns.None<Document>().Add(x=>x.FileName,x=>x.Data)
  79. ).ToDictionary<Document, String, byte[]>(c => c.FileName, c => c.Data);
  80. StringBuilder subject = new StringBuilder();
  81. subject.AppendFormat("{0}: ", row.Get<JobDocumentSetMileStoneKanban, int>(x => x.Kanban.Number));
  82. subject.AppendFormat("{0}", row.Get<JobDocumentSetMileStoneKanban, String>(x => x.Kanban.Title));
  83. StringBuilder body = new StringBuilder();
  84. body.AppendLine("Dear ,");
  85. body.AppendLine();
  86. body.AppendLine("Some information is required regarding the attached documents:");
  87. body.AppendLine();
  88. body.AppendLine(row.Get<JobDocumentSetMileStoneKanban, String>(x => x.Kanban.Summary));
  89. body.AppendLine();
  90. body.AppendLine("Regards,");
  91. body.AppendLine(App.EmployeeName);
  92. EmailUtils.CreateEMLFile(
  93. files,
  94. "",
  95. subject.ToString(),
  96. body.ToString()
  97. );
  98. }
  99. protected override DynamicGridColumns LoadColumns()
  100. {
  101. return new DynamicGridColumns()
  102. {
  103. new DynamicGridColumn()
  104. {
  105. ColumnName = CoreUtils.GetFullPropertyName<JobDocumentSetMileStoneKanban, int>(x => x.Kanban.Number, "."),
  106. Caption = "#",
  107. Width = 50,
  108. Alignment = Alignment.MiddleCenter
  109. },
  110. new DynamicGridColumn()
  111. {
  112. ColumnName = CoreUtils.GetFullPropertyName<JobDocumentSetMileStoneKanban, String>(x => x.Kanban.Title, "."),
  113. Caption = "Title",
  114. Width = 0
  115. },
  116. new DynamicGridColumn()
  117. {
  118. ColumnName = CoreUtils.GetFullPropertyName<JobDocumentSetMileStoneKanban, KanbanStatus>(x => x.Kanban.Status, "."),
  119. Caption = "Status",
  120. Width = 70,
  121. Alignment = Alignment.MiddleCenter
  122. }
  123. };
  124. }
  125. // private readonly BitmapImage _status = PRSDesktop.Resources.tick.AsBitmapImage();
  126. //
  127. // private BitmapImage? StatusImage(CoreRow? arg)
  128. // {
  129. // return arg == null
  130. // ? _status
  131. // : arg.Get<JobDocumentSetMileStoneKanban, DateTime>(c => c.Kanban.Completed).IsEmpty()
  132. // ? null
  133. // : _status;
  134. // }
  135. private bool AddTask(Button arg1, CoreRow[] arg2)
  136. {
  137. if (MileStoneID == Guid.Empty)
  138. return false;
  139. var kg = DynamicGridUtils.CreateDynamicGrid(typeof(DynamicDataGrid<>), typeof(Kanban));
  140. kg.OnAfterSave += (editor, items) =>
  141. {
  142. List<JobDocumentSetMileStoneKanban> msks = new List<JobDocumentSetMileStoneKanban>();
  143. foreach (Kanban item in items.Cast<Kanban>())
  144. {
  145. JobDocumentSetMileStoneKanban msk = new JobDocumentSetMileStoneKanban();
  146. msk.Kanban.ID = item.ID;
  147. msk.Entity.ID = MileStoneID;
  148. msks.Add(msk);
  149. }
  150. new Client<JobDocumentSetMileStoneKanban>().Save(msks, "");
  151. };
  152. var kanban = new Kanban();
  153. kanban.EmployeeLink.ID = App.EmployeeID;
  154. kanban.ManagerLink.ID = App.EmployeeID;
  155. kanban.JobLink.ID = JobID;
  156. kanban.Status = KanbanStatus.Open;
  157. kanban.Type.ID = Settings?.DocumentMilestoneKanbanType.ID ?? Guid.Empty;
  158. var milestone = new Client<JobDocumentSetMileStone>().Query(
  159. new Filter<JobDocumentSetMileStone>(x => x.ID).IsEqualTo(MileStoneID),
  160. Columns.None<JobDocumentSetMileStone>().Add(x => x.DocumentSet.Code).Add(x => x.Type.Code).Add(x => x.Revision)
  161. ).ToTuples<JobDocumentSetMileStone, String, String, String>(x => x.DocumentSet.Code, x => x.Type.Code, x => x.Revision).FirstOrDefault();
  162. if (milestone != null)
  163. {
  164. kanban.Title = String.Format(
  165. "{0}: {1} ({2}{3})",
  166. Settings?.DocumentMilestoneKanbanType.Code,
  167. milestone.Item1,
  168. milestone.Item2,
  169. !String.IsNullOrWhiteSpace(milestone.Item3) ? $" Rev {milestone.Item3}" : ""
  170. );
  171. }
  172. if (kg.EditItems(new Kanban[] { kanban }) == true)
  173. {
  174. DoChanged();
  175. return true;
  176. }
  177. return false;
  178. }
  179. private bool EditTask(Button arg1, CoreRow[] arg2)
  180. {
  181. if (MileStoneID == Guid.Empty)
  182. return false;
  183. if (!SelectedRows.Any())
  184. return false;
  185. var ids = SelectedRows.ToArray().Select(r=>r.Get<JobDocumentSetMileStoneKanban,Guid>(x=>x.Kanban.ID)).ToArray();
  186. var kanbans = new Client<Kanban>().Load(new Filter<Kanban>(x => x.ID).InList(ids));
  187. var kg = DynamicGridUtils.CreateDynamicGrid(typeof(DynamicDataGrid<>), typeof(Kanban));
  188. if (kg.EditItems(kanbans) == true)
  189. {
  190. DoChanged();
  191. return true;
  192. }
  193. return false;
  194. }
  195. private bool DeleteTask(Button arg1, CoreRow[] arg2)
  196. {
  197. if (MileStoneID == Guid.Empty)
  198. return false;
  199. if (!SelectedRows.Any())
  200. return false;
  201. if (MessageBox.Show("This will unlink the selected task from this milestone, but leave the task active.\n\nDo You wish to continue?",
  202. "Unlink Task", MessageBoxButton.YesNo) != MessageBoxResult.Yes)
  203. return false;
  204. var ids = SelectedRows.ToArray().Select(r=>r.Get<JobDocumentSetMileStoneKanban,Guid>(x=>x.ID)).ToArray();
  205. var msks = ids.Select(x => new JobDocumentSetMileStoneKanban() { ID = x }).ToArray();
  206. new Client<JobDocumentSetMileStoneKanban>().Delete(msks, "Removed from Meeting");
  207. DoChanged();
  208. return true;
  209. }
  210. protected override void Reload(
  211. Filters<JobDocumentSetMileStoneKanban> criteria, Columns<JobDocumentSetMileStoneKanban> columns, ref SortOrder<JobDocumentSetMileStoneKanban>? sort,
  212. CancellationToken token, Action<CoreTable?, Exception?> action)
  213. {
  214. criteria.Add(MileStoneID == Guid.Empty
  215. ? new Filter<JobDocumentSetMileStoneKanban>(x=>x.Entity.ID).None()
  216. : new Filter<JobDocumentSetMileStoneKanban>(x => x.Entity.ID).IsEqualTo(MileStoneID));
  217. base.Reload(criteria, columns, ref sort, token, action);
  218. }
  219. }