TaskPlannerControl.xaml.cs 22 KB

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