Browse Source

add stunden pro woche

main
Christoph Marzell 3 weeks ago
parent
commit
8f8e78f38f
  1. 24
      app/controllers/entries_controller.rb
  2. 5
      app/views/entries/index.html.erb

24
app/controllers/entries_controller.rb

@ -95,47 +95,37 @@ class EntriesController < ApplicationController
@actual_hours_per_week = {} @actual_hours_per_week = {}
User::PRAKTIKUMSTYPEN.product(User::ENTRY_ARTEN).each do |typ, art| 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 total_req_hours = current_user.required_hours_matrix.to_h.dig(typ, art).to_f
next if total_req_hours <= 0 next if total_req_hours <= 0
total_req_minutes = (total_req_hours * 60).to_i total_req_minutes = (total_req_hours * 60).to_i
# 2) Alle entries dieser Kombi
entries = current_user.entries entries = current_user.entries
.where(praktikums_typ: typ, entry_art: art) .where(praktikums_typ: typ, entry_art: art)
.order(:date) .order(:date)
next if entries.empty? 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 remaining = @remaining_minutes_matrix.dig(typ, art).to_i
done_minutes = [total_req_minutes - remaining, 0].max 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 if remaining == 0 && done_minutes > 0
# Letzter Eintrag, an dem noch Stunden hinzugekommen sind
last_effort_entry = entries.select { |e| last_effort_entry = entries.select { |e|
((e.hours.to_i * 60) + e.minutes.to_i) > 0 ((e.hours.to_i * 60) + e.minutes.to_i) > 0
}.last }.last
end_date = last_effort_entry ? last_effort_entry.date : Date.today
relevant_entries = entries.select { |e| e.date <= last_effort_entry.date }
else else
end_date = Date.today
relevant_entries = entries
end 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 actual_per_week = (done_minutes / 60.0) / weeks
# Runde sinnvoll
@actual_hours_per_week[[typ, art]] = actual_per_week.round(2) @actual_hours_per_week[[typ, art]] = actual_per_week.round(2)
end end

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

@ -51,9 +51,8 @@
<td><%= spent ? "#{spent / 60} h #{spent % 60} min" : "—" %></td> <td><%= spent ? "#{spent / 60} h #{spent % 60} min" : "—" %></td>
<td><%= soll || "—" %> h</td> <td><%= soll || "—" %> h</td>
<td><%= weekly || "—" %> h/Woche</td> <td><%= weekly || "—" %> h/Woche</td>
<td>
<%= @actual_hours_per_week[[typ, art]] || "—" %> h/Woche
</td>
<td><%= @actual_hours_per_week[[typ, art]] ? "#{@actual_hours_per_week[[typ, art]]} h/Woche" : "—" %></td>
<td><%= ende.present? ? ende.strftime("%d.%m.%Y") : "—" %></td> <td><%= ende.present? ? ende.strftime("%d.%m.%Y") : "—" %></td>
<td> <td>

Loading…
Cancel
Save