From eb329e92a066fbf9a21102cf56a5e1da23fb4069 Mon Sep 17 00:00:00 2001 From: Brett Smith Date: Mon, 29 Jul 2019 14:52:02 -0400 Subject: [PATCH] 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. --- import2ledger/hooks/ledger_entry.py | 5 +++-- setup.py | 2 +- tests/data/templates.ini | 5 +++++ tests/test_hook_ledger_entry.py | 11 +++++++++++ 4 files changed, 20 insertions(+), 3 deletions(-) diff --git a/import2ledger/hooks/ledger_entry.py b/import2ledger/hooks/ledger_entry.py index f67a811..c4b1303 100644 --- a/import2ledger/hooks/ledger_entry.py +++ b/import2ledger/hooks/ledger_entry.py @@ -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): diff --git a/setup.py b/setup.py index ef0c095..3b8f35e 100755 --- a/setup.py +++ b/setup.py @@ -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+', diff --git a/tests/data/templates.ini b/tests/data/templates.ini index e7b7b71..dc75538 100644 --- a/tests/data/templates.ini +++ b/tests/data/templates.ini @@ -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 diff --git a/tests/test_hook_ledger_entry.py b/tests/test_hook_ledger_entry.py index d1c5213..6b2da61 100644 --- a/tests/test_hook_ledger_entry.py +++ b/tests/test_hook_ledger_entry.py @@ -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',