From 091928eea06cf80a009593beed042452f07b0d3e Mon Sep 17 00:00:00 2001 From: JasonFraser Date: Tue, 25 Aug 2026 16:32:11 -0400 Subject: [PATCH] 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 --- app/tools/financial_reports/logic.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/app/tools/financial_reports/logic.py b/app/tools/financial_reports/logic.py index 60df7b2..fc66472 100644 --- a/app/tools/financial_reports/logic.py +++ b/app/tools/financial_reports/logic.py @@ -280,7 +280,14 @@ 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()): + # Categories are ordered by their lowest account number (ascending), + # not alphabetically - so the account-number sequence a reader sees + # scrolling down the section is monotonic, not just ascending within + # each category block. Ties (categories sharing the same lowest + # account) fall back to alphabetical category order. + category_order = df.groupby("category")["account"].min().sort_values(kind="stable").index.tolist() + + for category in category_order: cat_df = df[df["category"] == category].sort_values(["account", "slob", "project_code"]) first_row = row + 1 # detail rows start after this category's own subtotal row subtotal_row = row