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 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01BiwYMjhoQvL7XsKKfRz4pM
This commit is contained in:
Claude committed 2026-11-01 13:11:41 -05:00
1 parent fae397adac
commit 53991ed087
7 files changed
+57 -6

No files matched your search

+2 -1
View File
@@ -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
+5 -1
View File
@@ -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)
@@ -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? %>
<p>
<%= 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 %>
</p>
</p>
<% end %>
<% end %>
<% end %>
<% end %>
+4 -1
View File
@@ -1,9 +1,12 @@
<h2><%= link_to l(:field_vehicles), vehicles_path %> &#187; <%= l(:label_new_vehicle) %></h2>
<%= 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)) -%>
+1
View File
@@ -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."
+1
View File
@@ -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
@@ -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