summaryrefslogtreecommitdiff
path: root/internal/reports/invoice.go
blob: 4ac5eb4c4ac5d4d440ddd7310953c9340d03496d (plain)
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
package reports

import (
	"database/sql"
	"fmt"
	"time"

	"git.tjp.lol/punchcard/internal/queries"
)

type InvoiceData struct {
	ClientID        int64
	ClientName      string
	ProjectName     string
	ContractorName  string
	ContractorLabel string
	ContractorEmail string
	InvoiceNumber   int64
	DateRange       DateRange
	LineItems       []LineItem
	TotalHours      float64
	TotalAmount     float64
	GeneratedDate   time.Time
}

type LineItem struct {
	Description string  `json:"description"`
	Hours       float64 `json:"hours"`
	Rate        float64 `json:"rate"`
	Amount      float64 `json:"amount"`
}

type RateSource string

const (
	RateSourceEntry   RateSource = "entry"
	RateSourceProject RateSource = "project"
	RateSourceClient  RateSource = "client"
)

type timeEntryData struct {
	TimeEntryID         int64
	StartTime           time.Time
	EndTime             sql.NullTime
	Description         sql.NullString
	EntryBillableRate   sql.NullInt64
	ClientID            int64
	ClientName          string
	ClientBillableRate  sql.NullInt64
	ProjectID           sql.NullInt64
	ProjectName         sql.NullString
	ProjectBillableRate sql.NullInt64
	DurationSeconds     int64
	RateSource          string
}

func GenerateInvoiceData(
	entries interface{},
	clientID int64,
	clientName,
	projectName string,
	contractor queries.Contractor,
	number int64,
	dateRange DateRange,
) (*InvoiceData, error) {
	var timeEntries []timeEntryData

	switch e := entries.(type) {
	case []queries.GetInvoiceDataByClientRow:
		for _, entry := range e {
			timeEntries = append(timeEntries, timeEntryData{
				TimeEntryID:         entry.TimeEntryID,
				StartTime:           entry.StartTime,
				EndTime:             entry.EndTime,
				Description:         entry.Description,
				EntryBillableRate:   entry.EntryBillableRate,
				ClientID:            entry.ClientID,
				ClientName:          entry.ClientName,
				ClientBillableRate:  entry.ClientBillableRate,
				ProjectID:           entry.ProjectID,
				ProjectName:         entry.ProjectName,
				ProjectBillableRate: entry.ProjectBillableRate,
				DurationSeconds:     entry.DurationSeconds,
				RateSource:          entry.RateSource,
			})
		}
	case []queries.GetInvoiceDataByProjectRow:
		for _, entry := range e {
			timeEntries = append(timeEntries, timeEntryData{
				TimeEntryID:         entry.TimeEntryID,
				StartTime:           entry.StartTime,
				EndTime:             entry.EndTime,
				Description:         entry.Description,
				EntryBillableRate:   entry.EntryBillableRate,
				ClientID:            entry.ClientID,
				ClientName:          entry.ClientName,
				ClientBillableRate:  entry.ClientBillableRate,
				ProjectID:           sql.NullInt64{Int64: entry.ProjectID, Valid: true},
				ProjectName:         sql.NullString{String: entry.ProjectName, Valid: true},
				ProjectBillableRate: entry.ProjectBillableRate,
				DurationSeconds:     entry.DurationSeconds,
				RateSource:          entry.RateSource,
			})
		}
	default:
		return nil, fmt.Errorf("unsupported entry type")
	}

	lineItems := groupTimeEntriesIntoLineItems(timeEntries)

	totalHours := 0.0
	totalAmount := 0.0
	for _, item := range lineItems {
		totalHours += item.Hours
		totalAmount += item.Amount
	}

	invoice := &InvoiceData{
		ClientID:        clientID,
		ClientName:      clientName,
		ProjectName:     projectName,
		ContractorName:  contractor.Name,
		ContractorLabel: contractor.Label,
		ContractorEmail: contractor.Email,
		InvoiceNumber:   number,
		DateRange:       dateRange,
		LineItems:       lineItems,
		TotalHours:      totalHours,
		TotalAmount:     totalAmount,
		GeneratedDate:   time.Now().UTC(),
	}

	return invoice, nil
}

