Changeset - 1fc9363b26cb
[Not reviewed]
0 4 0
Brett Smith - 4 years ago 2020-03-31 14:07:25
brettcsmith@brettcsmith.org
data: Add is_credit() and is_debit() methods to Posting.

The main motivation for this change is to make sure that higher-level
code deals with the fact that self.units.number can be None, and has
an easy way to do so.

I'm not sure all our code is *currently* doing the right thing for this
case, because I'm not sure it will ever actually come up. It's possible
that earlier Beancount plugins fill in decimal amounts for postings
that are originally loaded with self.units.number=None. I'll have to see
later whether this case comes up in reality, and then deal with it if so.
For now the safest strategy seems to be that most code should operate
when self.units.number is None.
4 files changed with 86 insertions and 25 deletions:
0 comments (0 inline, 0 general)
conservancy_beancount/data.py
Show inline comments
 
"""Enhanced Beancount data structures for Conservancy
 

	
 
The classes in this module are interface-compatible with Beancount's core data
 
structures, and provide additional business logic that we want to use
 
throughout Conservancy tools.
 
"""
 
# 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 collections
 
import decimal
 
import operator
 

	
 
from beancount.core import account as bc_account
 

	
 
from typing import (
 
    cast,
 
    Callable,
 
    Iterable,
 
    Iterator,
 
    MutableMapping,
 
    Optional,
 
    Sequence,
 
    Union,
 
)
 

	
 
from .beancount_types import (
 
    MetaKey,
 
    MetaValue,
 
    Posting as BasePosting,
 
    Transaction,
 
)
 

	
 
DecimalCompat = Union[decimal.Decimal, int]
 

	
 
