conservancy_beancount/conservancy_beancount/plugin/meta_approval.py

47 lines
1.5 KiB
Python

"""meta_approval - Validate approval metadata"""
# Copyright © 2020 Brett Smith
# License: AGPLv3-or-later WITH Beancount-Plugin-Additional-Permission-1.0
#
# Full copyright and licensing details can be found at toplevel file
# LICENSE.txt in the repository.
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']
SKIP_FLAGS = '!'
SKIP_TAX_IMPLICATIONS = frozenset([
'Bank-Transfer',
'Chargeback',
])
def __init__(self, config: configmod.Config) -> None:
self.payment_threshold = -config.payment_threshold()
def _run_on_txn(self, txn: Transaction) -> bool:
return (
super()._run_on_txn(txn)
# 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.
and self.payment_threshold > data.balance_of(
txn,
data.Account.is_cash_equivalent,
data.Account.is_credit_card,
).number
)
def _run_on_post(self, txn: Transaction, post: data.Posting) -> bool:
return (
post.account.is_cash_equivalent()
and post.units.number < 0
and str(post.meta.get('tax-implication')).title() not in self.SKIP_TAX_IMPLICATIONS
)