equals-wd-card-expense-proc.../eib-processor_how-it-works.md

5.5 KiB

EIB Processor — How It Works

EIB Processor — a small internal tool built by "Undo Designs" for Planeterra International Foundation. It converts a raw Equals Money card-spend export into Workday-ready Excel output, and can cross-check the result against bank/card statements to flag discrepancies before finance re-keys the numbers.

It's one tool hosted inside a small internal-tools platform — see CLAUDE.md for the platform-level architecture (landing page, tool registry, the Blueprint-per-tool convention). This doc is a deeper, narrative write-up of just the EIB tool itself, which lives at app/tools/eib/ and is served at /eib/.

Stack

  • Backend: Python 3.11 + Flask Blueprint (app/tools/eib/routes.py for request handling, app/tools/eib/logic.py for the actual data-processing/Excel logic — deliberately Flask-free), pandas/openpyxl for data wrangling.
  • Frontend: templates/eib/index.html — a dark navy/cyan-themed single-page UI with drag-and-drop upload zones, no JS framework, plain fetch() calls polling a status endpoint for progress.
  • PDF parsing: a second container running Docling (quay.io/docling-project/docling-serve), called over HTTP to OCR/convert PDF bank statements into markdown.
  • Storage: no database — everything is file-based (/uploads, /outputs), plus two module-level dicts in routes.py (PROCESS_STATUS for progress polling, LAST_RESULT holding the most recently processed output) — both explicitly single-job/single-user shortcuts, not safe for concurrent runs.

Core workflow

  1. Upload a raw Equals Money .xlsx export.
  2. process_raw() classifies each transaction row (Card, Exchange, Cash withdrawal, Fee, Credit, Refund, etc.) using a fairly intricate rules engine that:
    • Drops non-expense rows (credits, refunds, internal FX "From" legs, auto-exchange).
    • Collapses ATM cash-withdrawal + FX-exchange + fee row groups into a single clean "Cash withdrawal" / "Cash withdrawal fee" line.
    • Maps raw merchant description strings to clean names via a hardcoded MERCHANT_LOOKUP dict (~50 entries); anything unmapped halts processing and reports back to the UI for the merchant to be added manually.
  3. Optional audit, two modes:
    • CSV mode (run_audit): upload a combined card-statement CSV; per cardholder, checks sum(EIB Converted Amount) == sum(statement Total debited).
    • PDF mode (run_audit_pdf): upload per-person PDF statements; Docling converts each to markdown, extract_balances_from_markdown() parses the opening/credit/debit/closing balance table, and a 4-layer audit runs (raw-vs-PDF totals → EIB value fidelity → structural "every excluded row is zero-impact" check → statement arithmetic identity), producing a much more detailed reconciliation with human-readable failure explanations.
  4. populate_template() writes EIB_Processed[_Audited]_<date>.xlsx by loading finance's own eib_pre_template.xlsx and writing the processed rows into its Final Sheet (columns A:I), leaving finance's own corporate-card-ID lookup formulas (J:M) and a legacy block (N:Y) untouched, plus the audit table starting at column AA. The template file itself is never modified — every run saves a fresh dated copy. This is the file downloaded from the main "Download EIB File" button.
  5. populate_upload_template() — a second, on-demand output. A "Generate EIB File (for Upload)" button (shown after a successful run) reuses that same run's data to populate planeterra_equals_cards_transactions_eib_v45.xlsx, the actual Workday EIB import template ("Expense Credit Card Transaction" + "Credit Card Transaction Header" sheets). Corporate-card identifiers here are resolved by reading eib_pre_template.xlsx's "Equals Card IDs" sheet directly, so both output files always agree on the same mapping without duplicating it.
  6. Frontend polls /eib/api/process/status for live progress text/log while processing runs, then offers both downloads.

There's also a diagnostic-only /eib/api/debug/probe-pdf endpoint that dumps raw Docling responses to outputs/_debug_markdown/*.PROBE.json for troubleshooting extraction logic, unrelated to the main flow.

The two bundled Excel templates

Both live in app/tools/eib/ and are tracked in git despite the repo's blanket *.xlsx gitignore rule (each has an explicit ! exception) — they're finance-owned reference files, not generated output:

  • eib_pre_template.xlsx — finance's working file. Its Final Sheet has our 9 output columns (A:I), finance's own VLOOKUP formulas resolving corporate-card identifiers per row (J:M, keyed off cardholder name against the Equals Card IDs sheet), and a legacy static block (O:Y) that predates automation and is left alone. Populated by populate_template().
  • planeterra_equals_cards_transactions_eib_v45.xlsx — the actual Workday EIB "Credit Card Transaction Header" + "Expense Credit Card Transaction" import template (fixed schema, Required/Optional/Format rows baked in). Populated by populate_upload_template().

Notable characteristics

  • Heavily domain-specific: the transaction-classification logic encodes many real-world Equals Money quirks (ATM fee/exchange grouping, FX top-up over/under-funding detection, refund matching by description+amount+currency tuples).
  • No auth, no database — designed as a trusted, local/single-user tool.
  • uploads/ currently has dozens of leftover UUID-prefixed files from prior runs (PDFs, CSVs, xlsx) — this is working/scratch data, not part of the app itself.