Skip to content

Commit

Permalink
refactor: clean up hours logic
Browse files Browse the repository at this point in the history
  • Loading branch information
nikoheikkila committed May 13, 2024
1 parent 8f58141 commit a3e8bff
Showing 1 changed file with 28 additions and 30 deletions.
58 changes: 28 additions & 30 deletions hours.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import (
)

// GetOvertime counts overtime hours from TimeEntries, using also dayTotal function as a helper.
func (e *TimeEntries) GetOvertime(from time.Time, to time.Time) (totalOvertime float64) {
func (entries *TimeEntries) GetOvertime(from time.Time, to time.Time) (totalOvertime float64) {
for d := from; d.Before(to) || d.Equal(to); d = d.AddDate(0, 0, 1) {
_, saldo, overtime := e.DailyTotals(d)
_, saldo, overtime := entries.DailyTotals(d)

totalOvertime = totalOvertime + overtime
if saldo != 0 {
Expand All @@ -21,20 +21,20 @@ func (e *TimeEntries) GetOvertime(from time.Time, to time.Time) (totalOvertime f
}

// TotalHours counts total logged hours from the TimeEntries struct
func (e *TimeEntries) TotalHours() float64 {
func (entries *TimeEntries) TotalHours() float64 {
var hours float64

for _, v := range e.Entries {
for _, v := range entries.Entries {
hours = hours + v.Hours
}

return hours
}

// Total counts total logged hours from the TimeEntries struct
func (e *TimeEntries) Total() float64 {
func (entries *TimeEntries) Total() float64 {
var hours float64
for _, v := range e.Entries {
for _, v := range entries.Entries {
hours = hours + v.Hours
}

Expand All @@ -47,45 +47,43 @@ func (e *TimeEntries) Total() float64 {
// Hours is logged hours excluding spent flexitime.
// Saldo is spent flexitime for that day.
// Overtime is extra time worked for that day.
func (e *TimeEntries) DailyTotals(daySelector time.Time) (hours float64, saldo float64, overtime float64) {
// var selection Entries

func (entries *TimeEntries) DailyTotals(daySelector time.Time) (hours float64, saldo float64, overtime float64) {
const fullDay = 7.5
date := daySelector.Format("2006-01-02") // TODO: Switch to get formatter from config
workday := IsWorkday(daySelector)

for _, v := range e.Entries {
if v.SpentDate == date {
// selection = append(selection, v)
if IsWorkday(daySelector) {
// if v.Task.Id == 8814697 { // TODO: Switch to use variable from config.
if containsInt(flexitimeIDs, v.Task.Id) { // TODO: Switch to use variable from config.
saldo = saldo + v.Hours
} else {
hours = hours + v.Hours
}
for _, entry := range entries.Entries {
if entry.SpentDate != date {
continue
}

if workday {
if containsInt(flexitimeIDs, entry.Task.Id) {
saldo = saldo + entry.Hours
} else {
hours = v.Hours
overtime = v.Hours
hours = hours + entry.Hours
}
} else {
hours = entry.Hours
overtime = entry.Hours
}
}
if IsWorkday(daySelector) {
if hours+saldo != 7.5 {
// Calculate if the day full 7.5 hours, even if saldo using saldos.
overtime = (hours + saldo) - 7.5
}

if workday && hours+saldo != fullDay {
overtime = (hours + saldo) - fullDay
}

return
}

// DailyHours counts total logged in hours for selected date.
// Needs daySelector(time.Time) as parameter for selected date.
func (e *TimeEntries) DailyHours(daySelector time.Time) float64 {
func (entries *TimeEntries) DailyHours(daySelector time.Time) float64 {
var selection Entries

date := daySelector.Format("2006-01-02")

for _, v := range e.Entries {
for _, v := range entries.Entries {
if v.SpentDate == date {
selection = append(selection, v)
}
Expand Down Expand Up @@ -114,8 +112,8 @@ func IsWorkday(date time.Time) bool { // Should this be placed in helpers.go?
}

// Filter is generic function to filter Entries
func (e *TimeEntries) Filter(f func(structs.Entries) bool) (ret []structs.Entries) {
for _, v := range e.Entries {
func (entries *TimeEntries) Filter(f func(structs.Entries) bool) (ret []structs.Entries) {
for _, v := range entries.Entries {
if f(v) {
ret = append(ret, v)
}
Expand Down

0 comments on commit a3e8bff

Please sign in to comment.