diff --git a/superx_budget/pyramid/overview.py b/superx_budget/pyramid/overview.py index 51ea0bc..b77a6ed 100644 --- a/superx_budget/pyramid/overview.py +++ b/superx_budget/pyramid/overview.py @@ -26,6 +26,22 @@ bei Fragen bitte an Holgi wenden """ +def _map_from_form_data(request, prefix): + if not prefix.endswith("-"): + prefix = f"{prefix}-" + result = {} + for key, value in request.POST.items(): + if key.startswith(prefix): + row_str = key.split("-")[-1] + row = int(row_str) + try: + value = float(value) + except ValueError: + value = 0 + result[row] = value + return result + + @view_config( context=Root, request_method="GET", @@ -112,22 +128,17 @@ def send_overview(context, request): recipients = tmp_recipients.splitlines() budget_template = request.POST.get("template") budget_file = request.budgets_dir / budget_template - expenses = {} - for key, value in request.POST.items(): - if key.startswith("expense-"): - row_str = key.split("-")[-1] - row = int(row_str) - try: - value = float(value) - except ValueError: - value = 0 - expenses[row] = value + + budgets = _map_from_form_data(request, "budget-") + expenses = _map_from_form_data(request, "expense-") + available = {k: (v - expenses[k]) for k, v in budgets.items()} # sanity check if ( not export_date or not recipients or not expenses + or not available or not budget_file.is_file() ): request.session.flash( @@ -140,6 +151,9 @@ def send_overview(context, request): for row, value in expenses.items(): cell = f"F{row}" sheet[cell] = value + for row, value in available.items(): + cell = f"G{row}" + sheet[cell] = value budget_year = budget_file.stem.split("-")[-1] message = Message( diff --git a/superx_budget/pyramid/templates/overview.jinja2 b/superx_budget/pyramid/templates/overview.jinja2 index b6fef14..a3dc499 100644 --- a/superx_budget/pyramid/templates/overview.jinja2 +++ b/superx_budget/pyramid/templates/overview.jinja2 @@ -31,7 +31,10 @@ {{budget.budget_data.project_name}} {{budget.budget_data.project}} - {{ "{:,.2f}".format(budget.budget_data.budget) }} € + + + {{ "{:,.2f}".format(budget.budget_data.budget) }} € + {% if budget.found %}