2017-06-16 20:55:22 -04:00
|
|
|
function account_series_map(accounts, tabulation)
|
2017-02-22 08:26:21 -05:00
|
|
|
map = {}
|
|
|
|
|
|
|
|
for i=1,100 do -- we're not messing with accounts more than 100 levels deep
|
|
|
|
all_handled = true
|
|
|
|
for id, acct in pairs(accounts) do
|
|
|
|
if not map[id] then
|
|
|
|
all_handled = false
|
|
|
|
if not acct.parent then
|
2017-06-16 20:55:22 -04:00
|
|
|
map[id] = tabulation:series(acct.name)
|
2017-02-22 08:26:21 -05:00
|
|
|
elseif map[acct.parent.accountid] then
|
|
|
|
map[id] = map[acct.parent.accountid]:series(acct.name)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
if all_handled then
|
|
|
|
return map
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
error("Accounts nested (at least) 100 levels deep")
|
|
|
|
end
|
|
|
|
|
|
|
|
function generate()
|
|
|
|
year = date.now().year
|
|
|
|
account_type = account.Income
|
|
|
|
|
|
|
|
accounts = get_accounts()
|
2017-06-23 06:01:54 -04:00
|
|
|
t = tabulation.new(1)
|
|
|
|
t:title(year .. " Income")
|
|
|
|
series_map = account_series_map(accounts, t)
|
2017-02-22 08:26:21 -05:00
|
|
|
|
|
|
|
begin_date = date.new(year, 1, 1)
|
|
|
|
end_date = date.new(year+1, 1, 1)
|
|
|
|
|
2017-06-23 06:01:54 -04:00
|
|
|
t:label(1, year .. " Income")
|
2017-02-22 08:26:21 -05:00
|
|
|
|
|
|
|
for id, acct in pairs(accounts) do
|
|
|
|
series = series_map[id]
|
|
|
|
if acct.type == account_type then
|
|
|
|
balance = acct:balance(begin_date, end_date)
|
|
|
|
series:value(1, balance.amount)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-06-23 06:01:54 -04:00
|
|
|
return t
|
2017-02-22 08:26:21 -05:00
|
|
|
end
|