RosterUtils.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Comal.Classes;
  5. using InABox.Core;
  6. namespace Comal.Classes
  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 currentdate = from;
  80. while (currentdate <= to)
  81. {
  82. if (
  83. (employee.StartDate.IsEmpty() || (employee.StartDate <= currentdate))
  84. && (employee.FinishDate.IsEmpty() || (employee.FinishDate >= currentdate))
  85. )
  86. {
  87. var currentroster = GetRoster(roster, employee.RosterStart, currentdate);
  88. if (currentroster?.Enabled == true)
  89. {
  90. var currentfrom = (currentdate.Date == from.Date) ? fromtime : new TimeSpan(0);
  91. var currentto = (currentdate.Date == to.Date) ? totime : new TimeSpan(1, 0, 0, 0);
  92. var shifts = currentroster.GetBlocks(currentdate, currentfrom, currentto);
  93. foreach (var shift in shifts)
  94. {
  95. DateTime now = DateTime.Now;
  96. var timesheet = new TimeSheet();
  97. timesheet.EmployeeLink.ID = employee.ID;
  98. timesheet.Date = currentdate;
  99. timesheet.Start = shift.Start;
  100. timesheet.Finish = shift.Finish;
  101. //timesheet.Duration = shift.Item2 - shift.Item1;
  102. timesheet.ApprovedStart = timesheet.Start;
  103. timesheet.ApprovedFinish = timesheet.Finish;
  104. //timesheet.ApprovedDuration = timesheet.Finish - timesheet.Start;
  105. timesheet.Confirmed = now;
  106. timesheet.Approved = now;
  107. timesheet.ActivityLink.ID = activityid;
  108. customise?.Invoke(timesheet);
  109. result.Add(timesheet);
  110. }
  111. }
  112. }
  113. currentdate = currentdate.AddDays(1);
  114. }
  115. return result.ToArray();
  116. }
  117. }
  118. }