TimesheetWidget.xaml.cs 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.Linq;
  5. using System.Windows.Controls;
  6. using Comal.Classes;
  7. using InABox.Clients;
  8. using InABox.Core;
  9. using PRSDesktop.WidgetGroups;
  10. namespace PRSDesktop
  11. {
  12. public class TimesheetWidgetProperties : IDashboardProperties { }
  13. public class TimesheetWidgetElement : DashboardElement<TimesheetWidget, HumanResources, TimesheetWidgetProperties> { }
  14. /// <summary>
  15. /// Interaction logic for TimesheetWidget.xaml
  16. /// </summary>
  17. public partial class TimesheetWidget : UserControl, IDashboardWidget<HumanResources, TimesheetWidgetProperties>
  18. {
  19. private DateTime _date = DateTime.Today;
  20. private Guid _groupid = CoreUtils.FullGuid;
  21. private CoreTable activities;
  22. private CoreTable employees;
  23. private readonly DataTable report = new();
  24. private CoreTable timesheets;
  25. public TimesheetWidget()
  26. {
  27. InitializeComponent();
  28. report.Columns.Add("ID", typeof(Guid));
  29. report.Columns.Add("Name", typeof(string));
  30. report.Columns.Add("Leave", typeof(string));
  31. report.Columns.Add("LateStart", typeof(TimeSpan));
  32. report.Columns.Add("EarlyFinish", typeof(TimeSpan));
  33. report.Columns.Add("ClockIn", typeof(string));
  34. report.Columns.Add("ClockOut", typeof(string));
  35. report.PrimaryKey = new[] { report.Columns[0] };
  36. }
  37. public DateTime Date
  38. {
  39. get => _date;
  40. set
  41. {
  42. _date = value;
  43. LoadTimeSheets();
  44. }
  45. }
  46. public Guid GroupID
  47. {
  48. get => _groupid;
  49. set
  50. {
  51. _groupid = value;
  52. LoadEmployees(value);
  53. }
  54. }
  55. public TimesheetWidgetProperties Properties { get; set; }
  56. public void Setup()
  57. {
  58. LoadEmployees(GroupID);
  59. LoadActivities();
  60. }
  61. public void Refresh()
  62. {
  63. LoadTimeSheets();
  64. }
  65. public void Shutdown()
  66. {
  67. }
  68. private void ClearReport()
  69. {
  70. Report.ItemsSource = null;
  71. }
  72. private void LoadEmployees(Guid id)
  73. {
  74. ClearReport();
  75. employees = null;
  76. new Client<Employee>().Query(
  77. id != CoreUtils.FullGuid ? new Filter<Employee>(x => x.Group.ID).IsEqualTo(id) : null,
  78. null,
  79. null,
  80. (o, e) =>
  81. {
  82. employees = o;
  83. CheckData();
  84. }
  85. );
  86. }
  87. private void LoadActivities()
  88. {
  89. ClearReport();
  90. activities = null;
  91. new Client<Activity>().Query(
  92. new Filter<Activity>(x => x.IsLeave).IsEqualTo(true),
  93. new Columns<Activity>(
  94. x => x.ID,
  95. x => x.Description
  96. ),
  97. null,
  98. (o, e) =>
  99. {
  100. activities = o;
  101. CheckData();
  102. }
  103. );
  104. }
  105. private void LoadTimeSheets()
  106. {
  107. ClearReport();
  108. timesheets = null;
  109. new Client<TimeSheet>().Query(
  110. new Filter<TimeSheet>(x => x.Date).IsEqualTo(_date),
  111. null,
  112. new SortOrder<TimeSheet>(x => x.EmployeeLink.Name),
  113. (o, e) =>
  114. {
  115. timesheets = o;
  116. CheckData();
  117. }
  118. );
  119. }
  120. private void CheckData()
  121. {
  122. if (employees != null && activities != null && timesheets != null)
  123. ProcessData();
  124. }
  125. private string Codify(string name)
  126. {
  127. var result = "";
  128. var comps = name.ToUpper().Split(' ');
  129. foreach (var comp in comps)
  130. if (comp.Any())
  131. result += comp.First();
  132. return string.IsNullOrWhiteSpace(result) ? "??" : result;
  133. }
  134. private void ProcessData()
  135. {
  136. try
  137. {
  138. report.Rows.Clear();
  139. foreach (var time in timesheets.Rows)
  140. {
  141. var empid = time.Get<TimeSheet, Guid>(x => x.EmployeeLink.ID);
  142. var empname = time.Get<TimeSheet, string>(x => x.EmployeeLink.Name);
  143. if (report.Rows.Find(empid) == null)
  144. {
  145. var emprow = employees.Rows.FirstOrDefault(r => r.Get<Employee, Guid>(c => c.ID).Equals(empid));
  146. if (emprow != null)
  147. {
  148. var userid = emprow.Get<Employee, string>(x => x.UserLink.UserID);
  149. var usualstart = emprow.Get<Employee, TimeSpan>(x => x.UsualStart);
  150. var usualfinish = emprow.Get<Employee, TimeSpan>(x => x.UsualFinish);
  151. var rows = timesheets.Rows.Where(r => r.Get<TimeSheet, Guid>(c => c.EmployeeLink.ID).Equals(empid));
  152. var start = new TimeSpan(long.MaxValue);
  153. TimeSpan? late = null;
  154. var finish = new TimeSpan(long.MinValue);
  155. TimeSpan? early = null;
  156. var leave = "";
  157. var clockin = "";
  158. var clockout = "";
  159. var leaves = new List<string>();
  160. foreach (var row in rows)
  161. {
  162. var activity = row.Get<TimeSheet, Guid>(x => x.ActivityLink.ID);
  163. var activityrow = activities.Rows.FirstOrDefault(r => r.Get<Activity, Guid>(c => c.ID).Equals(activity));
  164. if (activityrow != null)
  165. {
  166. var thisleave = Codify(activityrow.Get<Activity, string>(x => x.Description));
  167. if (!leaves.Contains(thisleave))
  168. leaves.Add(thisleave);
  169. }
  170. leave = string.Join(",", leaves);
  171. var createdby = row.Get<TimeSheet, string>(x => x.CreatedBy);
  172. if (createdby == null)
  173. createdby = "";
  174. if (usualstart.Ticks != 0)
  175. {
  176. var thisstart = row.Get<TimeSheet, TimeSpan>(x => x.Start);
  177. if (thisstart < start)
  178. {
  179. start = thisstart;
  180. if (start > usualstart)
  181. late = !late.HasValue || start < late.Value ? start : late;
  182. else
  183. late = null;
  184. clockin = createdby.Equals(userid) ? "" : createdby;
  185. }
  186. }
  187. if (usualfinish.Ticks != 0)
  188. {
  189. var thisfinish = row.Get<TimeSheet, TimeSpan>(x => x.Finish);
  190. if (thisfinish.Ticks == 0)
  191. {
  192. usualfinish = new TimeSpan(0);
  193. early = null;
  194. }
  195. else
  196. {
  197. if (thisfinish > finish)
  198. {
  199. finish = thisfinish;
  200. if (finish < usualfinish)
  201. early = !early.HasValue || finish > early.Value ? finish : early;
  202. else
  203. early = null;
  204. clockout = createdby.Equals(userid) ? "" : createdby;
  205. }
  206. }
  207. }
  208. }
  209. leave = string.Join(",", leaves);
  210. if (!string.IsNullOrEmpty(leave) || late.HasValue || early.HasValue || !string.IsNullOrEmpty(clockin) ||
  211. !string.IsNullOrEmpty(clockout))
  212. report.Rows.Add(empid, empname, leave, late, early, clockin, clockout);
  213. }
  214. }
  215. }
  216. }
  217. catch (Exception e)
  218. {
  219. Logger.Send(LogType.Error, "", string.Format("*** Unknown Error: {0}\n{1}", e.Message, e.StackTrace));
  220. }
  221. Dispatcher.Invoke(() => { Report.ItemsSource = report; });
  222. }
  223. }
  224. }