TaskWidget.xaml.cs 6.3 KB

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