From 07430b8bceff5327cda3518a1528cf71e351ee74 Mon Sep 17 00:00:00 2001 From: Christoph Marzell Date: Thu, 13 Nov 2025 11:08:03 +0100 Subject: [PATCH] add routes for month report --- app/controllers/entries_controller.rb | 31 +++++++++++++++++++++++ app/views/entries/index.html.erb | 2 +- app/views/entries/monthly_report.html.erb | 29 +++++++++++++++++++++ app/views/layouts/application.html.erb | 5 +++- config/routes.rb | 2 +- 5 files changed, 66 insertions(+), 3 deletions(-) create mode 100644 app/views/entries/monthly_report.html.erb diff --git a/app/controllers/entries_controller.rb b/app/controllers/entries_controller.rb index d4bfd79..56b928e 100644 --- a/app/controllers/entries_controller.rb +++ b/app/controllers/entries_controller.rb @@ -122,6 +122,37 @@ class EntriesController < ApplicationController end end + def monthly_report + # Nur die wirklich benötigten Spalten abfragen + rows = current_user.entries + .select( + Arel.sql("DATE_TRUNC('month', date) AS month"), + :praktikums_typ, + :entry_art, + Arel.sql("SUM(hours * 60 + minutes) AS total_minutes") + ) + .group( + Arel.sql("DATE_TRUNC('month', date)"), + :praktikums_typ, + :entry_art + ) + .order(Arel.sql("DATE_TRUNC('month', date) DESC")) + + # In Hash-Struktur für die View umwandeln + @report = {} + rows.each do |r| + month = r.attributes["month"].to_date + total = r.attributes["total_minutes"].to_i + typ_art = [r.praktikums_typ, r.entry_art] + + @report[month] ||= {} + @report[month][typ_art] = { + total_minutes: total, + hours: total / 60, + minutes: total % 60 + } + end + end private def set_entry diff --git a/app/views/entries/index.html.erb b/app/views/entries/index.html.erb index 9a17d14..0d841d7 100644 --- a/app/views/entries/index.html.erb +++ b/app/views/entries/index.html.erb @@ -144,7 +144,7 @@ -<%= link_to '➕ Neuer Eintrag', new_entry_path, class: 'btn btn-info mt-3 w-100 w-md-auto mb-3' %> +<%= link_to '➕ Neuer Eintrag', new_entry_path, class: 'btn btn-outline-primary mt-3 w-100 w-md-auto mb-3' %> diff --git a/app/views/entries/monthly_report.html.erb b/app/views/entries/monthly_report.html.erb new file mode 100644 index 0000000..59c0f90 --- /dev/null +++ b/app/views/entries/monthly_report.html.erb @@ -0,0 +1,29 @@ +

Monats‑Report

+ +<% @report.each do |month, groups| %> +
+
+ <%= month.strftime("%B %Y") %> +
+
+ + + + + + + + + + <% groups.each do |(typ, art), data| %> + + + + + + <% end %> + +
TypArtGesamtzeit
<%= typ.capitalize %><%= art %><%= data[:hours] %>h <%= data[:minutes] %>min
+
+
+<% end %> diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index 5e15f80..0edb058 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -115,13 +115,16 @@ + <% if current_user.is_admin? %> <% end %> diff --git a/config/routes.rb b/config/routes.rb index 1225f6f..a97299f 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -20,7 +20,7 @@ Rails.application.routes.draw do registrations: 'users/registrations' } get '/impressum', to: 'static_pages#impressum' - + get "/monthly_report", to: "entries#monthly_report", as: :monthly_report_entries post "/db_dump/restore", to: "db_dump#restore" get "/db_dump/dump", to: "db_dump#dump"