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