meta_approval: Required when payables are accrued. RT#10259.

This commit is contained in:
Brett Smith 2020-04-05 14:43:54 -04:00
parent 6658696d06
commit bce438167c
2 changed files with 25 additions and 12 deletions

View file

@ -34,14 +34,26 @@ class MetaApproval(core._RequireLinksPostingMetadataHook):
def _run_on_txn(self, txn: Transaction) -> bool: def _run_on_txn(self, txn: Transaction) -> bool:
if not super()._run_on_txn(txn): if not super()._run_on_txn(txn):
return False return False
assets_sum = decimal.Decimal(0) debits_sum = decimal.Decimal(0)
creditcard_sum = decimal.Decimal(0)
for post in data.iter_postings(txn): for post in data.iter_postings(txn):
if post.is_payment(): # approval is required:
assets_sum -= post.units.number or 0 # 1. When a payable is accrued
elif post.account.is_under(self.CREDIT_CARD_ACCT): if (post.account.is_under('Liabilities:Payable:Accounts')
creditcard_sum += post.units.number or 0 and post.is_debit(self.payment_threshold)):
return (assets_sum - creditcard_sum) > 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: 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

View file

@ -24,6 +24,7 @@ REQUIRED_ACCOUNTS = {
'Assets:Bank:Checking', 'Assets:Bank:Checking',
'Assets:Cash', 'Assets:Cash',
'Assets:Savings', 'Assets:Savings',
'Liabilities:Payable:Accounts',
} }
NON_REQUIRED_ACCOUNTS = { NON_REQUIRED_ACCOUNTS = {
@ -32,7 +33,8 @@ NON_REQUIRED_ACCOUNTS = {
'Equity:QpeningBalance', 'Equity:QpeningBalance',
'Expenses:Other', 'Expenses:Other',
'Income:Other', 'Income:Other',
'Liabilities:Payable:Accounts', 'Liabilities:Payable:Vacation',
'Liabilities:UnearnedIncome:Donations',
} }
CREDITCARD_ACCOUNT = 'Liabilities:CreditCard' CREDITCARD_ACCOUNT = 'Liabilities:CreditCard'
@ -148,10 +150,9 @@ def test_approval_not_required_to_charge_credit_card(hook):
]) ])
assert not list(hook.run(txn)) assert not list(hook.run(txn))
@pytest.mark.parametrize('acct', REQUIRED_ACCOUNTS) def test_approval_not_required_to_pay_credit_card(hook):
def test_approval_not_required_to_pay_credit_card(hook, acct):
txn = testutil.Transaction(postings=[ txn = testutil.Transaction(postings=[
(acct, -25), ('Assets:Checking', -25),
(CREDITCARD_ACCOUNT, 25), (CREDITCARD_ACCOUNT, 25),
]) ])
assert not list(hook.run(txn)) assert not list(hook.run(txn))