diff --git a/app/tools/donor_report/logic.py b/app/tools/donor_report/logic.py index 3dab563..e8c8303 100644 --- a/app/tools/donor_report/logic.py +++ b/app/tools/donor_report/logic.py @@ -248,14 +248,69 @@ def _write_donor_sheet(wb, template_ws, donor, revenue_df, expense_df): c.font = REGULAR_FONT c.number_format = AMOUNT_FORMAT - return revenue_total, expense_total + return { + "sheet_title": ws.title, + "total_revenue_row": total_revenue_row, + "total_expenses_row": total_expenses_row, + "revenue_total": revenue_total, + "expense_total": expense_total, + } + + +def _write_summary_sheet(wb, donor_infos): + """ + Insert a "Summary" sheet at the front of the workbook: one row per + donor with Revenue/Expenses/Surplus, each a live formula referencing + that donor's own Total Revenue/Total Expenses cell (so it can never + drift out of sync with the per-donor sheets), plus a grand-total row + that's the same figure shown in the web UI's result screen. + """ + ws = wb.create_sheet("Summary", 0) + + ws.column_dimensions["A"].width = 28.5 + for col in "BCD": + ws.column_dimensions[col].width = 22 + + _write_cell(ws, 1, 1, "Donor Revenue Report — Summary", bold=True) + _write_cell(ws, 2, 1, datetime.today().strftime("%Y-%m-%d"), bold=False) + + header_row = 4 + for col, label in enumerate(["Donor", "Revenue", "Expenses", "Surplus/(Deficit)"], start=1): + _write_cell(ws, header_row, col, label, bold=True) + + row = header_row + 1 + first_data_row = row + for info in donor_infos: + ref = f"'{info['sheet_title']}'!B{{}}" + _write_cell(ws, row, 1, info["sheet_title"], bold=False) + c = ws.cell(row=row, column=2, value=f"={ref.format(info['total_revenue_row'])}") + c.font = REGULAR_FONT + c.number_format = AMOUNT_FORMAT + c = ws.cell(row=row, column=3, value=f"={ref.format(info['total_expenses_row'])}") + c.font = REGULAR_FONT + c.number_format = AMOUNT_FORMAT + c = ws.cell(row=row, column=4, value=f"=B{row}-C{row}") + c.font = REGULAR_FONT + c.number_format = AMOUNT_FORMAT + row += 1 + last_data_row = row - 1 + + total_row = row + 1 + _write_cell(ws, total_row, 1, "Total", bold=True) + for col in (2, 3, 4): + letter = "BCD"[col - 2] + c = ws.cell(row=total_row, column=col, value=f"=SUM({letter}{first_data_row}:{letter}{last_data_row})") + c.font = BOLD_FONT + c.number_format = AMOUNT_FORMAT def build_donor_workbook(revenue_grouped, expense_grouped, output_path, template_path=TEMPLATE_PATH): """ Write one sheet per donor (the union of donors appearing in either grouped DataFrame) into a copy of report_summary_template.xlsx, - sorted alphabetically. Returns (donor_count, total_revenue, total_expense). + sorted alphabetically, plus a "Summary" sheet at the front totaling + each donor's Revenue/Expenses/Surplus. Returns (donor_count, + total_revenue, total_expense). """ wb = openpyxl.load_workbook(template_path) template_ws = wb[TEMPLATE_SHEET] @@ -263,17 +318,18 @@ def build_donor_workbook(revenue_grouped, expense_grouped, output_path, template donors = sorted(set(revenue_grouped["donor"]).union(expense_grouped["donor"])) total_revenue = 0.0 total_expense = 0.0 - written = 0 + donor_infos = [] for donor in donors: rev_df = revenue_grouped[revenue_grouped["donor"] == donor] exp_df = expense_grouped[expense_grouped["donor"] == donor] if rev_df.empty and exp_df.empty: continue - r_total, e_total = _write_donor_sheet(wb, template_ws, donor, rev_df, exp_df) - total_revenue += r_total - total_expense += e_total - written += 1 + info = _write_donor_sheet(wb, template_ws, donor, rev_df, exp_df) + total_revenue += info["revenue_total"] + total_expense += info["expense_total"] + donor_infos.append(info) + _write_summary_sheet(wb, donor_infos) del wb[TEMPLATE_SHEET] wb.save(output_path) - return written, round(total_revenue, 2), round(total_expense, 2) + return len(donor_infos), round(total_revenue, 2), round(total_expense, 2)