"""Re-runnable check of output/invoices.json and output/review-note.md against inputs/purchase-orders.csv. Standard library only. Run from the workspace root or from output/. Values in invoices.json were transcribed by eye from the PNGs; this script cannot re-read the images.""" import json, csv, os, hashlib from decimal import Decimal as D root = next(b for b in ('.', '..') if os.path.exists(os.path.join(b, 'inputs', 'purchase-orders.csv'))) P = lambda *a: os.path.join(root, *a) inv = json.load(open(P('output', 'invoices.json'), encoding='utf-8'), parse_float=D, parse_int=D) po = {} for r in csv.DictReader(open(P('inputs', 'purchase-orders.csv'), encoding='utf-8')): po.setdefault(r['po'], []).append(r) KEYS = ['supplier', 'invoice_number', 'invoice_date', 'po', 'currency', 'line_items', 'subtotal', 'tax', 'shipping', 'total', 'bank_account_suffix', 'exceptions'] print('records:', len(inv)) for rec in inv: print('---', rec['invoice_number'], rec['supplier'], rec['po']) print(' keys exact:', list(rec.keys()) == KEYS, '| invoice_number str:', isinstance(rec['invoice_number'], str), '| suffix str:', isinstance(rec['bank_account_suffix'], str)) rows = po[rec['po']] sub = D(0) for li in rec['line_items']: calc = li['qty'] * li['unit_price'] sub += calc p = next((x for x in rows if x['item'] == li['description']), None) print(' line', repr(li['description']), '| amount', li['amount'], 'recalc', calc, calc == li['amount'], '| qty inv', li['qty'], 'PO', p and p['qty'], bool(p) and D(p['qty']) == li['qty'], '| unit inv', li['unit_price'], 'PO', p and p['unit_cad'], bool(p) and D(p['unit_cad']) == li['unit_price']) print(' PO lines not invoiced:', [x['item'] for x in rows if x['item'] not in [l['description'] for l in rec['line_items']]]) tax = (sub * D('0.12')).quantize(D('0.01')) tot = rec['subtotal'] + rec['tax'] + rec['shipping'] print(' subtotal', rec['subtotal'], 'recalc', sub, sub == rec['subtotal']) print(' tax', rec['tax'], 'recalc 12% of merchandise', tax, tax == rec['tax']) print(' total', rec['total'], 'recalc', tot, tot == rec['total'], 'diff', rec['total'] - tot) print(' supplier matches PO:', {x['supplier'] for x in rows} == {rec['supplier']}, '| bank inv', rec['bank_account_suffix'], 'PO', sorted({x['bank_suffix'] for x in rows}), {x['bank_suffix'] for x in rows} == {rec['bank_account_suffix']}) posub = sum(D(x['qty']) * D(x['unit_cad']) for x in rows) potax = (posub * D('0.12')).quantize(D('0.01')) print(' PO-basis: subtotal', posub, 'tax', potax, 'total excl. shipping', posub + potax, '| printed total minus that minus shipping:', rec['total'] - posub - potax - rec['shipping']) print(' exceptions:', [(e['code'], e['severity']) for e in rec['exceptions']]) note = open(P('output', 'review-note.md'), encoding='utf-8').read() print('review-note.md whitespace-delimited tokens (incl. table pipes):', len(note.split())) for f in ('invoices.json', 'review-note.md'): o = P('output', 'original-submission', f) if os.path.exists(o): print('original preserved:', f, hashlib.sha256(open(o, 'rb').read()).hexdigest()[:16])