diff --git a/app/tools/financial_reports/logic.py b/app/tools/financial_reports/logic.py index a8b111c..aa456f4 100644 --- a/app/tools/financial_reports/logic.py +++ b/app/tools/financial_reports/logic.py @@ -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})")