Changeset - e22e63dcca78
[Not reviewed]
0 3 0
Brett Smith - 4 years ago 2020-06-05 14:54:35
brettcsmith@brettcsmith.org
accrual: Make accruals consistent by entity on the accrual side.

It is more common than I realized that we split an invoice by
entity on the accrual side, so this supports that better.

It still disregards inconsistency between accrual entity and payment entity
for reporting purposes, to help keep reporting clean around automatic
imports.

The changes to BaseReport._report shook out because at this point, the group
key is effectively arbitrary and shouldn't be used for any reporting
purposes.
3 files changed with 81 insertions and 40 deletions:
0 comments (0 inline, 0 general)
conservancy_beancount/reports/accrual.py
Show inline comments
...
 
@@ -248,2 +248,6 @@ class AccrualPostings(core.RelatedPostings):
 
        account_ok = isinstance(self.account, str)
 
        if len(self.accrued_entities) == 1:
 
            entity = next(iter(self.accrued_entities))
 
        else:
 
            entity = None
 
        # `'/' in self.invoice` is just our heuristic to ensure that the
...
 
@@ -252,3 +256,3 @@ class AccrualPostings(core.RelatedPostings):
 
        invoice_ok = isinstance(self.invoice, str) and '/' in self.invoice
 
        if account_ok and invoice_ok:
 
        if account_ok and entity is not None and invoice_ok:
 
            yield (self.invoice, self)
...
 
@@ -257,7 +261,9 @@ class AccrualPostings(core.RelatedPostings):
 
        for post in self:
 
            if invoice_ok:
 
                key = f'{self.invoice} {post.account}'
 
            else:
 
                key = f'{post.account} {post.meta.get("entity")} {post.meta.get("invoice")}'
 
            groups[key].append(post)
 
            post_invoice = self.invoice if invoice_ok else (
 
                post.meta.get('invoice') or 'BlankInvoice'
 
            )
 
            post_entity = entity if entity is not None else (
 
                post.meta.get('entity') or 'BlankEntity'
 
            )
 
            groups[f'{post.account} {post_invoice} {post_entity}'].append(post)
 
        type_self = type(self)
...
 
@@ -316,7 +322,3 @@ class BaseReport:
 

	
 
    def _report(self,
 
                invoice: str,
 
                posts: AccrualPostings,
 
                index: int,
 
    ) -> Iterable[str]:
 
    def _report(self, posts: AccrualPostings, index: int) -> Iterable[str]:
 
        raise NotImplementedError("BaseReport._report")
...
 
@@ -325,3 +327,3 @@ class BaseReport:
 
        for index, invoice in enumerate(groups):
 
            for line in self._report(str(invoice), groups[invoice], index):
 
            for line in self._report(groups[invoice], index):
 
                print(line, file=self.out_file)
...
 
@@ -492,7 +494,3 @@ class AgingReport(BaseReport):
 
class BalanceReport(BaseReport):
 
    def _report(self,
 
                invoice: str,
 
                posts: AccrualPostings,
 
                index: int,
 
    ) -> Iterable[str]:
 
    def _report(self, posts: AccrualPostings, index: int) -> Iterable[str]:
 
        posts = posts.since_last_nonzero()
...
 
@@ -501,3 +499,3 @@ class BalanceReport(BaseReport):
 
            yield ""
 
        yield f"{invoice}:"
 
        yield f"{posts.invoice}:"
 
        yield f"  {posts.balance_at_cost()} outstanding since {date_s}"
...
 
@@ -522,7 +520,3 @@ class OutgoingReport(BaseReport):
 

	
 
    def _report(self,
 
                invoice: str,
 
                posts: AccrualPostings,
 
                index: int,
 
    ) -> Iterable[str]:
 
    def _report(self, posts: AccrualPostings, index: int) -> Iterable[str]:
 
        posts = posts.since_last_nonzero()
...
 
@@ -539,3 +533,3 @@ class OutgoingReport(BaseReport):
 
                "can't generate outgoings report for %s because no RT ticket available: %s",
 
                invoice, errmsg,
 
                posts.invoice, errmsg,
 
            )
setup.py
Show inline comments
...
 
