Commit Graph

7 Commits

Author SHA1 Message Date
JasonFraser 6d0e07ae78 Lift GL Dump helpers into app/common.py; add GL Analyzer, PDF Unlock, Summary Reconciliation Check, and Donor Report (GL Only) tools
Shared GL Dump loading/netting logic (find_gl_header_row, load_gl_dump,
reversed-by/FIFO/wash netting passes) now lives in app/common.py, used
by Donor Report's new GL-detail mode plus three new tools: Donor
Report (GL Only), GL Analyzer, and Summary Reconciliation Check.
PDF Unlock (via pikepdf) is unrelated but added alongside.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01HToTtpxzoxeugsa1Ve61gn
2026-10-01 20:00:00 -04:00
JasonFraser c32ab5984e Include the Planeterra International Foundation Ltd entity column
Amounts were only pulling from "Planeterra Foundation" (column D);
"Planeterra International Foundation Ltd" (column E) is now added in
too via a shared _entity_total() helper, used both for the per-row
amounts and the full-file balance integrity check. Eliminations
(column F) stays excluded - it's zero on every row in both sample
files, so D+E already equals the trial balance's own precomputed
"Total" (D+E+F) column; nothing currently depends on including it.

Verified against the real files: revenue moves from 4,384,170.35 to
5,338,866.02 (exactly sum(D)+sum(E) on the filtered rows) and expense
from 3,611,064.64 to 3,743,572.26, matching hand-calculated sums
before touching the report generator. Re-ran the full formula
verification (every Total Revenue/Expenses/Surplus cell across all 12
donor sheets plus the Summary sheet's cross-sheet references) with no
mismatches.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-08-25 13:57:01 -04:00
JasonFraser 65062f6f36 Add a Summary sheet listing Revenue/Expenses/Surplus by donor
New "Summary" sheet inserted at the front of the workbook: one row per
donor with Revenue, Expenses, and Surplus/(Deficit), each a live
formula referencing that donor's own Total Revenue/Total Expenses
cell (e.g. ='G Adventures'!B16) rather than a precomputed number, so
it can't drift out of sync if a donor sheet is edited later. A Total
row at the bottom sums all donors - this is the same figure the web
UI's result screen reports.

Verified by recursively resolving every formula (including the
cross-sheet references) independently of Excel: every donor row and
the grand total match the API's reported totals exactly
(revenue 4,384,170.35 / expense 3,611,064.64 / surplus 773,105.71).

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-08-25 13:42:56 -04:00
JasonFraser 7cbd8ee7eb Drop subtotals, group revenue/expenses as a flat list by project code
Replaces the Account -> SLOB -> Category -> Subtotal hierarchy with a
single flat list per section, sorted by project code then category,
with no header or subtotal rows breaking it up - aimed at non-finance
readers who want to scan the numbers quickly, not a full accounting
breakdown (subtotals can be added manually for deeper analysis).

- Grouping is now (donor, project code, category) instead of (donor,
  account, project code, category) - dropping the Ledger Account
  dimension merges rows that were only "duplicated" because the same
  project code/category combination happened to post to two different
  GL accounts (e.g. the same spend category under two expense
  accounts), which would otherwise show as two near-identical rows.
- Total Revenue/Total Expenses are now a single SUM over each
  section's contiguous row range rather than a sum of subtotal cells.
- Fixed a bug introduced while removing the account-header rows: the
  template's own placeholder text ("Donations & Fundraising Revenue",
  "Disbursement to Projects") was bleeding through on each section's
  first row, because ws.cell(value=None) is a no-op in openpyxl rather
  than a clear - needed a direct .value = None assignment instead.

