This commit is contained in:
@@ -50,14 +50,14 @@ func (a *Absence) IsMultiDay() bool {
|
||||
}
|
||||
|
||||
func (a *Absence) GetWorktimeReal(u User, base WorktimeBase) time.Duration {
|
||||
if a.AbwesenheitTyp.WorkTime <= 1 {
|
||||
if a.AbwesenheitTyp.WorkTime <= 0 {
|
||||
return 0
|
||||
}
|
||||
switch base {
|
||||
case WorktimeBaseDay:
|
||||
return u.ArbeitszeitProTag()
|
||||
return u.ArbeitszeitProTagFrac(float32(a.AbwesenheitTyp.WorkTime) / 100)
|
||||
case WorktimeBaseWeek:
|
||||
return u.ArbeitszeitProWoche() / 5
|
||||
return u.ArbeitszeitProWocheFrac(0.2 * float32(a.AbwesenheitTyp.WorkTime) / 100)
|
||||
}
|
||||
return 0
|
||||
}
|
||||
@@ -66,7 +66,7 @@ func (a *Absence) GetPausetimeReal(u User, base WorktimeBase) time.Duration {
|
||||
}
|
||||
|
||||
func (a *Absence) GetOvertimeReal(u User, base WorktimeBase) time.Duration {
|
||||
if a.AbwesenheitTyp.WorkTime > 1 {
|
||||
if a.AbwesenheitTyp.WorkTime > 0 {
|
||||
return 0
|
||||
}
|
||||
switch base {
|
||||
|
||||
92
Backend/models/absence_test.go
Normal file
92
Backend/models/absence_test.go
Normal file
@@ -0,0 +1,92 @@
|
||||
package models_test
|
||||
|
||||
import (
|
||||
"arbeitszeitmessung/helper"
|
||||
"arbeitszeitmessung/models"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
var testAbsence = models.Absence{
|
||||
Day: CatchError(time.Parse(time.DateOnly, "2025-01-01")),
|
||||
AbwesenheitTyp: models.AbsenceType{},
|
||||
DateFrom: CatchError(time.Parse(time.DateOnly, "2025-01-01")),
|
||||
DateTo: CatchError(time.Parse(time.DateOnly, "2025-01-03")),
|
||||
}
|
||||
|
||||
var testKurzarbeit = models.AbsenceType{
|
||||
Name: "Kurzarbeit",
|
||||
WorkTime: -1,
|
||||
}
|
||||
|
||||
var testUrlaub = models.AbsenceType{
|
||||
Name: "Urlaub",
|
||||
WorkTime: 100,
|
||||
}
|
||||
|
||||
var testUrlaubUntertags = models.AbsenceType{
|
||||
Name: "Urlaub untertags",
|
||||
WorkTime: 50,
|
||||
}
|
||||
|
||||
func TestCalcRealWorkTimeDayAbsence(t *testing.T) {
|
||||
testCases := []struct {
|
||||
absenceType models.AbsenceType
|
||||
expectedTime time.Duration
|
||||
}{
|
||||
{
|
||||
absenceType: testUrlaub,
|
||||
expectedTime: time.Hour * 8,
|
||||
},
|
||||
{
|
||||
absenceType: testUrlaubUntertags,
|
||||
expectedTime: time.Hour * 4,
|
||||
},
|
||||
{
|
||||
absenceType: testKurzarbeit,
|
||||
expectedTime: 0,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
t.Run("Calc Absence Worktime: "+tc.absenceType.Name, func(t *testing.T) {
|
||||
var testCase = testAbsence
|
||||
testCase.AbwesenheitTyp = tc.absenceType
|
||||
workTime := testCase.GetWorktimeReal(testUser, models.WorktimeBaseDay)
|
||||
if workTime != tc.expectedTime {
|
||||
t.Errorf("Calc Worktime Default not working, time should be %s, but was %s", helper.FormatDurationFill(tc.expectedTime, true), helper.FormatDurationFill(workTime, true))
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestCalcRealWorkTimeWeekAbsence(t *testing.T) {
|
||||
testCases := []struct {
|
||||
absenceType models.AbsenceType
|
||||
expectedTime time.Duration
|
||||
}{
|
||||
{
|
||||
absenceType: testUrlaub,
|
||||
expectedTime: time.Hour * 7,
|
||||
},
|
||||
{
|
||||
absenceType: testUrlaubUntertags,
|
||||
expectedTime: time.Hour*3 + time.Minute*30,
|
||||
},
|
||||
{
|
||||
absenceType: testKurzarbeit,
|
||||
expectedTime: 0,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
t.Run("Calc Absence Worktime: "+tc.absenceType.Name, func(t *testing.T) {
|
||||
var testCase = testAbsence
|
||||
testCase.AbwesenheitTyp = tc.absenceType
|
||||
workTime := testCase.GetWorktimeReal(testUser, models.WorktimeBaseWeek)
|
||||
if workTime != tc.expectedTime {
|
||||
t.Errorf("Calc Worktime Default not working, time should be %s, but was %s", helper.FormatDurationFill(tc.expectedTime, true), helper.FormatDurationFill(workTime, true))
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -10,36 +10,36 @@ var testBookingType = models.BookingType{
|
||||
Name: "Büro",
|
||||
}
|
||||
|
||||
var testBookings8hrs = []models.Booking{models.Booking{
|
||||
var testBookings8hrs = []models.Booking{{
|
||||
CardUID: "aaaa-aaaa",
|
||||
CheckInOut: 1,
|
||||
Timestamp: CatchError(time.Parse("2006-01-02 15:04", "2025-01-01 08:00")),
|
||||
BookingType: testBookingType,
|
||||
}, models.Booking{
|
||||
}, {
|
||||
CardUID: "aaaa-aaaa",
|
||||
CheckInOut: 2,
|
||||
Timestamp: CatchError(time.Parse("2006-01-02 15:04", "2025-01-01 16:00")),
|
||||
BookingType: testBookingType,
|
||||
}}
|
||||
|
||||
var testBookings6hrs = []models.Booking{models.Booking{
|
||||
var testBookings6hrs = []models.Booking{{
|
||||
CardUID: "aaaa-aaaa",
|
||||
CheckInOut: 1,
|
||||
Timestamp: CatchError(time.Parse("2006-01-02 15:04", "2025-01-01 08:00")),
|
||||
BookingType: testBookingType,
|
||||
}, models.Booking{
|
||||
}, {
|
||||
CardUID: "aaaa-aaaa",
|
||||
CheckInOut: 2,
|
||||
Timestamp: CatchError(time.Parse("2006-01-02 15:04", "2025-01-01 14:00")),
|
||||
BookingType: testBookingType,
|
||||
}}
|
||||
|
||||
var testBookings10hrs = []models.Booking{models.Booking{
|
||||
var testBookings10hrs = []models.Booking{{
|
||||
CardUID: "aaaa-aaaa",
|
||||
CheckInOut: 1,
|
||||
Timestamp: CatchError(time.Parse("2006-01-02 15:04", "2025-01-01 08:00")),
|
||||
BookingType: testBookingType,
|
||||
}, models.Booking{
|
||||
}, {
|
||||
CardUID: "aaaa-aaaa",
|
||||
CheckInOut: 2,
|
||||
Timestamp: CatchError(time.Parse("2006-01-02 15:04", "2025-01-01 18:00")),
|
||||
|
||||
59
Backend/models/iworkday.go
Normal file
59
Backend/models/iworkday.go
Normal file
@@ -0,0 +1,59 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"arbeitszeitmessung/helper"
|
||||
"log"
|
||||
"time"
|
||||
)
|
||||
|
||||
type IWorkDay interface {
|
||||
Date() time.Time
|
||||
ToString() string
|
||||
IsWorkDay() bool
|
||||
IsKurzArbeit() bool
|
||||
GetDayProgress(User) int8
|
||||
RequiresAction() bool
|
||||
GetWorktimeReal(User, WorktimeBase) time.Duration
|
||||
GetPausetimeReal(User, WorktimeBase) time.Duration
|
||||
GetOvertimeReal(User, WorktimeBase) time.Duration
|
||||
GetWorktimeVirtual(User, WorktimeBase) time.Duration
|
||||
GetPausetimeVirtual(User, WorktimeBase) time.Duration
|
||||
GetOvertimeVirtual(User, WorktimeBase) time.Duration
|
||||
GetTimesReal(User, WorktimeBase) (work, pause, overtime time.Duration)
|
||||
GetTimesVirtual(User, WorktimeBase) (work, pause, overtime time.Duration)
|
||||
}
|
||||
|
||||
func GetDays(user User, tsFrom, tsTo time.Time, orderedForward bool) []IWorkDay {
|
||||
var allDays map[string]IWorkDay = make(map[string]IWorkDay)
|
||||
|
||||
for _, day := range GetWorkDays(user, tsFrom, tsTo) {
|
||||
allDays[day.Date().Format(time.DateOnly)] = &day
|
||||
}
|
||||
absences, err := GetAbsencesByCardUID(user.CardUID, tsFrom, tsTo)
|
||||
if err != nil {
|
||||
log.Println("Error gettings absences for all Days!", err)
|
||||
return nil
|
||||
}
|
||||
for _, day := range absences {
|
||||
if helper.IsWeekend(day.Date()) {
|
||||
continue
|
||||
}
|
||||
// Absence should be integrated in workday
|
||||
switch {
|
||||
case day.AbwesenheitTyp.WorkTime < 0:
|
||||
if workDay, ok := allDays[day.Date().Format(time.DateOnly)].(*WorkDay); ok {
|
||||
workDay.kurzArbeit = true
|
||||
workDay.kurzArbeitAbsence = day
|
||||
}
|
||||
case day.AbwesenheitTyp.WorkTime < 100:
|
||||
if workDay, ok := allDays[day.Date().Format(time.DateOnly)].(*WorkDay); ok {
|
||||
workDay.worktimeAbsece = day
|
||||
}
|
||||
default:
|
||||
allDays[day.Date().Format(time.DateOnly)] = &day
|
||||
}
|
||||
}
|
||||
|
||||
sortedDays := sortDays(allDays, orderedForward)
|
||||
return sortedDays
|
||||
}
|
||||
@@ -113,13 +113,21 @@ func (u *User) GetAll() ([]User, error) {
|
||||
return users, nil
|
||||
}
|
||||
|
||||
// Returns the worktime per day rounded to minutes
|
||||
func (u *User) ArbeitszeitProTag() time.Duration {
|
||||
return time.Duration(u.ArbeitszeitPerTag * float32(time.Hour)).Round(time.Minute)
|
||||
return u.ArbeitszeitProTagFrac(1)
|
||||
}
|
||||
|
||||
// Returns the worktime per day rounded to minutes
|
||||
func (u *User) ArbeitszeitProTagFrac(fraction float32) time.Duration {
|
||||
return time.Duration(u.ArbeitszeitPerTag * float32(time.Hour) * fraction).Round(time.Minute)
|
||||
}
|
||||
|
||||
func (u *User) ArbeitszeitProWoche() time.Duration {
|
||||
return time.Duration(u.ArbeitszeitPerWoche * float32(time.Hour)).Round(time.Minute)
|
||||
return u.ArbeitszeitProWocheFrac(1)
|
||||
}
|
||||
|
||||
func (u *User) ArbeitszeitProWocheFrac(fraction float32) time.Duration {
|
||||
return time.Duration(u.ArbeitszeitPerWoche * float32(time.Hour) * fraction).Round(time.Minute)
|
||||
}
|
||||
|
||||
// Returns true if there is a booking 1 for today -> meaning the user is at work
|
||||
|
||||
@@ -6,7 +6,7 @@ import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
var testUser models.User = models.User{Vorname: "Kim", Name: "Mustermensch", PersonalNummer: 456, CardUID: "aaaa-aaaa", ArbeitszeitPerTag: 8, ArbeitszeitPerWoche: 40}
|
||||
var testUser models.User = models.User{Vorname: "Kim", Name: "Mustermensch", PersonalNummer: 456, CardUID: "aaaa-aaaa", ArbeitszeitPerTag: 8, ArbeitszeitPerWoche: 35}
|
||||
|
||||
func SetupUserFixture(t *testing.T, db models.IDatabase) {
|
||||
t.Helper()
|
||||
|
||||
@@ -10,39 +10,17 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
type IWorkDay interface {
|
||||
Date() time.Time
|
||||
TimeWorkVirtual(User) time.Duration
|
||||
TimeWorkReal(User) time.Duration
|
||||
TimePauseReal(User) (work, pause time.Duration)
|
||||
TimeOvertimeReal(User) time.Duration
|
||||
GetAllWorkTimesVirtual(User) (work, pause, overtime time.Duration)
|
||||
ToString() string
|
||||
IsWorkDay() bool
|
||||
IsKurzArbeit() bool
|
||||
GetDayProgress(User) int8
|
||||
RequiresAction() bool
|
||||
GetWorktimeReal(User, WorktimeBase) time.Duration
|
||||
GetPausetimeReal(User, WorktimeBase) time.Duration
|
||||
GetOvertimeReal(User, WorktimeBase) time.Duration
|
||||
GetWorktimeVirtual(User, WorktimeBase) time.Duration
|
||||
GetPausetimeVirtual(User, WorktimeBase) time.Duration
|
||||
GetOvertimeVirtual(User, WorktimeBase) time.Duration
|
||||
GetTimesReal(User, WorktimeBase) (work, pause, overtime time.Duration)
|
||||
GetTimesVirtual(User, WorktimeBase) (work, pause, overtime time.Duration)
|
||||
}
|
||||
|
||||
type WorkDay struct {
|
||||
Day time.Time `json:"day"`
|
||||
Bookings []Booking `json:"bookings"`
|
||||
workTime time.Duration
|
||||
pauseTime time.Duration
|
||||
realWorkTime time.Duration
|
||||
realPauseTime time.Duration
|
||||
TimeFrom time.Time
|
||||
TimeTo time.Time
|
||||
kurzArbeit bool
|
||||
kurzArbeitAbsence Absence
|
||||
// Urlaub untertags
|
||||
worktimeAbsece Absence
|
||||
}
|
||||
|
||||
type WorktimeBase string
|
||||
@@ -52,62 +30,39 @@ const (
|
||||
WorktimeBaseDay WorktimeBase = "day"
|
||||
)
|
||||
|
||||
func GetDays(user User, tsFrom, tsTo time.Time, orderedForward bool) []IWorkDay {
|
||||
var allDays map[string]IWorkDay = make(map[string]IWorkDay)
|
||||
|
||||
for _, day := range GetWorkDays(user, tsFrom, tsTo) {
|
||||
allDays[day.Date().Format(time.DateOnly)] = &day
|
||||
}
|
||||
absences, err := GetAbsencesByCardUID(user.CardUID, tsFrom, tsTo)
|
||||
if err != nil {
|
||||
log.Println("Error gettings absences for all Days!", err)
|
||||
return nil
|
||||
}
|
||||
for _, day := range absences {
|
||||
if helper.IsWeekend(day.Date()) {
|
||||
continue
|
||||
}
|
||||
if day.AbwesenheitTyp.WorkTime == 1 {
|
||||
if workDay, ok := allDays[day.Date().Format(time.DateOnly)].(*WorkDay); ok && len(workDay.Bookings) > 0 {
|
||||
workDay.kurzArbeit = true
|
||||
workDay.kurzArbeitAbsence = day
|
||||
}
|
||||
} else {
|
||||
allDays[day.Date().Format(time.DateOnly)] = &day
|
||||
}
|
||||
}
|
||||
|
||||
sortedDays := sortDays(allDays, orderedForward)
|
||||
return sortedDays
|
||||
}
|
||||
|
||||
// Gets the time as is in the db (with corrected pause times)
|
||||
func (d *WorkDay) GetWorktimeReal(u User, base WorktimeBase) time.Duration {
|
||||
work, pause := calcWorkPause(d.Bookings)
|
||||
work, pause = correctWorkPause(work, pause)
|
||||
return work
|
||||
if (d.worktimeAbsece != Absence{}) {
|
||||
work += d.worktimeAbsece.GetWorktimeReal(u, WorktimeBaseDay)
|
||||
}
|
||||
return work.Round(time.Minute)
|
||||
}
|
||||
|
||||
// Gets the corrected pause times based on db entries
|
||||
func (d *WorkDay) GetPausetimeReal(u User, base WorktimeBase) time.Duration {
|
||||
work, pause := calcWorkPause(d.Bookings)
|
||||
work, pause = correctWorkPause(work, pause)
|
||||
return pause
|
||||
return pause.Round(time.Minute)
|
||||
}
|
||||
|
||||
// Returns the overtime based on the db entries
|
||||
func (d *WorkDay) GetOvertimeReal(u User, base WorktimeBase) time.Duration {
|
||||
work, pause := calcWorkPause(d.Bookings)
|
||||
work, pause = correctWorkPause(work, pause)
|
||||
if (d.worktimeAbsece != Absence{}) {
|
||||
work += d.worktimeAbsece.GetWorktimeReal(u, base)
|
||||
}
|
||||
|
||||
var targetHours time.Duration
|
||||
switch base {
|
||||
case WorktimeBaseDay:
|
||||
targetHours = u.ArbeitszeitProTag()
|
||||
case WorktimeBaseWeek:
|
||||
targetHours = u.ArbeitszeitProWoche() / 5
|
||||
targetHours = u.ArbeitszeitProWocheFrac(0.2)
|
||||
}
|
||||
return work - targetHours
|
||||
return (work - targetHours).Round(time.Minute)
|
||||
}
|
||||
|
||||
// Returns the worktime based on absence or kurzarbeit
|
||||
@@ -119,9 +74,10 @@ func (d *WorkDay) GetWorktimeVirtual(u User, base WorktimeBase) time.Duration {
|
||||
case WorktimeBaseDay:
|
||||
return u.ArbeitszeitProTag()
|
||||
case WorktimeBaseWeek:
|
||||
return u.ArbeitszeitProWoche() / 5
|
||||
return u.ArbeitszeitProWocheFrac(0.2)
|
||||
default:
|
||||
return 0
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func (d *WorkDay) GetPausetimeVirtual(u User, base WorktimeBase) time.Duration {
|
||||
@@ -136,9 +92,9 @@ func (d *WorkDay) GetOvertimeVirtual(u User, base WorktimeBase) time.Duration {
|
||||
case WorktimeBaseDay:
|
||||
targetHours = u.ArbeitszeitProTag()
|
||||
case WorktimeBaseWeek:
|
||||
targetHours = u.ArbeitszeitProWoche() / 5
|
||||
targetHours = u.ArbeitszeitProWocheFrac(0.2)
|
||||
}
|
||||
return work - targetHours
|
||||
return (work - targetHours).Round(time.Minute)
|
||||
}
|
||||
|
||||
func (d *WorkDay) GetTimesReal(u User, base WorktimeBase) (work, pause, overtime time.Duration) {
|
||||
@@ -217,70 +173,10 @@ func (d *WorkDay) GenerateKurzArbeitBookings(u User) (time.Time, time.Time) {
|
||||
return timeFrom, timeTo
|
||||
}
|
||||
|
||||
func (d *WorkDay) TimeWorkVirtual(u User) time.Duration {
|
||||
if d.IsKurzArbeit() {
|
||||
return u.ArbeitszeitProTag()
|
||||
}
|
||||
return d.workTime
|
||||
}
|
||||
|
||||
func (d *WorkDay) GetKurzArbeit() *Absence {
|
||||
return &d.kurzArbeitAbsence
|
||||
}
|
||||
|
||||
func (d *WorkDay) TimeWorkReal(u User) time.Duration {
|
||||
d.realWorkTime, d.realPauseTime = 0, 0
|
||||
var lastBooking Booking
|
||||
for _, booking := range d.Bookings {
|
||||
if booking.CheckInOut%2 == 1 {
|
||||
if !lastBooking.Timestamp.IsZero() {
|
||||
d.realPauseTime += booking.Timestamp.Sub(lastBooking.Timestamp)
|
||||
}
|
||||
} else {
|
||||
d.realWorkTime += booking.Timestamp.Sub(lastBooking.Timestamp)
|
||||
}
|
||||
lastBooking = booking
|
||||
}
|
||||
if helper.IsSameDate(d.Date(), time.Now()) && len(d.Bookings)%2 == 1 {
|
||||
d.realWorkTime += time.Since(lastBooking.Timestamp.Local())
|
||||
}
|
||||
// slog.Debug("Calculated RealWorkTime for user", "user", u, slog.String("worktime", d.realWorkTime.String()))
|
||||
return d.realWorkTime
|
||||
}
|
||||
|
||||
func (d *WorkDay) TimeOvertimeReal(u User) time.Duration {
|
||||
workTime := d.TimeWorkVirtual(u)
|
||||
if workTime == 0 {
|
||||
workTime, _ = d.TimePauseReal(u)
|
||||
}
|
||||
if helper.IsWeekend(d.Day) && len(d.Bookings) == 0 {
|
||||
return 0
|
||||
}
|
||||
var overtime time.Duration
|
||||
overtime = workTime - u.ArbeitszeitProTag()
|
||||
return overtime
|
||||
}
|
||||
|
||||
func (d *WorkDay) TimePauseReal(u User) (work, pause time.Duration) {
|
||||
if d.realWorkTime == 0 {
|
||||
d.TimeWorkReal(u)
|
||||
}
|
||||
d.workTime, d.pauseTime = d.realWorkTime, d.realPauseTime
|
||||
if d.realWorkTime <= 6*time.Hour || d.realPauseTime > 45*time.Minute {
|
||||
return d.realWorkTime, d.realPauseTime
|
||||
}
|
||||
if d.realWorkTime <= (9*time.Hour) && d.realPauseTime < 30*time.Minute {
|
||||
diff := 30*time.Minute - d.pauseTime
|
||||
d.workTime -= diff
|
||||
d.pauseTime += diff
|
||||
} else if d.realPauseTime < 45*time.Minute {
|
||||
diff := 45*time.Minute - d.pauseTime
|
||||
d.workTime -= diff
|
||||
d.pauseTime += diff
|
||||
}
|
||||
return d.workTime, d.pauseTime
|
||||
}
|
||||
|
||||
func (d *WorkDay) ToString() string {
|
||||
return fmt.Sprintf("WorkDay: %s with %d bookings and worktime: %s", d.Date().Format(time.DateOnly), len(d.Bookings), helper.FormatDuration(d.workTime))
|
||||
}
|
||||
@@ -388,7 +284,6 @@ func GetWorkDays(user User, tsFrom, tsTo time.Time) []WorkDay {
|
||||
if len(workDay.Bookings) == 1 && workDay.Bookings[0].CounterId == 0 {
|
||||
workDay.Bookings = []Booking{}
|
||||
}
|
||||
workDay.TimePauseReal(user)
|
||||
if len(workDay.Bookings) > 1 || !helper.IsWeekend(workDay.Date()) {
|
||||
workDays = append(workDays, workDay)
|
||||
}
|
||||
@@ -400,18 +295,6 @@ func GetWorkDays(user User, tsFrom, tsTo time.Time) []WorkDay {
|
||||
return workDays
|
||||
}
|
||||
|
||||
func (d *WorkDay) GetAllWorkTimesReal(user User) (work, pause, overtime time.Duration) {
|
||||
if d.pauseTime == 0 || d.workTime == 0 {
|
||||
d.TimePauseReal(user)
|
||||
}
|
||||
return d.workTime.Round(time.Minute), d.pauseTime.Round(time.Minute), d.TimeOvertimeReal(user)
|
||||
}
|
||||
|
||||
func (d *WorkDay) GetAllWorkTimesVirtual(user User) (work, pause, overtime time.Duration) {
|
||||
_, pause, overtime = d.GetAllWorkTimesReal(user)
|
||||
return d.TimeWorkVirtual(user), pause, overtime
|
||||
}
|
||||
|
||||
// returns bool wheter the workday was ended with an automatic logout
|
||||
func (d *WorkDay) RequiresAction() bool {
|
||||
if len(d.Bookings) == 0 {
|
||||
@@ -424,19 +307,7 @@ func (d *WorkDay) GetDayProgress(u User) int8 {
|
||||
if d.RequiresAction() {
|
||||
return -1
|
||||
}
|
||||
workTime := d.TimeWorkVirtual(u)
|
||||
workTime := d.GetWorktimeVirtual(u, WorktimeBaseDay)
|
||||
progress := (workTime.Seconds() / u.ArbeitszeitProTag().Seconds()) * 100
|
||||
return int8(progress)
|
||||
}
|
||||
|
||||
// func (d *WorkDay) CalcOvertime(user User) time.Duration {
|
||||
// if d.workTime == 0 {
|
||||
// d.TimePauseReal(user)
|
||||
// }
|
||||
// if helper.IsWeekend(d.Day) && len(d.Bookings) == 0 {
|
||||
// return 0
|
||||
// }
|
||||
// var overtime time.Duration
|
||||
// overtime = d.workTime - user.ArbeitszeitProTag()
|
||||
// return overtime
|
||||
// }
|
||||
|
||||
@@ -22,59 +22,141 @@ var testWorkDay = models.WorkDay{
|
||||
TimeTo: CatchError(time.Parse("2006-01-02 15:04", "2025-01-01 16:30")),
|
||||
}
|
||||
|
||||
func TestCalcRealWorkTime(t *testing.T) {
|
||||
workTime := testWorkDay.TimeWorkReal(testUser)
|
||||
if workTime != time.Hour*8 {
|
||||
t.Errorf("Calc Worktime Default not working, time should be 8h, but was %s", helper.FormatDuration(workTime))
|
||||
}
|
||||
}
|
||||
|
||||
func TestCalcWorkPauseDiff(t *testing.T) {
|
||||
type testCase struct {
|
||||
Name string
|
||||
bookings []models.Booking
|
||||
expectedWorkTime time.Duration
|
||||
expectedPauseTime time.Duration
|
||||
expectedOvertime time.Duration
|
||||
}
|
||||
|
||||
testCases := []testCase{testCase{
|
||||
Name: "6hrs no pause",
|
||||
bookings: testBookings6hrs,
|
||||
expectedWorkTime: 6 * time.Hour,
|
||||
expectedPauseTime: 0,
|
||||
expectedOvertime: -2 * time.Hour,
|
||||
},
|
||||
testCase{
|
||||
Name: "8hrs - 30min pause",
|
||||
bookings: testBookings8hrs,
|
||||
expectedWorkTime: 7*time.Hour + 30*time.Minute,
|
||||
expectedPauseTime: 30 * time.Minute,
|
||||
expectedOvertime: -30 * time.Minute,
|
||||
func TestWorkdayWorktimeDay(t *testing.T) {
|
||||
testCases := []struct {
|
||||
testName string
|
||||
bookings []models.Booking
|
||||
expectedTime time.Duration
|
||||
}{
|
||||
{
|
||||
testName: "Bookings6hrs",
|
||||
bookings: testBookings6hrs,
|
||||
expectedTime: time.Hour * 6,
|
||||
},
|
||||
testCase{
|
||||
Name: "10hrs - 45min pause",
|
||||
bookings: testBookings10hrs,
|
||||
expectedWorkTime: 9*time.Hour + 15*time.Minute,
|
||||
expectedPauseTime: 45 * time.Minute,
|
||||
expectedOvertime: 1*time.Hour + 15*time.Minute,
|
||||
}}
|
||||
{
|
||||
testName: "Bookings8hrs",
|
||||
bookings: testBookings8hrs,
|
||||
expectedTime: time.Hour*7 + time.Minute*30,
|
||||
},
|
||||
{
|
||||
testName: "Bookings10hrs",
|
||||
bookings: testBookings10hrs,
|
||||
expectedTime: time.Hour*9 + time.Minute*15,
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range testCases {
|
||||
t.Run(test.Name, func(t *testing.T) {
|
||||
testWorkDay.Bookings = test.bookings
|
||||
testWorkDay.TimeWorkReal(testUser)
|
||||
testWorkDay.TimePauseReal(testUser)
|
||||
testWorkDay.TimeOvertimeReal(testUser)
|
||||
workTime, pauseTime, overTime := testWorkDay.GetAllWorkTimesReal(testUser)
|
||||
if workTime != test.expectedWorkTime {
|
||||
t.Errorf("Calculated wrong workTime: should be %s, but was %s", helper.FormatDuration(test.expectedWorkTime), helper.FormatDuration(workTime))
|
||||
}
|
||||
if pauseTime != test.expectedPauseTime {
|
||||
t.Errorf("Calculated wrong pauseTime: should be %s, but was %s", helper.FormatDuration(test.expectedPauseTime), helper.FormatDuration(pauseTime))
|
||||
}
|
||||
if overTime != test.expectedOvertime {
|
||||
t.Errorf("Calculated wrong overtime: should be %s, but was %s", helper.FormatDuration(test.expectedOvertime), helper.FormatDuration(overTime))
|
||||
for _, tc := range testCases {
|
||||
t.Run("Calc Absence Worktime: "+tc.testName, func(t *testing.T) {
|
||||
var testCase = testWorkDay
|
||||
testCase.Bookings = tc.bookings
|
||||
workTime := testCase.GetWorktimeReal(testUser, models.WorktimeBaseDay)
|
||||
if workTime != tc.expectedTime {
|
||||
t.Errorf("GetWorktimeReal not working, time should be %s, but was %s", helper.FormatDurationFill(tc.expectedTime, true), helper.FormatDurationFill(workTime, true))
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestWorkdayWorktimeWeek(t *testing.T) {
|
||||
testCases := []struct {
|
||||
testName string
|
||||
bookings []models.Booking
|
||||
expectedTime time.Duration
|
||||
}{
|
||||
{
|
||||
testName: "Bookings6hrs",
|
||||
bookings: testBookings6hrs,
|
||||
expectedTime: time.Hour * 6,
|
||||
},
|
||||
{
|
||||
testName: "Bookings8hrs",
|
||||
bookings: testBookings8hrs,
|
||||
expectedTime: time.Hour*7 + time.Minute*30,
|
||||
},
|
||||
{
|
||||
testName: "Bookings10hrs",
|
||||
bookings: testBookings10hrs,
|
||||
expectedTime: time.Hour*9 + time.Minute*15,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
t.Run("Calc Absence Worktime: "+tc.testName, func(t *testing.T) {
|
||||
var testCase = testWorkDay
|
||||
testCase.Bookings = tc.bookings
|
||||
workTime := testCase.GetWorktimeReal(testUser, models.WorktimeBaseWeek)
|
||||
if workTime != tc.expectedTime {
|
||||
t.Errorf("GetWorktimeReal not working, time should be %s, but was %s", helper.FormatDurationFill(tc.expectedTime, true), helper.FormatDurationFill(workTime, true))
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestWorkdayPausetimeDay(t *testing.T) {
|
||||
testCases := []struct {
|
||||
testName string
|
||||
bookings []models.Booking
|
||||
expectedTime time.Duration
|
||||
}{
|
||||
{
|
||||
testName: "Bookings6hrs",
|
||||
bookings: testBookings6hrs,
|
||||
expectedTime: 0,
|
||||
},
|
||||
{
|
||||
testName: "Bookings8hrs",
|
||||
bookings: testBookings8hrs,
|
||||
expectedTime: time.Minute * 30,
|
||||
},
|
||||
{
|
||||
testName: "Bookings10hrs",
|
||||
bookings: testBookings10hrs,
|
||||
expectedTime: time.Minute * 45,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
t.Run("Calc Absence Worktime: "+tc.testName, func(t *testing.T) {
|
||||
var testCase = testWorkDay
|
||||
testCase.Bookings = tc.bookings
|
||||
workTime := testCase.GetPausetimeReal(testUser, models.WorktimeBaseDay)
|
||||
if workTime != tc.expectedTime {
|
||||
t.Errorf("GetPausetimeReal not working, time should be %s, but was %s", helper.FormatDurationFill(tc.expectedTime, true), helper.FormatDurationFill(workTime, true))
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestWorkdayPausetimeWeek(t *testing.T) {
|
||||
testCases := []struct {
|
||||
testName string
|
||||
bookings []models.Booking
|
||||
expectedTime time.Duration
|
||||
}{
|
||||
{
|
||||
testName: "Bookings6hrs",
|
||||
bookings: testBookings6hrs,
|
||||
expectedTime: 0,
|
||||
},
|
||||
{
|
||||
testName: "Bookings8hrs",
|
||||
bookings: testBookings8hrs,
|
||||
expectedTime: time.Minute * 30,
|
||||
},
|
||||
{
|
||||
testName: "Bookings10hrs",
|
||||
bookings: testBookings10hrs,
|
||||
expectedTime: time.Minute * 45,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
t.Run("Calc Absence Worktime: "+tc.testName, func(t *testing.T) {
|
||||
var testCase = testWorkDay
|
||||
testCase.Bookings = tc.bookings
|
||||
workTime := testCase.GetPausetimeReal(testUser, models.WorktimeBaseWeek)
|
||||
if workTime != tc.expectedTime {
|
||||
t.Errorf("GetPausetimeReal not working, time should be %s, but was %s", helper.FormatDurationFill(tc.expectedTime, true), helper.FormatDurationFill(workTime, true))
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
@@ -47,18 +47,20 @@ func NewWorkWeek(user User, tsMonday time.Time, populate bool) WorkWeek {
|
||||
}
|
||||
|
||||
func (w *WorkWeek) PopulateWithDays(worktime time.Duration, overtime time.Duration) {
|
||||
slog.Debug("Populating Workweek for user", "user", w.User)
|
||||
slog.Debug("Got Days with overtime and worktime", slog.String("worktime", worktime.String()), slog.String("overtime", overtime.String()))
|
||||
w.Days = GetDays(w.User, w.WeekStart, w.WeekStart.Add(6*24*time.Hour), false)
|
||||
|
||||
for _, day := range w.Days {
|
||||
work, _ := day.TimePauseReal(w.User)
|
||||
w.Worktime += work
|
||||
w.WorkTimeVirtual += day.TimeWorkVirtual(w.User)
|
||||
w.Worktime += day.GetWorktimeReal(w.User, WorktimeBaseDay)
|
||||
w.WorkTimeVirtual += day.GetWorktimeVirtual(w.User, WorktimeBaseDay)
|
||||
}
|
||||
slog.Debug("Got worktime for user", "user", w.User, "worktime", w.Worktime.String(), "virtualWorkTime", w.WorkTimeVirtual.String())
|
||||
slog.Debug("Got worktime for user", "worktime", w.Worktime.String(), "virtualWorkTime", w.WorkTimeVirtual.String())
|
||||
|
||||
w.Overtime = w.WorkTimeVirtual - w.User.ArbeitszeitProWoche()
|
||||
|
||||
slog.Debug("Calculated overtime", "worktime", w.Worktime.String(), "virtualWorkTime", w.WorkTimeVirtual.String())
|
||||
|
||||
w.Worktime = w.Worktime.Round(time.Minute)
|
||||
w.Overtime = w.Overtime.Round(time.Minute)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user