From 7c5e98c58814a9311fd29c0d391bf890d5759fa1 Mon Sep 17 00:00:00 2001 From: Ben Sturmfels Date: Wed, 23 Feb 2022 17:34:52 +1100 Subject: [PATCH] reconcile: Simplify multirecord sorting. --- .../reconcile/statement_reconciler.py | 26 +++++++++---------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/conservancy_beancount/reconcile/statement_reconciler.py b/conservancy_beancount/reconcile/statement_reconciler.py index 897c369..9d247f7 100644 --- a/conservancy_beancount/reconcile/statement_reconciler.py +++ b/conservancy_beancount/reconcile/statement_reconciler.py @@ -181,9 +181,9 @@ def format_multirecord(r1s, r2s, note): assert len(r1s) == 1 assert len(r2s) > 1 match_output = [] - match_output.append([r1s[0]['date'].isoformat() + ' ' + r1s[0]['payee'], f'{format_record(r1s[0])} → {format_record(r2s[0])} ✓ Matched{note}']) - for i, r2 in enumerate(r2s[1:]): - match_output.append([r1s[0]['date'].isoformat() + str(i) + r1s[0]['payee'], f'{r1s[0]["date"].isoformat()}: ↳ → {format_record(r2)} ✓ Matched{note}']) + match_output.append([r1s[0]['date'], f'{format_record(r1s[0])} → {format_record(r2s[0])} ✓ Matched{note}']) + for r2 in r2s[1:]: + match_output.append([r1s[0]['date'], f'{r1s[0]["date"].isoformat()}: ↳ → {format_record(r2)} ✓ Matched{note}']) return match_output def sort_records(records: List) -> List: @@ -290,19 +290,19 @@ def match_statement_and_books(statement_trans: list, books_trans: list): def format_matches(matches, csv_statement: str, show_reconciled_matches): match_output = [] - for r1, r2, note in matches: + for r1s, r2s, note in matches: note = ', '.join(note) note = ': ' + note if note else note - if r1 and r2: - if show_reconciled_matches and all(x['bank_statement'] for x in r2): - if len(r2) == 1: - match_output.append([r1[0]['date'].isoformat() + r1[0]['payee'], f'{format_record(r1[0])} → {format_record(r2[0])} ✓ Matched{note}']) + if r1s and r2s: + if show_reconciled_matches and all(x['bank_statement'] for x in r2s): + if len(r2s) == 1: + match_output.append([r1s[0]['date'], f'{format_record(r1s[0])} → {format_record(r2s[0])} ✓ Matched{note}']) else: - match_output.extend(format_multirecord(r1, r2, note)) - elif r1: - match_output.append([r1[0]['date'].isoformat() + r1[0]['payee'], Fore.RED + Style.BRIGHT + f'{format_record(r1[0])} → {" ":^59} ✗ NOT IN BOOKS ({os.path.basename(csv_statement)}:{r1[0]["line"]})' + Style.RESET_ALL]) + match_output.extend(format_multirecord(r1s, r2s, note)) + elif r1s: + match_output.append([r1s[0]['date'], Fore.RED + Style.BRIGHT + f'{format_record(r1s[0])} → {" ":^59} ✗ NOT IN BOOKS ({os.path.basename(csv_statement)}:{r1s[0]["line"]})' + Style.RESET_ALL]) else: - match_output.append([r2[0]['date'].isoformat() + r2[0]['payee'], Fore.RED + Style.BRIGHT + f'{" ":^59} → {format_record(r2[0])} ✗ NOT ON STATEMENT ({os.path.basename(r2[0]["filename"])}:{r2[0]["line"]})' + Style.RESET_ALL]) + match_output.append([r2s[0]['date'], Fore.RED + Style.BRIGHT + f'{" ":^59} → {format_record(r2s[0])} ✗ NOT ON STATEMENT ({os.path.basename(r2s[0]["filename"])}:{r2s[0]["line"]})' + Style.RESET_ALL]) return match_output @@ -507,7 +507,7 @@ def main(args): print('-' * 155) print(f'{"Statement transaction":<52} {"Books transaction":<58} Notes') print('-' * 155) - for _, output in sorted(match_output): + for _, output in sorted(match_output, key=lambda x: x[0]): print(output) print('-' * 155) print(f'Statement period {begin_date} to {end_date}')