conservancy_beancount/conservancy_beancount/plugin/meta_tax_reporting.py
2021-02-11 13:38:11 -05:00

54 lines
2.1 KiB
Python

"""meta_tax_reporting - Validate tax-reporting metadata links"""
# Copyright © 2021 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 datetime
from . import core
from .. import config as configmod
from .. import data
from .. import errors as errormod
from ..beancount_types import (
Transaction,
)
from .meta_tax_implication import MetaTaxImplication
from ..ranges import DateRange
class MetaTaxReporting(core._RequireLinksPostingMetadataHook):
CHECKED_IMPLICATIONS = frozenset(
# We load values through the MetadataEnum to future-proof against
# changes to tax-implication. This ensures that the set contains
# canonical values, or else this code will crash if canonical values
# can't be found.
MetaTaxImplication.VALUES_ENUM[value] for value in [
'1099-MISC-Other',
'1099-NEC',
'Foreign-Grantee',
'Foreign-Individual-Contractor',
'USA-501c3',
'USA-Grantee',
])
CHECKED_METADATA = ['tax-reporting']
SKIP_FLAGS = '!'
TXN_DATE_RANGE = DateRange(datetime.date(2020, 3, 1), datetime.date.max)
def __init__(self, config: configmod.Config) -> None:
self._implication_hook = MetaTaxImplication(config)
# Yes, we create our own MetaTaxImplication hook. This is a little
# weird but it does two things for us:
# 1. We can check MetaTaxImplication._run_on_post() as part of our own
# implementation without duplicating the logic.
# 2. We can canonicalize values through the hook. We don't strictly
# need an instance for that, but we have it anyway so doing it this way
# is nicer.
def _run_on_post(self, txn: Transaction, post: data.Posting) -> bool:
if not self._implication_hook._run_on_post(txn, post):
return False
implication = str(post.meta.get('tax-implication') or '')
normalized = self._implication_hook.VALUES_ENUM.get(implication)
return normalized in self.CHECKED_IMPLICATIONS