Fix permissions, holiday, hours, visibility and scheduling bugs

- customer_options is now covered by the add/edit permissions, so
  non-admin staff get the vehicle/estimate dropdowns
- create_issues is now a POST member route with its own permission,
  checks add_issues on the target project, no longer redirects to a
  missing route, and has a button on the appointment page
- "Last" week-of-month holidays (e.g. Memorial Day) now match every year
- Cancelled and Call Out appointments no longer count toward hours
- Calendar only shows appointments to users with View customer
  appointments, and only loads those near the displayed month
- due_date skips weekends and holidays, matching the calendar layout
- Subjects keep their casing; only the first letter is capitalized

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01BiwYMjhoQvL7XsKKfRz4pM
This commit is contained in:
Claude committed 2026-10-28 17:14:13 -04:00
1 parent 21538ad95b
commit 73115da2b9
10 files changed
+130 -17

No files matched your search

@@ -11,7 +11,7 @@
class CustomerAppointmentsController < ApplicationController
before_action :require_login
before_action :find_project, except: [:index]
before_action :find_appointment, only: [ :show, :edit, :update, :destroy ]
before_action :find_appointment, only: [ :show, :edit, :update, :destroy, :create_issues ]
# This tells Redmine to check global permissions for the current controller/action
before_action :authorize_global
@@ -26,17 +26,22 @@ class CustomerAppointmentsController < ApplicationController
}
end
# Creates one issue per "Labor" line on the appointment's QBO estimate.
def create_issues
@appointment = CustomerAppointment.find(params[:id])
created_issues = @appointment.create_issues_from_labor_lines(project: @project, author: User.current)
# Same fallback order as CustomerAppointment#create_issues_from_labor_lines
target_project = @project || @appointment.project || Project.first
# The user must also be allowed to add issues to the project they'll be created in
return deny_access unless target_project && User.current.allowed_to?(:add_issues, target_project)
created_issues = @appointment.create_issues_from_labor_lines(project: target_project, author: User.current)
if created_issues.any?
flash[:notice] = l(:notice_successful_create)
redirect_to project_customer_appointments_path(@project)
flash[:notice] = l(:notice_appointment_issues_created, count: created_issues.size)
else
flash[:error] = l(:error_no_labor_lines_found)
redirect_to customer_appointment_path(@appointment)
end
redirect_to customer_appointment_path(@appointment)
end
def index
+25 -5
View File
@@ -22,7 +22,8 @@ class CustomerAppointment < ActiveRecord::Base
# Prevent scheduling on non-working days or holidays
validate :validate_working_day
normalizes :subject, with: ->(subject) { subject.strip.titleize }
# Capitalize the first letter only; titleize mangled vehicle names and acronyms (JL -> Jl, LED -> Led)
normalizes :subject, with: ->(subject) { subject.strip.sub(/\A[a-z]/, &:upcase) }
STATUSES = %w[
Scheduled
@@ -34,6 +35,17 @@ class CustomerAppointment < ActiveRecord::Base
Call_Out
]
# Statuses whose time should not count toward scheduled hours totals
NON_SCHEDULED_STATUSES = %w[
Cancelled
Call_Out
]
# False for cancelled / call-out appointments, which stay on the calendar but free up their hours
def counts_toward_hours?
!NON_SCHEDULED_STATUSES.include?(status.to_s)
end
# Scans the QBO Estimate's InvoiceLineItems for "Labor" sales items
# and generates a Redmine Issue for each matching line.
#
@@ -98,14 +110,22 @@ class CustomerAppointment < ActiveRecord::Base
date&.to_date
end
# Last working day of the appointment. Weekends and holidays are skipped, matching how the
# calendar spreads a multi-day appointment (a 3-day job starting Friday ends Tuesday).
def due_date
return nil unless date
# Accounts for multi-day duration spanning across dates
start_date + ([duration.to_i, 1].max - 1).days
@due_date_cache ||= {}
@due_date_cache[[start_date, duration.to_i]] ||=
RedmineQboCalendar::WorkingDays.nth_working_day(start_date, duration)
end
def self.visible(user)
return CustomerAppointment.all
# Appointments the user may see. Requires the global "View customer appointments" permission.
def self.visible(user = User.current)
if user.allowed_to?(:view_customer_appointments, nil, global: true)
all
else
none
end
end
def to_s
+9 -1
View File
@@ -15,6 +15,9 @@ class Holiday < ApplicationRecord
dynamic
]
# Value saved by the holiday form for "Last" in the week-of-month select
LAST_WEEK = 5
validates :recurrence_type, inclusion: { in: TYPES }, allow_blank: true
def occurs_on?(target_date)
@@ -23,6 +26,7 @@ class Holiday < ApplicationRecord
end
if recurrence_type == 'fixed'
return false if exact_date.nil?
return target_date.month == exact_date.month && target_date.day == exact_date.day
end
@@ -30,7 +34,11 @@ class Holiday < ApplicationRecord
# Check if month and day of week match
return false unless target_date.month == month && target_date.wday == day_of_week
# Calculate if it's the correct week of the month (1st, 2nd, 3rd, etc.)
# Week 5 means "Last": the final occurrence of that weekday in the month
# (e.g. Memorial Day, which falls in week 4 in some years and week 5 in others)
return (target_date + 7).month != target_date.month if week_of_month == LAST_WEEK
# Otherwise the Nth occurrence (1st, 2nd, 3rd, 4th)
week_in_month = ((target_date.day - 1) / 7) + 1
return week_in_month == week_of_month
end
+6 -1
View File
@@ -15,7 +15,9 @@
all_holidays = Holiday.all
# Safely collect all unique CustomerAppointments across the entire calendar date range
all_appointments = (calendar.startdt..calendar.enddt).flat_map { |date|
# 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
@@ -62,6 +64,9 @@
calculate_event_hours = lambda do |event, target_day|
if event.is_a?(Issue)
event.estimated_hours.to_f
elsif event.is_a?(CustomerAppointment) && !event.counts_toward_hours?
# Cancelled / call-out appointments stay on the calendar but don't use scheduled time
0.0
elsif event.is_a?(CustomerAppointment)
if event.duration.to_i > 1
app_total_hours = if event.respond_to?(:estimated_hours) && event.estimated_hours.present?
@@ -14,6 +14,13 @@
<% end %>
<% end %>
<% if @appointment.estimate && User.current.allowed_to?(:create_customer_appointment_issues, nil, global: true) %>
<%= link_to create_issues_customer_appointment_path(@appointment),
method: :post,
data: { confirm: l(:text_are_you_sure) } do %>
<i class="fa-solid fa-list-check"></i> <%= l(:button_create_issues_from_estimate) %>
<% end %>
<% end %>
<% if User.current.allowed_to?(:edit_customer_appointments, nil, global: true) %>
<%= link_to edit_customer_appointment_path(@appointment) do %>
<i class="fa-solid fa-pen-to-square"></i> <%= l(:button_edit) %>