Files @ c6dc2d83aca7
Branch filter:

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

Brett Smith
data.Amount: Introduce class and simplify code to use it.

See docstring for full rationale. This greatly reduces the need for other
plugin code to handle the case of `post.units.number is None`, eliminating
the need for entire methods and letting it do plain numeric comparisons.
"""meta_receivable_documentation - Validate receivables have supporting docs"""
# Copyright © 2020  Brett Smith
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <https://www.gnu.org/licenses/>.

import re

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

from typing import (
    Dict,
    Optional,
)

class MetaReceivableDocumentation(core._RequireLinksPostingMetadataHook):
    HOOK_GROUPS = frozenset(['network', 'rt'])
    CHECKED_METADATA = ['approval', 'contract', 'purchase-order']
    # Conservancy invoice filenames have followed two patterns.
    # The pre-RT pattern: `YYYY-MM-DD_Entity_invoice-YYYYMMDDNN??_as-sent.pdf`
    # The RT pattern: `ProjectInvoice-30NNNN??.pdf`
    # This regexp matches both, with a little slack to try to reduce the false
    # negative rate due to minor renames, etc.
    ISSUED_INVOICE_RE = re.compile(
        r'[Ii]nvoice[-_ ]*(?:2[0-9]{9,}|30[0-9]+)[A-Za-z]*[-_ .]',
    )

    def __init__(self, config: configmod.Config) -> None:
        rt_wrapper = config.rt_wrapper()
        # In principle, we could still check for non-RT invoices and enforce
        # checks on them without an RT wrapper. In practice, that would
        # provide so little utility today it's not worth bothering with.
        if rt_wrapper is None:
            raise errormod.ConfigurationError("can't log in to RT")
        self.rt = rt_wrapper

    def _run_on_post(self, txn: Transaction, post: data.Posting) -> bool:
        if not post.account.is_under('Assets:Receivable'):
            return False
        elif post.units.number < 0:
            return False

        # Get the first invoice, or return False if it doesn't exist.
        try:
            invoice_link = post.meta.get_links('invoice')[0]
        except (IndexError, TypeError):
            return False

        # Get the filename, following an RT link if necessary.
        rt_args = self.rt.parse(invoice_link)
        if rt_args is not None:
            ticket_id, attachment_id = rt_args
            invoice_link = self.rt.url(ticket_id, attachment_id) or invoice_link
        return self.ISSUED_INVOICE_RE.search(invoice_link) is not None