TasksByStatusControl.xaml.cs 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060
  1. using Comal.Classes;
  2. using InABox.Clients;
  3. using InABox.Core;
  4. using InABox.WPF;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Globalization;
  8. using System.Linq;
  9. using System.Linq.Expressions;
  10. using System.Reflection;
  11. using System.Text;
  12. using System.Threading.Tasks;
  13. using System.Windows;
  14. using System.Windows.Controls;
  15. using System.Windows.Data;
  16. using System.Windows.Documents;
  17. using System.Windows.Input;
  18. using System.Drawing;
  19. using System.Windows.Media;
  20. using System.Windows.Media.Imaging;
  21. using Color = System.Drawing.Color;
  22. using InABox.DynamicGrid;
  23. using InABox.Wpf;
  24. using MailKit.Search;
  25. using NPOI.SS.Formula.Functions;
  26. using System.ComponentModel;
  27. using System.Runtime.CompilerServices;
  28. using System.Collections.ObjectModel;
  29. using System.Collections.Specialized;
  30. using Syncfusion.UI.Xaml.Kanban;
  31. namespace PRSDesktop;
  32. public class TasksByStatusColumn : INotifyPropertyChanged
  33. {
  34. public string Category { get; }
  35. public string Title { get; }
  36. public int NumTasks { get => Tasks.Count; }
  37. public double NumHours { get => Tasks.Sum(x => x.EstimatedTime.TotalHours); }
  38. public ObservableCollection<TaskModel> Tasks { get; } = new();
  39. public TasksByStatusColumn(string category, string title)
  40. {
  41. Category = category;
  42. Title = title;
  43. Tasks.CollectionChanged += Tasks_CollectionChanged;
  44. }
  45. private void Tasks_CollectionChanged(object? sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
  46. {
  47. OnPropertyChanged(nameof(Tasks));
  48. }
  49. public event PropertyChangedEventHandler? PropertyChanged;
  50. // Create the OnPropertyChanged method to raise the event
  51. // The calling member's name will be used as the parameter.
  52. protected void OnPropertyChanged([CallerMemberName] string? name = null)
  53. {
  54. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
  55. if(name == nameof(Tasks))
  56. {
  57. OnPropertyChanged(nameof(NumTasks));
  58. OnPropertyChanged(nameof(NumHours));
  59. }
  60. }
  61. }
  62. public class EmployeeModel
  63. {
  64. public Guid ID { get; set; }
  65. public string Name { get; set; }
  66. public BitmapImage? Image { get; set; }
  67. public Guid ThumbnailID { get; set; }
  68. public EmployeeModel(Guid id, string name, Guid thumbnail, BitmapImage? image)
  69. {
  70. ID = id;
  71. Name = name;
  72. Image = image;
  73. ThumbnailID = thumbnail;
  74. }
  75. }
  76. public class SuspendableObservableCollection<T> : ObservableCollection<T>
  77. {
  78. private bool _notificationSupressed = false;
  79. private bool _supressNotification = false;
  80. public bool SupressNotification
  81. {
  82. get
  83. {
  84. return _supressNotification;
  85. }
  86. set
  87. {
  88. _supressNotification = value;
  89. if (_supressNotification == false && _notificationSupressed)
  90. {
  91. this.OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
  92. _notificationSupressed = false;
  93. }
  94. }
  95. }
  96. protected override void OnCollectionChanged(NotifyCollectionChangedEventArgs e)
  97. {
  98. if (SupressNotification)
  99. {
  100. _notificationSupressed = true;
  101. return;
  102. }
  103. base.OnCollectionChanged(e);
  104. }
  105. }
  106. /// <summary>
  107. /// Interaction logic for TasksByStatusControl.xaml
  108. /// </summary>
  109. public partial class TasksByStatusControl : UserControl, ITaskControl, INotifyPropertyChanged
  110. {
  111. private enum Suppress
  112. {
  113. This
  114. }
  115. public SuspendableObservableCollection<TasksByStatusColumn> Columns { get; private set; } = new();
  116. private List<EmployeeModel> Employees = new();
  117. public TasksByStatusControl()
  118. {
  119. InitializeComponent();
  120. }
  121. #region INotifyPropertyChanged
  122. public event PropertyChangedEventHandler? PropertyChanged;
  123. // Create the OnPropertyChanged method to raise the event
  124. // The calling member's name will be used as the parameter.
  125. protected void OnPropertyChanged([CallerMemberName] string? name = null)
  126. {
  127. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
  128. }
  129. #endregion
  130. #region Setup
  131. public void Setup()
  132. {
  133. var employeesTask = Task.Run(() => LoadEmployees());
  134. var kanbanTypesTask = Task.Run(() => LoadKanbanTypes());
  135. SetupToolbar();
  136. SetupColumns();
  137. employeesTask.Wait();
  138. SetupEmployeeList();
  139. kanbanTypesTask.Wait();
  140. SetupKanbanTypesLookup(kanbanTypesTask.Result);
  141. Mode = Host.KanbanSettings.StatusSettings.CompactView ? KanbanViewMode.Compact : KanbanViewMode.Full;
  142. }
  143. private void SetupToolbar()
  144. {
  145. IncludeCompleted.Visibility = Security.IsAllowed<CanHideTaskCompletedColumn>() ? Visibility.Visible : Visibility.Collapsed;
  146. IncludeCompleted.IsChecked = IncludeCompleted.Visibility == Visibility.Visible ? Host.KanbanSettings.StatusSettings.IncludeCompleted : true;
  147. IncludeObserved.IsChecked = Host.KanbanSettings.StatusSettings.IncludeObserved;
  148. ViewType.SelectedIndex = Host.KanbanSettings.StatusSettings.CompactView ? 1 : 0;
  149. }
  150. #endregion
  151. #region Columns
  152. private void SetupColumns()
  153. {
  154. Columns.SupressNotification = true;
  155. Columns.Clear();
  156. Columns.Add(new TasksByStatusColumn("Open", "To Do"));
  157. Columns.Add(new TasksByStatusColumn("In Progress", "In Progress"));
  158. Columns.Add(new TasksByStatusColumn("Waiting", "Waiting for Others"));
  159. if (Host.KanbanSettings.StatusSettings.IncludeCompleted)
  160. Columns.Add(new TasksByStatusColumn("Complete", "Completed"));
  161. Columns.SupressNotification = false;
  162. }
  163. private TasksByStatusColumn? GetColumn(string category)
  164. {
  165. return Columns.FirstOrDefault(x => x.Category.Equals(category));
  166. }
  167. private void Column_ContextMenuOpening(object sender, ContextMenuEventArgs e)
  168. {
  169. if (sender is not FrameworkElement element || element.Tag is not TasksByStatusColumn column) return;
  170. var menu = element.ContextMenu;
  171. menu.Items.Clear();
  172. menu.AddItem("New Task", null, CreateTask);
  173. menu.AddItem($"Select all {column.Title} tasks", null, column, SelectAll);
  174. menu.AddItem($"Deselect all {column.Title} tasks", null, column, DeselectAll);
  175. }
  176. private void SelectColumn(TasksByStatusColumn column, bool selected)
  177. {
  178. SelectedTasks.Clear();
  179. if (selected)
  180. {
  181. SelectedTasks.AddRange(column.Tasks);
  182. foreach (var task in Tasks)
  183. task.Checked = string.Equals(task.Category, column.Category) ? selected : !selected;
  184. }
  185. FilterKanbans();
  186. }
  187. private void DeselectAll(TasksByStatusColumn column)
  188. {
  189. SelectColumn(column, false);
  190. }
  191. private void SelectAll(TasksByStatusColumn column)
  192. {
  193. SelectColumn(column, true);
  194. }
  195. #endregion
  196. #region Kanban Types
  197. private IEnumerable<KanbanType> LoadKanbanTypes()
  198. {
  199. return Client.Query(
  200. new Filter<KanbanType>(x => x.Hidden).IsEqualTo(false),
  201. new Columns<KanbanType>(x => x.ID, x => x.Description),
  202. new SortOrder<KanbanType>(x => x.Description))
  203. .ToObjects<KanbanType>();
  204. }
  205. private void SetupKanbanTypesLookup(IEnumerable<KanbanType> kanbanTypes)
  206. {
  207. if (ClientFactory.IsSupported<KanbanType>())
  208. {
  209. var tasktypes = new Dictionary<Guid, string>
  210. {
  211. { CoreUtils.FullGuid, "All Types" },
  212. { Guid.Empty, "Unallocated Types" }
  213. };
  214. foreach(var type in kanbanTypes)
  215. {
  216. tasktypes.Add(type.ID, type.Description);
  217. }
  218. TaskTypes.ItemsSource = tasktypes;
  219. if (tasktypes.ContainsKey(Host.KanbanSettings.StatusSettings.SelectedType))
  220. TaskTypes.SelectedValue = Host.KanbanSettings.StatusSettings.SelectedType;
  221. else
  222. TaskTypes.SelectedValue = CoreUtils.FullGuid;
  223. TaskTypesLabel.Visibility = Visibility.Visible;
  224. TaskTypes.Visibility = Visibility.Visible;
  225. }
  226. else
  227. {
  228. TaskTypesLabel.Visibility = Visibility.Collapsed;
  229. TaskTypes.Visibility = Visibility.Collapsed;
  230. }
  231. }
  232. #endregion
  233. #region Employees
  234. private EmployeeModel SelectedEmployee;
  235. private void LoadEmployees()
  236. {
  237. Employees.Clear();
  238. var employeeFilter = Security.IsAllowed<CanViewOthersTasks>()
  239. ? new Filter<Employee>(x => x.CanAllocateTasks).IsEqualTo(true)
  240. .And(new Filter<Employee>(x => x.FinishDate).IsEqualTo(DateTime.MinValue)
  241. .Or(x => x.FinishDate).IsGreaterThan(DateTime.Today))
  242. : new Filter<Employee>(x => x.ID).IsEqualTo(App.EmployeeID);
  243. var employees = Client.Query<Employee>(
  244. employeeFilter,
  245. new Columns<Employee>(x => x.ID)
  246. .Add(x => x.Thumbnail.ID)
  247. .Add(x => x.Name));
  248. var anonymous = PRSDesktop.Resources.anonymous.AsBitmapImage();
  249. anonymous.Freeze();
  250. if (Security.IsAllowed<CanViewOthersTasks>())
  251. {
  252. var everyone = PRSDesktop.Resources.everyone.AsBitmapImage();
  253. everyone.Freeze();
  254. Employees.Add(new EmployeeModel(CoreUtils.FullGuid, "All Staff", Guid.Empty, everyone));
  255. Employees.Add(new EmployeeModel(Guid.Empty, "Unallocated", Guid.Empty, null));
  256. }
  257. foreach (var employee in employees.ToObjects<Employee>())
  258. {
  259. var name = employee.ID == App.EmployeeID ? "My Tasks" : employee.Name;
  260. var model = new EmployeeModel(employee.ID, name, employee.Thumbnail.ID, anonymous);
  261. if (employee.ID == App.EmployeeID)
  262. {
  263. Employees.Insert(0, model);
  264. }
  265. else
  266. {
  267. Employees.Add(model);
  268. }
  269. }
  270. }
  271. private void SetupEmployeeList()
  272. {
  273. if (Security.IsAllowed<CanViewOthersTasks>())
  274. {
  275. EmployeeListColumn.Width = new GridLength(1.0F, GridUnitType.Auto);
  276. var thumbnails = Employees
  277. .Select(e => e.ThumbnailID)
  278. .Where(x => x != Guid.Empty).ToArray();
  279. EmployeeList.ItemsSource = Employees;
  280. SelectedEmployee = Employees.First();
  281. EmployeeList.SelectedItem = SelectedEmployee;
  282. if (thumbnails.Any())
  283. new Client<Document>().Query(
  284. new Filter<Document>(x => x.ID).InList(thumbnails),
  285. new Columns<Document>(x => x.ID, x => x.Data),
  286. null,
  287. (data, error) =>
  288. {
  289. if (data != null)
  290. ProcessThumbnails(data);
  291. }
  292. );
  293. }
  294. else
  295. {
  296. EmployeeListColumn.Width = new GridLength(0.0F, GridUnitType.Pixel);
  297. EmployeeList.ItemsSource = Employees;
  298. SelectedEmployee = Employees.First();
  299. EmployeeList.SelectedItem = SelectedEmployee;
  300. }
  301. }
  302. private bool _updatingEmployees = false;
  303. private void ProcessThumbnails(CoreTable data)
  304. {
  305. Dispatcher.Invoke(() =>
  306. {
  307. foreach (var row in data.Rows)
  308. {
  309. var id = row.Get<Document, Guid>(x => x.ID);
  310. var model = Employees.FirstOrDefault(x => x.ThumbnailID.Equals(id));
  311. if (model != null)
  312. {
  313. model.Image = new BitmapImage();
  314. model.Image.LoadImage(row.Get<Document, byte[]>(x => x.Data));
  315. }
  316. }
  317. _updatingEmployees = true;
  318. EmployeeList.ItemsSource = null;
  319. EmployeeList.ItemsSource = Employees;
  320. SelectedEmployee = Employees.First();
  321. EmployeeList.SelectedItem = SelectedEmployee;
  322. _updatingEmployees = false;
  323. });
  324. }
  325. private void Employees_SelectionChanged(object sender, SelectionChangedEventArgs e)
  326. {
  327. if (_updatingEmployees || EventSuppressor.IsSet(Suppress.This))
  328. return;
  329. SelectedEmployee = (EmployeeList.SelectedItem as EmployeeModel)!;
  330. SelectedTasks.Clear();
  331. if (IsReady)
  332. Refresh();
  333. }
  334. #endregion
  335. #region Kanbans
  336. private List<TaskModel> AllTasks { get; set; } = new();
  337. private IEnumerable<TaskModel> Tasks => Columns.SelectMany(x => x.Tasks);
  338. private readonly List<TaskModel> SelectedTasks = new();
  339. private void CreateTask()
  340. {
  341. var result = Host.CreateKanban(
  342. kanban =>
  343. {
  344. kanban.EmployeeLink.ID = SelectedEmployee.ID != CoreUtils.FullGuid ? SelectedEmployee.ID : App.EmployeeID;
  345. kanban.ManagerLink.ID = App.EmployeeID;
  346. kanban.ManagerLink.UserLink.ID = ClientFactory.UserGuid;
  347. });
  348. if (result != null)
  349. Refresh();
  350. }
  351. private void DoEdit(TaskModel task)
  352. {
  353. var result = Host.EditReferences(new[] { task });
  354. if (result)
  355. {
  356. Refresh();
  357. }
  358. }
  359. private void EditTask_Executed(object sender, ExecutedRoutedEventArgs e)
  360. {
  361. if (e.Parameter is not TaskModel model) return;
  362. DoEdit(model);
  363. }
  364. private void OpenTaskMenu_Executed(object sender, ExecutedRoutedEventArgs e)
  365. {
  366. if (e.Parameter is not KanbanResources.OpenTaskMenuCommandArgs args) return;
  367. Host.PopulateMenu(this, args.Model, args.Menu);
  368. }
  369. private void SelectTask_Executed(object sender, ExecutedRoutedEventArgs e)
  370. {
  371. if (e.Parameter is not TaskModel model) return;
  372. if (!SelectedTasks.Remove(model))
  373. {
  374. SelectedTasks.Add(model);
  375. }
  376. }
  377. private void CommandBinding_CanExecute(object sender, CanExecuteRoutedEventArgs e)
  378. {
  379. e.CanExecute = true;
  380. }
  381. private void ItemsControl_DragOver(object sender, DragEventArgs e)
  382. {
  383. if (sender is not FrameworkElement element || element.Tag is not TasksByStatusColumn column) return;
  384. e.Effects = DragDropEffects.None;
  385. if (e.Data.GetDataPresent(typeof(TaskModel)))
  386. {
  387. var model = e.Data.GetData(typeof(TaskModel)) as TaskModel;
  388. if(model is not null && model.Category != column.Category && !SelectedTasks.Any(x => x.Locked))
  389. {
  390. e.Effects = DragDropEffects.Move;
  391. }
  392. }
  393. }
  394. private void ChangeStatus(IEnumerable<TaskModel> tasks, string status)
  395. {
  396. var models = tasks
  397. .Where(x => !x.Category.Equals(status))
  398. .Where(x => !x.Locked)
  399. .ToList();
  400. if (!models.Any())
  401. {
  402. return;
  403. }
  404. if(status.Equals(Kanban.COMPLETE))
  405. {
  406. if (MessageBox.Show($"Are you sure you want to complete the selected tasks?", "Confirm Completion",
  407. MessageBoxButton.YesNo) != MessageBoxResult.Yes)
  408. return;
  409. }
  410. var completed = DateTime.Now;
  411. var kanbans = Host.LoadKanbans(tasks, new Columns<Kanban>(x => x.ID).Add(x => x.Category));
  412. foreach (var kanban in kanbans)
  413. {
  414. kanban.Category = status;
  415. }
  416. if (status.Equals(Kanban.COMPLETE))
  417. {
  418. foreach (var kanban in kanbans)
  419. {
  420. kanban.Completed = completed;
  421. }
  422. }
  423. Client.Save(kanbans, $"Task Status Updated to {status}", (o, err) =>
  424. {
  425. if (err is not null)
  426. {
  427. CoreUtils.LogException("", err);
  428. }
  429. });
  430. foreach (var model in models)
  431. {
  432. model.Checked = false;
  433. model.Category = status;
  434. }
  435. if (status.Equals(Kanban.COMPLETE))
  436. {
  437. foreach (var model in models)
  438. {
  439. model.CompletedDate = completed;
  440. }
  441. }
  442. FilterKanbans();
  443. }
  444. private void ItemsControl_Drop(object sender, DragEventArgs e)
  445. {
  446. if (sender is not FrameworkElement element || element.Tag is not TasksByStatusColumn column) return;
  447. if (e.Data.GetDataPresent(typeof(TaskModel)))
  448. {
  449. ChangeStatus(SelectedModels(e.Data.GetData(typeof(TaskModel)) as TaskModel), column.Category);
  450. }
  451. }
  452. #endregion
  453. #region Filters
  454. private Guid JobFilterID = Guid.Empty;
  455. private string SearchText = "";
  456. private Guid SelectedType = CoreUtils.FullGuid;
  457. private static Filter<T> GetSearchFilter<T>(Expression<Func<T, object?>> expression, string searchText) where T : Entity, new()
  458. {
  459. Filter<T>? result = null;
  460. var comps = searchText.Trim().Split(' ');
  461. foreach (var comp in comps)
  462. result = result == null ? new Filter<T>(expression).Contains(comp) : result.And(expression).Contains(comp);
  463. return result ?? new Filter<T>().All();
  464. }
  465. private Filter<KanbanSubscriber> GetKanbanSubscriberFilter()
  466. {
  467. var filter = new Filter<KanbanSubscriber>(x => x.Kanban.Closed).IsEqualTo(DateTime.MinValue);
  468. if (Host.Job != null)
  469. {
  470. if (Host.Job.ID != Guid.Empty)
  471. filter = filter.And(x => x.Kanban.JobLink.ID).IsEqualTo(Host.Job.ID);
  472. else
  473. filter = filter.And(x => x.Kanban.JobLink.ID).None();
  474. }
  475. // All Tasks (EmployeeID.HasValue == false) or Unallocated (EmployeeID = Guid.Empty) are retrieved directly from the Kanban Table
  476. // so if we are here, we can assume that we are pulling subscriber data
  477. var empfilter = new Filter<KanbanSubscriber>(x => x.Employee.ID).IsEqualTo(SelectedEmployee.ID);
  478. filter.Ands.Add(empfilter);
  479. if (SelectedEmployee.ID != App.EmployeeID)
  480. filter = filter.And(x => x.Kanban.Private).IsEqualTo(false);
  481. return filter;
  482. }
  483. private Filter<Kanban> GetKanbanFilter()
  484. {
  485. var filter = new Filter<Kanban>(x => x.Closed).IsEqualTo(DateTime.MinValue);
  486. if (Host.Job != null)
  487. {
  488. if (Host.Job.ID != Guid.Empty)
  489. filter = filter.And(x => x.JobLink.ID).IsEqualTo(Host.Job.ID);
  490. else
  491. filter = filter.And(x => x.JobLink.ID).None();
  492. }
  493. if (SelectedEmployee.ID != CoreUtils.FullGuid)
  494. {
  495. if (SelectedEmployee.ID != Guid.Empty)
  496. {
  497. var empfilter = new Filter<Kanban>(x => x.EmployeeLink.ID).IsEqualTo(SelectedEmployee.ID)
  498. .Or(x => x.ManagerLink.ID).IsEqualTo(SelectedEmployee.ID);
  499. filter.Ands.Add(empfilter);
  500. }
  501. else
  502. {
  503. filter = filter.And(x => x.EmployeeLink.ID).IsEqualTo(SelectedEmployee.ID);
  504. }
  505. }
  506. if (SelectedEmployee.ID != App.EmployeeID)
  507. filter = filter.And(x => x.Private).IsEqualTo(false);
  508. return filter;
  509. }
  510. private void TaskTypesLabel_OnClick(object sender, RoutedEventArgs e)
  511. {
  512. var list = new MasterList(typeof(KanbanType));
  513. list.ShowDialog();
  514. SetupKanbanTypesLookup(LoadKanbanTypes());
  515. }
  516. private void TaskTypes_SelectionChanged(object sender, SelectionChangedEventArgs e)
  517. {
  518. if (!IsReady || EventSuppressor.IsSet(Suppress.This))
  519. return;
  520. if (e.AddedItems.Count > 0)
  521. {
  522. var item = (KeyValuePair<Guid, string>)e.AddedItems[0];
  523. SelectedType = item.Key;
  524. }
  525. else
  526. {
  527. SelectedType = CoreUtils.FullGuid;
  528. }
  529. Host.KanbanSettings.StatusSettings.SelectedType = SelectedType;
  530. Host.SaveSettings();
  531. FilterKanbans();
  532. }
  533. private void JobFilterBtn_OnClick(object sender, RoutedEventArgs e)
  534. {
  535. if (JobFilterID != Guid.Empty)
  536. {
  537. JobFilterBtn.Content = "Filter Job";
  538. JobFilterID = Guid.Empty;
  539. FilterKanbans();
  540. return;
  541. }
  542. var window = new ThemableWindow();
  543. var grid = new JobGrid();
  544. grid.Reconfigure(options =>
  545. {
  546. options.Remove(DynamicGridOption.EditRows);
  547. options.Remove(DynamicGridOption.DeleteRows);
  548. options.Remove(DynamicGridOption.AddRows);
  549. options.Remove(DynamicGridOption.MultiSelect);
  550. options.Remove(DynamicGridOption.ExportData);
  551. options.Remove(DynamicGridOption.ImportData);
  552. });
  553. grid.OnSelectItem += (object sender, DynamicGridSelectionEventArgs e) =>
  554. {
  555. if (grid.SelectedRows.Count() == 0)
  556. return;
  557. else
  558. {
  559. var item = grid.SelectedRows[0];
  560. AddJobFilter(item);
  561. window.Close();
  562. }
  563. };
  564. grid.Refresh(true, true);
  565. window.Content = grid;
  566. window.ShowDialog();
  567. }
  568. private void AddJobFilter(CoreRow item)
  569. {
  570. JobFilterID = item.Get<Job, Guid>(x => x.ID);
  571. JobFilterBtn.Content = item.Get<Job, string>(x => x.JobNumber) + " (click to cancel)";
  572. FilterKanbans();
  573. }
  574. private void IncludeCompleted_Checked(object sender, RoutedEventArgs e)
  575. {
  576. if (!IsReady)
  577. return;
  578. Host.KanbanSettings.StatusSettings.IncludeCompleted = IncludeCompleted.IsChecked ?? false;
  579. Host.SaveSettings();
  580. SetupColumns();
  581. FilterKanbans();
  582. }
  583. private void IncludeObserved_Checked(object sender, RoutedEventArgs e)
  584. {
  585. if (!IsReady)
  586. return;
  587. Host.KanbanSettings.StatusSettings.IncludeObserved = IncludeObserved.IsChecked ?? false;
  588. Host.SaveSettings();
  589. FilterKanbans();
  590. }
  591. private void IncludeLocked_Checked(object sender, RoutedEventArgs e)
  592. {
  593. if (!IsReady)
  594. return;
  595. Host.KanbanSettings.StatusSettings.IncludeLocked = IncludeLocked.IsChecked ?? false;
  596. Host.SaveSettings();
  597. FilterKanbans();
  598. }
  599. private void Search_KeyUp(object sender, KeyEventArgs e)
  600. {
  601. if (string.IsNullOrWhiteSpace(Search.Text) || e.Key == Key.Return)
  602. {
  603. SearchText = Search.Text;
  604. FilterKanbans();
  605. }
  606. }
  607. #endregion
  608. #region Refresh
  609. private static Columns<IKanban> GetKanbanColumns()
  610. {
  611. return new Columns<IKanban>(
  612. x => x.ID,
  613. x => x.DueDate,
  614. x => x.Completed,
  615. x => x.Summary,
  616. x => x.Category,
  617. x => x.EmployeeLink.ID,
  618. x => x.ManagerLink.ID,
  619. x => x.Notes,
  620. x => x.Title,
  621. x => x.JobLink.ID,
  622. x => x.JobLink.JobNumber,
  623. x => x.JobLink.Name,
  624. x => x.Type.ID,
  625. x => x.Type.Code,
  626. x => x.Number,
  627. x => x.Attachments,
  628. x => x.Locked);
  629. }
  630. public void Refresh()
  631. {
  632. using var cursor = new WaitCursor();
  633. IEnumerable<IKanban> kanbans;
  634. if (SelectedEmployee.ID != CoreUtils.FullGuid && SelectedEmployee.ID != Guid.Empty)
  635. {
  636. kanbans = Client.Query(
  637. new Filter<Kanban>(x => x.ID).InQuery(GetKanbanSubscriberFilter(), x => x.Kanban.ID),
  638. GetKanbanColumns().Cast<Kanban>(),
  639. new SortOrder<Kanban>(x => x.DueDate) { Direction = SortDirection.Ascending }
  640. ).ToObjects<Kanban>();
  641. }
  642. else
  643. {
  644. kanbans = new Client<Kanban>().Query(
  645. GetKanbanFilter(),
  646. GetKanbanColumns().Cast<Kanban>(),
  647. new SortOrder<Kanban>(x => x.DueDate) { Direction = SortDirection.Ascending }
  648. ).ToObjects<Kanban>();
  649. }
  650. AllTasks = CreateModels(kanbans).ToList();
  651. FilterKanbans();
  652. }
  653. private IEnumerable<TaskModel> CreateModels(IEnumerable<IKanban> kanbans)
  654. {
  655. foreach(var kanban in kanbans)
  656. {
  657. TaskModel? model = null;
  658. try
  659. {
  660. model = new TaskModel();
  661. var employee = Employees.FirstOrDefault(x => x.ID == kanban.EmployeeLink.ID);
  662. var manager = Employees.FirstOrDefault(x => x.ID == kanban.ManagerLink.ID);
  663. model.Title = kanban.Title;
  664. model.ID = kanban.ID;
  665. model.Description = kanban.Summary ?? "";
  666. model.Category = kanban.Category;
  667. var colour = SelectedEmployee.ID == Guid.Empty
  668. || SelectedEmployee.ID == CoreUtils.FullGuid
  669. || kanban.EmployeeLink.ID == SelectedEmployee.ID
  670. ? TaskModel.KanbanColor(kanban.DueDate, kanban.Completed)
  671. : (kanban.ManagerLink.ID == SelectedEmployee.ID
  672. ? Color.Silver
  673. : Color.Plum);
  674. if (kanban.Locked)
  675. {
  676. colour = colour.MixColors(0.5F, Color.White);
  677. }
  678. model.Color = System.Windows.Media.Color.FromArgb(colour.A, colour.R, colour.G, colour.B);
  679. model.Image = employee?.Image;
  680. model.Attachments = kanban.Attachments > 0;
  681. model.DueDate = kanban.DueDate;
  682. model.CompletedDate = kanban.Completed;
  683. model.Locked = kanban.Locked;
  684. var notes = new List<List<string>> { new() };
  685. if (kanban.Notes is not null)
  686. {
  687. foreach (var line in kanban.Notes)
  688. {
  689. if (line.Equals("==================================="))
  690. {
  691. notes.Add(new());
  692. }
  693. else
  694. {
  695. notes.Last().Add(line);
  696. }
  697. }
  698. model.Notes = string.Join(
  699. "\n===================================\n",
  700. notes.Reverse<List<string>>().Select(x => string.Join('\n', x)));
  701. }
  702. model.EmployeeID = kanban.EmployeeLink.ID;
  703. model.ManagerID = kanban.ManagerLink.ID;
  704. var employeeString = "";
  705. if (kanban.EmployeeLink.ID != SelectedEmployee.ID)
  706. {
  707. if (kanban.EmployeeLink.ID == Guid.Empty)
  708. {
  709. employeeString = "Unallocated";
  710. }
  711. else
  712. {
  713. employeeString = employee is not null
  714. ? (employee.ID == App.EmployeeID
  715. ? App.EmployeeName
  716. : employee.Name)
  717. : "";
  718. }
  719. }
  720. var managerString = "";
  721. if (kanban.ManagerLink.ID != SelectedEmployee.ID)
  722. {
  723. if (kanban.ManagerLink.ID != Guid.Empty)
  724. {
  725. managerString = manager is not null
  726. ? (manager.ID == App.EmployeeID
  727. ? App.EmployeeName
  728. : manager.Name)
  729. : "";
  730. }
  731. }
  732. model.Manager = managerString;
  733. if (!string.IsNullOrEmpty(employeeString))
  734. {
  735. if (!managerString.IsNullOrWhiteSpace() && !managerString.Equals(employeeString))
  736. {
  737. model.AssignedTo = $"Assigned to {employeeString} by {managerString}";
  738. }
  739. else
  740. {
  741. model.AssignedTo = $"Assigned to {employeeString}";
  742. }
  743. }
  744. else
  745. {
  746. if (!managerString.IsNullOrWhiteSpace())
  747. {
  748. model.AssignedTo = $"Allocated by {managerString}";
  749. }
  750. }
  751. model.JobID = kanban.JobLink.ID;
  752. model.JobNumber = kanban.JobLink.JobNumber.NotWhiteSpaceOr("");
  753. model.JobName = kanban.JobLink.Name;
  754. model.Checked = SelectedTasks.Any(x => x.ID == model.ID);
  755. model.Type = new KanbanType
  756. {
  757. ID = kanban.Type.ID,
  758. Code = kanban.Type.Code
  759. };
  760. model.Number = kanban.Number;
  761. }
  762. catch(Exception e)
  763. {
  764. CoreUtils.LogException("", e);
  765. }
  766. if(model is not null)
  767. {
  768. yield return model;
  769. }
  770. }
  771. }
  772. /// <summary>
  773. /// Take the full list of kanbans loaded from the database, and filter based on the search UI elements, filtering into the columns.
  774. /// </summary>
  775. private void FilterKanbans()
  776. {
  777. Progress.Show("Loading");
  778. IEnumerable<TaskModel> filtered;
  779. if (JobFilterID == Guid.Empty)
  780. {
  781. filtered = AllTasks;
  782. }
  783. else
  784. {
  785. filtered = AllTasks.Where(x => x.JobSearch(JobFilterID));
  786. }
  787. if (!Host.KanbanSettings.StatusSettings.IncludeLocked)
  788. {
  789. filtered = filtered.Where(x => !x.Locked);
  790. }
  791. if (!Host.KanbanSettings.StatusSettings.IncludeObserved
  792. && SelectedEmployee.ID != CoreUtils.FullGuid)
  793. {
  794. filtered = filtered.Where(x => x.EmployeeID == SelectedEmployee.ID || x.ManagerID == SelectedEmployee.ID);
  795. }
  796. if (!Host.KanbanSettings.StatusSettings.IncludeCompleted)
  797. {
  798. filtered = filtered.Where(x => x.CompletedDate.IsEmpty());
  799. }
  800. if (SelectedType != CoreUtils.FullGuid)
  801. {
  802. filtered = filtered.Where(x => x.Type.ID == SelectedType);
  803. }
  804. filtered = filtered.Where(x => x.Search(SearchText.Split()))
  805. .OrderBy(x => x.EmployeeID == SelectedEmployee.ID ? 0 : 1)
  806. .ThenBy(x => x.DueDate);
  807. SelectedTasks.Clear();
  808. foreach (var column in Columns)
  809. {
  810. column.Tasks.Clear();
  811. }
  812. foreach (var task in filtered)
  813. {
  814. var column = GetColumn(task.Category);
  815. column?.Tasks.Add(task);
  816. if (task.Checked)
  817. {
  818. SelectedTasks.Add(task);
  819. }
  820. }
  821. Progress.Close();
  822. }
  823. #endregion
  824. #region ITaskControl
  825. public ITaskHost Host { get; set; }
  826. public KanbanViewType KanbanViewType => KanbanViewType.Status;
  827. public bool IsReady { get; set; }
  828. public string SectionName => "Tasks By Status";
  829. public DataModel DataModel(Selection selection)
  830. {
  831. var ids = SelectedModels().Select(x => x.ID).ToArray();
  832. return new AutoDataModel<Kanban>(new Filter<Kanban>(x => x.ID).InList(ids));
  833. }
  834. public IEnumerable<TaskModel> SelectedModels(TaskModel? sender = null)
  835. {
  836. if(sender is null)
  837. {
  838. return SelectedTasks;
  839. }
  840. else
  841. {
  842. var result = SelectedTasks.ToList();
  843. if (!result.Contains(sender))
  844. {
  845. result.Add(sender);
  846. }
  847. return result;
  848. }
  849. }
  850. #endregion
  851. private void Export_Click(object sender, RoutedEventArgs e)
  852. {
  853. var form = new DynamicExportForm(typeof(Kanban), GetKanbanColumns().ColumnNames());
  854. if (form.ShowDialog() != true)
  855. return;
  856. var export = new Client<Kanban>().Query(
  857. GetKanbanFilter(),
  858. new Columns<Kanban>(form.Fields),
  859. LookupFactory.DefineSort<Kanban>()
  860. );
  861. var employee = "Tasks for All Staff";
  862. if (SelectedEmployee.ID != CoreUtils.FullGuid)
  863. {
  864. if (SelectedEmployee.ID == Guid.Empty)
  865. {
  866. employee = "Unallocated Tasks";
  867. }
  868. else
  869. {
  870. var model = Employees.FirstOrDefault(x => x.ID == SelectedEmployee.ID);
  871. employee = model == null ? "Tasks for (Unknown)" : "Tasks for " + (model.ID == App.EmployeeID ? App.EmployeeName : model.Name);
  872. }
  873. }
  874. ExcelExporter.DoExport<Kanban>(
  875. export,
  876. string.Format(
  877. "{0} ({1:dd-MMM-yy})",
  878. employee,
  879. DateTime.Today
  880. )
  881. );
  882. }
  883. private KanbanViewMode _mode;
  884. public KanbanViewMode Mode
  885. {
  886. get => _mode;
  887. set
  888. {
  889. _mode = value;
  890. OnPropertyChanged();
  891. }
  892. }
  893. private void ViewType_SelectionChanged(object sender, SelectionChangedEventArgs e)
  894. {
  895. if (!IsReady || EventSuppressor.IsSet(Suppress.This))
  896. return;
  897. Mode = ViewType.SelectedIndex switch
  898. {
  899. 0 => KanbanViewMode.Full,
  900. 1 => KanbanViewMode.Compact,
  901. _ => KanbanViewMode.Full
  902. };
  903. Host.KanbanSettings.StatusSettings.CompactView = Mode == KanbanViewMode.Compact;
  904. Host.SaveSettings();
  905. }
  906. }