1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060 |
- using Comal.Classes;
- using InABox.Clients;
- using InABox.Core;
- using InABox.WPF;
- using System;
- using System.Collections.Generic;
- using System.Globalization;
- using System.Linq;
- using System.Linq.Expressions;
- using System.Reflection;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Data;
- using System.Windows.Documents;
- using System.Windows.Input;
- using System.Drawing;
- using System.Windows.Media;
- using System.Windows.Media.Imaging;
- using Color = System.Drawing.Color;
- using InABox.DynamicGrid;
- using InABox.Wpf;
- using MailKit.Search;
- using NPOI.SS.Formula.Functions;
- using System.ComponentModel;
- using System.Runtime.CompilerServices;
- using System.Collections.ObjectModel;
- using System.Collections.Specialized;
- using Syncfusion.UI.Xaml.Kanban;
- namespace PRSDesktop;
- public class TasksByStatusColumn : INotifyPropertyChanged
- {
- public string Category { get; }
- public string Title { get; }
- public int NumTasks { get => Tasks.Count; }
- public double NumHours { get => Tasks.Sum(x => x.EstimatedTime.TotalHours); }
- public ObservableCollection<TaskModel> Tasks { get; } = new();
- public TasksByStatusColumn(string category, string title)
- {
- Category = category;
- Title = title;
- Tasks.CollectionChanged += Tasks_CollectionChanged;
- }
- private void Tasks_CollectionChanged(object? sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
- {
- OnPropertyChanged(nameof(Tasks));
- }
- public event PropertyChangedEventHandler? PropertyChanged;
- // Create the OnPropertyChanged method to raise the event
- // The calling member's name will be used as the parameter.
- protected void OnPropertyChanged([CallerMemberName] string? name = null)
- {
- PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
- if(name == nameof(Tasks))
- {
- OnPropertyChanged(nameof(NumTasks));
- OnPropertyChanged(nameof(NumHours));
- }
- }
- }
- public class EmployeeModel
- {
- public Guid ID { get; set; }
- public string Name { get; set; }
- public BitmapImage? Image { get; set; }
- public Guid ThumbnailID { get; set; }
- public EmployeeModel(Guid id, string name, Guid thumbnail, BitmapImage? image)
- {
- ID = id;
- Name = name;
- Image = image;
- ThumbnailID = thumbnail;
- }
- }
- public class SuspendableObservableCollection<T> : ObservableCollection<T>
- {
- private bool _notificationSupressed = false;
- private bool _supressNotification = false;
- public bool SupressNotification
- {
- get
- {
- return _supressNotification;
- }
- set
- {
- _supressNotification = value;
- if (_supressNotification == false && _notificationSupressed)
- {
- this.OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
- _notificationSupressed = false;
- }
- }
- }
- protected override void OnCollectionChanged(NotifyCollectionChangedEventArgs e)
- {
- if (SupressNotification)
- {
- _notificationSupressed = true;
- return;
- }
- base.OnCollectionChanged(e);
- }
- }
- /// <summary>
- /// Interaction logic for TasksByStatusControl.xaml
- /// </summary>
- public partial class TasksByStatusControl : UserControl, ITaskControl, INotifyPropertyChanged
- {
- private enum Suppress
- {
- This
- }
- public SuspendableObservableCollection<TasksByStatusColumn> Columns { get; private set; } = new();
- private List<EmployeeModel> Employees = new();
- public TasksByStatusControl()
- {
- InitializeComponent();
- }
- #region INotifyPropertyChanged
- public event PropertyChangedEventHandler? PropertyChanged;
- // Create the OnPropertyChanged method to raise the event
- // The calling member's name will be used as the parameter.
- protected void OnPropertyChanged([CallerMemberName] string? name = null)
- {
- PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
- }
- #endregion
- #region Setup
- public void Setup()
- {
- var employeesTask = Task.Run(() => LoadEmployees());
- var kanbanTypesTask = Task.Run(() => LoadKanbanTypes());
- SetupToolbar();
- SetupColumns();
- employeesTask.Wait();
- SetupEmployeeList();
- kanbanTypesTask.Wait();
- SetupKanbanTypesLookup(kanbanTypesTask.Result);
- Mode = Host.KanbanSettings.StatusSettings.CompactView ? KanbanViewMode.Compact : KanbanViewMode.Full;
- }
- private void SetupToolbar()
- {
- IncludeCompleted.Visibility = Security.IsAllowed<CanHideTaskCompletedColumn>() ? Visibility.Visible : Visibility.Collapsed;
- IncludeCompleted.IsChecked = IncludeCompleted.Visibility == Visibility.Visible ? Host.KanbanSettings.StatusSettings.IncludeCompleted : true;
- IncludeObserved.IsChecked = Host.KanbanSettings.StatusSettings.IncludeObserved;
- ViewType.SelectedIndex = Host.KanbanSettings.StatusSettings.CompactView ? 1 : 0;
- }
- #endregion
- #region Columns
- private void SetupColumns()
- {
- Columns.SupressNotification = true;
- Columns.Clear();
- Columns.Add(new TasksByStatusColumn("Open", "To Do"));
- Columns.Add(new TasksByStatusColumn("In Progress", "In Progress"));
- Columns.Add(new TasksByStatusColumn("Waiting", "Waiting for Others"));
- if (Host.KanbanSettings.StatusSettings.IncludeCompleted)
- Columns.Add(new TasksByStatusColumn("Complete", "Completed"));
- Columns.SupressNotification = false;
- }
- private TasksByStatusColumn? GetColumn(string category)
- {
- return Columns.FirstOrDefault(x => x.Category.Equals(category));
- }
- private void Column_ContextMenuOpening(object sender, ContextMenuEventArgs e)
- {
- if (sender is not FrameworkElement element || element.Tag is not TasksByStatusColumn column) return;
- var menu = element.ContextMenu;
- menu.Items.Clear();
- menu.AddItem("New Task", null, CreateTask);
- menu.AddItem($"Select all {column.Title} tasks", null, column, SelectAll);
- menu.AddItem($"Deselect all {column.Title} tasks", null, column, DeselectAll);
- }
- private void SelectColumn(TasksByStatusColumn column, bool selected)
- {
- SelectedTasks.Clear();
- if (selected)
- {
- SelectedTasks.AddRange(column.Tasks);
- foreach (var task in Tasks)
- task.Checked = string.Equals(task.Category, column.Category) ? selected : !selected;
- }
- FilterKanbans();
- }
- private void DeselectAll(TasksByStatusColumn column)
- {
- SelectColumn(column, false);
- }
- private void SelectAll(TasksByStatusColumn column)
- {
- SelectColumn(column, true);
- }
- #endregion
- #region Kanban Types
- private IEnumerable<KanbanType> LoadKanbanTypes()
- {
- return Client.Query(
- new Filter<KanbanType>(x => x.Hidden).IsEqualTo(false),
- new Columns<KanbanType>(x => x.ID, x => x.Description),
- new SortOrder<KanbanType>(x => x.Description))
- .ToObjects<KanbanType>();
- }
- private void SetupKanbanTypesLookup(IEnumerable<KanbanType> kanbanTypes)
- {
- if (ClientFactory.IsSupported<KanbanType>())
- {
- var tasktypes = new Dictionary<Guid, string>
- {
- { CoreUtils.FullGuid, "All Types" },
- { Guid.Empty, "Unallocated Types" }
- };
- foreach(var type in kanbanTypes)
- {
- tasktypes.Add(type.ID, type.Description);
- }
- TaskTypes.ItemsSource = tasktypes;
- if (tasktypes.ContainsKey(Host.KanbanSettings.StatusSettings.SelectedType))
- TaskTypes.SelectedValue = Host.KanbanSettings.StatusSettings.SelectedType;
- else
- TaskTypes.SelectedValue = CoreUtils.FullGuid;
- TaskTypesLabel.Visibility = Visibility.Visible;
- TaskTypes.Visibility = Visibility.Visible;
- }
- else
- {
- TaskTypesLabel.Visibility = Visibility.Collapsed;
- TaskTypes.Visibility = Visibility.Collapsed;
- }
- }
- #endregion
- #region Employees
- private EmployeeModel SelectedEmployee;
- private void LoadEmployees()
- {
- Employees.Clear();
- var employeeFilter = Security.IsAllowed<CanViewOthersTasks>()
- ? new Filter<Employee>(x => x.CanAllocateTasks).IsEqualTo(true)
- .And(new Filter<Employee>(x => x.FinishDate).IsEqualTo(DateTime.MinValue)
- .Or(x => x.FinishDate).IsGreaterThan(DateTime.Today))
- : new Filter<Employee>(x => x.ID).IsEqualTo(App.EmployeeID);
- var employees = Client.Query<Employee>(
- employeeFilter,
- new Columns<Employee>(x => x.ID)
- .Add(x => x.Thumbnail.ID)
- .Add(x => x.Name));
- var anonymous = PRSDesktop.Resources.anonymous.AsBitmapImage();
- anonymous.Freeze();
- if (Security.IsAllowed<CanViewOthersTasks>())
- {
- var everyone = PRSDesktop.Resources.everyone.AsBitmapImage();
- everyone.Freeze();
- Employees.Add(new EmployeeModel(CoreUtils.FullGuid, "All Staff", Guid.Empty, everyone));
- Employees.Add(new EmployeeModel(Guid.Empty, "Unallocated", Guid.Empty, null));
- }
- foreach (var employee in employees.ToObjects<Employee>())
- {
- var name = employee.ID == App.EmployeeID ? "My Tasks" : employee.Name;
- var model = new EmployeeModel(employee.ID, name, employee.Thumbnail.ID, anonymous);
- if (employee.ID == App.EmployeeID)
- {
- Employees.Insert(0, model);
- }
- else
- {
- Employees.Add(model);
- }
- }
- }
- private void SetupEmployeeList()
- {
- if (Security.IsAllowed<CanViewOthersTasks>())
- {
- EmployeeListColumn.Width = new GridLength(1.0F, GridUnitType.Auto);
- var thumbnails = Employees
- .Select(e => e.ThumbnailID)
- .Where(x => x != Guid.Empty).ToArray();
- EmployeeList.ItemsSource = Employees;
- SelectedEmployee = Employees.First();
- EmployeeList.SelectedItem = SelectedEmployee;
- if (thumbnails.Any())
- new Client<Document>().Query(
- new Filter<Document>(x => x.ID).InList(thumbnails),
- new Columns<Document>(x => x.ID, x => x.Data),
- null,
- (data, error) =>
- {
- if (data != null)
- ProcessThumbnails(data);
- }
- );
- }
- else
- {
- EmployeeListColumn.Width = new GridLength(0.0F, GridUnitType.Pixel);
- EmployeeList.ItemsSource = Employees;
- SelectedEmployee = Employees.First();
- EmployeeList.SelectedItem = SelectedEmployee;
- }
- }
- private bool _updatingEmployees = false;
- private void ProcessThumbnails(CoreTable data)
- {
- Dispatcher.Invoke(() =>
- {
- foreach (var row in data.Rows)
- {
- var id = row.Get<Document, Guid>(x => x.ID);
- var model = Employees.FirstOrDefault(x => x.ThumbnailID.Equals(id));
- if (model != null)
- {
- model.Image = new BitmapImage();
- model.Image.LoadImage(row.Get<Document, byte[]>(x => x.Data));
- }
- }
- _updatingEmployees = true;
- EmployeeList.ItemsSource = null;
- EmployeeList.ItemsSource = Employees;
- SelectedEmployee = Employees.First();
- EmployeeList.SelectedItem = SelectedEmployee;
- _updatingEmployees = false;
- });
- }
- private void Employees_SelectionChanged(object sender, SelectionChangedEventArgs e)
- {
- if (_updatingEmployees || EventSuppressor.IsSet(Suppress.This))
- return;
- SelectedEmployee = (EmployeeList.SelectedItem as EmployeeModel)!;
- SelectedTasks.Clear();
- if (IsReady)
- Refresh();
- }
- #endregion
- #region Kanbans
- private List<TaskModel> AllTasks { get; set; } = new();
- private IEnumerable<TaskModel> Tasks => Columns.SelectMany(x => x.Tasks);
- private readonly List<TaskModel> SelectedTasks = new();
- private void CreateTask()
- {
- var result = Host.CreateKanban(
- kanban =>
- {
- kanban.EmployeeLink.ID = SelectedEmployee.ID != CoreUtils.FullGuid ? SelectedEmployee.ID : App.EmployeeID;
- kanban.ManagerLink.ID = App.EmployeeID;
- kanban.ManagerLink.UserLink.ID = ClientFactory.UserGuid;
- });
- if (result != null)
- Refresh();
- }
- private void DoEdit(TaskModel task)
- {
- var result = Host.EditReferences(new[] { task });
- if (result)
- {
- Refresh();
- }
- }
- private void EditTask_Executed(object sender, ExecutedRoutedEventArgs e)
- {
- if (e.Parameter is not TaskModel model) return;
- DoEdit(model);
- }
- private void OpenTaskMenu_Executed(object sender, ExecutedRoutedEventArgs e)
- {
- if (e.Parameter is not KanbanResources.OpenTaskMenuCommandArgs args) return;
- Host.PopulateMenu(this, args.Model, args.Menu);
- }
- private void SelectTask_Executed(object sender, ExecutedRoutedEventArgs e)
- {
- if (e.Parameter is not TaskModel model) return;
- if (!SelectedTasks.Remove(model))
- {
- SelectedTasks.Add(model);
- }
- }
- private void CommandBinding_CanExecute(object sender, CanExecuteRoutedEventArgs e)
- {
- e.CanExecute = true;
- }
- private void ItemsControl_DragOver(object sender, DragEventArgs e)
- {
- if (sender is not FrameworkElement element || element.Tag is not TasksByStatusColumn column) return;
- e.Effects = DragDropEffects.None;
- if (e.Data.GetDataPresent(typeof(TaskModel)))
- {
- var model = e.Data.GetData(typeof(TaskModel)) as TaskModel;
- if(model is not null && model.Category != column.Category && !SelectedTasks.Any(x => x.Locked))
- {
- e.Effects = DragDropEffects.Move;
- }
- }
- }
- private void ChangeStatus(IEnumerable<TaskModel> tasks, string status)
- {
- var models = tasks
- .Where(x => !x.Category.Equals(status))
- .Where(x => !x.Locked)
- .ToList();
- if (!models.Any())
- {
- return;
- }
- if(status.Equals(Kanban.COMPLETE))
- {
- if (MessageBox.Show($"Are you sure you want to complete the selected tasks?", "Confirm Completion",
- MessageBoxButton.YesNo) != MessageBoxResult.Yes)
- return;
- }
- var completed = DateTime.Now;
- var kanbans = Host.LoadKanbans(tasks, new Columns<Kanban>(x => x.ID).Add(x => x.Category));
- foreach (var kanban in kanbans)
- {
- kanban.Category = status;
- }
- if (status.Equals(Kanban.COMPLETE))
- {
- foreach (var kanban in kanbans)
- {
- kanban.Completed = completed;
- }
- }
- Client.Save(kanbans, $"Task Status Updated to {status}", (o, err) =>
- {
- if (err is not null)
- {
- CoreUtils.LogException("", err);
- }
- });
- foreach (var model in models)
- {
- model.Checked = false;
- model.Category = status;
- }
- if (status.Equals(Kanban.COMPLETE))
- {
- foreach (var model in models)
- {
- model.CompletedDate = completed;
- }
- }
- FilterKanbans();
- }
- private void ItemsControl_Drop(object sender, DragEventArgs e)
- {
- if (sender is not FrameworkElement element || element.Tag is not TasksByStatusColumn column) return;
- if (e.Data.GetDataPresent(typeof(TaskModel)))
- {
- ChangeStatus(SelectedModels(e.Data.GetData(typeof(TaskModel)) as TaskModel), column.Category);
- }
- }
- #endregion
- #region Filters
- private Guid JobFilterID = Guid.Empty;
- private string SearchText = "";
- private Guid SelectedType = CoreUtils.FullGuid;
- private static Filter<T> GetSearchFilter<T>(Expression<Func<T, object?>> expression, string searchText) where T : Entity, new()
- {
- Filter<T>? result = null;
- var comps = searchText.Trim().Split(' ');
- foreach (var comp in comps)
- result = result == null ? new Filter<T>(expression).Contains(comp) : result.And(expression).Contains(comp);
- return result ?? new Filter<T>().All();
- }
- private Filter<KanbanSubscriber> GetKanbanSubscriberFilter()
- {
- var filter = new Filter<KanbanSubscriber>(x => x.Kanban.Closed).IsEqualTo(DateTime.MinValue);
- if (Host.Job != null)
- {
- if (Host.Job.ID != Guid.Empty)
- filter = filter.And(x => x.Kanban.JobLink.ID).IsEqualTo(Host.Job.ID);
- else
- filter = filter.And(x => x.Kanban.JobLink.ID).None();
- }
- // All Tasks (EmployeeID.HasValue == false) or Unallocated (EmployeeID = Guid.Empty) are retrieved directly from the Kanban Table
- // so if we are here, we can assume that we are pulling subscriber data
- var empfilter = new Filter<KanbanSubscriber>(x => x.Employee.ID).IsEqualTo(SelectedEmployee.ID);
- filter.Ands.Add(empfilter);
- if (SelectedEmployee.ID != App.EmployeeID)
- filter = filter.And(x => x.Kanban.Private).IsEqualTo(false);
- return filter;
- }
- private Filter<Kanban> GetKanbanFilter()
- {
- var filter = new Filter<Kanban>(x => x.Closed).IsEqualTo(DateTime.MinValue);
- if (Host.Job != null)
- {
- if (Host.Job.ID != Guid.Empty)
- filter = filter.And(x => x.JobLink.ID).IsEqualTo(Host.Job.ID);
- else
- filter = filter.And(x => x.JobLink.ID).None();
- }
- if (SelectedEmployee.ID != CoreUtils.FullGuid)
- {
- if (SelectedEmployee.ID != Guid.Empty)
- {
- var empfilter = new Filter<Kanban>(x => x.EmployeeLink.ID).IsEqualTo(SelectedEmployee.ID)
- .Or(x => x.ManagerLink.ID).IsEqualTo(SelectedEmployee.ID);
- filter.Ands.Add(empfilter);
- }
- else
- {
- filter = filter.And(x => x.EmployeeLink.ID).IsEqualTo(SelectedEmployee.ID);
- }
- }
- if (SelectedEmployee.ID != App.EmployeeID)
- filter = filter.And(x => x.Private).IsEqualTo(false);
- return filter;
- }
- private void TaskTypesLabel_OnClick(object sender, RoutedEventArgs e)
- {
- var list = new MasterList(typeof(KanbanType));
- list.ShowDialog();
- SetupKanbanTypesLookup(LoadKanbanTypes());
- }
- private void TaskTypes_SelectionChanged(object sender, SelectionChangedEventArgs e)
- {
- if (!IsReady || EventSuppressor.IsSet(Suppress.This))
- return;
- if (e.AddedItems.Count > 0)
- {
- var item = (KeyValuePair<Guid, string>)e.AddedItems[0];
- SelectedType = item.Key;
- }
- else
- {
- SelectedType = CoreUtils.FullGuid;
- }
- Host.KanbanSettings.StatusSettings.SelectedType = SelectedType;
- Host.SaveSettings();
- FilterKanbans();
- }
- private void JobFilterBtn_OnClick(object sender, RoutedEventArgs e)
- {
- if (JobFilterID != Guid.Empty)
- {
- JobFilterBtn.Content = "Filter Job";
- JobFilterID = Guid.Empty;
- FilterKanbans();
- return;
- }
- var window = new ThemableWindow();
- var grid = new JobGrid();
- grid.Reconfigure(options =>
- {
- options.Remove(DynamicGridOption.EditRows);
- options.Remove(DynamicGridOption.DeleteRows);
- options.Remove(DynamicGridOption.AddRows);
- options.Remove(DynamicGridOption.MultiSelect);
- options.Remove(DynamicGridOption.ExportData);
- options.Remove(DynamicGridOption.ImportData);
- });
- grid.OnSelectItem += (object sender, DynamicGridSelectionEventArgs e) =>
- {
- if (grid.SelectedRows.Count() == 0)
- return;
- else
- {
- var item = grid.SelectedRows[0];
- AddJobFilter(item);
- window.Close();
- }
- };
- grid.Refresh(true, true);
- window.Content = grid;
- window.ShowDialog();
- }
- private void AddJobFilter(CoreRow item)
- {
- JobFilterID = item.Get<Job, Guid>(x => x.ID);
- JobFilterBtn.Content = item.Get<Job, string>(x => x.JobNumber) + " (click to cancel)";
- FilterKanbans();
- }
- private void IncludeCompleted_Checked(object sender, RoutedEventArgs e)
- {
- if (!IsReady)
- return;
- Host.KanbanSettings.StatusSettings.IncludeCompleted = IncludeCompleted.IsChecked ?? false;
- Host.SaveSettings();
- SetupColumns();
- FilterKanbans();
- }
- private void IncludeObserved_Checked(object sender, RoutedEventArgs e)
- {
- if (!IsReady)
- return;
- Host.KanbanSettings.StatusSettings.IncludeObserved = IncludeObserved.IsChecked ?? false;
- Host.SaveSettings();
- FilterKanbans();
- }
- private void IncludeLocked_Checked(object sender, RoutedEventArgs e)
- {
- if (!IsReady)
- return;
- Host.KanbanSettings.StatusSettings.IncludeLocked = IncludeLocked.IsChecked ?? false;
- Host.SaveSettings();
- FilterKanbans();
- }
- private void Search_KeyUp(object sender, KeyEventArgs e)
- {
- if (string.IsNullOrWhiteSpace(Search.Text) || e.Key == Key.Return)
- {
- SearchText = Search.Text;
- FilterKanbans();
- }
- }
- #endregion
- #region Refresh
- private static Columns<IKanban> GetKanbanColumns()
- {
- return new Columns<IKanban>(
- x => x.ID,
- x => x.DueDate,
- x => x.Completed,
- x => x.Summary,
- x => x.Category,
- x => x.EmployeeLink.ID,
- x => x.ManagerLink.ID,
- x => x.Notes,
- x => x.Title,
- x => x.JobLink.ID,
- x => x.JobLink.JobNumber,
- x => x.JobLink.Name,
- x => x.Type.ID,
- x => x.Type.Code,
- x => x.Number,
- x => x.Attachments,
- x => x.Locked);
- }
- public void Refresh()
- {
- using var cursor = new WaitCursor();
- IEnumerable<IKanban> kanbans;
- if (SelectedEmployee.ID != CoreUtils.FullGuid && SelectedEmployee.ID != Guid.Empty)
- {
- kanbans = Client.Query(
- new Filter<Kanban>(x => x.ID).InQuery(GetKanbanSubscriberFilter(), x => x.Kanban.ID),
- GetKanbanColumns().Cast<Kanban>(),
- new SortOrder<Kanban>(x => x.DueDate) { Direction = SortDirection.Ascending }
- ).ToObjects<Kanban>();
- }
- else
- {
- kanbans = new Client<Kanban>().Query(
- GetKanbanFilter(),
- GetKanbanColumns().Cast<Kanban>(),
- new SortOrder<Kanban>(x => x.DueDate) { Direction = SortDirection.Ascending }
- ).ToObjects<Kanban>();
- }
- AllTasks = CreateModels(kanbans).ToList();
- FilterKanbans();
- }
- private IEnumerable<TaskModel> CreateModels(IEnumerable<IKanban> kanbans)
- {
- foreach(var kanban in kanbans)
- {
- TaskModel? model = null;
- try
- {
- model = new TaskModel();
- var employee = Employees.FirstOrDefault(x => x.ID == kanban.EmployeeLink.ID);
- var manager = Employees.FirstOrDefault(x => x.ID == kanban.ManagerLink.ID);
- model.Title = kanban.Title;
- model.ID = kanban.ID;
- model.Description = kanban.Summary ?? "";
- model.Category = kanban.Category;
- var colour = SelectedEmployee.ID == Guid.Empty
- || SelectedEmployee.ID == CoreUtils.FullGuid
- || kanban.EmployeeLink.ID == SelectedEmployee.ID
- ? TaskModel.KanbanColor(kanban.DueDate, kanban.Completed)
- : (kanban.ManagerLink.ID == SelectedEmployee.ID
- ? Color.Silver
- : Color.Plum);
- if (kanban.Locked)
- {
- colour = colour.MixColors(0.5F, Color.White);
- }
- model.Color = System.Windows.Media.Color.FromArgb(colour.A, colour.R, colour.G, colour.B);
- model.Image = employee?.Image;
- model.Attachments = kanban.Attachments > 0;
- model.DueDate = kanban.DueDate;
- model.CompletedDate = kanban.Completed;
- model.Locked = kanban.Locked;
- var notes = new List<List<string>> { new() };
- if (kanban.Notes is not null)
- {
- foreach (var line in kanban.Notes)
- {
- if (line.Equals("==================================="))
- {
- notes.Add(new());
- }
- else
- {
- notes.Last().Add(line);
- }
- }
- model.Notes = string.Join(
- "\n===================================\n",
- notes.Reverse<List<string>>().Select(x => string.Join('\n', x)));
- }
- model.EmployeeID = kanban.EmployeeLink.ID;
- model.ManagerID = kanban.ManagerLink.ID;
- var employeeString = "";
- if (kanban.EmployeeLink.ID != SelectedEmployee.ID)
- {
- if (kanban.EmployeeLink.ID == Guid.Empty)
- {
- employeeString = "Unallocated";
- }
- else
- {
- employeeString = employee is not null
- ? (employee.ID == App.EmployeeID
- ? App.EmployeeName
- : employee.Name)
- : "";
- }
- }
- var managerString = "";
- if (kanban.ManagerLink.ID != SelectedEmployee.ID)
- {
- if (kanban.ManagerLink.ID != Guid.Empty)
- {
- managerString = manager is not null
- ? (manager.ID == App.EmployeeID
- ? App.EmployeeName
- : manager.Name)
- : "";
- }
- }
- model.Manager = managerString;
- if (!string.IsNullOrEmpty(employeeString))
- {
- if (!managerString.IsNullOrWhiteSpace() && !managerString.Equals(employeeString))
- {
- model.AssignedTo = $"Assigned to {employeeString} by {managerString}";
- }
- else
- {
- model.AssignedTo = $"Assigned to {employeeString}";
- }
- }
- else
- {
- if (!managerString.IsNullOrWhiteSpace())
- {
- model.AssignedTo = $"Allocated by {managerString}";
- }
- }
- model.JobID = kanban.JobLink.ID;
- model.JobNumber = kanban.JobLink.JobNumber.NotWhiteSpaceOr("");
- model.JobName = kanban.JobLink.Name;
- model.Checked = SelectedTasks.Any(x => x.ID == model.ID);
- model.Type = new KanbanType
- {
- ID = kanban.Type.ID,
- Code = kanban.Type.Code
- };
- model.Number = kanban.Number;
- }
- catch(Exception e)
- {
- CoreUtils.LogException("", e);
- }
- if(model is not null)
- {
- yield return model;
- }
- }
- }
- /// <summary>
- /// Take the full list of kanbans loaded from the database, and filter based on the search UI elements, filtering into the columns.
- /// </summary>
- private void FilterKanbans()
- {
- Progress.Show("Loading");
- IEnumerable<TaskModel> filtered;
- if (JobFilterID == Guid.Empty)
- {
- filtered = AllTasks;
- }
- else
- {
- filtered = AllTasks.Where(x => x.JobSearch(JobFilterID));
- }
- if (!Host.KanbanSettings.StatusSettings.IncludeLocked)
- {
- filtered = filtered.Where(x => !x.Locked);
- }
- if (!Host.KanbanSettings.StatusSettings.IncludeObserved
- && SelectedEmployee.ID != CoreUtils.FullGuid)
- {
- filtered = filtered.Where(x => x.EmployeeID == SelectedEmployee.ID || x.ManagerID == SelectedEmployee.ID);
- }
- if (!Host.KanbanSettings.StatusSettings.IncludeCompleted)
- {
- filtered = filtered.Where(x => x.CompletedDate.IsEmpty());
- }
- if (SelectedType != CoreUtils.FullGuid)
- {
- filtered = filtered.Where(x => x.Type.ID == SelectedType);
- }
- filtered = filtered.Where(x => x.Search(SearchText.Split()))
- .OrderBy(x => x.EmployeeID == SelectedEmployee.ID ? 0 : 1)
- .ThenBy(x => x.DueDate);
- SelectedTasks.Clear();
- foreach (var column in Columns)
- {
- column.Tasks.Clear();
- }
- foreach (var task in filtered)
- {
- var column = GetColumn(task.Category);
- column?.Tasks.Add(task);
- if (task.Checked)
- {
- SelectedTasks.Add(task);
- }
- }
- Progress.Close();
- }
- #endregion
- #region ITaskControl
- public ITaskHost Host { get; set; }
- public KanbanViewType KanbanViewType => KanbanViewType.Status;
- public bool IsReady { get; set; }
- public string SectionName => "Tasks By Status";
- public DataModel DataModel(Selection selection)
- {
- var ids = SelectedModels().Select(x => x.ID).ToArray();
- return new AutoDataModel<Kanban>(new Filter<Kanban>(x => x.ID).InList(ids));
- }
- public IEnumerable<TaskModel> SelectedModels(TaskModel? sender = null)
- {
- if(sender is null)
- {
- return SelectedTasks;
- }
- else
- {
- var result = SelectedTasks.ToList();
- if (!result.Contains(sender))
- {
- result.Add(sender);
- }
- return result;
- }
- }
- #endregion
- private void Export_Click(object sender, RoutedEventArgs e)
- {
- var form = new DynamicExportForm(typeof(Kanban), GetKanbanColumns().ColumnNames());
- if (form.ShowDialog() != true)
- return;
- var export = new Client<Kanban>().Query(
- GetKanbanFilter(),
- new Columns<Kanban>(form.Fields),
- LookupFactory.DefineSort<Kanban>()
- );
- var employee = "Tasks for All Staff";
- if (SelectedEmployee.ID != CoreUtils.FullGuid)
- {
- if (SelectedEmployee.ID == Guid.Empty)
- {
- employee = "Unallocated Tasks";
- }
- else
- {
- var model = Employees.FirstOrDefault(x => x.ID == SelectedEmployee.ID);
- employee = model == null ? "Tasks for (Unknown)" : "Tasks for " + (model.ID == App.EmployeeID ? App.EmployeeName : model.Name);
- }
- }
- ExcelExporter.DoExport<Kanban>(
- export,
- string.Format(
- "{0} ({1:dd-MMM-yy})",
- employee,
- DateTime.Today
- )
- );
- }
- private KanbanViewMode _mode;
- public KanbanViewMode Mode
- {
- get => _mode;
- set
- {
- _mode = value;
- OnPropertyChanged();
- }
- }
- private void ViewType_SelectionChanged(object sender, SelectionChangedEventArgs e)
- {
- if (!IsReady || EventSuppressor.IsSet(Suppress.This))
- return;
- Mode = ViewType.SelectedIndex switch
- {
- 0 => KanbanViewMode.Full,
- 1 => KanbanViewMode.Compact,
- _ => KanbanViewMode.Full
- };
- Host.KanbanSettings.StatusSettings.CompactView = Mode == KanbanViewMode.Compact;
- Host.SaveSettings();
- }
- }
|