58 lines
2.4 KiB
Python
58 lines
2.4 KiB
Python
"""meta_receipt - Validate receipt metadata"""
|
|
# 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/>.
|
|
|
|
from . import core
|
|
from .. import config as configmod
|
|
from .. import data
|
|
from .. import errors as errormod
|
|
from ..beancount_types import (
|
|
Transaction,
|
|
)
|
|
|
|
class MetaReceipt(core._RequireLinksPostingMetadataHook):
|
|
METADATA_KEY = 'receipt'
|
|
|
|
def __init__(self, config: configmod.Config) -> None:
|
|
self.payment_threshold = -abs(config.payment_threshold())
|
|
|
|
def _run_on_post(self, txn: Transaction, post: data.Posting) -> bool:
|
|
return bool(
|
|
(post.account.is_real_asset() or post.account.is_under('Liabilities'))
|
|
and post.units.number is not None
|
|
and post.units.number < self.payment_threshold
|
|
)
|
|
|
|
def post_run(self, txn: Transaction, post: data.Posting) -> errormod.Iter:
|
|
try:
|
|
self._check_links(txn, post, 'receipt')
|
|
except errormod.Error as receipt_error:
|
|
# Outgoing check may omit a receipt if they have a check link
|
|
# instead.
|
|
if ':Check' not in post.account:
|
|
yield receipt_error
|
|
else:
|
|
try:
|
|
self._check_links(txn, post, 'check')
|
|
except errormod.Error as check_error:
|
|
if (receipt_error.message.endswith(" missing receipt")
|
|
and check_error.message.endswith(" missing check")):
|
|
check_error.message = check_error.message.replace(
|
|
" missing check", " missing receipt or check",
|
|
)
|
|
yield check_error
|
|
else:
|
|
yield receipt_error
|
|
yield check_error
|