Files
arbeitszeitmessung/Backend/models/iworkday.go
tom_trgr 855cd6f34f
Some checks failed
Tests / Run Go Tests (push) Failing after 44s
working compound days + working public holidays
2025-12-24 01:38:16 +01:00

92 lines
2.8 KiB
Go

package models
import (
"log/slog"
"time"
)
type IWorkDay interface {
Date() time.Time
ToString() string
Type() DayType
IsWorkDay() bool
IsKurzArbeit() bool
GetDayProgress(User) int8
RequiresAction() bool
GetWorktime(User, WorktimeBase, bool) time.Duration
GetPausetime(User, WorktimeBase, bool) time.Duration
GetTimes(User, WorktimeBase, bool) (work, pause, overtime time.Duration)
GetOvertime(User, WorktimeBase, bool) time.Duration
IsEmpty() bool
}
type DayType int
const (
DayTypeWorkday DayType = 1
DayTypeAbsence DayType = 2
DayTypeHoliday DayType = 3
DayTypeCompound DayType = 4
)
func GetDays(user User, tsFrom, tsTo time.Time, orderedForward bool) []IWorkDay {
var allDays map[string]IWorkDay = make(map[string]IWorkDay)
workdays := GetWorkDays(user, tsFrom, tsTo)
absences, err := GetAbsencesByCardUID(user.CardUID, tsFrom, tsTo)
if err != nil {
slog.Warn("Error gettings absences!", slog.Any("Error", err))
return nil
}
holidays, err := GetHolidaysFromTo(tsFrom, tsTo)
if err != nil {
slog.Warn("Error getting holidays!", slog.Any("Error", err))
return nil
}
for _, day := range workdays {
allDays[day.Date().Format(time.DateOnly)] = &day
}
for _, absentDay := range absences {
// Kurzarbeit should be integrated in workday
existingDay, ok := allDays[absentDay.Date().Format(time.DateOnly)]
if !ok {
allDays[absentDay.Date().Format(time.DateOnly)] = &absentDay
continue
}
switch {
case absentDay.AbwesenheitTyp.WorkTime < 0:
if workDay, ok := allDays[absentDay.Date().Format(time.DateOnly)].(*WorkDay); ok {
workDay.kurzArbeit = true
workDay.kurzArbeitAbsence = absentDay
}
case !existingDay.IsEmpty():
allDays[absentDay.Date().Format(time.DateOnly)] = NewCompondDay(absentDay.Date(), existingDay, &absentDay)
default:
allDays[absentDay.Date().Format(time.DateOnly)] = &absentDay
}
}
for _, holiday := range holidays {
existingDay, ok := allDays[holiday.Date().Format(time.DateOnly)]
if !ok {
allDays[holiday.Date().Format(time.DateOnly)] = &holiday
continue
}
slog.Info("Existing Day", "day", existingDay)
switch {
case existingDay.Type() == DayTypeCompound:
allDays[holiday.Date().Format(time.DateOnly)].(*CompoundDay).AddDayPart(&holiday)
case existingDay.Type() != DayTypeCompound && !existingDay.IsEmpty():
allDays[holiday.Date().Format(time.DateOnly)] = NewCompondDay(holiday.Date(), existingDay, &holiday)
default:
allDays[holiday.Date().Format(time.DateOnly)] = &holiday
}
slog.Debug("Logging Holiday: ", slog.String("HolidayName", allDays[holiday.Date().Format(time.DateOnly)].ToString()), slog.Any("Overtime", holiday.GetOvertime(user, WorktimeBaseDay, false).String()), "wokrtie", float32(holiday.worktime)/100)
}
sortedDays := sortDays(allDays, orderedForward)
return sortedDays
}