Browse Source

add routes for month report

main
Christoph Marzell 4 weeks ago
parent
commit
07430b8bce
  1. 31
      app/controllers/entries_controller.rb
  2. 2
      app/views/entries/index.html.erb
  3. 29
      app/views/entries/monthly_report.html.erb
  4. 5
      app/views/layouts/application.html.erb
  5. 2
      config/routes.rb

31
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

2
app/views/entries/index.html.erb

@ -144,7 +144,7 @@
</div>
<%= 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' %>

29
app/views/entries/monthly_report.html.erb

@ -0,0 +1,29 @@
<h1 class="mb‑4">Monats‑Report</h1>
<% @report.each do |month, groups| %>
<div class="card mb‑4 mt-4">
<div class="card-header">
<strong><%= month.strftime("%B %Y") %></strong>
</div>
<div class="card-body">
<table class="table table‑sm table‑bordered table-striped ">
<thead>
<tr>
<th style="width: 33%">Typ</th>
<th style="width: 33%">Art</th>
<th style="width: 33%">Gesamtzeit</th>
</tr>
</thead>
<tbody>
<% groups.each do |(typ, art), data| %>
<tr>
<td><%= typ.capitalize %></td>
<td><%= art %></td>
<td><%= data[:hours] %>h <%= data[:minutes] %>min</td>
</tr>
<% end %>
</tbody>
</table>
</div>
</div>
<% end %>

5
app/views/layouts/application.html.erb

@ -115,13 +115,16 @@
<li class="nav-item">
<%= link_to "Einträge", entries_path, class: "nav-link" %>
</li>
<li class="nav-item">
<%= link_to "Monatsreport", monthly_report_entries_path, class: "nav-link" %>
</li>
<li class="nav-item">
<%= link_to "Profil", edit_user_registration_path, class: "nav-link" %>
</li>
<% if current_user.is_admin? %>
<li class="nav-item">
<%= link_to "Database", db_dump_path, class: "nav-link" %>
<%= link_to "Admin", admin_root_url, class: "nav-link" %>
</li>
<% end %>

2
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"

Loading…
Cancel
Save