removed Formatduration and fixed rounding error, working on overtime calculation

This commit is contained in:
2025-07-20 19:34:16 +02:00
parent 68000a0f0a
commit 4201ed7b1c
8 changed files with 230 additions and 179 deletions

View File

@@ -70,7 +70,7 @@ func (b *Booking) Insert() error {
return nil
}
func (b *Booking) InsertTimestamp() error {
func (b *Booking) InsertWithTimestamp() error {
if b.Timestamp.IsZero() {
return b.Insert()
}

View File

@@ -4,7 +4,6 @@ import (
"arbeitszeitmessung/helper"
"database/sql"
"encoding/json"
"fmt"
"log"
"strconv"
"time"
@@ -180,23 +179,9 @@ func (d *WorkDay) getWorkTime() {
d.calcPauseTime()
}
// Converts duration to string
func formatDuration(d time.Duration) string {
hours := int(d.Abs().Hours())
minutes := int(d.Abs().Minutes()) % 60
switch {
case hours > 0:
return fmt.Sprintf("%dh %dmin", hours, minutes)
case minutes > 0:
return fmt.Sprintf("%dmin", minutes)
default:
return ""
}
}
func (d *WorkDay) GetWorkTimeString() (string, string) {
workString := formatDuration(d.workTime)
pauseString := formatDuration(d.pauseTime)
workString := helper.FormatDuration(d.workTime)
pauseString := helper.FormatDuration(d.pauseTime)
return workString, pauseString
}
@@ -210,7 +195,12 @@ func (d *WorkDay) RequiresAction() bool {
// returns a integer percentage of how much day has been worked of
func (d *WorkDay) GetWorkDayProgress(user User) uint8 {
defaultWorkTime := time.Duration(user.ArbeitszeitPerTag * float32(time.Hour))
defaultWorkTime := time.Duration(user.ArbeitszeitPerTag * float32(time.Hour)).Round(time.Minute)
progress := (d.workTime.Seconds() / defaultWorkTime.Seconds()) * 100
return uint8(progress)
}
func (d *WorkDay) CalcOvertime(user User) time.Duration {
overtime := d.workTime - time.Duration(user.ArbeitszeitPerTag*float32(time.Hour)).Round(time.Minute)
return overtime
}

View File

@@ -1,6 +1,7 @@
package models
import (
"arbeitszeitmessung/helper"
"database/sql"
"errors"
"log"
@@ -60,7 +61,7 @@ func (w *WorkWeek) CheckStatus() WeekStatus {
}
func (w *WorkWeek) GetWorkHourString() string {
return formatDuration(w.WorkHours)
return helper.FormatDuration(w.WorkHours)
}
func aggregateWorkTime(days []WorkDay) time.Duration {