Files
redmine_qbo_calendar/app/views/common/_calendar.html.erb
T
rickyandClaude Opus 5.5 3ebe3505ca Weekday in appointment header; monthly totals on the calendar
- Appointment view: show the day of the week on the start and end dates, and lay
  the header out as a flex row so it no longer wraps under the action links
- Calendar: month total of estimated hours and appointments in the toolbar,
  counting only the shown month's days (hidden past weeks included)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-09 19:54:44 -05:00

376 lines
18 KiB
ERB

<%= 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 || []
# Weeks flagged "no new work" (keyed by their Monday): circled in red, and new appointments in them warn
flagged_weeks = NoWorkWeek.where(week_start: (calendar.startdt - 6)..calendar.enddt).pluck(:week_start).to_set
can_flag_weeks = User.current.allowed_to?(:manage_no_work_weeks, nil, global: true)
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
# A day's estimated hours: its share of issues plus its appointments
day_hours = lambda do |day|
issue_hours_by_day[day] + appointments_by_day.fetch(day, []).sum { |appointment, number| appointment_hours.call(appointment, number) }
end
# Month totals: only the shown month's own days (not the neighbouring months' days that fill out the
# first and last weeks), and including weeks hidden by "Hide Past Weeks"
month_days = (calendar.startdt..calendar.enddt).select { |d| d.month == calendar.month }
monthly_total_hours = month_days.sum { |d| day_hours.call(d) }
monthly_appointment_count = month_days.flat_map { |d| appointments_by_day.fetch(d, []).map(&:first) }
.uniq.count { |a| !a.call_out? && a.counts_toward_hours? }
%>
<%# Calendar Status Legend Key: the statuses set up in the plugin settings, then call-outs %>
<% statuses = CustomerAppointment.statuses %>
<% if statuses.any? %>
<div class="calendar-status-legend" style="display: flex; align-items: center; gap: 10px; flex-wrap: wrap; margin: 8px 0;">
<span class="legend-label" style="font-weight: 600; font-size: 0.85em; color: #666;">
<%= l(:label_legend, default: 'Legend') %>:
</span>
<% statuses.each do |status| %>
<span class="status-key-badge" style="<%= status.style %>; padding: 2px 8px; border-radius: 4px; font-size: 0.8em; font-weight: 600;">
<%= status.name %>
</span>
<% end %>
<span class="status-key-badge status-call-out" style="padding: 2px 8px; border-radius: 4px; font-size: 0.8em; font-weight: 600;">
<i class="fa-solid fa-user-slash"></i> <%= l(:label_call_out) %>
</span>
</div>
<% end %>
<!-- Toggle Bar -->
<div class="calendar-filter-toolbar" style="margin-bottom: 12px; display: flex; align-items: center; justify-content: flex-end; gap: 16px; flex-wrap: wrap;">
<%# Month totals, on the left; cancelled appointments and call-outs aren't counted %>
<div class="monthly-totals" style="margin-right: auto; display: inline-flex; align-items: center; gap: 12px; font-size: 0.95em; color: #444;">
<strong><%= l(:label_month_total, month: month_name(calendar.month), default: "#{month_name(calendar.month)} total") %>:</strong>
<span class="monthly-hours-total" title="<%= l(:label_estimated_hours, default: 'Estimated hours') %>">
<i class="fa-regular fa-clock"></i> <strong><%= monthly_total_hours.round(1) %>h</strong>
</span>
<span class="monthly-appointments-total" title="<%= l(:label_appointments) %>">
<i class="fa-regular fa-calendar-check"></i> <strong><%= monthly_appointment_count %></strong> <%= l(:label_appointments).downcase %>
</span>
</div>
<% if is_current_month %>
<button type="button"
onclick="scrollToToday()"
class="button-small"
style="display: inline-flex; align-items: center; gap: 5px; cursor: pointer; font-weight: 600; font-size: 0.9em; padding: 4px 10px; border-radius: 3px;"
title="<%= l(:label_today) %>">
<i class="fa fa-calendar-check-o" aria-hidden="true"></i>
<%= l(:label_today) %>
</button>
<% else %>
<%= link_to url_for(request.query_parameters.merge(year: today.year, month: today.month)),
class: 'button-small',
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 %>
<i class="fa fa-calendar-check-o" aria-hidden="true"></i>
<%= l(:label_today) %>
<% end %>
<% end %>
<label for="toggle-hide-past-weeks" style="cursor: pointer; font-weight: 600; font-size: 0.9em; color: #444; display: inline-flex; align-items: center; gap: 6px;">
<%= check_box_tag 'hide_past_weeks',
'1',
hide_past_weeks,
id: 'toggle-hide-past-weeks',
onchange: 'toggleHidePastWeeks(this.checked)' %>
<%= l(:label_hide_past_weeks, default: 'Hide Past Weeks') %>
</label>
<label for="toggle-only-appointments" style="cursor: pointer; font-weight: 600; font-size: 0.9em; color: #444; display: inline-flex; align-items: center; gap: 6px;">
<%= check_box_tag 'only_appointments',
'1',
only_appointments,
id: 'toggle-only-appointments',
onchange: 'toggleOnlyAppointments(this.checked)' %>
<%= l(:label_only_customer_appointments, default: 'Show Customer Appointments Only') %>
</label>
</div>
<%= form_tag({}, data: { cm_url: issues_context_menu_path }) do %>
<%= hidden_field_tag 'back_url', url_for(params: request.query_parameters), id: nil %>
<%# data-no-work-weeks-url: where toggleNoWorkWeek (calendar.js) saves a week's flag %>
<ul class="cal" data-no-work-weeks-url="<%= no_work_weeks_path %>" data-no-work-week-error="<%= l(:error_no_work_week_save) %>">
<li scope="col" title="<%= l(:label_week) %>" class="calhead week-number"></li>
<% 7.times do |i| %>
<li scope="col" class="calhead">
<%= day_name((calendar.first_wday + i) % 7) %>
</li>
<% 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 = day_hours.call(day)
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
%>
<%
# Every 7-day row has exactly one Monday, whatever day the calendar starts on
week_monday = week.find { |d| d.cwday == 1 }
no_work_week = flagged_weeks.include?(week_monday)
week_css = no_work_week ? ['no-work-week'] : []
%>
<li class="week-number <%= week_css.join(' ') %>" data-no-work-week="<%= week_monday %>">
<span class="label-week"><%= l(:label_week) %></span>
<%= calendar.week_number(week.first) %>
<%# No-new-work flag: a checkbox for users who may set it, otherwise shown only when set %>
<% if can_flag_weeks || no_work_week %>
<label class="no-work-week-toggle" title="<%= l(:text_no_work_week_toggle) %>">
<%# No name, so the issues context menu form never sends it %>
<%= tag.input type: 'checkbox', checked: no_work_week, disabled: !can_flag_weeks,
onchange: "toggleNoWorkWeek(this, '#{week_monday}')" %>
<span><%= l(:label_no_work_week) %></span>
</label>
<% end %>
<%# Render the Weekly Total Hours under the Week Number %>
<% if weekly_total_hours > 0 %>
<div class="weekly-hours-total" style="margin-top: 12px; font-weight: bold; font-size: 0.95em; color: #444;" title="<%= l(:label_estimated_hours, default: 'Estimated hours') %>">
<%= weekly_total_hours.round(1) %>h
</div>
<% end %>
</li>
<% week.each_with_index do |day, day_index| %>
<%
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 %>
<li class="<%= calendar.day_css_classes(day) %> calbody <%= 'week-end' if is_custom_holiday %> <%= week_css.join(' ') %> <%= 'no-work-week-last' if day_index == 6 %>" data-no-work-week="<%= week_monday %>" style="<%= 'background-color: #f6f6f6;' if is_custom_holiday %>"<%= ' id="today"'.html_safe if is_today %>>
<p class="day-num" style="display: grid; grid-template-columns: 1fr auto 1fr; align-items: center; margin-bottom: 4px;">
<!-- Left: Day number and abbreviated weekday -->
<span class="day-num-left" style="display: inline-flex; align-items: center; justify-self: start; gap: 3px;">
<a name="<%= day.to_s %>" <%= 'id="today-anchor"'.html_safe if is_today %> style="text-decoration: none; color: inherit;">
<span class="day-value" style="font-weight: bold;"><%= day.day %></span>
</a>
<span class="abbr-day" style="font-size: 0.85em; color: #777;">(<%= abbr_day_name(day.cwday) %>)</span>
</span>
<!-- Center: New Appointment Action -->
<% if User.current.allowed_to?(:add_customer_appointments, nil, global: true) %>
<span class="day-num-center" style="display: inline-flex; align-items: center; justify-self: center;">
<%# 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 %>
<i class="fa-solid fa-user-slash"></i>
<% end %>
<% end %>
</span>
<% end %>
<!-- Right: Total Hours -->
<span class="day-num-right" style="display: inline-flex; align-items: center; justify-self: end;">
<% if total_hours > 0 %>
<span class="hours-total" style="font-weight: bold; color: #555;" title="<%= l(:label_estimated_hours, default: 'Estimated hours') %>">
<%= total_hours.round(1) %>h
</span>
<% end %>
</span>
</p>
<%# Render Holiday Name Badge if it's a holiday %>
<% if is_custom_holiday %>
<div class="holiday-badge" style="background-color: #f2dede; color: #a94442; padding: 3px 6px; border-radius: 3px; font-size: 0.8em; font-weight: bold; margin-bottom: 4px; border: 1px solid #ebccd1; display: flex; align-items: center; gap: 4px;">
<span class="fa-stack fa-lg">
<i class="fas fa-calendar fa-stack-1x"></i>
<i class="fas fa-slash fa-stack-2x" style="color:Tomato"></i>
</span>
<span style="overflow: hidden; text-overflow: ellipsis; white-space: nowrap;"><%= current_holiday.name %></span>
</div>
<% end %>
<% day_events.each do |event| %>
<% if event.is_a?(Issue) %>
<% starting = (day == event.start_date) %>
<% ending = (day == event.due_date) %>
<%= tag.div class: [
event.css_classes,
'tooltip',
'hascontextmenu',
('starting' if starting),
('ending' if ending)
].compact do %>
<%= "#{event.project} -" unless @project && @project == event.project %>
<%= sprite_icon('bullet-go', rtl: true) if starting && !ending %>
<%= sprite_icon('bullet-end', rtl: true) if ending && !starting %>
<%= sprite_icon('bullet-go-end') if starting && ending %>
<%= link_to_issue(event, truncate: 30) %>
<span class="tip">
<%= render_issue_tooltip(event) %>
</span>
<%= check_box_tag 'ids[]',
event.id,
false,
style: 'display:none;',
id: nil %>
<% end %>
<% elsif event.is_a?(Version) %>
<span class="icon icon-package">
<%= sprite_icon 'package' %>
<%= "#{event.project} -" unless @project && @project == event.project %>
<%= link_to_version(event) %>
</span>
<% elsif event.is_a?(CustomerAppointment) %>
<%= render partial: 'customer_appointments/calendar_event',
locals: {
appointment: event,
day: day,
day_number: day_numbers[event.id],
project: @project
} %>
<% else %>
<% Rails.logger.warn("Unknown calendar event: #{event.class}") %>
<% end %>
<% end %>
</li>
<% end %>
<% end %>
</ul>
<% end %>
<%= context_menu %>