Getting balances of parent and empty/zero accounts

828 views
Skip to first unread message

Owen Heisler

unread,
Mar 15, 2017, 11:11:17 PM3/15/17
to ledge...@googlegroups.com
I am trying to use the ledger 3.1 `balance` command to produce output
for another script. I am using `--balance-format
"%A=%(display_total)\n"` for this purpose. Unfortunately, I have
found that though I have declared all accounts with the `account`
command, it is impossible to get values for any accounts that have
not had transactions during the period selected, including parent
accounts with non-zero balances.

For example, consider the following journal.

~~~
account Assets
account Expenses
account Expenses:Clothing
account Expenses:Food
account Income

2017-03-15 Employer
Income $-100
Assets $100

2017-03-15 Restaurant
Assets $-20
Expenses:Food $20
~~~

The balance of the Expenses (parent) account is $20. The balance of
the Expenses:Clothing account is 0. But the balance command shows
neither.

~~~
$ ledger --no-total --balance-format "%A=%(display_total)\n" balance
Assets=$80
Expenses:Food=$20
Income=$-100
~~~

I would like to get balances of all accounts, including empty/zero
accounts (Expenses:Clothing) and parent accounts (Expenses) that do
not have transactions of their own.

I can make ledger provide values for parent accounts if I add a dummy
automated transaction like this with a line for every account.

~~~
= expr true
Assets 0
Expenses 0
Expenses:Clothing 0
Expenses:Food 0
Income 0
~~~

Now I at least get the parent accounts (Expenses), but still not
empty/zero accounts (Expenses:Clothing).

~~~
$ ledger --no-total --balance-format "%A=%(display_total)\n" balance
Assets=$80
Expenses=$20
Expenses:Food=$20
Income=$-100
~~~

Note that this is not resolved by simply adding more transactions,
because a selected `--period` for a report may or may not contain
transactions that touch a specific account.

Am I missing something?

Thanks for your help!

Owen Heisler

unread,
Mar 16, 2017, 12:46:45 PM3/16/17
to ledge...@googlegroups.com
Richard Lawrence wrote at 2017-03-16 10:16 -0500:
>I think maybe you just need to add the --empty flag for this. Not
>sure if this will still work with the custom format string you're
>using, but if it does, it's a simple fix!

Thank you for the suggestion. The `--empty` flag actually has no
effect in this case, unless I again use the dummy automated
transaction. In that case, it does solve the problem, after a
fashion.

Here is the same example journal, with the dummy automated
transaction commented out.

~~~
account Assets
account Expenses
account Expenses:Clothing
account Expenses:Food
account Income

#= expr true
# Assets 0
# Expenses 0
# Expenses:Clothing 0
# Expenses:Food 0
# Income 0

2017-03-15 Employer
Income $-100
Assets $100

2017-03-15 Restaurant
Assets $-20
Expenses:Food $20
~~~

The `--empty` option has no effect, empty/zero accounts
(Expenses:Clothing) and parent accounts (Expenses) are still skipped.
Removing the `--balance-format` has no effect on which accounts are
shown.

~~~
$ ledger --no-total --empty balance
$80 Assets
$20 Expenses:Food
$-100 Income
~~~

But if I enable the dummy automated transaction shown above, I get
all the accounts.

~~~
$ ledger --no-total --empty balance
$80 Assets
$20 Expenses
0 Clothing
$20 Food
$-100 Income
~~~

~~~
$ ledger --version | head -n1
Ledger 3.1.1-20160111, the command-line accounting tool
~~~

I have filed a bug:
<http://bugs.ledger-cli.org/show_bug.cgi?id=1204>

John Wiegley

unread,
Mar 16, 2017, 8:21:21 PM3/16/17
to Owen Heisler, ledge...@googlegroups.com
>>>>> "OH" == Owen Heisler <owen...@gmail.com> writes:

OH> Thank you for the suggestion. The `--empty` flag actually has no effect in
OH> this case, unless I again use the dummy automated transaction. In that
OH> case, it does solve the problem, after a fashion.

Strange, I tried your command here on my own ledger file, and --empty causes
the "=0" accounts to all appear... Maybe some other interaction is happening.

--
John Wiegley GPG fingerprint = 4710 CF98 AF9B 327B B80F
http://newartisans.com 60E1 46C4 BD1A 7AC1 4BA2

Owen Heisler

unread,
Mar 16, 2017, 9:44:06 PM3/16/17
to John Wiegley, ledge...@googlegroups.com
John Wiegley wrote at 2017-03-16 19:22 -0500:
>Strange, I tried your command here on my own ledger file, and
>--empty causes the "=0" accounts to all appear... Maybe some other
>interaction is happening.

Thanks for your reply!

