-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmonth.go
100 lines (90 loc) · 3.52 KB
/
month.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
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
package budget
import (
"encoding/json"
"fmt"
)
// Month contains the overall financial picture for a specific month.
type Month struct {
Date string
ReadyToAssign Balance
Assigned Balance
Underfunded Balance
Income Balance
Expenses Balance
}
// UnmarshalJSON unmarshals the JSON data retrieved from YNAB's API into the Month struct.
func (m *Month) UnmarshalJSON(data []byte) error {
var format MonthData
var underfunded int
err := json.Unmarshal(data, &format)
if err != nil {
return err
}
for _, category := range format.Data.Month.Categories {
if category.GoalUnderFunded != nil {
underfunded += *category.GoalUnderFunded
}
}
// Assigning the values into NewRat avoids floating point errors
m.ReadyToAssign = NewRat(format.Data.Month.ToBeBudgeted)
m.Assigned = NewRat(format.Data.Month.Budgeted)
m.Underfunded = NewRat(underfunded)
m.Income = NewRat(format.Data.Month.Income)
m.Expenses = NewRat(-format.Data.Month.Activity)
m.Date = format.Data.Month.Month
// Calculate payments made to the Credit Card Payments category as an expense
for _, category := range format.Data.Month.Categories {
if category.CategoryGroupName == "Credit Card Payments" {
m.Expenses = m.Expenses + NewRat(category.Activity)
}
}
return nil
}
// Report prints the current state of finances for a Month.
func (m Month) Report() {
fmt.Printf("Ready to Assign: %v\n", m.ReadyToAssign)
fmt.Printf("Assigned: %v\n", m.Assigned)
fmt.Printf("Underfunded: %v\n", m.Underfunded)
fmt.Printf("Income: %v\n", m.Income)
fmt.Printf("Expenses: %v\n", m.Expenses)
}
// MonthData https://api.ynab.com/v1/#/Months/getBudgetMonth
type MonthData struct {
Data struct {
Month struct {
Month string `json:"month"`
Note string `json:"note"`
Income int `json:"income"`
Budgeted int `json:"budgeted"`
Activity int `json:"activity"`
ToBeBudgeted int `json:"to_be_budgeted"`
AgeOfMoney int `json:"age_of_money"`
Deleted bool `json:"deleted"`
Categories []struct {
Id string `json:"id"`
CategoryGroupId string `json:"category_group_id"`
CategoryGroupName string `json:"category_group_name"`
Name string `json:"name"`
Hidden bool `json:"hidden"`
OriginalCategoryGroupId interface{} `json:"original_category_group_id"`
Note *string `json:"note"`
Budgeted int `json:"budgeted"`
Activity int `json:"activity"`
Balance int `json:"balance"`
GoalType *string `json:"goal_type"`
GoalDay *int `json:"goal_day"`
GoalCadence *int `json:"goal_cadence"`
GoalCadenceFrequency *int `json:"goal_cadence_frequency"`
GoalCreationMonth *string `json:"goal_creation_month"`
GoalTarget int `json:"goal_target"`
GoalTargetMonth *string `json:"goal_target_month"`
GoalPercentageComplete *int `json:"goal_percentage_complete"`
GoalMonthsToBudget *int `json:"goal_months_to_budget"`
GoalUnderFunded *int `json:"goal_under_funded"`
GoalOverallFunded *int `json:"goal_overall_funded"`
GoalOverallLeft *int `json:"goal_overall_left"`
Deleted bool `json:"deleted"`
} `json:"categories"`
} `json:"month"`
} `json:"data"`
}