Commit Graph

4 Commits

Author SHA1 Message Date
JasonFraser 091928eea0 Order categories by account number, not alphabetically
Within a category, rows were already ascending by account number, but
categories themselves were visited alphabetically - so the overall
account-number sequence down a section jumped around (e.g. 78000,
81000, 72400, 70000, 71060, 72600, 78000 again...) even though each
individual category block was internally ordered. Categories are now
visited in ascending order of their lowest account number (ties break
alphabetically), so the account-number sequence is monotonic across
the whole Revenue/Expenses section, not just within each category.

Verified: extracted the lead account code of every category block in
the Expenses section and confirmed the sequence is non-decreasing
end to end. Same totals as before (this only changes row order, not
values): revenue 5,338,866.02 / expense 3,743,572.26 /
balance_check 0.00.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-08-25 16:32:11 -04:00
JasonFraser d0ebc3f995 Category subtotal headings + live formulas against embedded source sheets
Two changes, confirmed with the user via concrete preview mockups
before building (avoiding a third wrong guess on the subtotal
structure after the account-grouping change):

- Reverted subtotal/heading grouping from Account back to Category
  (bold subtotal row per category, sorted alphabetically; within a
  category, detail rows sorted by account number then strategic area
  then full project code). Account still shows as the full unsplit
  "code:name" string on every detail row - that part of the account
  change stays, only the grouping key reverted.

- The two uploaded trial balance files are now embedded as their own
  sheets ("Revenue TB", "Expense TB") in the generated workbook, and
  every Category Summary/Balance Sheet cell is a live formula against
  them rather than a precomputed number - directly modeled on
  "1. Summary (Manual)"/"Balance Sheet (FS)"'s own SUMIFS/SUMIF
  formulas:
  - Category Summary detail rows: SUMIFS matching Account+Category+
    Project against the embedded sheet (blank category/project use ""
    criteria, mirroring the template's ISBLANK(...) branches).
  - Balance Sheet lines: SUMIF per mapped account code, wildcard-
    matched ("<code>:*") since the sheet stores code+name together.
    Totals (Total Current Assets, Total Assets, etc.) are formulas
    summing the line-item cells above them, not baked-in numbers.
  - Unrestricted (Net Assets) adds a live cross-sheet reference to
    Category Summary's own Total Revenue/Total Expenses cells for the
    period's Net Income, instead of a Python-computed literal.
  All formulas use only SUM/SUMIF/SUMIFS - no Excel-only functions -
  so the workbook is Google Sheets compatible, not just Excel.

Verified far more rigorously than a cross-check against my own Python
computation this time: wrote a SUMIFS/SUMIF simulator that evaluates
every formula against the actual embedded sheet data (not a
side-channel recomputation), recursively resolving subtotals, grand
totals, and the Balance Sheet's cross-sheet reference. Every result
matches exactly: revenue 5,338,866.02 / expense 3,743,572.26 /
surplus 1,595,293.76 / balance_check 0.00.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-08-25 16:23:22 -04:00
JasonFraser 8f1ffe57c6 Category Summary: group/sort by account number, show full account string
Two changes per closer re-reading of the reference template:

- "Account" column now shows the full raw Ledger Account string
  (e.g. "43000:Donation and Fundraising Revenue"), unsplit - same
  treatment as Project Code already gets, rather than parsing out just
  the name.
- Subtotal grouping switches from Category to Account, sorted by
  account number ascending (43000, 48000, then 68010..82000 in the
  Expenses section). String-sorting the full account value works here
  since every code in this chart of accounts is the same 5-digit
  width, so no separate numeric-sort key was needed. Within each
  account, detail rows are still sorted by strategic area (C&B, CTI,
  GCTN, General) then full project code. Category is now a plain
  column rather than a grouping level.

Purely a grouping/display change - re-verified end to end that every
total is unaffected: Category Summary reconciliation still MATCHES
exactly, Balance Sheet balance_check is still 0.00, and every Total
Revenue/Total Expenses/Surplus formula resolves to the same figures
as before (revenue 5,338,866.02 / expense 3,743,572.26 /
surplus 1,595,293.76).

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-08-25 16:09:25 -04:00
JasonFraser c9fd95572a Add Financial Reports tool: Category Summary + Balance Sheet snapshot
New tool at /financial-reports/, same two-file trial balance upload as
the donor report, generating a workbook built fresh with openpyxl
(no bundled Excel template - the sheets are constructed to match
fs.xlsx's own structure/formatting rather than adjusting a copy of it):

- Category Summary: mirrors "1. Summary (Manual)" from the reference
  workbook - Account | Category | Project Code (full raw string,
  unsplit) | Canada YTD | US YTD | Consolidation Adjustments (blank) |
  Total YTD (formula) | Budget (header only, left blank for manual
  formulas). Category-level subtotal rows (SUM formulas), sorted within
  a category by strategic area then full project code. Total Revenue/
  Total Expenses/Surplus as formulas summing the category subtotals.
- Balance Sheet: single-period snapshot matching "Balance Sheet (FS)"'s
  labels/section order/Arial-11 formatting - the source file's
  month-over-month and prior-year columns are static historical
  snapshots that can't be regenerated from one trial balance upload, so
  this is one consolidated (Canada+US) column for the current period.
  Account-code-to-line-item mapping lives in
  balance_sheet_account_map.csv (bind-mounted, same pattern as the
  other tools' lookup tables), seeded from fs.xlsx's own N-S helper
  columns. Net Assets/Unrestricted = Retained Earnings (36000) + CTA
  (36300) + this period's computed Net Income - avoids needing prior-
  period data since 36000 already carries the cumulative prior position.
- Arithmetic check back to the uploaded files: every run independently
  re-sums Planeterra Foundation/Planeterra International Foundation Ltd
  across the filtered revenue+expense rows and compares against the
  Category Summary's own Canada/US totals, surfaced on both the results
  screen and embedded in the workbook. The Balance Sheet's own
  Assets-vs-Liabilities+NetAssets check is exposed the same way.
- UI: multi-file upload (auto-classifies revenue vs. expense by column
  headers), checkboxes for which sections to include, report date,
  report title, and a whole-dollars-vs-cents number format toggle.

Two real bugs found and fixed during testing, not just simplified logic:
- numpy.bool_ (from pandas comparisons) isn't JSON-serializable, unlike
  native Python bool - silently 500'd the endpoint until wrapped.
- compute_balance_sheet() was concatenating both uploaded files before
  summing Balance Sheet accounts - but both files are independently
  complete trial balance exports (same non-P&L rows in both, just
  tagged with a different category column), so this double-counted
  every single Balance Sheet account by exactly 2x. Caught via a
  balance_check that came out as -net_income instead of zero - too
  precise to be a coincidence, traced it back to the concat.

Verified end to end against the real trial balance files: Category
Summary Canada/US reconciliation both MATCH exactly, and the Balance
Sheet balance_check is exactly 0.00 (Assets = Liabilities + Net
Assets). Independently re-resolved every formula (including the
53-term Total Expenses SUM) and confirmed all totals match the API
response: revenue 5,338,866.02 / expense 3,743,572.26 /
surplus 1,595,293.76.

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