
The main impetus of this change is to rename accounts that were outside Beancount's accepted five root accounts, to move them into that structure. This includes: Accrued:*Payable: → Liabilities:Payable:* Accrued:*Receivable: → Assets:Receivable:* UneanedIncome:* → Liabilities:UnearnedIncome:* Note the last change did inspire in a change to our validation rules. We no longer require income-type on unearned income, because it's no longer considered income at all. Once it's earned and converted to an Income account, that has an income-type of course. This did inspire another rename that was not required, but provided more consistency with the other account names above: Assets:Prepaid* → Assets:Prepaid:* Where applicable, I have generally extended tests to make sure one of each of the five account types is tested. (This mostly meant adding an Equity account to the tests.) I also added tests for key parts of the hierarchy, like Assets:Receivable and Liabilities:Payable, where applicable. As part of this change, Account.is_real_asset() got renamed to Account.is_cash_equivalent(), to better self-document its purpose.
92 lines
3.5 KiB
Python
92 lines
3.5 KiB
Python
"""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)
|