From a3e8bff5fc7dfc492528e4d09742e0b5aea1b820 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Niko=20Heikkil=C3=A4?= Date: Wed, 8 May 2024 16:30:45 +0300 Subject: [PATCH] refactor: clean up hours logic --- hours.go | 58 +++++++++++++++++++++++++++----------------------------- 1 file changed, 28 insertions(+), 30 deletions(-) diff --git a/hours.go b/hours.go index 85e1f4d..bf88451 100644 --- a/hours.go +++ b/hours.go @@ -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 { @@ -21,10 +21,10 @@ 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 } @@ -32,9 +32,9 @@ func (e *TimeEntries) TotalHours() float64 { } // 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 } @@ -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) } @@ -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) }