diff --git a/app/controllers/entries_controller.rb b/app/controllers/entries_controller.rb index 24a5f11..10131a4 100644 --- a/app/controllers/entries_controller.rb +++ b/app/controllers/entries_controller.rb @@ -95,47 +95,37 @@ class EntriesController < ApplicationController @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 + # Wenn alles erledigt: nur bis zum letzten Eintrag mit Stunden 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 + relevant_entries = entries.select { |e| e.date <= last_effort_entry.date } else - end_date = Date.today + relevant_entries = entries 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 + # Gruppiere nach Kalenderjahr + Woche + active_weeks = relevant_entries.group_by { |e| [e.date.cwyear, e.date.cweek] }.keys.size - # 7) Tatsächliche Stunden pro Woche + weeks = [active_weeks, 1].max actual_per_week = (done_minutes / 60.0) / weeks - # Runde sinnvoll + @actual_hours_per_week[[typ, art]] = actual_per_week.round(2) end diff --git a/app/views/entries/index.html.erb b/app/views/entries/index.html.erb index a0a2ba9..e139a51 100644 --- a/app/views/entries/index.html.erb +++ b/app/views/entries/index.html.erb @@ -51,9 +51,8 @@