<%= l(:label_upcoming_appointments, default: 'My Upcoming Appointments') %>

<% today = User.current.today holidays = Holiday.all.to_a # Safely filter appointments by logged-in user across possible user column names appointments_scope = CustomerAppointment.includes(:customer, :vehicle) if CustomerAppointment.column_names.include?('user_id') appointments_scope = appointments_scope.where(user_id: User.current.id) elsif CustomerAppointment.column_names.include?('assigned_to_id') appointments_scope = appointments_scope.where(assigned_to_id: User.current.id) elsif CustomerAppointment.column_names.include?('author_id') appointments_scope = appointments_scope.where(author_id: User.current.id) end # Look back up to 60 days to capture multi-day appointments starting in the past raw_appointments = appointments_scope.where('date >= ?', today - 60.days) # One row per working day (weekends and holidays skipped, as on the calendar), from today on expanded_appointments = raw_appointments.flat_map do |appointment| days = appointment.working_days(holidays: holidays) days.each_with_index.filter_map do |date, index| { appointment: appointment, date: date, day_number: index + 1, total_days: days.size } if date >= today end end # Sort upcoming occurrences chronologically and take top 10 expanded_appointments = expanded_appointments.sort_by { |item| [item[:date], item[:appointment].date] }.first(10) %> <% if expanded_appointments.any? %> <% expanded_appointments.each do |item| %> <% appt = item[:appointment] %> <% end %>
<%= l(:label_title) %> <%= l(:label_customer) %> <%= l(:label_vehicle) %> <%= l(:label_date) %> <%= l(:label_status) %>
<%= link_to appt.subject, customer_appointment_path(appt) %> <% if item[:total_days] > 1 %> (Day <%= item[:day_number] %> of <%= item[:total_days] %>) <% end %> <%= link_to appt.customer unless appt.customer.nil? %> <%= link_to appt.vehicle unless appt.vehicle.nil? %> <%= format_date(item[:date]) %> <%= appointment_status_badge(appt, css: 'badge status-badge') %>
<% else %>

<%= l(:label_no_data) %>

<% end %>