From bce438167c7cb6e1f1c3d92942d35ee6ca6ae3f9 2020-04-05 18:49:16 From: Brett Smith Date: 2020-04-05 18:49:16 Subject: [PATCH] meta_approval: Required when payables are accrued. RT#10259. --- diff --git a/conservancy_beancount/plugin/meta_approval.py b/conservancy_beancount/plugin/meta_approval.py index 15188474092999dcb9b953b0cf3b5d571b6df29b..8bc8d973098ade2828d1af20308ed8c6552a340c 100644 --- a/conservancy_beancount/plugin/meta_approval.py +++ b/conservancy_beancount/plugin/meta_approval.py @@ -34,14 +34,26 @@ class MetaApproval(core._RequireLinksPostingMetadataHook): def _run_on_txn(self, txn: Transaction) -> bool: if not super()._run_on_txn(txn): return False - assets_sum = decimal.Decimal(0) - creditcard_sum = decimal.Decimal(0) + debits_sum = decimal.Decimal(0) for post in data.iter_postings(txn): - if post.is_payment(): - assets_sum -= post.units.number or 0 - elif post.account.is_under(self.CREDIT_CARD_ACCT): - creditcard_sum += post.units.number or 0 - return (assets_sum - creditcard_sum) > self.payment_threshold + # approval is required: + # 1. When a payable is accrued + if (post.account.is_under('Liabilities:Payable:Accounts') + and post.is_debit(self.payment_threshold)): + return True + # 2. When funds leave a cash equivalent asset, UNLESS that + # transaction is a transfer to another asset, or paying off a + # credit card. + # In this case, debits_sum keeps a running tally of how much is + # moving in each direction, and we'll return True if it ends up over + # the payment threshold. + elif post.is_payment(0) or post.account.is_credit_card(): + debits_sum -= post.units.number or 0 + return debits_sum > self.payment_threshold def _run_on_post(self, txn: Transaction, post: data.Posting) -> bool: - return post.is_payment(0) is not False + if post.account.is_under('Liabilities:Payable:Accounts'): + threshold_check = post.is_debit(self.payment_threshold) + else: + threshold_check = post.is_payment(0) + return threshold_check is not False diff --git a/tests/test_meta_approval.py b/tests/test_meta_approval.py index 90f01ec84f109d2446202918e2980ca9fd81e2f9..2bc883cd683687d068fb763ec56a55cf5b9bf19e 100644 --- a/tests/test_meta_approval.py +++ b/tests/test_meta_approval.py @@ -24,6 +24,7 @@ REQUIRED_ACCOUNTS = { 'Assets:Bank:Checking', 'Assets:Cash', 'Assets:Savings', + 'Liabilities:Payable:Accounts', } NON_REQUIRED_ACCOUNTS = { @@ -32,7 +33,8 @@ NON_REQUIRED_ACCOUNTS = { 'Equity:QpeningBalance', 'Expenses:Other', 'Income:Other', - 'Liabilities:Payable:Accounts', + 'Liabilities:Payable:Vacation', + 'Liabilities:UnearnedIncome:Donations', } CREDITCARD_ACCOUNT = 'Liabilities:CreditCard' @@ -148,10 +150,9 @@ def test_approval_not_required_to_charge_credit_card(hook): ]) assert not list(hook.run(txn)) -@pytest.mark.parametrize('acct', REQUIRED_ACCOUNTS) -def test_approval_not_required_to_pay_credit_card(hook, acct): +def test_approval_not_required_to_pay_credit_card(hook): txn = testutil.Transaction(postings=[ - (acct, -25), + ('Assets:Checking', -25), (CREDITCARD_ACCOUNT, 25), ]) assert not list(hook.run(txn))