From d78c539bd16b7afc2ebf9f2ff4524bf06cb2dc47 Mon Sep 17 00:00:00 2001 From: Mika Tuominen Date: Thu, 3 Oct 2019 23:35:24 +0300 Subject: [PATCH 1/3] Overtime calculation with reducing spent Flexitime Also added DailyTotals func. --- hours.go | 63 +++++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 60 insertions(+), 3 deletions(-) diff --git a/hours.go b/hours.go index 111d65a..071ca11 100644 --- a/hours.go +++ b/hours.go @@ -7,8 +7,27 @@ import ( ) // GetOvertime counts overtime hours from TimeEntries, using also dayTotal function as a helper. -func (e *TimeEntries) GetOvertime(from time.Time, to time.Time) { +func (e *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) { + // dailyHours := e.DailyHours(d) + _, saldo, overtime := e.DailyTotals(d) + + totalOvertime = totalOvertime + overtime + if saldo != 0 { + totalOvertime = totalOvertime - saldo + } + // if IsWorkday(d) { + // if dailyHours > 7.5 { + // overtime = overtime + dailyHours - 7.5 + // } + // if dailyHours < 7.5 { + // overtime = overtime + dailyHours - 7.5 + // } + // } else { + // overtime = overtime + dailyHours + // } + } return } @@ -32,6 +51,44 @@ func (e TimeEntries) Total() float64 { return hours } +// DailyTotals counts total logged in hours for selected date. +// Needs daySelector(time.Time) as parameter for selected date. +// Will outout hours, saldo and overtime. +// Hours is logged hours excluding spent flexitime +// Saldo is spent flexitime for that day +// Overtime is overtime for that day +func (e *TimeEntries) DailyTotals(daySelector time.Time) (hours float64, saldo float64, overtime float64) { + // var selection Entries + // var saldoused = false + + date := daySelector.Format("2006-01-02") // TODO: Switch to get formatter from config + + 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. + // saldoused = true + saldo = saldo + v.Hours + } else { + hours = hours + v.Hours + } + + // Calculate is the day full 7.5 hours, even if saldo using saldos. + } else { + hours = v.Hours + overtime = v.Hours + } + } + } + if IsWorkday(daySelector) { + if hours+saldo != 7.5 { + overtime = (hours + saldo) - 7.5 + } + } + 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 { @@ -58,8 +115,8 @@ func (e Entries) dayTotal() float64 { return hours } -// isWorkday functions as a helper function, to determine if selected date is workday or not. -func isWorkday(date time.Time) bool { // Should this be placed in helpers.go? +// IsWorkday functions as a helper function, to determine if selected date is workday or not. +func IsWorkday(date time.Time) bool { // Should this be placed in helpers.go? if date.Weekday().String() != "Saturday" && date.Weekday().String() != "Sunday" { return true } From f26c5a331fee7d670611c71815f75446fd38e823 Mon Sep 17 00:00:00 2001 From: Mika Tuominen Date: Thu, 3 Oct 2019 23:40:11 +0300 Subject: [PATCH 2/3] Fixing typos --- hours.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/hours.go b/hours.go index 071ca11..4895016 100644 --- a/hours.go +++ b/hours.go @@ -53,10 +53,10 @@ func (e TimeEntries) Total() float64 { // DailyTotals counts total logged in hours for selected date. // Needs daySelector(time.Time) as parameter for selected date. -// Will outout hours, saldo and overtime. -// Hours is logged hours excluding spent flexitime -// Saldo is spent flexitime for that day -// Overtime is overtime for that day +// Will output hours, saldo and overtime. +// Hours is logged hours excluding spent flexitime. +// Saldo is spent flexitime for that day. +// Overtime is overtime for that day. func (e *TimeEntries) DailyTotals(daySelector time.Time) (hours float64, saldo float64, overtime float64) { // var selection Entries // var saldoused = false From 8df681a7f8bdc03d583eb4556d3ed3e980000eba Mon Sep 17 00:00:00 2001 From: Mika Tuominen Date: Wed, 23 Oct 2019 16:00:56 +0300 Subject: [PATCH 3/3] Small cleanup on DailyTotals function --- hours.go | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/hours.go b/hours.go index 4895016..68411b4 100644 --- a/hours.go +++ b/hours.go @@ -59,7 +59,6 @@ func (e TimeEntries) Total() float64 { // Overtime is overtime for that day. func (e *TimeEntries) DailyTotals(daySelector time.Time) (hours float64, saldo float64, overtime float64) { // var selection Entries - // var saldoused = false date := daySelector.Format("2006-01-02") // TODO: Switch to get formatter from config @@ -68,13 +67,11 @@ func (e *TimeEntries) DailyTotals(daySelector time.Time) (hours float64, saldo f // selection = append(selection, v) if IsWorkday(daySelector) { if v.Task.Id == 8814697 { // TODO: Switch to use variable from config. - // saldoused = true saldo = saldo + v.Hours } else { hours = hours + v.Hours } - // Calculate is the day full 7.5 hours, even if saldo using saldos. } else { hours = v.Hours overtime = v.Hours @@ -83,6 +80,7 @@ func (e *TimeEntries) DailyTotals(daySelector time.Time) (hours float64, saldo f } 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 } }