JobDocumentSetMilestoneTasks.cs 11 KB

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