Re-verified: no stray placeholder text on any of the 12 donor sheets,
every Total Revenue/Total Expenses/Surplus formula independently
re-evaluated and matches the same grand totals as before this change
(revenue 4,384,170.35 / expense 3,611,064.64 / surplus 773,105.71).

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-08-25 13:17:47 -04:00
JasonFraser 87ca8d585f Simplify donor sheet detail rows, drop redundant text in column A
Project code and category were already their own structured columns
(C, D), so repeating them as text in column A (a "  C&B" header row,
then "    Corporate Supporters" per category) was redundant. Detail
rows now leave column A blank; each project code's rows are followed
directly by its "<Project Code> Subtotal" row, one row shorter per
project code than before. Re-verified every formula in a full
regenerated report still resolves correctly and matches the same
grand totals as before this change.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-08-25 13:09:21 -04:00
JasonFraser 8d2da18bfb Add expense trial balance support to Donor Revenue Report
Multi-file upload (auto-classifies each uploaded file as revenue or
expense by column headers - "Revenue Category" vs "Spend Category" -
so the order doesn't matter), with a clear prompt if only one file is
uploaded or a role is duplicated/unrecognized.

- logic.py: process_revenue()/process_expense() share one
  _process_side() implementation (revenue flips sign, expense doesn't -
  expense accounts are already debit-normal). EXPENSE_ACCOUNT_CODES
  covers all 19 P&L expense accounts (68010-82000), not just the
  "Disbursements to Projects" ones - real per-donor Project attribution
  turned out to be present across nearly all of them (salaries, travel,
  currency gain/loss, etc.), confirmed against the actual sample data.
  full_file_balance() checks each uploaded file's raw, unfiltered total
  nets to ~zero - a basic trial-balance integrity check independent of
  the revenue/expense filtering - and blocks processing if not.
- Donor sheets now write both Revenue and Expenses sections (same
  Account -> SLOB -> Category -> Subtotal hierarchy for both), and
  every subtotal/total/surplus cell is a real Excel SUM/subtraction
  formula rather than a precomputed value, so the numbers are
  auditable directly in the workbook.
- Fixed a latent bug found while adding the Expenses section: the
  previous row-insert logic could leave a stale duplicate "Total
  Revenue" label when a donor had fewer lines than the template
  reserves. _adjust_block() now inserts OR deletes rows symmetrically
  so the template's own total labels always land exactly where
  expected - no more relying on need >= the template's reserved slack.
- routes.py: /api/process now accepts trial_balance_file_0.. (multiple),
  classifies each by its columns, and returns total_revenue,
  total_expense, and surplus_deficit alongside the existing donor count.
- templates/donor_report/index.html: multi-file drop zone (mirroring
  the EIB tool's PDF upload pattern) and two more stat boxes (Total
  Expense, Surplus/(Deficit), red/green colored).
- Added "Easyjet" (lowercase) to the donor merge map - a variant not
  present in the revenue file, found only once the expense file was
  processed.

Verified end to end: every SLOB subtotal, Total Revenue, Total
Expenses, and Surplus/(Deficit) formula in the generated workbook was
independently re-evaluated (recursively resolving formula-to-formula
references) and cross-checked against the API's reported totals -
exact match on all 12 donor sheets and the grand total
(revenue 4,384,170.35 / expense 3,611,064.64 / surplus 773,105.71).
Also verified the zero-revenue and zero-expense edge cases produce no
duplicate total labels.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-08-25 13:00:11 -04:00
JasonFraser a9f4b5a597 Add Donor Revenue Report tool
New Blueprint at app/tools/donor_report/ (registered on the platform
home page), following the eib tool's conventions:

- logic.py: parse_project() splits "<Company> - <SLOB> - <Donor...>"
  with the join-back fix for hyphenated donor names; donor merge map is
  a bind-mounted CSV (donor_merge_map.csv, same pattern as
  merchant_lookup.csv) covering the confirmed G Adventures/EasyJet/
  TruTravels/IGF spelling variants. Per stakeholder direction, any row
  with no real donor segment (blank Project, or a 2-part Project with
  no donor at the end) and any row whose donor segment is literally
  "General" both collapse into one unified "General" report.
- Aggregation groups to (donor, account, slob, category) and sums -
  since this is a trial balance, groups that net to exactly zero are
  real (reclassifications, offsetting entries) rather than bugs, so
  they're simply not displayed rather than needing special-case
  duplicate detection. Verified the grand total across all donors
  matches summing the raw filtered rows directly.
- build_donor_workbook() copies report_summary_template.xlsx per donor,
  expanding the template's fixed 5-row detail area via insert_rows() to
  fit each donor's actual row count (1-30+), writing Category -> SLOB
  subtotal -> Account header -> Total Revenue, with the Expenses
  section shifted down accordingly and left unpopulated per the current
  scope (revenue only).
- routes.py: /donor-report/api/process (single synchronous call, no
  progress polling needed - unlike the eib tool there's no slow
  external step) and /api/download/<filename>.
- templates/donor_report/index.html: single-upload page in the same
  theme as the platform shell.

Verified end to end against the real sample trial balance: 10 donor
sheets generated, all merge-map variants resolved correctly, no empty
sheets for accounts that fully net to zero, and the total revenue
figure matches an independent manual calculation from the raw file
(4,384,170.35).

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-08-25 12:36:11 -04:00