94 lines
2.9 KiB
Go
94 lines
2.9 KiB
Go
package models
|
|
|
|
// this file desribes the IWorkDay interface which is used, to combine the diffent kinds
|
|
// of day types (absence, holidy, workday) and make them more compatimble with the rest
|
|
//
|
|
// the IWorkDay as an interface is not in the database
|
|
|
|
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 {
|
|
|
|
// Check if there is already a day
|
|
existingDay, ok := allDays[absentDay.Date().Format(time.DateOnly)]
|
|
switch {
|
|
case absentDay.AbwesenheitTyp.WorkTime < 0:
|
|
if workDay, ok := allDays[absentDay.Date().Format(time.DateOnly)].(*WorkDay); ok {
|
|
workDay.kurzArbeit = true
|
|
workDay.kurzArbeitAbsence = absentDay
|
|
}
|
|
case ok && !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
|
|
}
|