From 53991ed0870ab66efdc7f319d25248085765d197 Mon Sep 17 00:00:00 2001
From: Claude
Date: Sun, 1 Nov 2026 13:11:41 -0500
Subject: [PATCH] Add Vehicle from an appointment sets it there and returns
The appointment page's Add Vehicle link (shown when the appointment has none)
now carries the appointment and a back_url. Creating the vehicle calls a new
:controller_vehicles_new_after_save hook; this plugin's listener sets the
vehicle on that appointment (same customer, no vehicle yet, user may edit
appointments) and the form redirects back to the appointment. The calendar
plugin is unchanged.
Co-Authored-By: Claude Opus 5.5
Claude-Session: https://claude.ai/code/session_01BiwYMjhoQvL7XsKKfRz4pM
---
README.md | 3 +-
app/controllers/vehicles_controller.rb | 6 ++-
.../customer_appointments/_show_hook.html.erb | 9 +++--
app/views/vehicles/new.html.erb | 5 ++-
config/locales/en.yml | 1 +
lib/redmine_qbo_vehicles.rb | 1 +
.../appointment_vehicle_hook_listener.rb | 38 +++++++++++++++++++
7 files changed, 57 insertions(+), 6 deletions(-)
create mode 100644 lib/vehicles/hooks/appointment_vehicle_hook_listener.rb
diff --git a/README.md b/README.md
index efaf904..22b084d 100644
--- a/README.md
+++ b/README.md
@@ -23,7 +23,8 @@ The goal of this project is to enable vehicle tracking for customer vehicles wit
* **Invoices (redmine_qbo 2026.11.2+):** importing a work order's time or estimate fills the invoice's VIN box with its vehicle's VIN and the mileage box with the highest value of the work orders' **Mileage** issue custom field (Redmine's own custom field named "Mileage"), and an invoice whose work orders are for more than one vehicle isn't saved (one vehicle per invoice).
* **Estimates (redmine_qbo 2026.11.2+):** the VIN box suggests the customer's vehicles, an invoice made from an estimate without a VIN gets its work orders' vehicle, and the Create issues from estimate page has a **Vehicle** for all the issues: preselected with the appointment's vehicle, or else the customer's vehicle whose VIN is on the estimate; when none matches it says so, so the team can pick one before creating the issues.
* **Customer page:** ticking a vehicle adds it to the New issue link.
-* **Appointments (redmine_qbo_calendar):** an appointment added from an estimate gets the customer's vehicle with the estimate's VIN.
+* **Appointments (redmine_qbo_calendar):** an appointment added from an estimate gets the customer's vehicle with the estimate's VIN. On an appointment without a vehicle, **Add Vehicle** creates one for the customer, sets it on the appointment and returns to it (for users who may edit appointments).
+* **For other plugins:** creating a vehicle calls the `:controller_vehicles_new_after_save` hook (context: `params`, `vehicle`), and the new vehicle form returns to `back_url` when given, so a plugin can link to `new_vehicle_path(customer_id:, back_url:, ...)` and act on the vehicle it gets back.
## Installation
diff --git a/app/controllers/vehicles_controller.rb b/app/controllers/vehicles_controller.rb
index ed45204..02d108a 100644
--- a/app/controllers/vehicles_controller.rb
+++ b/app/controllers/vehicles_controller.rb
@@ -47,7 +47,11 @@ class VehiclesController < ApplicationController
if @vehicle.save
flash[:notice] = t :notice_vehicle_created
- redirect_to @vehicle
+ # For whatever the vehicle was added from (e.g. an appointment, which then gets this vehicle: see
+ # Vehicles::Hooks::AppointmentVehicleHookListener). Context: params, vehicle
+ call_hook(:controller_vehicles_new_after_save, params: params, vehicle: @vehicle)
+ # Back to where the vehicle was added from (back_url), else the vehicle
+ redirect_back_or_default @vehicle
else
# A duplicate VIN: point to the vehicle that already has it
@existing_vehicle = Vehicle.find_by(vin: @vehicle.vin) if @vehicle.errors.of_kind?(:vin, :taken)
diff --git a/app/views/customer_appointments/_show_hook.html.erb b/app/views/customer_appointments/_show_hook.html.erb
index e8f3a97..e17f651 100644
--- a/app/views/customer_appointments/_show_hook.html.erb
+++ b/app/views/customer_appointments/_show_hook.html.erb
@@ -1,11 +1,14 @@
+<%# On an appointment without a vehicle: add one for the customer; once created, it's set on this appointment
+ and you're brought back here (Vehicles::Hooks::AppointmentVehicleHookListener) %>
<% if User.current.allowed_to?(:add_vehicles, nil, global: true) %>
<% if appointment.customer.present? %>
<% unless appointment.vehicle.present? %>
- <%= link_to new_vehicle_path(customer_id: appointment.customer_id), class: 'icon icon-add' do %>
+ <%= link_to new_vehicle_path(customer_id: appointment.customer_id, appointment_id: appointment.id,
+ back_url: customer_appointment_path(appointment)), class: 'icon icon-add' do %>
<%= l(:label_add_vehicle) %>
<% end %>
-
+
<% end %>
<% end %>
-<% end %>
\ No newline at end of file
+<% end %>
diff --git a/app/views/vehicles/new.html.erb b/app/views/vehicles/new.html.erb
index c8eed15..729ed17 100644
--- a/app/views/vehicles/new.html.erb
+++ b/app/views/vehicles/new.html.erb
@@ -1,9 +1,12 @@
<%= link_to l(:field_vehicles), vehicles_path %> » <%= l(:label_new_vehicle) %>
<%= labelled_form_for @vehicle do |f| %>
+ <%# Where to go back to, and what the vehicle is being added for (see VehiclesController#create) %>
+ <%= back_url_hidden_field_tag %>
+ <%= hidden_field_tag 'appointment_id', params[:appointment_id] if params[:appointment_id].present? %>
<%= render partial: 'vehicles/form', locals: { f: f } %>
<%= submit_tag l(:button_create) %>
- <%= link_to l(:button_cancel), @vehicle.customer ? customer_path(@vehicle.customer) : vehicles_path %>
+ <%= link_to l(:button_cancel), validate_back_url(params[:back_url].to_s) || (@vehicle.customer ? customer_path(@vehicle.customer) : vehicles_path) %>
<% end %>
<% html_title(l(:label_new_vehicle)) -%>
diff --git a/config/locales/en.yml b/config/locales/en.yml
index 860ef4a..f527d01 100644
--- a/config/locales/en.yml
+++ b/config/locales/en.yml
@@ -43,6 +43,7 @@ en:
no_customer: "Customer no longer exists"
notice_decoding_vin: "Decoding VIN… this page will update automatically."
notice_vehicle_created: "Vehicle was successfully created."
+ notice_vehicle_added_to_appointment: "%{vehicle} was added to the customer and to this appointment."
notice_vehicle_deleted: "Vehicle was successfully deleted."
notice_vehicle_updated: "Vehicle was successfully updated."
diff --git a/lib/redmine_qbo_vehicles.rb b/lib/redmine_qbo_vehicles.rb
index 7c5e7d1..768074d 100644
--- a/lib/redmine_qbo_vehicles.rb
+++ b/lib/redmine_qbo_vehicles.rb
@@ -19,6 +19,7 @@ module RedmineQboVehicles
Vehicles::Hooks::PdfHookListener
Vehicles::Hooks::ViewHookListener
Vehicles::Hooks::CustomerAppointmentShowHookListener
+ Vehicles::Hooks::AppointmentVehicleHookListener
Vehicles::Hooks::BillingHookListener
# If the SubtaskFieldCopier plugin is installed, register our fields
diff --git a/lib/vehicles/hooks/appointment_vehicle_hook_listener.rb b/lib/vehicles/hooks/appointment_vehicle_hook_listener.rb
new file mode 100644
index 0000000..ed545ca
--- /dev/null
+++ b/lib/vehicles/hooks/appointment_vehicle_hook_listener.rb
@@ -0,0 +1,38 @@
+#The MIT License (MIT)
+#
+#Copyright (c) 2016 - 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 Vehicles
+ module Hooks
+
+ # A vehicle added from an appointment (redmine_qbo_calendar) that has none becomes the appointment's vehicle
+ class AppointmentVehicleHookListener < Redmine::Hook::Listener
+
+ def controller_vehicles_new_after_save(context = {})
+ appointment_id = context[:params] && context[:params][:appointment_id]
+ vehicle = context[:vehicle]
+ return if appointment_id.blank? || vehicle.nil? || !Redmine::Plugin.installed?(:redmine_qbo_calendar)
+
+ appointment = CustomerAppointment.find_by(id: appointment_id)
+ # Only the customer's own appointment, only when it has no vehicle yet, and only for someone who may edit it
+ return if appointment.nil? || appointment.vehicle_id.present? || appointment.customer_id != vehicle.customer_id
+ return unless User.current.allowed_to?(:edit_customer_appointments, nil, global: true)
+
+ # Just the vehicle: saving the whole appointment would re-check its date and hours
+ appointment.update_columns(vehicle_id: vehicle.id)
+ if (controller = context[:controller])
+ controller.flash[:notice] = I18n.t(:notice_vehicle_added_to_appointment, vehicle: vehicle.to_s)
+ end
+ Rails.logger.info "[Vehicles] Vehicle ##{vehicle.id} set on appointment ##{appointment.id}"
+ rescue StandardError => e
+ Rails.logger.warn "[Vehicles] Could not set vehicle ##{vehicle&.id} on appointment ##{appointment_id}: #{e.message}"
+ end
+ end
+ end
+end