Skip to content

Commit

Permalink
Add calc for average weekly hours
Browse files Browse the repository at this point in the history
  • Loading branch information
y2kbugger committed Apr 26, 2024
1 parent cc96555 commit c951df3
Showing 1 changed file with 30 additions and 2 deletions.
32 changes: 30 additions & 2 deletions TeamsTimecardHelperClient/Pages/Index.razor
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,15 @@
</div>
</div>
<h2 class="text-center">Timecard Total</h2>
<div class="d-flex justify-content-center">
<div class="d-flex flex-column justify-content-center mx-auto" style="max-width:fit-content">
<HoursGauge TimeCards="@current_timecards" />
<p>Average Weekly Hours since @(new DateOnly(year:2024, month:1, day:31)): <b>@(FormatDuration(average_weekly_hours_since(new DateTime(year:2024, month:1, day:31))))</b></p>

@{ var excess = excess_hours_banked(new DateTime(year:2024, month:1, day:31), new TimeSpan(25, 0, 0)); }
@if (excess > new TimeSpan(0))
{ <p>Excess Hours Banked: <b>@(FormatDuration(excess))</b></p> }
else
{ <p>Behind by: <b>@(FormatDuration(-1 * excess))</b></p> }
</div>

<RadzenCard class="w-100 mt-3 p-3">
Expand Down Expand Up @@ -79,6 +86,27 @@
&& (x.ClockInEvent.DateTime <= StartOfWeek.AddDays(7))
).ToList(); // todo: handle case where timecard spans midnight at end of week
private TimeSpan average_weekly_hours_since(DateTime start)
{
start = start.AddDays(-1 * (7 + (start.DayOfWeek - System.DayOfWeek.Sunday)) % 7).Date;
var weeks_since_start_week = 1+((StartOfWeek - start).TotalDays / 7);
var timecards_since_start_week = all_timecards?.Where(
x => (x.ClockInEvent.DateTime >= start)
&& (x.ClockInEvent.DateTime <= StartOfWeek.AddDays(7))
).ToList();
TimeSpan hours_since_start_week = TotalTimespan(timecards_since_start_week) ?? new TimeSpan();
return hours_since_start_week / weeks_since_start_week;
}
private TimeSpan excess_hours_banked(DateTime start, TimeSpan target_weekly_hours)
{
start = start.AddDays(-1 * (7 + (start.DayOfWeek - System.DayOfWeek.Sunday)) % 7).Date;
var weeks_since_start_week = 1+((StartOfWeek - start).TotalDays / 7);

var target_hours = weeks_since_start_week * target_weekly_hours;
var actual_hours = weeks_since_start_week * average_weekly_hours_since(start);
return actual_hours - target_hours;
}

void DateRenderSundays(DateRenderEventArgs args)
{
args.Disabled = args.Date.DayOfWeek != System.DayOfWeek.Sunday;
Expand Down Expand Up @@ -140,4 +168,4 @@
all_timecards = my_timecards;
StateHasChanged();
}
}
}

0 comments on commit c951df3

Please sign in to comment.