🐛 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 {
|
||||
|
||||
Reference in New Issue
Block a user