%= javascript_include_tag("calendar", plugin: :redmine_qbo_calendar) %>
<%
# Toggles: each user's own, kept in their preferences by CalendarsControllerPatch (always in params here)
only_appointments = params[:only_appointments] == '1'
hide_past_weeks = params[:hide_past_weeks] == '1'
# Current date context: the Today button scrolls when today is on the page, else opens today's month
today = User.current.today
is_current_month = (calendar.startdt..calendar.enddt).cover?(today)
# Read once for the whole page
non_working_days = Setting.non_working_week_days || []
all_holidays = Holiday.all.to_a
working_days_between = lambda do |first, last|
RedmineQboCalendar::WorkingDays.between(first, last, holidays: all_holidays, week_days: non_working_days)
end
# Safely collect all unique CustomerAppointments across the entire calendar date range
# Prefer the list loaded by CalendarsControllerPatch; fall back to the calendar's own events
# (e.g. if this partial is rendered somewhere else)
all_appointments = @qbo_calendar_appointments || (calendar.startdt..calendar.enddt).flat_map { |date|
calendar.events_on(date)
}.select { |e| e.is_a?(CustomerAppointment) }.uniq
# Each appointment on each of its working days (weekends and holidays skipped), worked out once:
# { date => [[appointment, day number], ...] }
appointments_by_day = Hash.new { |h, k| h[k] = [] }
all_appointments.each do |appointment|
appointment.working_days(holidays: all_holidays).each_with_index do |date, index|
appointments_by_day[date] << [appointment, index + 1] if date.between?(calendar.startdt, calendar.enddt)
end
end
# An appointment's hours on its Nth day: multi-day appointments use up to a working day's hours (plugin
# setting, 8 by default) a day from the start (front-loaded); cancelled appointments and call-outs use none
hours_per_day = RedmineQboCalendar.hours_per_day
appointment_hours = lambda do |appointment, day_number|
next 0.0 unless appointment.counts_toward_hours?
if appointment.duration.to_i > 1
total = appointment.estimated_hours.present? ? appointment.estimated_hours.to_f : appointment.duration.to_f * hours_per_day
[[total - (day_number - 1) * hours_per_day, hours_per_day].min, 0.0].max
elsif appointment.estimated_hours.present?
appointment.estimated_hours.to_f
else
appointment.duration.to_f
end
end
# Issues' estimated hours, spread evenly over the working days from start to due date. Redmine shows an
# issue on its start and due dates only, so counting it on the days it shows would count it twice and
# miss the days between. { date => hours }
issue_hours_by_day = Hash.new(0.0)
unless only_appointments
calendar.instance_variable_get(:@events).to_a.grep(Issue).each do |issue|
next unless issue.estimated_hours.to_f > 0
days = working_days_between.call(issue.start_date || issue.due_date, issue.due_date || issue.start_date)
days = [issue.start_date || issue.due_date].compact if days.empty?
days.each { |d| issue_hours_by_day[d] += issue.estimated_hours.to_f / days.size }
end
end
%>
<%# Calendar Status Legend Key: the statuses set up in the plugin settings, then call-outs %>
<% statuses = CustomerAppointment.statuses %>
<% if statuses.any? %>
<%= l(:label_legend, default: 'Legend') %>:
<% statuses.each do |status| %>
<%= status.name %>
<% end %>
<%= l(:label_call_out) %>
<% end %>
<% calendar.format_month.each_slice(7) do |week| %>
<%# 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) }
day_appointments = appointments_by_day.fetch(day, [])
matching_appointments = day_appointments.map(&:first)
# Call-outs first (who's off that day), then everything else by time
day_events = (standard_events + matching_appointments).sort_by do |event|
call_out = event.is_a?(CustomerAppointment) && event.call_out? ? 0 : 1
time = 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
[call_out, time]
end
day_total_hours = issue_hours_by_day[day] + day_appointments.sum { |appointment, number| appointment_hours.call(appointment, number) }
weekly_total_hours += day_total_hours
# Store it so we don't calculate it twice
week_data[day] = {
events: day_events,
day_numbers: day_appointments.to_h { |appointment, number| [appointment.id, number] },
total_hours: day_total_hours
}
end
%>
<%= l(:label_week) %>
<%= calendar.week_number(week.first) %>
<%# Render the Weekly Total Hours under the Week Number %>
<% if weekly_total_hours > 0 %>
<%= weekly_total_hours.round(1) %>h
<% end %>
<% week.each do |day| %>
<%
is_today = (day == User.current.today)
# Find the specific holiday for this day (if any)
current_holiday = all_holidays.find { |h| h.occurs_on?(day) }
is_custom_holiday = current_holiday.present?
is_standard_weekend = non_working_days.include?(day.cwday.to_s)
# Retrieve the pre-calculated data for this day
day_events = week_data[day][:events]
total_hours = week_data[day][:total_hours]
day_numbers = week_data[day][:day_numbers]
%>
<%# Added 'week-end' class and background color styling for custom holidays to mimic weekends %>
>
style="text-decoration: none; color: inherit;">
<%= day.day %>(<%= abbr_day_name(day.cwday) %>)
<% if User.current.allowed_to?(:add_customer_appointments, nil, global: true) %>
<%# Only show ADD button if it's in the future, NOT a weekend, NOT a holiday, AND user has permission %>
<% if day >= User.current.today && !is_standard_weekend && !is_custom_holiday && User.current.allowed_to?(:add_customer_appointments, nil, global: true) %>
<%= link_to sprite_icon('add'),
new_customer_appointment_path(start_date: day),
class: 'icon-only icon-add',
title: l(:label_appointment_new, default: 'New Appointment') %>
<%= link_to new_customer_appointment_path(start_date: day, event_type: CustomerAppointment::CALL_OUT),
class: 'calendar-add-call-out', title: l(:label_new_call_out) do %>
<% end %>
<% end %>
<% end %>
<% if total_hours > 0 %>
<%= total_hours.round(1) %>h
<% end %>
<%# Render Holiday Name Badge if it's a holiday %>
<% if is_custom_holiday %>