TaskPlannerControl.xaml.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562
  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. new Columns<Kanban>
  127. (
  128. x => x.ID,
  129. x => x.Created,
  130. x => x.DueDate,
  131. x => x.Completed,
  132. x => x.Description,
  133. x => x.Summary,
  134. x => x.Status,
  135. x => x.EmployeeLink.ID,
  136. x => x.EmployeeLink.Name,
  137. x => x.ManagerLink.ID,
  138. x => x.Title,
  139. x => x.Notes,
  140. x => x.Number,
  141. x => x.Attachments,
  142. x => x.Type.Code,
  143. x => x.StartDate,
  144. x => x.EstimatedTime,
  145. x => x.ActualTime,
  146. x => x.Type.ID
  147. ),
  148. new SortOrder<Kanban>(x => x.DueDate).ThenBy(x => x.StartDate)
  149. ),
  150. typeof(Kanban)
  151. );
  152. var stagefilter = (Host.Master != null)
  153. ? new Filter<JobStage>(x => x.Job.ID).IsEqualTo(Host.Master.ID)
  154. : new Filter<JobStage>(x => x.Job.ID).All();
  155. query.Add(
  156. new QueryDef<JobStage>(
  157. stagefilter,
  158. null,
  159. null
  160. ),
  161. typeof(JobStage)
  162. );
  163. var relfilter = (Host.Master != null)
  164. ? new Filter<KanbanRelationship>(x => x.Parent.ID).IsEqualTo(Host.Master.ID)
  165. : new Filter<KanbanRelationship>(x => x.Parent.ID).All();
  166. query.Add(
  167. new QueryDef<KanbanRelationship>(
  168. relfilter,
  169. new Columns<KanbanRelationship>(
  170. x => x.ID,
  171. x => x.Predecessor.ID,
  172. x => x.Successor.ID,
  173. x => x.Type,
  174. x => x.Parent.ID
  175. ),
  176. null
  177. ),
  178. typeof(KanbanRelationship)
  179. );
  180. query.Query();
  181. _kanbans = query.Get(typeof(Kanban));
  182. _stages = query.Get(typeof(JobStage));
  183. _relationships = query.Get(typeof(KanbanRelationship));
  184. }
  185. public void Refresh()
  186. {
  187. bLoading = true;
  188. using (new WaitCursor())
  189. {
  190. try
  191. {
  192. LoadData();
  193. _tasks.Clear();
  194. var startmarker = new GanttTask
  195. {
  196. StartDate = DateTime.Today,
  197. FinishDate = DateTime.Today,
  198. TaskName = "Start of Job",
  199. IsMileStone = true
  200. };
  201. var endmarker = new GanttTask
  202. {
  203. StartDate = DateTime.Today,
  204. FinishDate = DateTime.Today,
  205. TaskName = "End of Job",
  206. IsMileStone = true
  207. };
  208. var curstart = DateTime.MaxValue;
  209. var curend = DateTime.MinValue;
  210. _tasks.Add(startmarker);
  211. foreach (var row in _kanbans.Rows)
  212. if ((Host.KanbanSettings.PlannerSettings.IncludeCompleted || row.Get<Kanban, DateTime>(c => c.Completed).IsEmpty()) &&
  213. (Host.KanbanSettings.PlannerSettings.SelectedType == CoreUtils.FullGuid ||
  214. row.Get<Kanban, Guid>(c => c.Type.ID) == Host.KanbanSettings.PlannerSettings.SelectedType))
  215. {
  216. var task = new GanttTask();
  217. LoadTask(row, task);
  218. if (task.StartDate < curstart)
  219. curstart = task.StartDate;
  220. if (task.FinishDate > curend)
  221. curend = task.FinishDate;
  222. _tasks.Add(task);
  223. }
  224. _tasks.Add(endmarker);
  225. foreach (var row in _relationships.Rows)
  226. {
  227. var predtask = _tasks.FirstOrDefault(x => x.ID == row.Get<KanbanRelationship, Guid>(c => c.Predecessor.ID));
  228. var succtask = _tasks.FirstOrDefault(x => x.ID == row.Get<KanbanRelationship, Guid>(c => c.Successor.ID));
  229. if (predtask != null && succtask != null)
  230. {
  231. var type = row.Get<KanbanRelationship, GanttRelationshipType>(x => x.Type);
  232. var relationship = type == GanttRelationshipType.FinishToFinish
  233. ? GanttTaskRelationship.FinishToFinish
  234. : type == GanttRelationshipType.FinishToStart
  235. ? GanttTaskRelationship.FinishToStart
  236. : type == GanttRelationshipType.StartToFinish
  237. ? GanttTaskRelationship.StartToFinish
  238. : GanttTaskRelationship.StartToStart;
  239. succtask.Predecessor.Add(new Predecessor { GanttTaskIndex = predtask.TaskId, GanttTaskRelationship = relationship });
  240. }
  241. }
  242. var _striplines = new List<StripLineInfo>();
  243. foreach (var row in _stages.Rows)
  244. {
  245. if (!_striplines.Any())
  246. {
  247. var startline = new StripLineInfo();
  248. startline.Content = "Start of Job";
  249. startline.StartDate = row.Get<JobStage, DateTime>(c => c.StartDate);
  250. startline.EndDate = startline.StartDate;
  251. startline.HorizontalContentAlignment = HorizontalAlignment.Center;
  252. startline.VerticalContentAlignment = VerticalAlignment.Center;
  253. startline.Background = Brushes.Gold;
  254. startline.RepeatBehavior = Repeat.None;
  255. _striplines.Add(startline);
  256. }
  257. var stripline = new StripLineInfo();
  258. stripline.Content = row.Get<JobStage, string>(c => c.Name);
  259. stripline.StartDate = row.Get<JobStage, DateTime>(c => c.EndDate);
  260. stripline.EndDate = stripline.StartDate;
  261. stripline.HorizontalContentAlignment = HorizontalAlignment.Center;
  262. stripline.VerticalContentAlignment = VerticalAlignment.Center;
  263. stripline.Background = Brushes.Gold;
  264. stripline.RepeatBehavior = Repeat.None;
  265. _striplines.Add(stripline);
  266. if (stripline.StartDate < curstart)
  267. curstart = stripline.StartDate;
  268. if (stripline.EndDate > curend)
  269. curend = stripline.EndDate;
  270. }
  271. Gantt.StripLines = _striplines;
  272. Gantt.ShowStripLines = true;
  273. Gantt.UseOnDemandSchedule = true;
  274. if (curstart != DateTime.MaxValue)
  275. {
  276. startmarker.StartDate = curstart.Date;
  277. startmarker.FinishDate = curstart.Date;
  278. }
  279. if (curend != DateTime.MinValue)
  280. {
  281. endmarker.StartDate = curend.Date.AddDays(1);
  282. endmarker.FinishDate = curend.AddDays(1);
  283. }
  284. }
  285. finally
  286. {
  287. bLoading = false;
  288. }
  289. foreach (var task in _tasks)
  290. task.PropertyChanged += Task_PropertyChanged;
  291. }
  292. }
  293. private void LoadTask(CoreRow row, GanttTask task)
  294. {
  295. var start = row.Get<Kanban, DateTime>(x => x.StartDate);
  296. if (start.IsEmpty())
  297. start = row.Get<Kanban, DateTime>(x => x.Created);
  298. var estimated = row.Get<Kanban, TimeSpan>(x => x.EstimatedTime);
  299. if (estimated.TotalMilliseconds == 0L)
  300. estimated = TimeSpan.FromHours(1);
  301. var actual = row.Get<Kanban, TimeSpan>(x => x.ActualTime);
  302. var progress = estimated.TotalMilliseconds != 0L ? actual.TotalMilliseconds * 100L / estimated.TotalMilliseconds : 0F;
  303. task.TaskId = row.Get<Kanban, int>(c => c.Number);
  304. task.ID = row.Get<Kanban, Guid>(x => x.ID);
  305. task.TaskName = string.Format("{0} - {1}", row.Get<Kanban, int>(c => c.Number), row.Get<Kanban, string>(x => x.Title));
  306. task.StartDate = start.Date;
  307. task.FinishDate = row.Get<Kanban, DateTime>(x => x.DueDate).Date.AddDays(1).AddMilliseconds(-1);
  308. task.Manpower = estimated;
  309. task.Percentage = progress;
  310. task.IsMileStone = false;
  311. var empid = row.EntityLinkID<Kanban, EmployeeLink>(x => x.EmployeeLink) ?? Guid.Empty;
  312. if (empid != Guid.Empty)
  313. {
  314. var resource = _resources.FirstOrDefault(x => (x as GanttResource).Guid == empid) as GanttResource;
  315. if (resource == null)
  316. {
  317. resource = new GanttResource
  318. {
  319. ID = _resources.Count,
  320. Guid = empid,
  321. Name = row.Get<Kanban, string>(c => c.EmployeeLink.Name)
  322. };
  323. _resources.Add(resource);
  324. }
  325. task.Resources = new ObservableCollection<Resource> { resource };
  326. }
  327. }
  328. private void Task_PropertyChanged(object? sender, PropertyChangedEventArgs args)
  329. {
  330. if (bLoading)
  331. return;
  332. if (!string.Equals(args.PropertyName, "StartDate") && !string.Equals(args.PropertyName, "FinishDate"))
  333. return;
  334. var row = _kanbans.Rows.FirstOrDefault(r => r.Get<Kanban, Guid>(x => x.ID) == ((GanttTask)sender).ID);
  335. var kanban = row?.ToObject<Kanban>();
  336. var task = sender as GanttTask;
  337. var bChanged = false;
  338. if (task != null && kanban != null)
  339. {
  340. if (string.Equals(args.PropertyName, "FinishDate"))
  341. {
  342. kanban.DueDate = task.FinishDate.Date;
  343. bChanged = true;
  344. }
  345. else if (string.Equals(args.PropertyName, "StartDate"))
  346. {
  347. kanban.StartDate = task.StartDate.Date;
  348. bChanged = true;
  349. }
  350. }
  351. if (bChanged)
  352. {
  353. row.Set<Kanban, DateTime>(x => x.DueDate, kanban.DueDate.Date);
  354. row.Set<Kanban, DateTime>(x => x.StartDate, kanban.StartDate.Date);
  355. new Client<Kanban>().Save(kanban, "Updated by Planner", (o, e) => { });
  356. }
  357. }
  358. private void Gantt_RelationshipEstablished(object sender, GanttRelationshipEstablishedEventArgs args)
  359. {
  360. var pred = args.StartTask as GanttTask;
  361. var succ = args.EndTask as GanttTask;
  362. if (pred == null || pred.IsMileStone)
  363. throw new Exception("Cannot make a connection here");
  364. if (succ == null || succ.IsMileStone)
  365. throw new Exception("Cannot make a connection here");
  366. var relationship = new KanbanRelationship();
  367. relationship.Parent.ID = Host.Master?.ID ?? Guid.Empty;
  368. relationship.Parent.Synchronise(Host.Master ?? new Job());
  369. relationship.Predecessor.ID = pred.ID;
  370. relationship.Successor.ID = succ.ID;
  371. relationship.Type = args.Relationship == GanttTaskRelationship.FinishToFinish
  372. ? GanttRelationshipType.FinishToFinish
  373. : args.Relationship == GanttTaskRelationship.FinishToStart
  374. ? GanttRelationshipType.FinishToStart
  375. : args.Relationship == GanttTaskRelationship.StartToFinish
  376. ? GanttRelationshipType.StartToFinish
  377. : GanttRelationshipType.StartToStart;
  378. new Client<KanbanRelationship>().Save(relationship, "", (o, e) =>
  379. {
  380. _relationships.LoadRow(relationship);
  381. });
  382. }
  383. private void GanttContextMenu_Opened(object sender, RoutedEventArgs e)
  384. {
  385. var task = Gantt.SelectedItems.FirstOrDefault() as GanttTask;
  386. UnlinkTaskMenu.IsEnabled = task != null && task.Predecessor.Any();
  387. UnlinkTaskMenu.Tag = task;
  388. }
  389. private void UnlinkTaskMenu_Click(object sender, RoutedEventArgs args)
  390. {
  391. var successor = (sender as MenuItem).Tag as GanttTask;
  392. if (successor == null)
  393. return;
  394. var deletes = new List<KanbanRelationship>();
  395. foreach (var link in successor.Predecessor)
  396. {
  397. var predecessor = _tasks.FirstOrDefault(x => x.TaskId == link.GanttTaskIndex);
  398. if (predecessor != null)
  399. {
  400. var rows = _relationships.Rows.Where(r =>
  401. r.Get<KanbanRelationship, Guid>(c => c.Successor.ID).Equals(successor.ID) &&
  402. r.Get<KanbanRelationship, Guid>(c => c.Predecessor.ID).Equals(predecessor.ID)).ToArray();
  403. foreach (var row in rows)
  404. {
  405. deletes.Add(row.ToObject<KanbanRelationship>());
  406. _relationships.Rows.Remove(row);
  407. }
  408. }
  409. }
  410. if (deletes.Any())
  411. new Client<KanbanRelationship>().Delete(deletes, "", (o, e) => { });
  412. successor.Predecessor.Clear();
  413. }
  414. private void EditTask_Click(object sender, RoutedEventArgs e)
  415. {
  416. DoEditTask();
  417. }
  418. private void DoEditTask()
  419. {
  420. var task = Gantt.SelectedItems.FirstOrDefault() as GanttTask;
  421. if (task == null)
  422. return;
  423. var row = _kanbans.Rows.FirstOrDefault(r => r.Get<Kanban, Guid>(x => x.ID) == task.ID);
  424. var kanban = row?.ToObject<Kanban>();
  425. if (kanban != null)
  426. {
  427. var bOK = new TaskGrid().EditItems(new[] { kanban });
  428. if (bOK)
  429. {
  430. _kanbans.FillRow(row, kanban);
  431. LoadTask(row, task);
  432. }
  433. }
  434. }
  435. private void AddTask_Click(object sender, RoutedEventArgs e)
  436. {
  437. var kanban = new Kanban();
  438. kanban.JobLink.ID = Host.Master?.ID ?? Guid.Empty;
  439. kanban.JobLink.Synchronise(Host.Master ?? new Job());
  440. var bOK = new TaskGrid().EditItems(new[] { kanban });
  441. if (bOK)
  442. {
  443. var row = _kanbans.NewRow();
  444. _kanbans.FillRow(row, kanban);
  445. var task = new GanttTask();
  446. LoadTask(row, task);
  447. _tasks.Add(task);
  448. task.PropertyChanged += Task_PropertyChanged;
  449. }
  450. }
  451. private void SelectedType_SelectionChanged(object sender, SelectionChangedEventArgs e)
  452. {
  453. if (EventSuppressor.IsSet(Suppress.This))
  454. {
  455. Host.KanbanSettings.PlannerSettings.SelectedType = SelectedType.SelectedValue == null ? Guid.Empty : (Guid)SelectedType.SelectedValue;
  456. Host.SaveSettings();
  457. Refresh();
  458. }
  459. }
  460. private void IncludeCompleted_Checked(object sender, RoutedEventArgs e)
  461. {
  462. Host.KanbanSettings.PlannerSettings.IncludeCompleted = IncludeCompleted.IsChecked == true;
  463. Host.SaveSettings();
  464. Refresh();
  465. }
  466. #region ITaskControl Support
  467. public KanbanViewType KanbanViewType => KanbanViewType.Planner;
  468. public ITaskHost Host { get; set; }
  469. public bool IsReady { get; set; }
  470. public string SectionName => "Task Planner";
  471. public DataModel DataModel(Selection selection)
  472. {
  473. return new AutoDataModel<Kanban>(new Filter<Kanban>(x => x.ID).IsEqualTo(Guid.Empty));
  474. }
  475. public IEnumerable<TaskModel> SelectedModels(TaskModel sender = null)
  476. {
  477. MessageBox.Show("TaskPlannerControl.SelectedModels() is not Implemented!");
  478. return new TaskModel[] { };
  479. }
  480. #endregion
  481. private void TaskTypeButton_OnClick(object sender, RoutedEventArgs e)
  482. {
  483. var list = new MasterList(typeof(KanbanType));
  484. list.ShowDialog();
  485. LoadKanbanTypes();
  486. }
  487. }
  488. }