TaskWidget.xaml.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. using System;
  2. using System.ComponentModel;
  3. using System.Data;
  4. using System.Linq;
  5. using System.Windows.Controls;
  6. using Comal.Classes;
  7. using InABox.Clients;
  8. using InABox.Configuration;
  9. using InABox.Core;
  10. using PRSDesktop.WidgetGroups;
  11. namespace PRSDesktop
  12. {
  13. public class TaskWidgetProperties : IUserConfigurationSettings, IDashboardProperties { }
  14. public class TaskWidgetElement : DashboardElement<TaskWidget, Common, TaskWidgetProperties> { }
  15. /// <summary>
  16. /// Interaction logic for TaskWidget.xaml
  17. /// </summary>
  18. public partial class TaskWidget : UserControl, IDashboardWidget<Common, TaskWidgetProperties>
  19. {
  20. private DateTime _date = DateTime.Today;
  21. private Guid _groupid = CoreUtils.FullGuid;
  22. private readonly DataTable report = new();
  23. private CoreTable tasks;
  24. public TaskWidget()
  25. {
  26. InitializeComponent();
  27. report.Columns.Add("ID", typeof(Guid));
  28. report.Columns.Add("Name", typeof(string));
  29. report.Columns.Add("Current", typeof(int));
  30. report.Columns.Add("Others", typeof(int));
  31. report.Columns.Add("New", typeof(int));
  32. report.Columns.Add("Closed", typeof(int));
  33. report.Columns.Add("Overdue", typeof(int));
  34. report.PrimaryKey = new[] { report.Columns[0] };
  35. }
  36. public DateTime Date
  37. {
  38. get => _date;
  39. set
  40. {
  41. _date = value;
  42. LoadTasks();
  43. }
  44. }
  45. public Guid GroupID
  46. {
  47. get => _groupid;
  48. set
  49. {
  50. _groupid = value;
  51. LoadTasks();
  52. }
  53. }
  54. public TaskWidgetProperties Properties { get; set; }
  55. public event LoadSettings<TaskWidgetProperties>? LoadSettings;
  56. public event SaveSettings<TaskWidgetProperties>? SaveSettings;
  57. public void Setup()
  58. {
  59. }
  60. public void Refresh()
  61. {
  62. LoadTasks();
  63. }
  64. public void Shutdown(CancelEventArgs? cancel)
  65. {
  66. }
  67. private void LoadTasks()
  68. {
  69. Report.ItemsSource = null;
  70. tasks = null;
  71. var filter = new Filter<Kanban>(x => x.Created).IsLessThanOrEqualTo(Date.AddDays(1));
  72. filter.Ands.Add(new Filter<Kanban>(x => x.EmployeeLink.StartDate).IsEqualTo(DateTime.MinValue).Or(x => x.EmployeeLink.StartDate)
  73. .IsLessThanOrEqualTo(Date));
  74. filter.Ands.Add(new Filter<Kanban>(x => x.EmployeeLink.FinishDate).IsEqualTo(DateTime.MinValue).Or(x => x.EmployeeLink.FinishDate)
  75. .IsGreaterThanOrEqualTo(Date));
  76. filter.Ands.Add(new Filter<Kanban>(x => x.Completed).IsEqualTo(DateTime.MinValue).Or(x => x.Completed).IsGreaterThanOrEqualTo(Date));
  77. filter.Ands.Add(new Filter<Kanban>(x => x.Closed).IsEqualTo(DateTime.MinValue).Or(x => x.Closed).IsGreaterThanOrEqualTo(Date));
  78. if (!_groupid.Equals(CoreUtils.FullGuid))
  79. filter.Ands.Add(new Filter<Kanban>(x => x.EmployeeLink.Group.ID).IsEqualTo(_groupid).Or(x => x.ManagerLink.ID).IsEqualTo(_groupid));
  80. new Client<Kanban>().Query(
  81. filter,
  82. null,
  83. new SortOrder<Kanban>(x => x.EmployeeLink.Name),
  84. (o, e) =>
  85. {
  86. tasks = o;
  87. CheckData();
  88. }
  89. );
  90. }
  91. private void CheckData()
  92. {
  93. if (tasks != null)
  94. ProcessData();
  95. }
  96. private void ProcessData()
  97. {
  98. try
  99. {
  100. report.Rows.Clear();
  101. foreach (var task in tasks.Rows)
  102. {
  103. var empid = task.Get<Kanban, Guid>(x => x.EmployeeLink.ID);
  104. var empValid = Entity.IsEntityLinkValid<Kanban, EmployeeLink>(x => x.EmployeeLink, task);
  105. var empname = task.Get<Kanban, string>(x => x.EmployeeLink.Name);
  106. if (empValid && report.Rows.Find(empid) == null)
  107. {
  108. var rows = tasks.Rows.Where(r => r.Get<Kanban, Guid>(c => c.EmployeeLink.ID).Equals(empid));
  109. int? mine = null;
  110. int? others = null;
  111. int? created = null;
  112. int? closed = null;
  113. int? overdue = null;
  114. foreach (var row in rows)
  115. {
  116. var taskemp = row.Get<Kanban, Guid>(x => x.EmployeeLink.ID);
  117. var taskmgr = row.Get<Kanban, Guid>(x => x.ManagerLink.ID);
  118. var taskcreated = row.Get<Kanban, DateTime>(x => x.Created).Date;
  119. var taskclosed = row.Get<Kanban, DateTime>(x => x.Completed).Date;
  120. if (taskclosed.IsEmpty())
  121. taskclosed = row.Get<Kanban, DateTime>(x => x.Closed).Date;
  122. var taskdue = row.Get<Kanban, DateTime>(x => x.DueDate).Date;
  123. if (taskemp.Equals(empid))
  124. mine = mine.HasValue ? mine.Value + 1 : 1;
  125. if (taskmgr.Equals(empid) && !taskemp.Equals(empid))
  126. others = others.HasValue ? others.Value + 1 : 1;
  127. if (taskcreated.Equals(_date) && taskemp.Equals(empid))
  128. created = created.HasValue ? created.Value + 1 : 1;
  129. if (taskclosed.Equals(_date) && taskemp.Equals(empid))
  130. closed = closed.HasValue ? closed.Value + 1 : 1;
  131. if (taskdue < _date && taskclosed.IsEmpty())
  132. overdue = overdue.HasValue ? overdue.Value + 1 : 1;
  133. }
  134. if (mine.HasValue || others.HasValue || created.HasValue || closed.HasValue || overdue.HasValue)
  135. report.Rows.Add(empid, empname, mine, others, created, closed, overdue);
  136. }
  137. }
  138. }
  139. catch (Exception e)
  140. {
  141. Logger.Send(LogType.Error, "", string.Format("*** Unknown Error: {0}\n{1}", e.Message, e.StackTrace));
  142. }
  143. Dispatcher.Invoke(() => { Report.ItemsSource = report; });
  144. }
  145. }
  146. }