Files
arbeitszeitmessung/Backend/models/workDay.go

83 lines
2.2 KiB
Go

package models
import (
"fmt"
"time"
)
type WorkDay struct {
Day time.Time
Bookings []Booking
workTime time.Duration
pauseTime time.Duration
}
// Gets the duration someone worked that day
func (d *WorkDay) GetWorkTime() time.Duration {
var workTime, pauseTime time.Duration
var lastBooking Booking
for _, booking := range d.Bookings {
if booking.CheckInOut%2 == 1 {
if !lastBooking.Timestamp.IsZero() {
pauseTime += booking.Timestamp.Sub(lastBooking.Timestamp)
}
} else {
workTime += booking.Timestamp.Sub(lastBooking.Timestamp)
}
lastBooking = booking
}
// checks if booking is today and has no gehen yet, so the time since last kommen booking is added to workTime
if d.Day.Day() == time.Now().Day() && len(d.Bookings)%2 == 1 {
workTime += time.Since(lastBooking.Timestamp.Local())
}
if workTime > 6*time.Hour && pauseTime < 45*time.Minute {
if workTime < 9*time.Hour && pauseTime < 30*time.Minute {
diff := 30*time.Minute - pauseTime
workTime -= diff
pauseTime += diff
} else if pauseTime < 45*time.Minute {
diff := 45*time.Minute - pauseTime
workTime -= diff
pauseTime += diff
}
}
d.workTime = workTime
d.pauseTime = pauseTime
return workTime
}
func formatDuration(d time.Duration) string {
hours := int(d.Hours())
minutes := int(d.Minutes()) % 60
switch {
case hours > 0:
return fmt.Sprintf("%dh %dmin", hours, minutes)
case minutes > 0:
return fmt.Sprintf("%dmin", minutes)
default:
return ""
}
}
// Converts duration to string and replaces 0s with in
//
// -> output xhxmin
func (d *WorkDay) GetWorkTimeString() (string, string) {
workString := formatDuration(d.workTime)
pauseString := formatDuration(d.pauseTime)
return workString, pauseString
}
// returns bool wheter the workday was ended with an automatic logout
func (d *WorkDay) RequiresAction() bool {
return d.Bookings[len(d.Bookings)-1].CheckInOut == 255
}
// returns a integer percentage of how much day has been worked of
func (d *WorkDay) GetWorkDayProgress(user User) uint8 {
defaultWorkTime := time.Duration(user.Arbeitszeit * float32(time.Hour))
progress := (d.workTime.Seconds() / defaultWorkTime.Seconds()) * 100
return uint8(progress)
}