Do you mind testing on the attached journal file (test.dat)?

I get the following result with ledger 3.1.1-20160111.

~~~
$ ledger --verbose --file test.dat --empty bal
0ms [INFO] Ledger starting
1ms [INFO] Parsing file "/home/user/test.dat"
2ms [INFO] Read journal file (1ms)
2ms [INFO] Found 2 transactions
$80 Assets
$20 Expenses:Food
$-100 Income
--------------------
0
7ms [INFO] Finished executing command (3ms)
8ms [INFO] Ledger ended
~~~

Do have any ideas for further testing?

Thanks!
test.dat

John Wiegley

unread,
Mar 16, 2017, 11:44:32 PM3/16/17
to Owen Heisler, ledge...@googlegroups.com
>>>>> "OH" == Owen Heisler <owen...@gmail.com> writes:

OH> Do you mind testing on the attached journal file (test.dat)?

So, you want both Expenses and Expenses:Clothing to be reported with a balance
of 0?

Owen Heisler

unread,
Mar 17, 2017, 10:58:28 AM3/17/17
to John Wiegley, ledge...@googlegroups.com
John Wiegley wrote at 2017-03-16 22:45 -0500:
>So, you want both Expenses and Expenses:Clothing to be reported with
>a balance of 0?

No, the expected output is below.

~~~
$ ledger --no-total --empty balance # EXAMPLE

John Wiegley

unread,
Mar 17, 2017, 12:27:19 PM3/17/17
to Owen Heisler, ledge...@googlegroups.com
>>>>> "OH" == Owen Heisler <owen...@gmail.com> writes:

OH> No, the expected output is below.

I believe the problem here is that because your data file never uses Clothing
in a transaction, Ledger doesn't actually create the account.

Owen Heisler

unread,
Mar 17, 2017, 3:32:10 PM3/17/17
to ledge...@googlegroups.com
John Wiegley wrote at 2017-03-17 11:28 -0500:
>I believe the problem here is that because your data file never uses
>Clothing in a transaction, Ledger doesn't actually create the
>account.

It seems that the account would be explicitly created by the
`account` command. This behavior makes integration more difficult. If
an external script receives no record for a particular account,
should it assume it is zero/empty?

The bigger issue is that the Expenses account, which is not zero (it
has a non-zero child account), is also not displayed. This makes it
impossible to use child account balances for detailed reports and
parent account balances for general reports (without using additional
processing or dummy automated transactions).

~~~
$ ledger --file test.dat --no-total --empty bal
$80 Assets
$20 Expenses:Food
$-100 Income
~~~

Owen Heisler

unread,
Mar 17, 2017, 9:33:06 PM3/17/17
to John Wiegley, ledge...@googlegroups.com
John Wiegley wrote at 2017-03-17 17:26 -0500:
>The thing is, the balance report iterates over all the
>*transactions* matching your query to build up the set of accounts
>it should report; that way, it doesn't report accounts outside of
>that query.

Okay, so `--empty` only affects accounts that *have* had activity in
the matching transaction set but have a final balance of zero.

>Even if I did create all the accounts mentioned in the "account"
>directives, there would be no associated transactions for the
>balance report to find. So maybe what we need to do is inject some
>dummy "Starting balance" transactions, that simply draw from
>Equity:Opening Balances in the amount of integer 0.

Do you mean, create the accounts mentioned by the `account` command
*if* the `--empty` option is used? This seems like it would work in
general, it would fix the problem for me, and it is similar to what I
did in the dummy automated transaction in the original post (and
repeated below). I am not aware of what effect this might have, if
any, on the output of other report commands.

~~~
= expr true
Assets 0
Expenses 0
Expenses:Clothing 0
Expenses:Food 0
Income 0
~~~

Also, is it necessary to assume that the user is using an account
structure that includes "Equity:Opening Balances"? Why not just use
the list of accounts (see the example above)? Or maybe this does not
matter.

John Wiegley

unread,
Mar 17, 2017, 11:36:14 PM3/17/17
to Owen Heisler, ledge...@googlegroups.com
>>>>> "OH" == Owen Heisler <owen...@gmail.com> writes:

OH> Also, is it necessary to assume that the user is using an account
OH> structure that includes "Equity:Opening Balances"? Why not just use the
OH> list of accounts (see the example above)? Or maybe this does not matter.

Actually, since the dummy transaction is a posting in the amount of zero, I
suppose it doesn't need to balance with anything else.

Owen Heisler

unread,
Mar 25, 2017, 9:06:39 PM3/25/17
to John Wiegley, ledge...@googlegroups.com
I have updated the bug report at:
<http://bugs.ledger-cli.org/show_bug.cgi?id=1204>

Thank you!
Reply all
Reply to author
Forward
0 new messages