diff --git a/app/views/common/_calendar.html.erb b/app/views/common/_calendar.html.erb index 6324879..950de23 100644 --- a/app/views/common/_calendar.html.erb +++ b/app/views/common/_calendar.html.erb @@ -57,6 +57,44 @@ false end + + # Lambda to calculate hours for a single event on a specific day (Front-Loaded Method) + calculate_event_hours = lambda do |event, target_day| + if event.is_a?(Issue) + event.estimated_hours.to_f + elsif event.is_a?(CustomerAppointment) + if event.duration.to_i > 1 + app_total_hours = if event.respond_to?(:estimated_hours) && event.estimated_hours.present? + event.estimated_hours.to_f + else + event.duration.to_f * 8.0 + end + + start_date = (event.respond_to?(:date) && event.date.present? ? event.date : event.start_date).to_date + + working_day_index = 0 + date_cursor = start_date + + while date_cursor < target_day + is_weekend = non_working_days.include?(date_cursor.cwday.to_s) + is_holiday = all_holidays.any? { |h| h.occurs_on?(date_cursor) } + working_day_index += 1 unless is_weekend || is_holiday + date_cursor += 1 + end + + hours_already_allocated = working_day_index * 8.0 + remaining_hours = app_total_hours - hours_already_allocated + + [[remaining_hours, 8.0].min, 0.0].max + elsif event.respond_to?(:estimated_hours) && event.estimated_hours.present? + event.estimated_hours.to_f + else + event.duration.to_f + end + else + 0.0 + end + end %> <%# Calendar Status Legend Key %> @@ -97,7 +135,7 @@ style: 'display: inline-flex; align-items: center; gap: 5px; text-decoration: none; font-weight: 600; font-size: 0.9em; padding: 4px 10px; border-radius: 3px;', title: l(:label_today) do %> - <%= l(:label_today,) %> + <%= l(:label_today) %> <% end %> <% end %> @@ -136,9 +174,46 @@ <%# Skip week if "hide past weeks" is active and every day in the week is in the past %> <% next if hide_past_weeks && week.last < User.current.today %> + <% + # --- Pre-calculate daily events and weekly total hours --- + week_data = {} + weekly_total_hours = 0.0 + + week.each do |day| + standard_events = only_appointments ? [] : calendar.events_on(day).reject { |e| e.is_a?(CustomerAppointment) } + matching_appointments = all_appointments.select { |app| appointment_occurs_on.call(app, day) } + + day_events = (standard_events + matching_appointments).sort_by do |event| + if event.is_a?(CustomerAppointment) && event.date.present? + event.date.strftime('%H:%M:%S') + elsif event.respond_to?(:start_date) && event.start_date.present? + event.start_date.to_time.strftime('%H:%M:%S') + else + '00:00:00' + end + end + + day_total_hours = day_events.sum { |event| calculate_event_hours.call(event, day) } + weekly_total_hours += day_total_hours + + # Store it so we don't calculate it twice + week_data[day] = { + events: day_events, + total_hours: day_total_hours + } + end + %> +