Changeset - 8f81530f6d97
[Not reviewed]
0 1 0
Brett Smith - 4 years ago 2020-04-08 18:20:00
brettcsmith@brettcsmith.org
meta_approval: Use data.balance_of.
1 file changed with 7 insertions and 9 deletions:
0 comments (0 inline, 0 general)
conservancy_beancount/plugin/meta_approval.py
Show inline comments
...
 
@@ -19,31 +19,29 @@ import decimal
 
from . import core
 
from .. import config as configmod
 
from .. import data
 
from .. import errors as errormod
 
from ..beancount_types import (
 
    Transaction,
 
)
 

	
 
class MetaApproval(core._RequireLinksPostingMetadataHook):
 
    CHECKED_METADATA = ['approval']
 

	
 
    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:
 
        if not super()._run_on_txn(txn):
 
            return False
 
        # approval is required when funds leave a cash equivalent asset,
 
        # UNLESS that transaction is a transfer to another asset,
 
        # or paying off a credit card.
 
        # 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.
 
        debits_sum = decimal.Decimal(0)
 
        for post in data.iter_postings(txn):
 
            if post.account.is_cash_equivalent() or post.account.is_credit_card():
 
                debits_sum -= post.units.number or 0
 
        return debits_sum > self.payment_threshold
 
        balance = data.balance_of(
 
            txn,
 
            data.Account.is_cash_equivalent,
 
            data.Account.is_credit_card,
 
        )
 
        return balance is None or balance < self.payment_threshold
 

	
 
    def _run_on_post(self, txn: Transaction, post: data.Posting) -> bool:
 
        return post.account.is_cash_equivalent() and not post.is_credit(0)
0 comments (0 inline, 0 general)