TaskWidget.xaml.cs 6.1 KB

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