-
Notifications
You must be signed in to change notification settings - Fork 0
/
balancereport.go
56 lines (48 loc) · 1.65 KB
/
balancereport.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package timesheet
import (
"fmt"
"time"
"github.com/vshn/odootools/pkg/odoo/model"
)
type BalanceReport struct {
Report Report
// PreviousBalance is the definitive balance from the previous month's payslip, if given.
PreviousBalance time.Duration
// CalculatedBalance is the sum of TotalOvertime with the previous month's payslip, if given.
CalculatedBalance time.Duration
// DefinitiveBalance contains the value of the current month's payslip, if given.
DefinitiveBalance *time.Duration
}
type BalanceReportBuilder struct {
report Report
payslips model.PayslipList
}
func NewBalanceReportBuilder(report Report, payslips model.PayslipList) *BalanceReportBuilder {
return &BalanceReportBuilder{
report: report,
payslips: payslips,
}
}
func (b *BalanceReportBuilder) CalculateBalanceReport() (BalanceReport, error) {
r := BalanceReport{Report: b.report}
// calculated balance
previousMonth := b.payslips.FilterInMonth(r.Report.From.AddDate(0, -1, 0))
if previousMonth != nil {
parsed, err := previousMonth.ParseOvertime()
if err != nil {
return r, fmt.Errorf("cannot parse overtime of payslip '%s': %w", previousMonth.Name, err)
}
r.PreviousBalance = parsed
}
r.CalculatedBalance = r.PreviousBalance + r.Report.Summary.TotalOvertime
// definitive balance
currentMonth := b.payslips.FilterInMonth(r.Report.From.AddDate(0, 0, 1)) // add a day to cover timezone offsets
if currentMonth != nil && currentMonth.Overtime() != "" {
parsed, err := currentMonth.ParseOvertime()
if err != nil {
return r, fmt.Errorf("cannot parse overtime of payslip '%s': %w", currentMonth.Name, err)
}
r.DefinitiveBalance = &parsed
}
return r, nil
}