mirror of
https://github.com/rickbarrette/redmine_qbo_calendar.git
synced 2026-11-17 13:50:36 -05:00
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:
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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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) %>
|
||||
|
||||
@@ -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
|
||||
|
||||
+3
-1
@@ -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
|
||||
@@ -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?}
|
||||
|
||||
@@ -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}"
|
||||
|
||||
|
||||
@@ -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
|
||||
Reference in new issue
Block a user