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

+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