conservancy_beancount/conservancy_beancount/plugin/meta_tax_implication.py
Brett Smith fdd9f2847b plugin: Skip enum value checks with a flag+FIXME.
We've long supported skipping documentation checks by flagging the
transaction. We haven't done the same for enumerated metadata because we
need it less often, and bad values tend to do more damage to reports.

However, occasionally when something very off-process happens, we do need it
as a matter of expediency. So support it.

In order to skip validation of these fields, the plugin requires that the
value start with the string "FIXME". This helps ensure that reports have a
consistent way to detect and warn about unfilled values in flagged
transactions.
2021-01-29 09:38:37 -05:00

69 lines
2 KiB
Python

"""meta_tax_implication - Validate tax-implication 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 typing import (
Iterator,
Optional,
Tuple,
)
from ..beancount_types import (
Transaction,
)
def _make_aliases(s: str, stdname: Optional[str]=None) -> Iterator[Tuple[str, str]]:
if stdname is None:
stdname = s
elif s != stdname:
yield (s, stdname)
yield (s.lower(), stdname)
if s.startswith('1099-'):
yield from _make_aliases(f'1099{s[5:]}', stdname)
elif s.startswith('USA-'):
yield from _make_aliases(f'US-{s[4:]}', stdname)
if s.endswith('-Corporation'):
yield from _make_aliases(f'{s[:-12]}-Corp', stdname)
class MetaTaxImplication(core._NormalizePostingMetadataHook):
_STDNAMES = [
'1099-MISC-Other',
'1099-NEC',
'Bank-Transfer',
'Chargeback',
'Foreign-Corporation',
'Foreign-Grantee',
'Foreign-Individual-Contractor',
'Loan',
'Refund',
'Reimbursement',
'Retirement-Pretax',
'Tax-Payment',
'USA-501c3',
'USA-Corporation',
'USA-Grantee',
'W2',
]
_ALIASES = dict(
alias for value in _STDNAMES for alias in _make_aliases(value)
)
_ALIASES['1099'] = '1099-NEC'
VALUES_ENUM = core.MetadataEnum('tax-implication', _STDNAMES, _ALIASES)
del _STDNAMES, _ALIASES
def __init__(self, config: configmod.Config) -> None:
self.payment_threshold = -config.payment_threshold()
def _run_on_post(self, txn: Transaction, post: data.Posting) -> bool:
return (
post.account.is_cash_equivalent()
and post.units.number < self.payment_threshold
)