🐛 Fix calculation of expected hours
This commit is contained in:
@@ -75,8 +75,32 @@ func location(tx core.App, owner, dayKey string) *time.Location {
|
||||
return loc
|
||||
}
|
||||
|
||||
func weekExpectedMs(set *core.Record) int64 {
|
||||
return int64(set.GetFloat("hours_per_week") * 3_600_000)
|
||||
// weekExpectedMs adds up the expected time of the workdays of the week. Each day
|
||||
// counts towards the settings effective on it, so a week in which the settings
|
||||
// change is only partly computed with the new hours.
|
||||
func weekExpectedMs(tx core.App, owner string, dayKeys []string) (int64, error) {
|
||||
var expectedMs int64
|
||||
for _, dayKey := range dayKeys {
|
||||
ms, err := dayExpectedMsAt(tx, owner, dayKey)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
expectedMs += ms
|
||||
}
|
||||
return expectedMs, nil
|
||||
}
|
||||
|
||||
// dayExpectedMsAt returns the expected daily working time of the settings effective
|
||||
// on dayKey, or 0 on a non-workday or without settings.
|
||||
func dayExpectedMsAt(tx core.App, owner, dayKey string) (int64, error) {
|
||||
set, err := settingsAt(tx, owner, dayKey)
|
||||
if err != nil || set == nil {
|
||||
return 0, err
|
||||
}
|
||||
if !isWorkday(set, parseDayKey(dayKey).Weekday()) {
|
||||
return 0, nil
|
||||
}
|
||||
return dayExpectedMs(set), nil
|
||||
}
|
||||
|
||||
func dayExpectedMs(set *core.Record) int64 {
|
||||
@@ -84,7 +108,7 @@ func dayExpectedMs(set *core.Record) int64 {
|
||||
if days == 0 {
|
||||
return 0
|
||||
}
|
||||
return weekExpectedMs(set) / int64(days)
|
||||
return int64(set.GetFloat("hours_per_week")*3_600_000) / int64(days)
|
||||
}
|
||||
|
||||
func isWorkday(set *core.Record, wd time.Weekday) bool {
|
||||
|
||||
@@ -38,7 +38,10 @@ func CloseWeek(app core.App, owner string, weekKey string, now time.Time) (*core
|
||||
if set == nil {
|
||||
return nil, ErrNoSettings
|
||||
}
|
||||
expectedMs := weekExpectedMs(set)
|
||||
expectedMs, err := weekExpectedMs(tx, owner, dayKeys)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var workedMs int64
|
||||
for _, dayKey := range dayKeys {
|
||||
|
||||
@@ -334,9 +334,41 @@ func TestCloseWeekUsesSettingsEffectiveToday(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Fatalf("CloseWeek: %v", err)
|
||||
}
|
||||
wantInt(t, cw, "expected_ms", 20*hourMs)
|
||||
// Mon to Wed are 8h each, Thu and Fri count as 20h / 5 = 4h.
|
||||
wantInt(t, cw, "expected_ms", 3*eightH+2*4*hourMs)
|
||||
wantInt(t, cw, "worked_ms", 3*eightH)
|
||||
wantInt(t, cw, "delta_ms", 3*eightH-20*hourMs)
|
||||
wantInt(t, cw, "delta_ms", 3*eightH-(3*eightH+2*4*hourMs))
|
||||
}
|
||||
|
||||
func TestCloseWeekUsesSettingsEffectiveOnEachDay(t *testing.T) {
|
||||
// 32h until 14.05., 36h from Friday 15.05. The week is closed a month later,
|
||||
// when the 36h row is the only one effective today.
|
||||
e := setup(t)
|
||||
e.settings("2026-01-01", 32, 31, "UTC")
|
||||
e.settings("2026-05-15", 36, 31, "UTC")
|
||||
markWeekDays(t, e, weekKey, 5)
|
||||
june := time.Date(2026, 6, 13, 10, 0, 0, 0, time.UTC)
|
||||
|
||||
cw, err := CloseWeek(e.app, e.owner.Id, weekKey, june)
|
||||
if err != nil {
|
||||
t.Fatalf("CloseWeek: %v", err)
|
||||
}
|
||||
// 4 days with 32h / 5 = 6.4h and Friday with 36h / 5 = 7.2h.
|
||||
want := int64(4*6.4*float64(hourMs)) + int64(7.2*float64(hourMs))
|
||||
wantInt(t, cw, "expected_ms", want)
|
||||
// Every day was marked, so the credited time adds up to exactly the expectation.
|
||||
wantInt(t, cw, "worked_ms", want)
|
||||
wantInt(t, cw, "delta_ms", 0)
|
||||
|
||||
// A week that lies before the change keeps 32h.
|
||||
markWeekDays(t, e, "2026-W19", 5)
|
||||
cw, err = CloseWeek(e.app, e.owner.Id, "2026-W19", june)
|
||||
if err != nil {
|
||||
t.Fatalf("CloseWeek: %v", err)
|
||||
}
|
||||
wantInt(t, cw, "expected_ms", 32*hourMs)
|
||||
wantInt(t, cw, "worked_ms", 32*hourMs)
|
||||
wantInt(t, cw, "delta_ms", 0)
|
||||
}
|
||||
|
||||
func TestReopenWeekWithoutClosedWeekFails(t *testing.T) {
|
||||
|
||||
Reference in New Issue
Block a user