func groupTimeEntriesIntoLineItems(entries []timeEntryData) []LineItem {
	var lineItems []LineItem

	// Group 1: Entries with overridden rates
	entryRateGroups := make(map[int64][]timeEntryData)

	// Group 2: Entries using project rates
	projectRateGroups := make(map[int64][]timeEntryData)

	// Group 3: Entries using client rates
	clientRateGroups := make(map[int64][]timeEntryData)

	for _, entry := range entries {
		switch RateSource(entry.RateSource) {
		case RateSourceEntry:
			rate := entry.EntryBillableRate.Int64
			entryRateGroups[rate] = append(entryRateGroups[rate], entry)
		case RateSourceProject:
			if entry.ProjectID.Valid {
				projectRateGroups[entry.ProjectID.Int64] = append(projectRateGroups[entry.ProjectID.Int64], entry)
			}
		case RateSourceClient:
			clientRateGroups[entry.ClientID] = append(clientRateGroups[entry.ClientID], entry)
		}
	}

	// Process overridden rates first
	for rate, entries := range entryRateGroups {
		if len(entries) > 0 {
			lineItem := createLineItem(entries, rate, "Custom rate work")
			lineItems = append(lineItems, lineItem)
		}
	}

	// Process project rates
	for _, entries := range projectRateGroups {
		if len(entries) > 0 {
			projectName := "Unknown Project"
			rateCents := int64(0)
			if entries[0].ProjectName.Valid {
				projectName = entries[0].ProjectName.String
			}
			if entries[0].ProjectBillableRate.Valid {
				rateCents = entries[0].ProjectBillableRate.Int64
			}

			lineItem := createLineItem(entries, rateCents, projectName)
			lineItems = append(lineItems, lineItem)
		}
	}

	// Process client rates
	for _, entries := range clientRateGroups {
		if len(entries) > 0 {
			clientName := entries[0].ClientName
			rateCents := int64(0)
			if entries[0].ClientBillableRate.Valid {
				rateCents = entries[0].ClientBillableRate.Int64
			}

			lineItem := createLineItem(entries, rateCents, fmt.Sprintf("General work - %s", clientName))
			lineItems = append(lineItems, lineItem)
		}
	}

	return lineItems
}

func createLineItem(entries []timeEntryData, hourlyRateCents int64, description string) LineItem {
	totalSeconds := int64(0)
	for _, entry := range entries {
		totalSeconds += entry.DurationSeconds
	}

	// Calculate whole hours and remaining seconds using integer arithmetic
	wholeHours := totalSeconds / 3600
	remainingSeconds := totalSeconds % 3600

	// Calculate amount for whole hours (integer arithmetic, result in cents)
	wholeHoursAmountCents := wholeHours * hourlyRateCents

	// Calculate amount for partial hour, rounded down to the minute
	// First convert remaining seconds to whole minutes (truncate seconds)
	partialMinutes := remainingSeconds / 60
	// Calculate amount for those whole minutes (integer arithmetic)
	partialHourAmountCents := partialMinutes * hourlyRateCents / 60

	// Total amount in cents, then convert to dollars
	totalAmountCents := wholeHoursAmountCents + partialHourAmountCents
	amount := float64(totalAmountCents) / 100.0

	// Convert total seconds to hours for display
	hours := float64(totalSeconds) / 3600.0

	// Convert rate to dollars for display
	hourlyRate := float64(hourlyRateCents) / 100.0

	return LineItem{
		Description: description,
		Hours:       hours,
		Rate:        hourlyRate,
		Amount:      amount,
	}
}