RosterUtils.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Comal.Classes;
  5. using InABox.Core;
  6. namespace PRS.Shared
  7. {
  8. public class RosterBlock
  9. {
  10. public DateTime Date { get; }
  11. public TimeSpan Start { get; }
  12. public TimeSpan Finish { get; }
  13. public TimeSpan Duration { get; }
  14. public RosterBlock(DateTime date, TimeSpan start, TimeSpan finish)
  15. {
  16. Date = date;
  17. Start = start;
  18. Finish = finish;
  19. Duration = TimeSpan.FromTicks(Math.Max(0L, finish.Ticks - start.Ticks));
  20. }
  21. }
  22. public static class RosterUtils
  23. {
  24. public static RosterBlock[] GetBlocks(this EmployeeRosterItem roster, DateTime date, TimeSpan start, TimeSpan finish)
  25. {
  26. var result = new List<RosterBlock>();
  27. if (roster.Enabled && (roster.Finish > start) && (roster.Start <= finish))
  28. {
  29. result.Add(new RosterBlock(
  30. date,
  31. start < roster.Start ? roster.Start : start,
  32. finish > roster.Finish ? roster.Finish : finish
  33. ));
  34. }
  35. if ( roster.Enabled && roster.SplitShift && (roster.Finish2 > start) && (roster.Start2 <= finish))
  36. {
  37. result.Add(new RosterBlock(
  38. date,
  39. start < roster.Start2 ? roster.Start2 : start,
  40. finish > roster.Finish2 ? roster.Finish2 : finish
  41. ));
  42. }
  43. return result.ToArray();
  44. }
  45. public static EmployeeRosterItem? GetRoster(IList<EmployeeRosterItem>? roster, DateTime? startdate, DateTime currentdate)
  46. {
  47. if (startdate > currentdate)
  48. return null;
  49. if ((roster == null) || !roster.Any())
  50. roster = DefaultRoster();
  51. if ((startdate == null) || startdate.Value.IsEmpty())
  52. startdate = currentdate.StartOfWeek(DayOfWeek.Monday);
  53. while (startdate.Value.AddDays(roster.Count) <= currentdate)
  54. startdate = startdate.Value.AddDays(roster.Count);
  55. int rosterday = (int)((currentdate.Date - startdate.Value.Date).TotalDays) % roster.Count;
  56. return roster[rosterday];
  57. }
  58. private static readonly EmployeeRosterItem[] DEFAULT_ROSTER = new EmployeeRosterItem[]
  59. {
  60. new EmployeeRosterItem()
  61. { Day = 1, Start = new TimeSpan(8, 0, 0), Finish = new TimeSpan(16, 30, 0), Break = new TimeSpan(0, 30, 0), Enabled = true },
  62. new EmployeeRosterItem()
  63. { Day = 2, Start = new TimeSpan(8, 0, 0), Finish = new TimeSpan(16, 30, 0), Break = new TimeSpan(0, 30, 0), Enabled = true },
  64. new EmployeeRosterItem()
  65. { Day = 3, Start = new TimeSpan(8, 0, 0), Finish = new TimeSpan(16, 30, 0), Break = new TimeSpan(0, 30, 0), Enabled = true },
  66. new EmployeeRosterItem()
  67. { Day = 4, Start = new TimeSpan(8, 0, 0), Finish = new TimeSpan(16, 30, 0), Break = new TimeSpan(0, 30, 0), Enabled = true },
  68. new EmployeeRosterItem()
  69. { Day = 5, Start = new TimeSpan(8, 0, 0), Finish = new TimeSpan(16, 30, 0), Break = new TimeSpan(0, 30, 0), Enabled = true },
  70. new EmployeeRosterItem()
  71. { Day = 6, Enabled = false },
  72. new EmployeeRosterItem()
  73. { Day = 7, Enabled = false },
  74. };
  75. public static EmployeeRosterItem[] DefaultRoster() => DEFAULT_ROSTER;
  76. public static TimeSheet[] CreateLeaveTimesheets(Employee employee, EmployeeRosterItem[]? roster, DateTime from, TimeSpan fromtime, DateTime to, TimeSpan totime, Guid activityid, Action<TimeSheet> customise)
  77. {
  78. var result = new List<TimeSheet>();
  79. DateTime rosterstart = employee.RosterStart.IsEmpty()
  80. ? from.StartOfWeek(DayOfWeek.Monday)
  81. : employee.RosterStart;
  82. if ((roster == null) || !roster.Any())
  83. roster = DefaultRoster();
  84. while (rosterstart.AddDays(roster.Length) <= from)
  85. rosterstart = rosterstart.AddDays(roster.Length);
  86. DateTime currentdate = from;
  87. while (currentdate <= to)
  88. {
  89. if (
  90. (employee.StartDate.IsEmpty() || (employee.StartDate <= currentdate))
  91. && (employee.FinishDate.IsEmpty() || (employee.FinishDate >= currentdate))
  92. )
  93. {
  94. var currentroster = GetRoster(roster, rosterstart, currentdate);
  95. if (currentroster?.Enabled == true)
  96. {
  97. TimeSpan currentfrom = (currentdate.Date == from.Date) ? fromtime : new TimeSpan(0);
  98. TimeSpan currentto = (currentdate.Date == to.Date) ? totime : new TimeSpan(1, 0, 0, 0);
  99. var shifts = currentroster.GetBlocks(currentdate, currentfrom, currentto);
  100. foreach (var shift in shifts)
  101. {
  102. DateTime now = DateTime.Now;
  103. var timesheet = new TimeSheet();
  104. timesheet.EmployeeLink.ID = employee.ID;
  105. timesheet.Date = currentdate;
  106. timesheet.Start = shift.Start;
  107. timesheet.Finish = shift.Finish;
  108. //timesheet.Duration = shift.Item2 - shift.Item1;
  109. timesheet.ApprovedStart = timesheet.Start;
  110. timesheet.ApprovedFinish = timesheet.Finish;
  111. //timesheet.ApprovedDuration = timesheet.Finish - timesheet.Start;
  112. timesheet.Confirmed = now;
  113. timesheet.Approved = now;
  114. timesheet.ActivityLink.ID = activityid;
  115. customise?.Invoke(timesheet);
  116. result.Add(timesheet);
  117. }
  118. }
  119. }
  120. currentdate = currentdate.AddDays(1);
  121. }
  122. return result.ToArray();
  123. }
  124. }
  125. }