@@ -7,3 +7,3 @@ setup(
 
    description="Plugin, library, and reports for reading Conservancy's books",
 
    version='1.1.2',
 
    version='1.1.3',
 
    author='Software Freedom Conservancy',
tests/test_reports_accrual.py
Show inline comments
...
 
@@ -22,2 +22,3 @@ import itertools
 
import logging
 
import operator
 
import re
...
 
@@ -480,3 +481,6 @@ def test_consistency_check_cost():
 
def test_make_consistent_not_needed():
 
    invoice = 'Invoices/ConsistentDoc.pdf'
 
    main_meta = {
 
        'entity': 'ConsistentTest',
 
        'invoice': 'Invoices/ConsistentDoc.pdf',
 
    }
 
    other_meta = {key: f'{key}.pdf' for key in CONSISTENT_METADATA}
...
 
@@ -485,4 +489,4 @@ def test_make_consistent_not_needed():
 
    txn = testutil.Transaction(postings=[
 
        (ACCOUNTS[0], 20, {**other_meta, 'invoice': invoice}),
 
        (ACCOUNTS[0], 25, {'invoice': invoice}),
 
        (ACCOUNTS[0], 20, {**main_meta, **other_meta}),
 
        (ACCOUNTS[0], 25, {**main_meta}),
 
    ])
...
 
@@ -491,3 +495,3 @@ def test_make_consistent_not_needed():
 
    actual_key, actual_postings = next(consistent)
 
    assert actual_key == invoice
 
    assert actual_key == main_meta['invoice']
 
    assert actual_postings is related
...
 
@@ -502,3 +506,3 @@ def test_make_consistent_bad_invoice(acct_name, invoice, day):
 
    txn = testutil.Transaction(date=datetime.date(2019, 1, day), postings=[
 
        (acct_name, index * 10, {'invoice': invoice})
 
        (acct_name, index * 10, {'invoice': invoice, 'entity': f'BadInvoice{day}'})
 
        for index in range(1, 4)
...
 
@@ -508,3 +512,7 @@ def test_make_consistent_bad_invoice(acct_name, invoice, day):
 
    assert len(consistent) == 1
 
    actual = consistent.get(f'{acct_name} None {invoice}')
 
    key = next(iter(consistent))
 
    assert acct_name in key
 
    if invoice:
 
        assert str(invoice) in key
 
    actual = consistent[key]
 
    assert actual
...
 
@@ -518,3 +526,3 @@ def test_make_consistent_across_accounts():
 
    txn = testutil.Transaction(date=datetime.date(2019, 2, 1), postings=[
 
        (acct_name, 100, {'invoice': invoice})
 
        (acct_name, 100, {'invoice': invoice, 'entity': 'CrossAccount'})
 
        for acct_name in ACCOUNTS
...
 
@@ -524,6 +532,5 @@ def test_make_consistent_across_accounts():
 
    assert len(consistent) == len(ACCOUNTS)
 
    for acct_name in ACCOUNTS:
 
        actual = consistent[f'{invoice} {acct_name}']
 
        assert len(actual) == 1
 
        assert actual[0].account == acct_name
 
    for key, posts in consistent.items():
 
        assert len(posts) == 1
 
        assert posts.account in key
 

	
...
 
@@ -536,6 +543,35 @@ def test_make_consistent_both_invoice_and_account():
 
    assert len(consistent) == len(ACCOUNTS)
 
    for acct_name in ACCOUNTS:
 
        actual = consistent[f'{acct_name} None None']
 
        assert len(actual) == 1
 
        assert actual[0].account == acct_name
 
    for key, posts in consistent.items():
 
        assert len(posts) == 1
 
        assert posts.account in key
 

	
 
@pytest.mark.parametrize('acct_name', ACCOUNTS)
 
def test_make_consistent_across_entity(acct_name):
 
    amt_sign = operator.pos if acct_name.startswith('Assets') else operator.neg
 
    txn = testutil.Transaction(postings=[
 
        (acct_name, amt_sign(n), {'invoice': 'Inv/1.pdf', 'entity': f'Entity{n}'})
 
        for n in range(1, 4)
 
    ])
 
    related = accrual.AccrualPostings(data.Posting.from_txn(txn))
 
    consistent = dict(related.make_consistent())
 
    assert len(consistent) == 3
 
    for key, posts in consistent.items():
 
        assert len(posts) == 1
 
        assert len(posts.accrued_entities) == 1
 
        assert next(posts.entities()) in key
 

	
 
@pytest.mark.parametrize('acct_name', ACCOUNTS)
 
def test_make_consistent_entity_differs_accrual_payment(acct_name):
 
    invoice = 'Invoices/DifferPay.pdf'
 
    txn = testutil.Transaction(postings=[
 
        # Depending on the account, the order of the accrual and payment might
 
        # be swapped here, but that shouldn't matter.
 
        (acct_name, 125, {'invoice': invoice, 'entity': 'Positive'}),
 
        (acct_name, -125, {'invoice': invoice, 'entity': 'Negative'}),
 
    ])
 
    related = accrual.AccrualPostings(data.Posting.from_txn(txn))
 
    consistent = related.make_consistent()
 
    _, actual = next(consistent)
 
    assert actual is related
 
    assert next(consistent, None) is None
 

	
...
 
@@ -661,2 +697,13 @@ def test_aging_report_date_cutoffs(accrual_postings, date, recv_end, pay_end):
 

	
 
def test_aging_report_entity_consistency(accrual_postings):
 
    output = run_aging_report((
 
        post for post in accrual_postings
 
        if post.meta.get('rt-id') == 'rt:480'
 
        and post.units.number < 0
 
    ))
 
    check_aging_ods(output, None, [], [
 
        AgingRow.make_simple('2010-04-15', 'MultiPartyA', 125, 'rt:480/4800'),
 
        AgingRow.make_simple('2010-04-15', 'MultiPartyB', 125, 'rt:480/4800'),
 
    ])
 

	
 
def run_main(arglist, config=None):
0 comments (0 inline, 0 general)