Files @ 6deaacb11bdd
Branch filter:

Location: NPO-Accounting/conservancy_beancount/conservancy_beancount/plugin/meta_paypal_id.py

bkuhn
Add US:TN:Unemployment as a valid `payroll-type` metadata for taxes
"""meta_paypal_id - Validate paypal-id 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 re

from . import core
from .. import data
from .. import errors as errormod
from ..beancount_types import (
    Transaction,
)

class MetaPayPalID(core._PostingHook):
    METADATA_KEY = 'paypal-id'
    HOOK_GROUPS = frozenset(['metadata', METADATA_KEY])
    TXN_ID_RE = re.compile(r'^[A-Z0-9]{17}$')
    INVOICE_ID_RE = re.compile(r'^INV2(?:-[A-Z0-9]{4}){4}$')

    def _run_on_post(self, txn: Transaction, post: data.Posting) -> bool:
        if post.account.is_under('Assets:PayPal'):
            return True
        elif post.account.is_under('Assets:Receivable'):
            return self.METADATA_KEY in post.meta
        else:
            return False

    def post_run(self, txn: Transaction, post: data.Posting) -> errormod.Iter:
        if post.account.is_under('Assets:Receivable'):
            regexp = self.INVOICE_ID_RE
        else:
            regexp = self.TXN_ID_RE
        value = post.meta.get(self.METADATA_KEY)
        try:
            # A bad argument type is okay because we catch the TypeError.
            match = regexp.match(value)  # type:ignore[arg-type]
        except TypeError:
            match = None
        if match is None and not self._is_flagged_fixme(post, value):
            yield errormod.InvalidMetadataError(txn, self.METADATA_KEY, value, post)