JobDocumentSetMilestoneTasks.cs 11 KB

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