JobDocumentSetMilestoneTasks.cs 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Windows;
  6. using System.Windows.Controls;
  7. using System.Windows.Media.Imaging;
  8. using Comal.Classes;
  9. using InABox.Clients;
  10. using InABox.Core;
  11. using InABox.DynamicGrid;
  12. using InABox.Wpf;
  13. using InABox.WPF;
  14. namespace PRSDesktop;
  15. public class JobDocumentSetMilestoneTasks : DynamicDataGrid<JobDocumentSetMileStoneKanban>
  16. {
  17. public Guid MileStoneID { get; set; }
  18. public Guid JobID { get; set; }
  19. public JobDocumentSetSettings Settings { get; set; }
  20. protected override void DoReconfigure(FluentList<DynamicGridOption> options)
  21. {
  22. base.DoReconfigure(options);
  23. options.BeginUpdate()
  24. .Clear()
  25. .Add(DynamicGridOption.RecordCount)
  26. .EndUpdate();
  27. }
  28. protected override void Init()
  29. {
  30. base.Init();
  31. HiddenColumns.Add(x => x.Kanban.ID);
  32. HiddenColumns.Add(x => x.Kanban.Summary);
  33. HiddenColumns.Add(x => x.Entity.DocumentSet.Code);
  34. HiddenColumns.Add(x => x.Entity.Type.Code);
  35. HiddenColumns.Add(x => x.Entity.Revision);
  36. // HiddenColumns.Add(x => x.Kanban.Completed);
  37. // ActionColumns.Add(new DynamicImageColumn(StatusImage));
  38. ActionColumns.Add(new DynamicMenuColumn(TaskMenuOpening));
  39. AddButton("", PRSDesktop.Resources.add.AsBitmapImage(), AddTask);
  40. AddButton("", PRSDesktop.Resources.pencil.AsBitmapImage(), EditTask);
  41. AddButton("", PRSDesktop.Resources.delete.AsBitmapImage(), DeleteTask, DynamicGridButtonPosition.Right);
  42. }
  43. private void TaskMenuOpening(DynamicMenuColumn column, CoreRow? row)
  44. {
  45. var status = column.AddItem("Set Status", PRSDesktop.Resources.kanban, null);
  46. column.AddItem("To Do", null, (r) => SetTaskStatus(r, KanbanStatus.Open,false), status);
  47. column.AddItem("In Progress", null, (r) => SetTaskStatus(r, KanbanStatus.InProgress,false), status);
  48. column.AddItem("Waiting", null, (r) => SetTaskStatus(r, KanbanStatus.Waiting,false), status);
  49. column.AddItem("Complete", null, (r) => SetTaskStatus(r, KanbanStatus.Complete,true), status);
  50. column.AddSeparator();
  51. column.AddItem("Email Task", PRSDesktop.Resources.email, EmailTask);
  52. }
  53. private void SetTaskStatus(CoreRow row, KanbanStatus category, bool complete)
  54. {
  55. using (new WaitCursor())
  56. {
  57. var kanbanID = row.Get<JobDocumentSetMileStoneKanban, Guid>(c => c.Kanban.ID);
  58. var kanban = new Client<Kanban>().Load(
  59. new Filter<Kanban>(x => x.ID).IsEqualTo(kanbanID)).FirstOrDefault();
  60. if (kanban == null)
  61. {
  62. MessageWindow.ShowError("Cannot Load Kanban", $"No Kanban with ID {kanbanID}");
  63. return;
  64. }
  65. if (category != kanban.Status)
  66. {
  67. kanban.Status = category;
  68. Client.Save(kanban, "Updated from Job Document Milestone List");
  69. DoChanged();
  70. Refresh(false,true);
  71. }
  72. }
  73. }
  74. private void EmailTask(CoreRow? row)
  75. {
  76. var files = new Client<Document>().Query(
  77. new Filter<Document>(x => x.ID).InQuery(new Filter<JobDocumentSetMileStoneFile>(x => x.EntityLink.ID).IsEqualTo(MileStoneID),
  78. x => x.DocumentLink.ID),
  79. new Columns<Document>(x=>x.FileName,x=>x.Data)
  80. ).ToDictionary<Document, String, byte[]>(c => c.FileName, c => c.Data);
  81. StringBuilder subject = new StringBuilder();
  82. subject.AppendFormat("{0}: ", row.Get<JobDocumentSetMileStoneKanban, int>(x => x.Kanban.Number));
  83. subject.AppendFormat("{0}", row.Get<JobDocumentSetMileStoneKanban, String>(x => x.Kanban.Title));
  84. StringBuilder body = new StringBuilder();
  85. body.AppendLine("Dear ,");
  86. body.AppendLine();
  87. body.AppendLine("Some information is required regarding the attached documents:");
  88. body.AppendLine();
  89. body.AppendLine(row.Get<JobDocumentSetMileStoneKanban, String>(x => x.Kanban.Summary));
  90. body.AppendLine();
  91. body.AppendLine("Regards,");
  92. body.AppendLine(App.EmployeeName);
  93. EmailUtils.CreateEMLFile(
  94. files,
  95. "",
  96. subject.ToString(),
  97. body.ToString()
  98. );
  99. }
  100. protected override DynamicGridColumns LoadColumns()
  101. {
  102. return new DynamicGridColumns()
  103. {
  104. new DynamicGridColumn()
  105. {
  106. ColumnName = CoreUtils.GetFullPropertyName<JobDocumentSetMileStoneKanban, int>(x => x.Kanban.Number, "."),
  107. Caption = "#",
  108. Width = 50,
  109. Alignment = Alignment.MiddleCenter
  110. },
  111. new DynamicGridColumn()
  112. {
  113. ColumnName = CoreUtils.GetFullPropertyName<JobDocumentSetMileStoneKanban, String>(x => x.Kanban.Title, "."),
  114. Caption = "Title",
  115. Width = 0
  116. },
  117. new DynamicGridColumn()
  118. {
  119. ColumnName = CoreUtils.GetFullPropertyName<JobDocumentSetMileStoneKanban, KanbanStatus>(x => x.Kanban.Status, "."),
  120. Caption = "Status",
  121. Width = 70,
  122. Alignment = Alignment.MiddleCenter
  123. }
  124. };
  125. }
  126. // private readonly BitmapImage _status = PRSDesktop.Resources.tick.AsBitmapImage();
  127. //
  128. // private BitmapImage? StatusImage(CoreRow? arg)
  129. // {
  130. // return arg == null
  131. // ? _status
  132. // : arg.Get<JobDocumentSetMileStoneKanban, DateTime>(c => c.Kanban.Completed).IsEmpty()
  133. // ? null
  134. // : _status;
  135. // }
  136. private bool AddTask(Button arg1, CoreRow[] arg2)
  137. {
  138. if (MileStoneID == Guid.Empty)
  139. return false;
  140. var kg = DynamicGridUtils.CreateDynamicGrid(typeof(DynamicDataGrid<>), typeof(Kanban));
  141. kg.OnAfterSave += (editor, items) =>
  142. {
  143. List<JobDocumentSetMileStoneKanban> msks = new List<JobDocumentSetMileStoneKanban>();
  144. foreach (Kanban item in items.Cast<Kanban>())
  145. {
  146. JobDocumentSetMileStoneKanban msk = new JobDocumentSetMileStoneKanban();
  147. msk.Kanban.ID = item.ID;
  148. msk.Entity.ID = MileStoneID;
  149. msks.Add(msk);
  150. }
  151. new Client<JobDocumentSetMileStoneKanban>().Save(msks, "");
  152. };
  153. var kanban = new Kanban();
  154. kanban.EmployeeLink.ID = App.EmployeeID;
  155. kanban.ManagerLink.ID = App.EmployeeID;
  156. kanban.JobLink.ID = JobID;
  157. kanban.Status = KanbanStatus.Open;
  158. kanban.Type.ID = Settings?.DocumentMilestoneKanbanType.ID ?? Guid.Empty;
  159. var milestone = new Client<JobDocumentSetMileStone>().Query(new Filter<JobDocumentSetMileStone>(x => x.ID).IsEqualTo(MileStoneID),
  160. new Columns<JobDocumentSetMileStone>(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(Filters<JobDocumentSetMileStoneKanban> criteria, Columns<JobDocumentSetMileStoneKanban> columns, ref SortOrder<JobDocumentSetMileStoneKanban>? sort, Action<CoreTable?, Exception?> action)
  211. {
  212. criteria.Add(MileStoneID == Guid.Empty
  213. ? new Filter<JobDocumentSetMileStoneKanban>(x=>x.Entity.ID).None()
  214. : new Filter<JobDocumentSetMileStoneKanban>(x => x.Entity.ID).IsEqualTo(MileStoneID));
  215. base.Reload(criteria, columns, ref sort, action);
  216. }
  217. }