- Canonicalize category/project/account text before grouping so
case-only variants (e.g. "OffSite Meetings" vs "Offsite Meetings")
collapse to one row instead of producing two SUMIFS formulas that
double-count each other under Excel/Sheets' case-insensitive text
matching - this was the source of the reported mismatch.
- Add _assert_no_case_collisions as a build-time safety net; the old
Python-only reconciliation check couldn't catch this class of bug
since grouped-vs-source totals are mathematically equal regardless
of labeling. routes.py now returns a clean 400 if it ever fires.
- Reposition 68020 out of the project-level (Account>Category>Project)
section into the collapsed section with 70000+, so only 68010/68030
remain above the Project Costs subtotal, matching the reference
template.
- Rename the two output sheets to "1. Summary (Manual)" and
"Balance Sheet (FS)", updating the cross-sheet Net Income formula
reference to match.
- Explicitly set calcMode="auto" (in addition to openpyxl's default
fullCalcOnLoad) so edits to the TB sheets keep recalculating
dependent formulas after the first open, not just on it.
- Add a "Report vs. Source Files" comparison table to the results UI
showing Total Income/Expenses/Surplus side by side against figures
computed independently from the uploaded files.
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Two changes drawn from a closer re-read of the reference template's
expense section:
- Accounts 70000 and up (Wages, Benefits, Occupancy, Travel, etc.) no
longer break out by Project Code - one row per category, combining
every project it was posted against (2-criteria SUMIFS: Account +
Category, no Project). The three "Project Costs" accounts
(68010/68020/68030) keep the full Account -> Category -> Project
Code breakdown, since those are the accounts genuinely tied to
specific donor projects.
- "Project Costs" (68010+68020+68030's account subtotals) and "Net
Revenues" (Total Revenue - Project Costs) are now inserted
immediately after the 68030 block, matching the reference template.
Neither feeds into the account-subtotal list Total Expenses sums
from, so the three accounts aren't double-counted.
Verified: every pre-existing total is byte-for-byte unchanged (this is
a pure display/grouping change, not a data change) - revenue
5,338,866.02 / expense 3,743,572.26 / surplus 1,595,293.76 /
balance_check 0.00 - and the new Project Costs (2,990,695.99) / Net
Revenues (2,348,170.03) figures were independently resolved via the
SUMIFS/SUM simulator and check out arithmetically
(5,338,866.02 - 2,990,695.99 = 2,348,170.03).
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Final structure, after two prior attempts (Account-only, then
Category-only) didn't match what was actually wanted: Account is now
the outer grouping (bold subtotal row, sorted ascending by account
number - resolving the "account sequence jumps around" issue directly,
since account is the primary sort key rather than something to
reorder categories by), Category is a second bold subtotal level
nested within each account (sorted alphabetically, SUMs its own detail
rows), and Project Code remains the leaf-level sort/display column. A
category spanning more than one account (e.g. "Planeterra Program
Costs - Administration" under both 68010 and 68030) now correctly gets
its own separate subtotal block under each account, rather than being
merged into one flat block.
Verified with the same SUMIFS/SUMIF simulator built for the previous
formula rewrite - resolved every formula against the actual embedded
source-sheet data, including account and category subtotals nesting
correctly. All totals unchanged (this is purely a row layout/grouping
change): 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>
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>
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>
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>
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>