TaskWidget.xaml.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. CoreRange.All,
  85. (o, e) =>
  86. {
  87. tasks = o;
  88. CheckData();
  89. }
  90. );
  91. }
  92. private void CheckData()
  93. {
  94. if (tasks != null)
  95. ProcessData();
  96. }
  97. private void ProcessData()
  98. {
  99. try
  100. {
  101. report.Rows.Clear();
  102. foreach (var task in tasks.Rows)
  103. {
  104. var empid = task.Get<Kanban, Guid>(x => x.EmployeeLink.ID);
  105. var empValid = Entity.IsEntityLinkValid<Kanban, EmployeeLink>(x => x.EmployeeLink, task);
  106. var empname = task.Get<Kanban, string>(x => x.EmployeeLink.Name);
  107. if (empValid && report.Rows.Find(empid) == null)
  108. {
  109. var rows = tasks.Rows.Where(r => r.Get<Kanban, Guid>(c => c.EmployeeLink.ID).Equals(empid));
  110. int? mine = null;
  111. int? others = null;
  112. int? created = null;
  113. int? closed = null;
  114. int? overdue = null;
  115. foreach (var row in rows)
  116. {
  117. var taskemp = row.Get<Kanban, Guid>(x => x.EmployeeLink.ID);
  118. var taskmgr = row.Get<Kanban, Guid>(x => x.ManagerLink.ID);
  119. var taskcreated = row.Get<Kanban, DateTime>(x => x.Created).Date;
  120. var taskclosed = row.Get<Kanban, DateTime>(x => x.Completed).Date;
  121. if (taskclosed.IsEmpty())
  122. taskclosed = row.Get<Kanban, DateTime>(x => x.Closed).Date;
  123. var taskdue = row.Get<Kanban, DateTime>(x => x.DueDate).Date;
  124. if (taskemp.Equals(empid))
  125. mine = mine.HasValue ? mine.Value + 1 : 1;
  126. if (taskmgr.Equals(empid) && !taskemp.Equals(empid))
  127. others = others.HasValue ? others.Value + 1 : 1;
  128. if (taskcreated.Equals(_date) && taskemp.Equals(empid))
  129. created = created.HasValue ? created.Value + 1 : 1;
  130. if (taskclosed.Equals(_date) && taskemp.Equals(empid))
  131. closed = closed.HasValue ? closed.Value + 1 : 1;
  132. if (taskdue < _date && taskclosed.IsEmpty())
  133. overdue = overdue.HasValue ? overdue.Value + 1 : 1;
  134. }
  135. if (mine.HasValue || others.HasValue || created.HasValue || closed.HasValue || overdue.HasValue)
  136. report.Rows.Add(empid, empname, mine, others, created, closed, overdue);
  137. }
  138. }
  139. }
  140. catch (Exception e)
  141. {
  142. Logger.Send(LogType.Error, "", string.Format("*** Unknown Error: {0}\n{1}", e.Message, e.StackTrace));
  143. }
  144. Dispatcher.Invoke(() => { Report.ItemsSource = report; });
  145. }
  146. }
  147. }