Changeset - def05b80072a
[Not reviewed]
0 1 0
Brett Smith - 4 years ago 2020-05-17 20:39:22
brettcsmith@brettcsmith.org
accrual: More robust payment-to fallback in outgoing report.
1 file changed with 1 insertions and 1 deletions:
0 comments (0 inline, 0 general)
conservancy_beancount/reports/accrual.py
Show inline comments
...
 
@@ -193,97 +193,97 @@ def outgoing_report(groups: PostGroups,
 
                    rt_client: Optional[rt.Rt]=None,
 
                    rt_wrapper: Optional[rtutil.RT]=None,
 
) -> None:
 
    if rt_client is None or rt_wrapper is None:
 
        raise ValueError("RT client is required but not configured")
 
    for invoice, related in groups.items():
 
        related = _since_last_nonzero(related)
 
        try:
 
            ticket_id, _ = _primary_rt_id(related)
 
            ticket = rt_client.get_ticket(ticket_id)
 
            # Note we only use this when ticket is None.
 
            errmsg = f"ticket {ticket_id} not found"
 
        except (ValueError, rt.RtError) as error:
 
            ticket = None
 
            errmsg = error.args[0]
 
        if ticket is None:
 
            print("error: can't generate outgoings report for {}"
 
                  " because no RT ticket available: {}".format(
 
                      invoice, errmsg,
 
                  ), file=err_file)
 
            continue
 

	
 
        try:
 
            rt_requestor = rt_client.get_user(ticket['Requestors'][0])
 
        except (IndexError, rt.RtError):
 
            rt_requestor = None
 
        if rt_requestor is None:
 
            requestor = ''
 
            requestor_name = ''
 
        else:
 
            requestor = '{RealName} <{EmailAddress}>'.format_map(rt_requestor)
 
            requestor_name = rt_requestor['RealName']
 

	
 
        contract_links = related.all_meta_links('contract')
 
        if contract_links:
 
            contract_s = ' , '.join(rt_wrapper.iter_urls(
 
                contract_links, '<{}>', '{}', '<BROKEN RT LINK: {}>',
 
            ))
 
        else:
 
            contract_s = "NO CONTRACT GOVERNS THIS TRANSACTION"
 
        projects = [v for v in related.meta_values('project')
 
                    if isinstance(v, str)]
 

	
 
        print(
 
            "PAYMENT FOR APPROVAL:",
 
            f"REQUESTOR: {requestor}",
 
            f"TOTAL TO PAY: {-related.balance()}",
 
            f"AGREEMENT: {contract_s}",
 
            f"PAYMENT TO: {ticket.get('CF.{payment-to}', requestor_name)}",
 
            f"PAYMENT TO: {ticket.get('CF.{payment-to}') or requestor_name}",
 
            f"PAYMENT METHOD: {ticket.get('CF.{payment-method}', '')}",
 
            f"PROJECT: {', '.join(projects)}",
 
            "\nBEANCOUNT ENTRIES:",
 
            sep='\n', file=out_file,
 
        )
 

	
 
        last_txn: Optional[Transaction] = None
 
        for post in related:
 
            txn = post.meta.txn
 
            if txn is not last_txn:
 
                last_txn = txn
 
                txn = rt_wrapper.txn_with_urls(txn)
 
                bc_printer.print_entry(txn, file=out_file)
 

	
 
def filter_search(postings: Iterable[data.Posting],
 
                  search_terms: Iterable[SearchTerm],
 
) -> Iterable[data.Posting]:
 
    postings = (post for post in postings if post.account.is_under(
 
        'Assets:Receivable', 'Liabilities:Payable',
 
    ))
 
    for meta_key, pattern in search_terms:
 
        postings = filters.filter_meta_match(postings, meta_key, re.compile(pattern))
 
    return postings
 

	
 
def parse_arguments(arglist: Optional[Sequence[str]]=None) -> argparse.Namespace:
 
    parser = argparse.ArgumentParser()
 
    parser.add_argument(
 
        '--report-type', '-t',
 
        metavar='NAME',
 
        type=ReportType.by_name,
 
        help="""The type of report to generate, either `balance` or `outgoing`.
 
If not specified, the default is `outgoing` for search criteria that return a
 
single outstanding payable, and `balance` any other time.
 
""")
 
    parser.add_argument(
 
        '--since',
 
        metavar='YEAR',
 
        type=int,
 
        default=-1,
 
        help="""How far back to search the books for related transactions.
 
You can either specify a fiscal year, or a negative offset from the current
 
fiscal year, to start loading entries from. The default is -1 (start from the
 
previous fiscal year).
 
""")
 
    parser.add_argument(
 
        'search',
 
        nargs=argparse.ZERO_OR_MORE,
 
        help="""Report on accruals that match this criteria. The format is
0 comments (0 inline, 0 general)