Files
redmine_qbo_calendar/app/models/customer_appointment.rb
T
Claude 73115da2b9 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
2026-10-28 17:14:13 -04:00

176 lines
6.4 KiB
Ruby

#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.
class CustomerAppointment < ActiveRecord::Base
belongs_to :project, optional: true
belongs_to :user, optional: true
belongs_to :customer, optional: true
belongs_to :vehicle, optional: true
belongs_to :estimate, optional: true
validates :subject, presence: true
validates :date, presence: true
# Prevent scheduling on non-working days or holidays
validate :validate_working_day
# 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
Checked_In
In_Progress
Ready
Completed
Cancelled
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.
#
# @param project [Project] Target project (defaults to appointment's project)
# @param author [User] Author for the new issues
# @param tracker [Tracker] Target tracker (defaults to project's first tracker)
# @return [Array<Issue>] Created issues
def create_issues_from_labor_lines(project: self.project, author: User.current, tracker: nil)
return [] if estimate.blank?
target_project = project || self.project || Project.first
target_tracker = tracker || target_project&.trackers&.first
return [] unless target_project && target_tracker
created_issues = []
# estimate.line_items returns array of Quickbooks::Model::InvoiceLineItem
estimate.line_items.each do |line|
detail = line.respond_to?(:sales_line_item_detail) ? line.sales_line_item_detail : nil
next if detail.nil? || detail.item_ref.nil?
item_name = detail.item_ref.name.to_s.strip
description = line.description.to_s.strip
# Match "Labor" (case-insensitive) and ensure description is present
next unless item_name.casecmp('Labor').zero?
next if description.blank?
# detail.quantity contains line quantity/hours; line.amount is total line cost fallback
qty = detail.respond_to?(:quantity) ? detail.quantity.to_f : 0.0
hours = qty.positive? ? qty : line.amount.to_f
issue = Issue.new(
project: target_project,
tracker: target_tracker,
assigned_to_id: self.user&.id,
author: author,
subject: description.truncate(255),
description: "The purpose of this work is to bill the time spent on this issue for Estimate ##{estimate.doc_number} for Appointment: #{subject}",
estimated_hours: hours,
start_date: self.start_date,
due_date: self.due_date,
vehicle: self.vehicle,
estimate: self.estimate,
customer: self.customer,
created_on: Time.current,
updated_on: Time.current
)
if issue.save
created_issues << issue
else
log "Failed to create issue for Estimate ##{estimate.id}: #{issue.errors.full_messages.join(', ')}"
end
end
created_issues
end
# Cast datetime to Date for Redmine Calendar compatibility
def start_date
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
@due_date_cache ||= {}
@due_date_cache[[start_date, duration.to_i]] ||=
RedmineQboCalendar::WorkingDays.nth_working_day(start_date, duration)
end
# 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
subject
end
private
def log(msg)
Rails.logger.info "[CustomerAppointment] #{msg}"
end
# Validates that the appointment date does not land on a weekend or holiday
def validate_working_day
return if date.blank?
target_date = date.to_date
non_working_days = Setting.non_working_week_days || []
if non_working_days.include?(target_date.cwday.to_s)
errors.add(:date, :invalid, message: I18n.t(:error_non_working_day))
elsif defined?(Holiday)
matched_holiday = Holiday.all.find { |h| h.occurs_on?(target_date) }
if matched_holiday.present?
errors.add(:date, :invalid, message: I18n.t(:error_holiday, name: matched_holiday.name))
end
end
end
# Helper to parse item name across Quickbooks::Model::Line and Hash structures
def extract_item_name(line)
if line.respond_to?(:sales_line_item_detail) && line.sales_line_item_detail.present?
line.sales_line_item_detail.item_ref&.name
elsif line.is_a?(Hash)
line[:item_name] || line.dig('sales_line_item_detail', 'item_ref', 'name')
end
end
# Helper to parse description across Quickbooks::Model::Line and Hash structures
def extract_description(line)
if line.respond_to?(:description)
line.description
elsif line.is_a?(Hash)
line[:description] || line['description']
end
end
end