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>
This commit is contained in:
JasonFraser 2026-08-25 16:09:25 -04:00
parent c9fd95572a
commit 8f1ffe57c6
1 changed files with 18 additions and 14 deletions

View File

@ -84,7 +84,11 @@ def process_category_summary(revenue_df, expense_df):
"""
Build the Category Summary rows: one per (account, category, project
code), Canada/US kept as separate columns (not combined, unlike the
donor report), grouped/subtotaled by category. Returns a dict with
donor report). "account" is the full raw "Ledger Account" string
(code:name together, unsplit) - same treatment as Project Code - and
is the primary grouping/subtotal key, sorted by account number
(string-sorting the full value works here since every code in this
chart of accounts is the same 5-digit width). Returns a dict with
'revenue' and 'expense' DataFrames plus the independently-computed
source totals used for the reconciliation check.
"""
@ -97,9 +101,7 @@ def process_category_summary(revenue_df, expense_df):
d = df.copy()
d = d[d["Ledger Account"].astype(str).str.strip().str.startswith(codes)]
acct_split = d["Ledger Account"].astype(str).str.split(":", n=1)
d["account_code"] = acct_split.str[0].str.strip()
d["account_name"] = acct_split.str[1].str.strip()
d["account"] = d["Ledger Account"].astype(str).str.strip()
d["category"] = d[cat_col].fillna("(Uncategorized)")
d["project_code"] = d["Project"].fillna("(No project)")
d["slob"] = d["Project"].apply(extract_slob)
@ -117,7 +119,7 @@ def process_category_summary(revenue_df, expense_df):
source_totals[f"{key}_us"] = round(us.sum(), 2)
grouped = (
d.groupby(["account_code", "account_name", "category", "slob", "project_code"], as_index=False)
d.groupby(["account", "category", "slob", "project_code"], as_index=False)
[["canada", "us"]].sum()
)
grouped = grouped[(grouped["canada"].abs() >= 0.005) | (grouped["us"].abs() >= 0.005)]
@ -192,9 +194,11 @@ def _write_cell(ws, row, col, value, bold=False, number_format=None, italic=Fals
def _write_category_section(ws, start_row, df, canada_col, us_col, total_col, number_format):
"""
Write one section (Revenue or Expenses): a bold subtotal row per
category (SUM formula over that category's own detail rows), then
account (SUM formula over that account's own detail rows), sorted by
account number - string-sorting the full "code:name" value works
since every code in this chart of accounts is the same width - then
its detail rows sorted by strategic area then full project code.
Returns (list of category-subtotal row numbers, next free row,
Returns (list of account-subtotal row numbers, next free row,
section Canada/US raw totals).
"""
row = start_row
@ -204,17 +208,17 @@ def _write_category_section(ws, start_row, df, canada_col, us_col, total_col, nu
if df.empty:
return subtotal_rows, row, 0.0, 0.0
for category in sorted(df["category"].unique()):
cat_df = df[df["category"] == category].sort_values(["slob", "project_code"])
first_row = row + 1 # detail rows start after this category's own subtotal row
for account in sorted(df["account"].unique()):
acct_df = df[df["account"] == account].sort_values(["slob", "project_code"])
first_row = row + 1 # detail rows start after this account's own subtotal row
subtotal_row = row
row += 1
for _, r in cat_df.iterrows():
for _, r in acct_df.iterrows():
canada = round(r["canada"], 2)
us = round(r["us"], 2)
_write_cell(ws, row, 1, r["account_name"])
_write_cell(ws, row, 2, category)
_write_cell(ws, row, 1, account)
_write_cell(ws, row, 2, r["category"])
_write_cell(ws, row, 3, r["project_code"])
_write_cell(ws, row, canada_col, canada, number_format=number_format)
_write_cell(ws, row, us_col, us, number_format=number_format)
@ -226,7 +230,7 @@ def _write_category_section(ws, start_row, df, canada_col, us_col, total_col, nu
row += 1
last_row = row - 1
_write_cell(ws, subtotal_row, 2, category, bold=True)
_write_cell(ws, subtotal_row, 1, account, bold=True)
for col in (canada_col, us_col, total_col):
letter = ws.cell(row=first_row, column=col).column_letter
c = ws.cell(row=subtotal_row, column=col, value=f"=SUM({letter}{first_row}:{letter}{last_row})")