meta_approval: Use data.balance_of.

This commit is contained in:
Brett Smith 2020-04-08 14:20:00 -04:00
parent bb84cb5741
commit 8f81530f6d

View file

@ -28,7 +28,7 @@ class MetaApproval(core._RequireLinksPostingMetadataHook):
CHECKED_METADATA = ['approval'] CHECKED_METADATA = ['approval']
def __init__(self, config: configmod.Config) -> None: def __init__(self, config: configmod.Config) -> None:
self.payment_threshold = config.payment_threshold() self.payment_threshold = -config.payment_threshold()
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):
@ -36,14 +36,12 @@ class MetaApproval(core._RequireLinksPostingMetadataHook):
# approval is required when funds leave a cash equivalent asset, # approval is required when funds leave a cash equivalent asset,
# UNLESS that transaction is a transfer to another asset, # UNLESS that transaction is a transfer to another asset,
# or paying off a credit card. # or paying off a credit card.
# debits_sum keeps a running tally of how much is moving in each balance = data.balance_of(
# direction, and we'll return True if it ends up over the payment txn,
# threshold. data.Account.is_cash_equivalent,
debits_sum = decimal.Decimal(0) data.Account.is_credit_card,
for post in data.iter_postings(txn): )
if post.account.is_cash_equivalent() or post.account.is_credit_card(): return balance is None or balance < self.payment_threshold
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.account.is_cash_equivalent() and not post.is_credit(0) return post.account.is_cash_equivalent() and not post.is_credit(0)