ledger_entry: Only try to balance entry amounts when necessary.

This gives template authors the ability to write entries that
balance something other than the "main" amount.
This commit is contained in:
Brett Smith 2019-07-29 14:52:02 -04:00
parent f6c1d7ed7a
commit eb329e92a0
4 changed files with 20 additions and 3 deletions

View file

@ -190,8 +190,9 @@ class AccountSplitter:
"{}: {}".format(type(error).__name__, error),
"template {!r}".format(self.template_name)
) from error
self._balance_amounts(amounts, template_vars['amount'])
self._balance_amounts(amounts, -template_vars['amount'])
if sum(amt for _, amt in amounts) != 0:
self._balance_amounts(amounts, template_vars['amount'])
self._balance_amounts(amounts, -template_vars['amount'])
return amounts
def _iter_splits(self, template_vars):

View file

@ -30,7 +30,7 @@ REQUIREMENTS['tests_require'] = [
setup(
name='import2ledger',
description="Import different sources of financial data to Ledger",
version='0.9.1',
version='0.9.2',
author='Brett Smith',
author_email='brettcsmith@brettcsmith.org',
license='GNU AGPLv3+',

View file

@ -60,6 +60,11 @@ template =
Income:Sales -3 if 'x' not in {false} else -{amount}
Assets:Cash {amount}
[SelfBalanced]
template =
Income:Sales -5
Assets:Cash 5
[NondecimalWord]
template =
Income:Sales -5

View file

@ -214,6 +214,17 @@ def test_string_conditionals():
" Assets:Cash 6.00 USD",
]
def test_self_balanced():
# The amount needs to be different than what's in the template.
render_vars = template_vars('NN', '0')
lines = render_lines(render_vars, 'SelfBalanced')
assert lines == [
"",
"2015/03/14 NN",
" Income:Sales -5.00 USD",
" Assets:Cash 5.00 USD",
]
@pytest.mark.parametrize('amount_expr', [
'',
'name',