Skip to content

Commit

Permalink
Add ability to have calculated accounts on reports
Browse files Browse the repository at this point in the history
  • Loading branch information
howeyc committed Jul 24, 2016
1 parent 03d0707 commit d9125f7
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 2 deletions.
25 changes: 25 additions & 0 deletions cmd/lweb/handler_report.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,13 @@ func ReportHandler(w http.ResponseWriter, r *http.Request, params martini.Params
colorIdx++
}

for _, calcAccount := range rConf.CalculatedAccounts {
lData.DataSets = append(lData.DataSets,
lineData{AccountName: calcAccount.Name,
RGBColor: colorlist[colorIdx]})
colorIdx++
}

var rType ledger.RangeType
switch rConf.Chart {
case "line":
Expand Down Expand Up @@ -287,6 +294,24 @@ func ReportHandler(w http.ResponseWriter, r *http.Request, params martini.Params
}
}
}
for _, calcAccount := range rConf.CalculatedAccounts {
for _, bal := range rb.Balances {
for _, acctOp := range calcAccount.AccountOperations {
if acctOp.Name == bal.Name {
fval, _ := bal.Balance.Float64()
fval = math.Abs(fval)
aval := accVals[calcAccount.Name]
switch acctOp.Operation {
case "+":
aval += fval
case "-":
aval -= fval
}
accVals[calcAccount.Name] = aval
}
}
}
}
for dIdx := range lData.DataSets {
lData.DataSets[dIdx].Values = append(lData.DataSets[dIdx].Values, accVals[lData.DataSets[dIdx].AccountName])
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/lweb/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ type accountOp struct {

type calculatedAccount struct {
Name string `toml:"name"`
AccountOperations []accountOp `toml:"account_operations"`
AccountOperations []accountOp `toml:"account_operation"`
}

type reportConfig struct {
Expand All @@ -56,7 +56,7 @@ type reportConfig struct {
DateFreq string `toml:"date_freq"`
Accounts []string
Exclude []string `toml:"exclude"`
CalculatedAccounts []calculatedAccount `toml:"calculated_accounts"`
CalculatedAccounts []calculatedAccount `toml:"calculated_account"`
}

type reportConfigStruct struct {
Expand Down
25 changes: 25 additions & 0 deletions cmd/lweb/reports-sample.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,24 @@ chart = "pie"
date_range = "YTD"
accounts = [ "Expenses" ]

[[report]]
name = "YTD Monthly Savings"
chart = "bar"
date_range = "YTD"
date_freq = "Monthly"
accounts = [ "Income", "Expenses" ]

[[report.calculated_account]]
name = "Savings"

[[report.calculated_account.account_operation]]
name = "Income"
operation = "+"

[[report.calculated_account.account_operation]]
name = "Expenses"
operation = "-"

[[report]]
name = "AT Net Worth"
chart = "line"
Expand All @@ -38,6 +56,13 @@ date_range = "All Time"
date_freq = "Quarterly"
accounts = [ "Assets:Fixed Assets", "Expenses:Depreciation" ,"Liabilities:Loans:Auto" ]

[[report]]
name = "AT Vehicle Expenses"
chart = "stackedbar"
date_range = "All Time"
date_freq = "Yearly"
accounts = [ "Expenses:Depreciation", "Expenses:Auto:Gas", "Expenses:Auto:Insurance", "Expenses:Auto:Maintenance" ]

[[report]]
name = "AT Yearly Income"
chart = "bar"
Expand Down

0 comments on commit d9125f7

Please sign in to comment.