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

3.6 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 a Workday EIB (Expense Import Batch) Excel file, and can cross-check the result against bank/card statements to flag discrepancies before finance re-keys the numbers.

Stack & architecture

  • Backend: Python 3.11 + Flask (app/main.py, single ~1000-line file), pandas/openpyxl for data wrangling and Excel generation.
  • Frontend: one static page (templates/index.html, ~800 lines) — 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.
  • Deployment: docker-compose.yml runs two services — docling (port 5001, healthchecked) and app (Flask, port 8080→8081 on host), the app depends on Docling being healthy first. Uploads/outputs are bind-mounted host volumes.
  • Storage: no database — everything is stateless, file-based (/uploads, /outputs), single-job global status dict (explicitly documented as a "single-user tool" assumption).

Core workflow (app/main.py)

  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. build_excel() writes the final EIB_Processed_<date>.xlsx, with the audit table appended 4 columns to the right of the EIB columns, color-coded green (MATCHED) / red (REVIEW REQUIRED).
  5. Frontend polls /api/process/status for live progress text/log while processing runs, then offers a download link.

There's also a diagnostic-only /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.

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.