LINK_METADATA = frozenset([
 
    'approval',
 
    'check',
 
    'contract',
 
    'invoice',
 
    'purchase-order',
 
    'receipt',
...
 
@@ -194,42 +196,62 @@ class PostingMeta(Metadata):
 
            raise KeyError(key)
 
        else:
 
            super().__delitem__(key)
 

	
 

	
 
class Posting(BasePosting):
 
    """Enhanced Posting objects
 

	
 
    This class is a subclass of Beancount's native Posting class where
 
    specific fields are replaced with enhanced versions:
 

	
 
    * The `account` field is an Account object
 
    * The `meta` field is a PostingMeta object
 
    """
 

	
 
    account: Account
 
    # mypy correctly complains that our MutableMapping is not compatible
 
    # with Beancount's meta type declaration of Optional[Dict]. IMO
 
    # Beancount's type declaration is a smidge too specific: I think its type
 
    # declaration should also use MutableMapping, because it would be very
 
    # unusual for code to specifically require a Dict over that.
 
    # If it did, this declaration would pass without issue.
 
    meta: Metadata  # type:ignore[assignment]
 

	
 
    def is_payment(self, threshold: DecimalCompat=0) -> bool:
 
        threshold = cast(decimal.Decimal, threshold)
 
        return (
 
            self.account.is_real_asset()
 
            and self.units.number is not None
 
            and self.units.number < -abs(threshold)
 
        )
 
    def _compare_amount(self,
 
                        op: Callable[[decimal.Decimal], decimal.Decimal],
 
                        threshold: DecimalCompat,
 
                        default: Optional[bool],
 
    ) -> Optional[bool]:
 
        if self.units.number is None:
 
            return default
 
        else:
 
            return op(self.units.number) > threshold
 

	
 
    def is_credit(self,
 
                  threshold: DecimalCompat=0,
 
                  default: Optional[bool]=None,
 
    ) -> Optional[bool]:
 
        return self._compare_amount(operator.pos, threshold, default)
 

	
 
    def is_debit(self,
 
                  threshold: DecimalCompat=0,
 
                  default: Optional[bool]=None,
 
    ) -> Optional[bool]:
 
        return self._compare_amount(operator.neg, threshold, default)
 

	
 
    def is_payment(self,
 
                  threshold: DecimalCompat=0,
 
                  default: Optional[bool]=None,
 
    ) -> Optional[bool]:
 
        return self.account.is_real_asset() and self.is_debit(threshold, default)
 

	
 

	
 
def iter_postings(txn: Transaction) -> Iterator[Posting]:
 
    """Yield an enhanced Posting object for every posting in the transaction"""
 
    for index, source in enumerate(txn.postings):
 
        yield Posting(
 
            Account(source.account),
 
            *source[1:5],
 
            # see rationale above about Posting.meta
 
            PostingMeta(txn, index, source), # type:ignore[arg-type]
 
        )
conservancy_beancount/plugin/meta_approval.py
Show inline comments
...
 
@@ -8,40 +8,40 @@
 
#
 
# 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 decimal
 

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

	
 
class MetaApproval(core._RequireLinksPostingMetadataHook):
 
    METADATA_KEY = 'approval'
 
    CREDIT_CARD_ACCT = 'Liabilities:CreditCard'
 

	
 
    def __init__(self, config: configmod.Config) -> None:
 
        self.payment_threshold = -abs(config.payment_threshold())
 
        self.payment_threshold = config.payment_threshold()
 

	
 
    def _run_on_txn(self, txn: Transaction) -> bool:
 
        if not super()._run_on_txn(txn):
 
            return False
 
        assets_sum = decimal.Decimal(0)
 
        creditcard_sum = decimal.Decimal(0)
 
        for post in data.iter_postings(txn):
 
            if post.is_payment(self.payment_threshold):
 
                assets_sum += post.units.number or 0
 
            if post.is_payment():
 
                assets_sum -= post.units.number or 0
 
            elif post.account.is_under(self.CREDIT_CARD_ACCT):
 
                creditcard_sum += post.units.number or 0
 
        return (assets_sum + creditcard_sum) < 0
 
        return (assets_sum - creditcard_sum) > self.payment_threshold
 

	
 
    def _run_on_post(self, txn: Transaction, post: data.Posting) -> bool:
 
        return post.is_payment()
 
        return post.is_payment(0) is not False
conservancy_beancount/plugin/meta_tax_implication.py
Show inline comments
...
 
@@ -27,25 +27,25 @@ class MetaTaxImplication(core._NormalizePostingMetadataHook):
 
    VALUES_ENUM = core.MetadataEnum('tax-implication', [
 
        '1099',
 
        'Accountant-Advises-No-1099',
 
        'Bank-Transfer',
 
        'Foreign-Corporation',
 
        'Foreign-Individual-Contractor',
 
        'Fraud',
 
        'HSA-Contribution',
 
        'Loan',
 
        'Payroll',
 
        'Refund',
 
        'Reimbursement',
 
        'Retirement-Pretax',
 
        'Tax-Payment',
 
        'USA-501c3',
 
        'USA-Corporation',
 
        'USA-LLC-No-1099',
 
        'W2',
 
    ])
 

	
 
    def __init__(self, config: configmod.Config) -> None:
 
        self.payment_threshold = config.payment_threshold()
 

	
 
    def _run_on_post(self, txn: Transaction, post: data.Posting) -> bool:
 
        return post.is_payment(self.payment_threshold)
 
        return post.is_payment(self.payment_threshold) is not False
tests/test_data_posting.py
Show inline comments
...
 
@@ -18,77 +18,116 @@ import pytest
 

	
 
from . import testutil
 

	
 
from decimal import Decimal
 

	
 
import beancount.core.amount as bc_amount
 

	
 
from conservancy_beancount import data
 

	
 
PAYMENT_ACCOUNTS = {
 
    'Assets:Cash',
 
    'Assets:Checking',
 
}
 

	
 
NON_PAYMENT_ACCOUNTS = {
 
    'Accrued:AccountsReceivable',
 
    'Assets:PrepaidExpenses',
 
    'Assets:PrepaidVacation',
 
    'Expenses:Other',
 
    'Income:Other',
 
    'Liabilities:CreditCard',
 
    'UnearnedIncome:MatchPledges',
 
}
 

	
 
AMOUNTS = [
 
    None,
 
    '-25.50',
 
    0,
 
    '25.75',
 
]
 

	
 
def Posting(account, number,
 
            currency='USD', cost=None, price=None, flag=None,
 
            **meta):
 
    if not meta:
 
        meta = None
 
    if number is not None:
 
        number = Decimal(number)
 
    return data.Posting(
 
        data.Account(account),
 
        bc_amount.Amount(Decimal(number), currency),
 
        bc_amount.Amount(number, currency),
 
        cost,
 
        price,
 
        flag,
 
        meta,
 
    )
 

	
 
def check_all_thresholds(post, threshold, expected):
 
    assert post.is_payment(threshold) is expected
 
    assert post.is_payment(-threshold) is expected
 
    assert post.is_payment(Decimal(threshold)) is expected
 
    assert post.is_payment(Decimal(-threshold)) is expected
 
def check_all_thresholds(expected, method, threshold, *args):
 
    assert method(threshold, *args) is expected
 
    assert method(Decimal(threshold), *args) is expected
 

	
 
@pytest.mark.parametrize('amount', AMOUNTS)
 
def test_is_credit(amount):
 
    expected = None if amount is None else float(amount) > 0
 
    assert Posting('Assets:Cash', amount).is_credit() is expected
 

	
 
def test_is_credit_threshold():
 
    post = Posting('Assets:Cash', 25)
 
    check_all_thresholds(True, post.is_credit, 0)
 
    check_all_thresholds(True, post.is_credit, 20)
 
    check_all_thresholds(False, post.is_credit, 40)
 

	
 
def test_is_credit_default():
 
    post = Posting('Assets:Cash', None)
 
    assert post.is_credit(default=True) is True
 
    assert post.is_credit(default=False) is False
 

	
 
@pytest.mark.parametrize('amount', AMOUNTS)
 
def test_is_debit(amount):
 
    expected = None if amount is None else float(amount) < 0
 
    assert Posting('Assets:Cash', amount).is_debit() is expected
 

	
 
def test_is_debit_threshold():
 
    post = Posting('Assets:Cash', -25)
 
    check_all_thresholds(True, post.is_debit, 0)
 
    check_all_thresholds(True, post.is_debit, 20)
 
    check_all_thresholds(False, post.is_debit, 40)
 

	
 
def test_is_debit_default():
 
    post = Posting('Assets:Cash', None)
 
    assert post.is_debit(default=True) is True
 
    assert post.is_debit(default=False) is False
 

	
 
@pytest.mark.parametrize('acct', PAYMENT_ACCOUNTS)
 
def test_is_payment(acct):
 
    assert Posting(acct, -500).is_payment()
 

	
 
@pytest.mark.parametrize('acct,amount,threshold', testutil.combine_values(
 
    NON_PAYMENT_ACCOUNTS,
 
    range(5, 20, 5),
 
    range(0, 30, 10),
 
))
 
def test_is_not_payment_account(acct, amount, threshold):
 
    post = Posting(acct, -amount)
 
    assert not post.is_payment()
 
    check_all_thresholds(post, threshold, False)
 
    check_all_thresholds(False, post.is_payment, threshold)
 

	
 
@pytest.mark.parametrize('acct', PAYMENT_ACCOUNTS)
 
def test_is_payment_with_threshold(acct):
 
    threshold = len(acct) * 10
 
    post = Posting(acct, -500)
 
    check_all_thresholds(post, threshold, True)
 
    check_all_thresholds(True, post.is_payment, threshold)
 

	
 
@pytest.mark.parametrize('acct', PAYMENT_ACCOUNTS)
 
def test_is_not_payment_by_threshold(acct):
 
    threshold = len(acct) * 10
 
    post = Posting(acct, -9)
 
    check_all_thresholds(post, threshold, False)
 
    check_all_thresholds(False, post.is_payment, threshold)
 

	
 
@pytest.mark.parametrize('acct', PAYMENT_ACCOUNTS)
 
def test_is_not_payment_but_credit(acct):
 
    post = Posting(acct, 9)
 
    assert not post.is_payment()
 
    check_all_thresholds(post, 0, False)
 
    check_all_thresholds(post, 5, False)
 
    check_all_thresholds(post, 10, False)
 
    check_all_thresholds(False, post.is_payment, 0)
 
    check_all_thresholds(False, post.is_payment, 5)
 
    check_all_thresholds(False, post.is_payment, 10)
0 comments (0 inline, 0 general)