From 53fb198d3e68d0af6882e1eec2d8b518bfd1573f Mon Sep 17 00:00:00 2001 From: Christoph Marzell Date: Tue, 13 Jan 2026 11:14:28 +0100 Subject: [PATCH] add stunden pro woche --- app/controllers/entries_controller.rb | 50 +++++++++++++++++++++++++++ app/views/entries/index.html.erb | 5 +++ 2 files changed, 55 insertions(+) diff --git a/app/controllers/entries_controller.rb b/app/controllers/entries_controller.rb index 90a2c6a..24a5f11 100644 --- a/app/controllers/entries_controller.rb +++ b/app/controllers/entries_controller.rb @@ -91,6 +91,56 @@ class EntriesController < ApplicationController end end end + + @actual_hours_per_week = {} + + User::PRAKTIKUMSTYPEN.product(User::ENTRY_ARTEN).each do |typ, art| + # 1) Gesamt erforderliche Minuten + total_req_hours = current_user.required_hours_matrix.to_h.dig(typ, art).to_f + next if total_req_hours <= 0 + + total_req_minutes = (total_req_hours * 60).to_i + + # 2) Alle entries dieser Kombi + entries = current_user.entries + .where(praktikums_typ: typ, entry_art: art) + .order(:date) + + next if entries.empty? + + # 3) Erster Eintrag + first_entry_date = entries.first.date + + # 4) Berechne erledigte Minuten + # (die gleiche Matrix, die du schon hast) + remaining = @remaining_minutes_matrix.dig(typ, art).to_i + done_minutes = [total_req_minutes - remaining, 0].max + + # 5) Wenn komplett erledigt, benutze letztes erreichendes Datum + if remaining == 0 && done_minutes > 0 + # Letzter Eintrag, an dem noch Stunden hinzugekommen sind + last_effort_entry = entries.select { |e| + ((e.hours.to_i * 60) + e.minutes.to_i) > 0 + }.last + + end_date = last_effort_entry ? last_effort_entry.date : Date.today + else + end_date = Date.today + end + + # 6) Berechne Wochen zwischen Erstem und Enddatum + total_days = (end_date - first_entry_date).to_i + weeks = total_days / 7.0 + weeks = 1 if weeks < 1 + + # 7) Tatsächliche Stunden pro Woche + actual_per_week = (done_minutes / 60.0) / weeks + # Runde sinnvoll + @actual_hours_per_week[[typ, art]] = actual_per_week.round(2) + end + + + end def new diff --git a/app/views/entries/index.html.erb b/app/views/entries/index.html.erb index d79a336..a0a2ba9 100644 --- a/app/views/entries/index.html.erb +++ b/app/views/entries/index.html.erb @@ -29,6 +29,7 @@ Erledigt Soll (h) Wöchentlich + Stunden/Woche Vorauss. Ende Fortschritt @@ -50,6 +51,10 @@ <%= spent ? "#{spent / 60} h #{spent % 60} min" : "—" %> <%= soll || "—" %> h <%= weekly || "—" %> h/Woche + + <%= @actual_hours_per_week[[typ, art]] || "—" %> h/Woche + + <%= ende.present? ? ende.strftime("%d.%m.%Y") : "—" %> <% percent = @completion_percent_by_typ_art[[typ, art]] %>