TaskPlannerControl.xaml.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.ObjectModel;
  4. using System.ComponentModel;
  5. using System.Linq;
  6. using System.Windows;
  7. using System.Windows.Controls;
  8. using System.Windows.Media;
  9. using Comal.Classes;
  10. using InABox.Clients;
  11. using InABox.Core;
  12. using InABox.DynamicGrid;
  13. using InABox.WPF;
  14. using Syncfusion.Windows.Controls.Gantt;
  15. using Syncfusion.Windows.Controls.Grid;
  16. using GridSelectionMode = Syncfusion.Windows.Controls.Grid.GridSelectionMode;
  17. namespace PRSDesktop
  18. {
  19. /// <summary>
  20. /// Interaction logic for TaskPlannerControl.xaml
  21. /// </summary>
  22. public partial class TaskPlannerControl : UserControl, ITaskControl
  23. {
  24. private enum Suppress
  25. {
  26. This
  27. }
  28. private CoreTable _kanbans;
  29. private CoreTable _relationships;
  30. private readonly ObservableCollection<Resource> _resources = new();
  31. private CoreTable _stages;
  32. private readonly ObservableCollection<GanttTask> _tasks = new();
  33. private bool bLoading;
  34. public TaskPlannerControl()
  35. {
  36. using (new EventSuppressor(Suppress.This))
  37. {
  38. InitializeComponent();
  39. Gantt.ItemsSource = _tasks;
  40. Gantt.ResourceCollection = _resources;
  41. }
  42. }
  43. public void Setup()
  44. {
  45. LoadKanbanTypes();
  46. }
  47. private void LoadKanbanTypes()
  48. {
  49. using (new EventSuppressor(Suppress.This))
  50. {
  51. var types = new Dictionary<Guid, string>
  52. {
  53. { CoreUtils.FullGuid, "All Task Types" },
  54. { Guid.Empty, "Uncategorized Tasks" }
  55. };
  56. new Client<KanbanType>().Query(new Filter<KanbanType>(x => x.Hidden).IsEqualTo(false))
  57. .IntoDictionary<Kanban, Guid>(types, x => x.ID, x => x.Description);
  58. SelectedType.ItemsSource = types;
  59. }
  60. }
  61. public void Refresh(bool resetselection)
  62. {
  63. Refresh();
  64. }
  65. public void Shutdown()
  66. {
  67. }
  68. private void Gantt_TemplateApplied(object sender, TemplateAppliedEventArgs args)
  69. {
  70. if (Gantt.GanttGrid != null)
  71. {
  72. Gantt.GanttGrid.Model.Options.ListBoxSelectionMode = GridSelectionMode.One;
  73. Gantt.GanttGrid.Model.Sizer.AllowAutoCalculateSize = true;
  74. Gantt.GanttGrid.Model.Sizer.ListenToSizeChanged = true;
  75. Gantt.GanttGrid.Model.Options.ColumnSizer = GridControlLengthUnitType.Star;
  76. Gantt.GanttGrid.RowHeaderWidth = 0;
  77. Gantt.GanttGrid.ShowRowHeader = false;
  78. CreateGanttColumns();
  79. //Gantt.GanttGrid.ReadOnly = true;
  80. }
  81. }
  82. private void CreateGanttColumns()
  83. {
  84. if (Gantt.GanttGrid == null)
  85. return;
  86. Gantt.GanttGrid.Columns.Clear();
  87. //Gantt.GanttGrid.Columns.Add(new Syncfusion.Windows.Controls.Grid.GridTreeColumn("TaskId") { Width = 60F, HeaderText = "#", StyleInfo = new Syncfusion.Windows.Controls.Grid.GridStyleInfo() { VerticalAlignment = VerticalAlignment.Center, HorizontalAlignment = HorizontalAlignment.Center } });
  88. Gantt.GanttGrid.Columns.Add(new GridTreeColumn("TaskName")
  89. {
  90. Width = 220F, PercentWidth = 100F, HeaderText = "Description",
  91. StyleInfo = new GridStyleInfo { VerticalAlignment = VerticalAlignment.Center }
  92. });
  93. Gantt.GanttGrid.Columns.Add(new GridTreeColumn("StartDate")
  94. {
  95. Width = 70F, HeaderText = "Start",
  96. StyleInfo = new GridStyleInfo
  97. { VerticalAlignment = VerticalAlignment.Center, Format = "dd MMM yy", HorizontalAlignment = HorizontalAlignment.Center }
  98. });
  99. Gantt.GanttGrid.Columns.Add(new GridTreeColumn("Manpower")
  100. {
  101. Width = 60F, HeaderText = "Hrs",
  102. StyleInfo = new GridStyleInfo { VerticalAlignment = VerticalAlignment.Center, HorizontalAlignment = HorizontalAlignment.Center }
  103. });
  104. Gantt.GanttGrid.Columns.Add(new GridTreeColumn("Percentage")
  105. {
  106. Width = 40F, HeaderText = "%",
  107. StyleInfo = new GridStyleInfo
  108. { VerticalAlignment = VerticalAlignment.Center, Format = "#0.##%", HorizontalAlignment = HorizontalAlignment.Center }
  109. });
  110. Gantt.GanttGrid.Columns.Add(new GridTreeColumn("FinishDate")
  111. {
  112. Width = 70F, HeaderText = "Due",
  113. StyleInfo = new GridStyleInfo
  114. { VerticalAlignment = VerticalAlignment.Center, Format = "dd MMM yy", HorizontalAlignment = HorizontalAlignment.Center }
  115. });
  116. }
  117. private void LoadData()
  118. {
  119. var query = new MultiQuery();
  120. var kanbanfilter = new Filter<Kanban>(x => x.Closed).IsEqualTo(DateTime.MinValue);
  121. if (Host.Master != null)
  122. kanbanfilter = kanbanfilter.And(x => x.JobLink.ID).IsEqualTo(Host.Master.ID);
  123. query.Add(
  124. new QueryDef<Kanban>(
  125. kanbanfilter,
  126. Columns.None<Kanban>().Add(
  127. x => x.ID,
  128. x => x.Created,
  129. x => x.DueDate,
  130. x => x.Completed,
  131. x => x.Description,
  132. x => x.Summary,
  133. x => x.Status,
  134. x => x.EmployeeLink.ID,
  135. x => x.EmployeeLink.Name,
  136. x => x.ManagerLink.ID,
  137. x => x.Title,
  138. x => x.Notes,
  139. x => x.Number,
  140. x => x.Attachments,
  141. x => x.Type.Code,
  142. x => x.StartDate,
  143. x => x.EstimatedTime,
  144. x => x.ActualTime,
  145. x => x.Type.ID
  146. ),
  147. new SortOrder<Kanban>(x => x.DueDate).ThenBy(x => x.StartDate)
  148. ),
  149. typeof(Kanban)
  150. );
  151. var stagefilter = (Host.Master != null)
  152. ? new Filter<JobStage>(x => x.Job.ID).IsEqualTo(Host.Master.ID)
  153. : new Filter<JobStage>(x => x.Job.ID).All();
  154. query.Add(
  155. new QueryDef<JobStage>(
  156. stagefilter,
  157. null,
  158. null
  159. ),
  160. typeof(JobStage)
  161. );
  162. var relfilter = (Host.Master != null)
  163. ? new Filter<KanbanRelationship>(x => x.Parent.ID).IsEqualTo(Host.Master.ID)
  164. : new Filter<KanbanRelationship>(x => x.Parent.ID).All();
  165. query.Add(
  166. new QueryDef<KanbanRelationship>(
  167. relfilter,
  168. Columns.None<KanbanRelationship>().Add(
  169. x => x.ID,
  170. x => x.Predecessor.ID,
  171. x => x.Successor.ID,
  172. x => x.Type,
  173. x => x.Parent.ID
  174. ),
  175. null
  176. ),
  177. typeof(KanbanRelationship)
  178. );
  179. query.Query();
  180. _kanbans = query.Get(typeof(Kanban));
  181. _stages = query.Get(typeof(JobStage));
  182. _relationships = query.Get(typeof(KanbanRelationship));
  183. }
  184. public void Refresh()
  185. {
  186. bLoading = true;
  187. using (new WaitCursor())
  188. {
  189. try
  190. {
  191. LoadData();
  192. _tasks.Clear();
  193. var startmarker = new GanttTask
  194. {
  195. StartDate = DateTime.Today,
  196. FinishDate = DateTime.Today,
  197. TaskName = "Start of Job",
  198. IsMileStone = true
  199. };
  200. var endmarker = new GanttTask
  201. {
  202. StartDate = DateTime.Today,
  203. FinishDate = DateTime.Today,
  204. TaskName = "End of Job",
  205. IsMileStone = true
  206. };
  207. var curstart = DateTime.MaxValue;
  208. var curend = DateTime.MinValue;
  209. _tasks.Add(startmarker);
  210. foreach (var row in _kanbans.Rows)
  211. if ((Host.KanbanSettings.PlannerSettings.IncludeCompleted || row.Get<Kanban, DateTime>(c => c.Completed).IsEmpty()) &&
  212. (Host.KanbanSettings.PlannerSettings.SelectedType == CoreUtils.FullGuid ||
  213. row.Get<Kanban, Guid>(c => c.Type.ID) == Host.KanbanSettings.PlannerSettings.SelectedType))
  214. {
  215. var task = new GanttTask();
  216. LoadTask(row, task);
  217. if (task.StartDate < curstart)
  218. curstart = task.StartDate;
  219. if (task.FinishDate > curend)
  220. curend = task.FinishDate;
  221. _tasks.Add(task);
  222. }
  223. _tasks.Add(endmarker);
  224. foreach (var row in _relationships.Rows)
  225. {
  226. var predtask = _tasks.FirstOrDefault(x => x.ID == row.Get<KanbanRelationship, Guid>(c => c.Predecessor.ID));
  227. var succtask = _tasks.FirstOrDefault(x => x.ID == row.Get<KanbanRelationship, Guid>(c => c.Successor.ID));
  228. if (predtask != null && succtask != null)
  229. {
  230. var type = row.Get<KanbanRelationship, GanttRelationshipType>(x => x.Type);
  231. var relationship = type == GanttRelationshipType.FinishToFinish
  232. ? GanttTaskRelationship.FinishToFinish
  233. : type == GanttRelationshipType.FinishToStart
  234. ? GanttTaskRelationship.FinishToStart
  235. : type == GanttRelationshipType.StartToFinish
  236. ? GanttTaskRelationship.StartToFinish
  237. : GanttTaskRelationship.StartToStart;
  238. succtask.Predecessor.Add(new Predecessor { GanttTaskIndex = predtask.TaskId, GanttTaskRelationship = relationship });
  239. }
  240. }
  241. var _striplines = new List<StripLineInfo>();
  242. foreach (var row in _stages.Rows)
  243. {
  244. if (!_striplines.Any())
  245. {
  246. var startline = new StripLineInfo();
  247. startline.Content = "Start of Job";
  248. startline.StartDate = row.Get<JobStage, DateTime>(c => c.StartDate);
  249. startline.EndDate = startline.StartDate;
  250. startline.HorizontalContentAlignment = HorizontalAlignment.Center;
  251. startline.VerticalContentAlignment = VerticalAlignment.Center;
  252. startline.Background = Brushes.Gold;
  253. startline.RepeatBehavior = Repeat.None;
  254. _striplines.Add(startline);
  255. }
  256. var stripline = new StripLineInfo();
  257. stripline.Content = row.Get<JobStage, string>(c => c.Name);
  258. stripline.StartDate = row.Get<JobStage, DateTime>(c => c.EndDate);
  259. stripline.EndDate = stripline.StartDate;
  260. stripline.HorizontalContentAlignment = HorizontalAlignment.Center;
  261. stripline.VerticalContentAlignment = VerticalAlignment.Center;
  262. stripline.Background = Brushes.Gold;
  263. stripline.RepeatBehavior = Repeat.None;
  264. _striplines.Add(stripline);
  265. if (stripline.StartDate < curstart)
  266. curstart = stripline.StartDate;
  267. if (stripline.EndDate > curend)
  268. curend = stripline.EndDate;
  269. }
  270. Gantt.StripLines = _striplines;
  271. Gantt.ShowStripLines = true;
  272. Gantt.UseOnDemandSchedule = true;
  273. if (curstart != DateTime.MaxValue)
  274. {
  275. startmarker.StartDate = curstart.Date;
  276. startmarker.FinishDate = curstart.Date;
  277. }
  278. if (curend != DateTime.MinValue)
  279. {
  280. endmarker.StartDate = curend.Date.AddDays(1);
  281. endmarker.FinishDate = curend.AddDays(1);
  282. }
  283. }
  284. finally
  285. {
  286. bLoading = false;
  287. }
  288. foreach (var task in _tasks)
  289. task.PropertyChanged += Task_PropertyChanged;
  290. }
  291. }
  292. private void LoadTask(CoreRow row, GanttTask task)
  293. {
  294. var start = row.Get<Kanban, DateTime>(x => x.StartDate);
  295. if (start.IsEmpty())
  296. start = row.Get<Kanban, DateTime>(x => x.Created);
  297. var estimated = row.Get<Kanban, TimeSpan>(x => x.EstimatedTime);
  298. if (estimated.TotalMilliseconds == 0L)
  299. estimated = TimeSpan.FromHours(1);
  300. var actual = row.Get<Kanban, TimeSpan>(x => x.ActualTime);
  301. var progress = estimated.TotalMilliseconds != 0L ? actual.TotalMilliseconds * 100L / estimated.TotalMilliseconds : 0F;
  302. task.TaskId = row.Get<Kanban, int>(c => c.Number);
  303. task.ID = row.Get<Kanban, Guid>(x => x.ID);
  304. task.TaskName = string.Format("{0} - {1}", row.Get<Kanban, int>(c => c.Number), row.Get<Kanban, string>(x => x.Title));
  305. task.StartDate = start.Date;
  306. task.FinishDate = row.Get<Kanban, DateTime>(x => x.DueDate).Date.AddDays(1).AddMilliseconds(-1);
  307. task.Manpower = estimated;
  308. task.Percentage = progress;
  309. task.IsMileStone = false;
  310. var empid = row.EntityLinkID<Kanban, EmployeeLink>(x => x.EmployeeLink) ?? Guid.Empty;
  311. if (empid != Guid.Empty)
  312. {
  313. var resource = _resources.FirstOrDefault(x => (x as GanttResource).Guid == empid) as GanttResource;
  314. if (resource == null)
  315. {
  316. resource = new GanttResource
  317. {
  318. ID = _resources.Count,
  319. Guid = empid,
  320. Name = row.Get<Kanban, string>(c => c.EmployeeLink.Name)
  321. };
  322. _resources.Add(resource);
  323. }
  324. task.Resources = new ObservableCollection<Resource> { resource };
  325. }
  326. }
  327. private void Task_PropertyChanged(object? sender, PropertyChangedEventArgs args)
  328. {
  329. if (bLoading)
  330. return;
  331. if (!string.Equals(args.PropertyName, "StartDate") && !string.Equals(args.PropertyName, "FinishDate"))
  332. return;
  333. var row = _kanbans.Rows.FirstOrDefault(r => r.Get<Kanban, Guid>(x => x.ID) == ((GanttTask)sender).ID);
  334. var kanban = row?.ToObject<Kanban>();
  335. var task = sender as GanttTask;
  336. var bChanged = false;
  337. if (task != null && kanban != null)
  338. {
  339. if (string.Equals(args.PropertyName, "FinishDate"))
  340. {
  341. kanban.DueDate = task.FinishDate.Date;
  342. bChanged = true;
  343. }
  344. else if (string.Equals(args.PropertyName, "StartDate"))
  345. {
  346. kanban.StartDate = task.StartDate.Date;
  347. bChanged = true;
  348. }
  349. }
  350. if (bChanged)
  351. {
  352. row.Set<Kanban, DateTime>(x => x.DueDate, kanban.DueDate.Date);
  353. row.Set<Kanban, DateTime>(x => x.StartDate, kanban.StartDate.Date);
  354. new Client<Kanban>().Save(kanban, "Updated by Planner", (o, e) => { });
  355. }
  356. }
  357. private void Gantt_RelationshipEstablished(object sender, GanttRelationshipEstablishedEventArgs args)
  358. {
  359. var pred = args.StartTask as GanttTask;
  360. var succ = args.EndTask as GanttTask;
  361. if (pred == null || pred.IsMileStone)
  362. throw new Exception("Cannot make a connection here");
  363. if (succ == null || succ.IsMileStone)
  364. throw new Exception("Cannot make a connection here");
  365. var relationship = new KanbanRelationship();
  366. relationship.Parent.ID = Host.Master?.ID ?? Guid.Empty;
  367. relationship.Parent.Synchronise(Host.Master ?? new Job());
  368. relationship.Predecessor.ID = pred.ID;
  369. relationship.Successor.ID = succ.ID;
  370. relationship.Type = args.Relationship == GanttTaskRelationship.FinishToFinish
  371. ? GanttRelationshipType.FinishToFinish
  372. : args.Relationship == GanttTaskRelationship.FinishToStart
  373. ? GanttRelationshipType.FinishToStart
  374. : args.Relationship == GanttTaskRelationship.StartToFinish
  375. ? GanttRelationshipType.StartToFinish
  376. : GanttRelationshipType.StartToStart;
  377. new Client<KanbanRelationship>().Save(relationship, "", (o, e) =>
  378. {
  379. _relationships.LoadRow(relationship);
  380. });
  381. }
  382. private void GanttContextMenu_Opened(object sender, RoutedEventArgs e)
  383. {
  384. var task = Gantt.SelectedItems.FirstOrDefault() as GanttTask;
  385. UnlinkTaskMenu.IsEnabled = task != null && task.Predecessor.Any();
  386. UnlinkTaskMenu.Tag = task;
  387. }
  388. private void UnlinkTaskMenu_Click(object sender, RoutedEventArgs args)
  389. {
  390. var successor = (sender as MenuItem).Tag as GanttTask;
  391. if (successor == null)
  392. return;
  393. var deletes = new List<KanbanRelationship>();
  394. foreach (var link in successor.Predecessor)
  395. {
  396. var predecessor = _tasks.FirstOrDefault(x => x.TaskId == link.GanttTaskIndex);
  397. if (predecessor != null)
  398. {
  399. var rows = _relationships.Rows.Where(r =>
  400. r.Get<KanbanRelationship, Guid>(c => c.Successor.ID).Equals(successor.ID) &&
  401. r.Get<KanbanRelationship, Guid>(c => c.Predecessor.ID).Equals(predecessor.ID)).ToArray();
  402. foreach (var row in rows)
  403. {
  404. deletes.Add(row.ToObject<KanbanRelationship>());
  405. _relationships.Rows.Remove(row);
  406. }
  407. }
  408. }
  409. if (deletes.Any())
  410. new Client<KanbanRelationship>().Delete(deletes, "", (o, e) => { });
  411. successor.Predecessor.Clear();
  412. }
  413. private void EditTask_Click(object sender, RoutedEventArgs e)
  414. {
  415. DoEditTask();
  416. }
  417. private void DoEditTask()
  418. {
  419. if (Gantt.SelectedItems.FirstOrDefault() is not GanttTask task)
  420. return;
  421. var row = _kanbans.Rows.FirstOrDefault(r => r.Get<Kanban, Guid>(x => x.ID) == task.ID);
  422. if (row is null) return;
  423. var kanban = row.ToObject<Kanban>();
  424. if (DynamicGridUtils.EditEntity(kanban))
  425. {
  426. _kanbans.FillRow(row, kanban);
  427. LoadTask(row, task);
  428. }
  429. }
  430. private void AddTask_Click(object sender, RoutedEventArgs e)
  431. {
  432. var kanban = new Kanban();
  433. kanban.JobLink.ID = Host.Master?.ID ?? Guid.Empty;
  434. kanban.JobLink.Synchronise(Host.Master ?? new Job());
  435. if (DynamicGridUtils.EditEntity(kanban))
  436. {
  437. var row = _kanbans.NewRow();
  438. _kanbans.FillRow(row, kanban);
  439. var task = new GanttTask();
  440. LoadTask(row, task);
  441. _tasks.Add(task);
  442. task.PropertyChanged += Task_PropertyChanged;
  443. }
  444. }
  445. private void SelectedType_SelectionChanged(object sender, SelectionChangedEventArgs e)
  446. {
  447. if (EventSuppressor.IsSet(Suppress.This))
  448. {
  449. Host.KanbanSettings.PlannerSettings.SelectedType = SelectedType.SelectedValue == null ? Guid.Empty : (Guid)SelectedType.SelectedValue;
  450. Host.SaveSettings();
  451. Refresh();
  452. }
  453. }
  454. private void IncludeCompleted_Checked(object sender, RoutedEventArgs e)
  455. {
  456. Host.KanbanSettings.PlannerSettings.IncludeCompleted = IncludeCompleted.IsChecked == true;
  457. Host.SaveSettings();
  458. Refresh();
  459. }
  460. #region ITaskControl Support
  461. public KanbanViewType KanbanViewType => KanbanViewType.Planner;
  462. public ITaskHost Host { get; set; }
  463. public bool IsReady { get; set; }
  464. public string SectionName => "Task Planner";
  465. public DataModel DataModel(Selection selection)
  466. {
  467. return new AutoDataModel<Kanban>(new Filter<Kanban>(x => x.ID).IsEqualTo(Guid.Empty));
  468. }
  469. public IEnumerable<TaskModel> SelectedModels(TaskModel sender = null)
  470. {
  471. MessageBox.Show("TaskPlannerControl.SelectedModels() is not Implemented!");
  472. return new TaskModel[] { };
  473. }
  474. #endregion
  475. private void TaskTypeButton_OnClick(object sender, RoutedEventArgs e)
  476. {
  477. var list = new MasterList(typeof(KanbanType));
  478. list.ShowDialog();
  479. LoadKanbanTypes();
  480. }
  481. }
  482. }