diff --git a/app/controllers/customer_appointments_controller.rb b/app/controllers/customer_appointments_controller.rb
index 1f32056..fdd4b41 100644
--- a/app/controllers/customer_appointments_controller.rb
+++ b/app/controllers/customer_appointments_controller.rb
@@ -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
diff --git a/app/models/customer_appointment.rb b/app/models/customer_appointment.rb
index 34cf3ed..98cf0ca 100644
--- a/app/models/customer_appointment.rb
+++ b/app/models/customer_appointment.rb
@@ -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
diff --git a/app/models/holiday.rb b/app/models/holiday.rb
index 0892844..9c5ed84 100644
--- a/app/models/holiday.rb
+++ b/app/models/holiday.rb
@@ -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
diff --git a/app/views/common/_calendar.html.erb b/app/views/common/_calendar.html.erb
index 8953f78..0258f8e 100644
--- a/app/views/common/_calendar.html.erb
+++ b/app/views/common/_calendar.html.erb
@@ -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?
diff --git a/app/views/customer_appointments/show.html.erb b/app/views/customer_appointments/show.html.erb
index e84d1c7..4bb2199 100644
--- a/app/views/customer_appointments/show.html.erb
+++ b/app/views/customer_appointments/show.html.erb
@@ -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 %>
+ <%= 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 %>
<%= l(:button_edit) %>
diff --git a/config/locales/en.yml b/config/locales/en.yml
index 7ea280a..8ef3f4b 100644
--- a/config/locales/en.yml
+++ b/config/locales/en.yml
@@ -21,6 +21,8 @@ en:
day_name_thursday: "Thursday"
day_name_tuesday: "Tuesday"
day_name_wednesday: "Wednesday"
+ button_create_issues_from_estimate: "Create Issues from Estimate"
+ error_no_labor_lines_found: "No Labor lines with a description were found on the estimate"
error_holiday: "Cannot be scheduled on holiday: %{name}"
error_non_working_day: "Cannot be scheduled on a non-working day"
field_customer_name: Customer
@@ -78,8 +80,12 @@ en:
label_week_3rd: "3rd"
label_week_4th: "4th"
label_week_last: "Last"
+ notice_appointment_issues_created:
+ one: "1 issue created from the estimate"
+ other: "%{count} issues created from the estimate"
notice_unable_delete_holiday: "Unable to delete holiday"
permission_add_customer_appointments: "Add customer appointments"
+ permission_create_customer_appointment_issues: "Create issues from appointment estimates"
permission_delete_customer_appointments: "Delete customer appointments"
permission_edit_customer_appointments: "Edit customer appointments"
permission_manage_customer_appointments: Manage Customer Appointments
diff --git a/config/routes.rb b/config/routes.rb
index 5100282..8dc1a4d 100644
--- a/config/routes.rb
+++ b/config/routes.rb
@@ -14,9 +14,11 @@ RedmineApp::Application.routes.draw do
resources :customer_appointments do
collection do
get :customer_options
- get :create_issues
get :history
end
+ member do
+ post :create_issues
+ end
end
end
\ No newline at end of file
diff --git a/init.rb b/init.rb
index 5a37760..9dd58ac 100644
--- a/init.rb
+++ b/init.rb
@@ -31,9 +31,11 @@ Redmine::Plugin.register :redmine_qbo_calendar do
# Global Permissions
permission :view_customer_appointments, { customer_appointments: [:index, :show, :history] }, global: true
- permission :add_customer_appointments, { customer_appointments: [:new, :create] }, global: true
- permission :edit_customer_appointments, { customer_appointments: [:edit, :update] }, global: true
+ # customer_options feeds the vehicle/estimate dropdowns on the new and edit forms
+ permission :add_customer_appointments, { customer_appointments: [:new, :create, :customer_options] }, global: true
+ permission :edit_customer_appointments, { customer_appointments: [:edit, :update, :customer_options] }, global: true
permission :delete_customer_appointments, { customer_appointments: [:destroy] }, global: true
+ permission :create_customer_appointment_issues, { customer_appointments: [:create_issues] }, global: true
# Register top menu items
menu :top_menu, :calendar, { controller: :calendars, action: :show }, caption: :label_calendar, if: Proc.new {User.current.logged?}
diff --git a/lib/redmine_qbo_calendar/patches/calendars_controller_patch.rb b/lib/redmine_qbo_calendar/patches/calendars_controller_patch.rb
index bf8ea42..bb09837 100644
--- a/lib/redmine_qbo_calendar/patches/calendars_controller_patch.rb
+++ b/lib/redmine_qbo_calendar/patches/calendars_controller_patch.rb
@@ -34,7 +34,20 @@ module RedmineQboCalendar
log "events: #{events.inspect}"
- appointments = CustomerAppointment.visible(User.current)
+ # Only users with "View customer appointments" see appointments on the calendar
+ appointments = CustomerAppointment.visible(User.current).includes(:customer, :vehicle, :user, :project)
+
+ # Only load appointments that can appear in the displayed range. Multi-day appointments
+ # can start before the range, so look back far enough to cover the longest one
+ # (working days only, so allow ~2x calendar days plus slack for holidays).
+ max_duration = [appointments.maximum(:duration).to_i, 1].max
+ range_start = (@calendar.startdt - (max_duration * 2 + 14)).beginning_of_day
+ range_end = @calendar.enddt.end_of_day
+ appointments = appointments.where(date: range_start..range_end).to_a
+
+ # Hand the list to the calendar view directly, so appointments that span the whole
+ # displayed range (neither starting nor ending in it) still render
+ @qbo_calendar_appointments = appointments
log "appointments: #{appointments.inspect}"
diff --git a/lib/redmine_qbo_calendar/working_days.rb b/lib/redmine_qbo_calendar/working_days.rb
new file mode 100644
index 0000000..b57e24f
--- /dev/null
+++ b/lib/redmine_qbo_calendar/working_days.rb
@@ -0,0 +1,45 @@
+#The MIT License (MIT)
+#
+#Copyright (c) 2026 rick barrette
+#
+#Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
+#
+#The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
+#
+#THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+
+module RedmineQboCalendar
+ # Working-day arithmetic that respects Redmine's non-working week days and Holidays.
+ module WorkingDays
+ # Upper bound on how far we'll walk the calendar, so bad data can't loop forever.
+ MAX_SCAN_DAYS = 3660
+
+ module_function
+
+ def non_working_week_days
+ Setting.non_working_week_days || []
+ end
+
+ def non_working?(date, holidays: Holiday.all.to_a, week_days: non_working_week_days)
+ week_days.include?(date.cwday.to_s) || holidays.any? { |h| h.occurs_on?(date) }
+ end
+
+ # Returns the date of the Nth working day, counting start_date itself if it is a working day.
+ # e.g. a 3-day job starting Friday ends on Tuesday (Fri, Mon, Tue).
+ def nth_working_day(start_date, count, holidays: Holiday.all.to_a, week_days: non_working_week_days)
+ return nil if start_date.nil?
+
+ count = [count.to_i, 1].max
+ cursor = start_date
+ worked = 0
+
+ MAX_SCAN_DAYS.times do
+ worked += 1 unless non_working?(cursor, holidays: holidays, week_days: week_days)
+ return cursor if worked >= count
+ cursor += 1
+ end
+
+ cursor
+ end
+ end
+end