conservancy_beancount/conservancy_beancount/plugin/meta_receivable_documentation.py

93 lines
3.5 KiB
Python
Raw Normal View History

"""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'])
SUPPORTING_METADATA = frozenset([
'approval',
'contract',
'purchase-order',
])
METADATA_KEY = '/'.join(sorted(SUPPORTING_METADATA))
# 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
# 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
def post_run(self, txn: Transaction, post: data.Posting) -> errormod.Iter:
errors: Dict[MetaKey, Optional[errormod.InvalidMetadataError]] = {
key: None for key in self.SUPPORTING_METADATA
}
have_support = False
for key in errors:
try:
self._check_links(txn, post, key)
except errormod.InvalidMetadataError as key_error:
errors[key] = key_error
else:
have_support = True
for key, error in errors.items():
if error is not None and error.value is not None:
yield error
if not have_support:
yield errormod.InvalidMetadataError(txn, self.METADATA_KEY, None, post)