117 lines
3.0 KiB
Go
117 lines
3.0 KiB
Go
package models
|
|
|
|
// this file contains all functions for the compound day class
|
|
// the compound can merge all kinds of daytypes together, as long as they
|
|
// implement the IWorkDay interface. This is used to have an absence + bookings
|
|
// in one day
|
|
//
|
|
// the compound day is a meta type, which means its not in the database
|
|
|
|
import (
|
|
"log/slog"
|
|
"time"
|
|
)
|
|
|
|
type CompoundDay struct {
|
|
Day time.Time
|
|
DayParts []IWorkDay
|
|
}
|
|
|
|
func NewCompondDay(date time.Time, dayParts ...IWorkDay) *CompoundDay {
|
|
return &CompoundDay{Day: date, DayParts: dayParts}
|
|
}
|
|
|
|
func (c *CompoundDay) AddDayPart(dayPart IWorkDay) {
|
|
c.DayParts = append(c.DayParts, dayPart)
|
|
}
|
|
|
|
func (c *CompoundDay) GetWorkDay() WorkDay {
|
|
workday, ok := c.DayParts[0].(*WorkDay)
|
|
if ok {
|
|
return *workday
|
|
}
|
|
return WorkDay{}
|
|
}
|
|
|
|
// IsEmpty implements [IWorkDay].
|
|
func (c *CompoundDay) IsEmpty() bool {
|
|
return len(c.DayParts) == 0
|
|
}
|
|
|
|
// Date implements [IWorkDay].
|
|
func (c *CompoundDay) Date() time.Time {
|
|
return c.Day
|
|
}
|
|
|
|
// GetDayProgress implements [IWorkDay].
|
|
func (c *CompoundDay) GetDayProgress(u User) int8 {
|
|
var dayProcess int8
|
|
for _, day := range c.DayParts {
|
|
dayProcess += day.GetDayProgress(u)
|
|
}
|
|
return dayProcess
|
|
}
|
|
|
|
// GetOvertime implements [IWorkDay].
|
|
func (c *CompoundDay) GetOvertime(u User, base WorktimeBase, includeKurzarbeit bool) time.Duration {
|
|
work := c.GetWorktime(u, base, includeKurzarbeit)
|
|
var targetHours time.Duration
|
|
|
|
switch base {
|
|
case WorktimeBaseDay:
|
|
targetHours = u.ArbeitszeitProTagFrac(1)
|
|
case WorktimeBaseWeek:
|
|
targetHours = u.ArbeitszeitProWocheFrac(.2)
|
|
}
|
|
return (work - targetHours).Round(time.Minute)
|
|
}
|
|
|
|
// GetPausetime implements [IWorkDay].
|
|
func (c *CompoundDay) GetPausetime(u User, base WorktimeBase, includeKurzarbeit bool) time.Duration {
|
|
var pausetime time.Duration
|
|
for _, day := range c.DayParts {
|
|
pausetime += day.GetPausetime(u, base, includeKurzarbeit)
|
|
}
|
|
return pausetime
|
|
}
|
|
|
|
// GetTimes implements [IWorkDay].
|
|
func (c *CompoundDay) GetTimes(u User, base WorktimeBase, includeKurzarbeit bool) (work time.Duration, pause time.Duration, overtime time.Duration) {
|
|
return c.GetWorktime(u, base, includeKurzarbeit), c.GetPausetime(u, base, includeKurzarbeit), c.GetOvertime(u, base, includeKurzarbeit)
|
|
}
|
|
|
|
// GetWorktime implements [IWorkDay].
|
|
func (c *CompoundDay) GetWorktime(u User, base WorktimeBase, includeKurzarbeit bool) time.Duration {
|
|
var worktime time.Duration
|
|
for _, day := range c.DayParts {
|
|
worktime += day.GetWorktime(u, base, includeKurzarbeit)
|
|
slog.Info("Calc worktime for day", "day", day, "worktime", worktime.String())
|
|
}
|
|
return worktime
|
|
}
|
|
|
|
// IsKurzArbeit implements [IWorkDay].
|
|
func (c *CompoundDay) IsKurzArbeit() bool {
|
|
return false
|
|
}
|
|
|
|
// IsWorkDay implements [IWorkDay].
|
|
func (c *CompoundDay) IsWorkDay() bool {
|
|
return true
|
|
}
|
|
|
|
// RequiresAction implements [IWorkDay].
|
|
func (c *CompoundDay) RequiresAction() bool {
|
|
return false
|
|
}
|
|
|
|
// ToString implements [IWorkDay].
|
|
func (c *CompoundDay) ToString() string {
|
|
return "Compound Day"
|
|
}
|
|
|
|
// Type implements [IWorkDay].
|
|
func (c *CompoundDay) Type() DayType {
|
|
return DayTypeCompound
|
|
}
|