-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcalculations.go
66 lines (60 loc) · 1.73 KB
/
calculations.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
57
58
59
60
61
62
63
64
65
66
/*
This file holds all calculation like sums, calculation of "magic number"
*/
package main
import "time"
// calcRate Calculates the so-called "Magic Number"
// The daily amount of money you can spend for a signle fixed expense/income
func calcRate(trans Transaction) float64 {
magicAdd := 0.0
switch trans.Recurrence {
case "monthly":
magicAdd = (trans.Amount * 12) / daysInYear(time.Now().Year())
case "yearly":
magicAdd = trans.Amount / daysInYear(time.Now().Year())
case "twice a year":
magicAdd = (trans.Amount * 2) / daysInYear(time.Now().Year())
case "quarterly":
magicAdd = (trans.Amount * 4) / daysInYear(time.Now().Year())
}
switch trans.Income {
case true:
return magicAdd
case false:
magicAdd = -magicAdd
}
return magicAdd
}
// Helper to calculate the amount of days in a month
func daysInMonth(y int, m time.Month) int {
return time.Date(y, m, 1, 0, 0, 0, 0, time.UTC).AddDate(0, 1, -1).Day()
}
// Gets the amount of days in a single year
func daysInYear(year int) float64 {
numdays := 0.0
for month := time.January; month <= time.December; month++ {
numdays += float64(daysInMonth(year, month))
}
return numdays
}
// Calculates the total expenses per period
func expensesPerPeriod(period string, channel chan float64) {
expenses := totalExpenses(db, period)
magicNumber := baseMagic(db)
var total float64
switch period {
case "week":
total = (magicNumber * 7) + expenses
case "month":
total = (magicNumber * float64(daysInMonth(time.Now().Year(), time.Now().Month()))) + expenses
case "year":
total = (magicNumber * daysInYear(time.Now().Year())) + expenses
}
channel <- total
}
func percentages(total, transam float64) float64 {
if transam < 0 {
transam *= -1
}
return (transam * 100) / total
}