Move vehicle code out to redmine_qbo_vehicles

- IssueMileageListener removed: redmine_qbo_vehicles answers qbo_billing_issue_values with mileage.
- New hook qbo_billing_build_issue sets plugins' fields on issues created from an estimate; the vehicle
  lookup by VIN moved to redmine_qbo_vehicles.
- New hook qbo_billing_vin_options gives the VIN box's suggestions (RedmineQboBilling.vin_options)
  instead of reading customer.vehicles.
- The customer page's New issue link script is generic: any checkbox with
  data-new-issue-param="issue[field]" fills that field.

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-10-31 09:12:52 -04:00
1 parent 3304fce92f
commit 06a78ebccb
11 files changed
+78 -115

No files matched your search

+3 -3
View File
@@ -106,18 +106,18 @@ class QboEstimatesController < ApplicationController
end
end
# Customer details for the editor when a customer is picked: email, billing address and vehicles (VINs)
# Customer details for the editor when a customer is picked: email, billing address and the VINs plugins
# suggest for the VIN box (RedmineQboBilling.vin_options)
def customer_info
customer = Customer.find_by(id: params[:customer_id])
return render(json: {}, status: :not_found) unless customer
vehicles = customer.respond_to?(:vehicles) ? customer.vehicles.to_a : []
render json: {
id: customer.id,
name: customer.name,
email: RedmineQboBilling::EstimateWriter.customer_email(customer),
billing_address: RedmineQboBilling::EstimateWriter.customer_address(customer),
vehicles: vehicles.select { |v| v.vin.present? }.map { |v| { vin: v.vin, name: v.to_s } }
vin_options: RedmineQboBilling.vin_options(customer)
}
end
@@ -1,59 +1,45 @@
<%#
Adds the estimate and vehicle ticked on the customer page to the "New issue" link
(#dynamic-new-issue-link in the contextual area). One of each can be ticked.
Adds what's ticked on the customer page to the "New issue" link (#dynamic-new-issue-link in the contextual
area). Any checkbox with data-new-issue-param="issue[field]" takes part, one ticked per field: this plugin's
estimates (issue[estimate_id]), and other plugins' lists (e.g. redmine_qbo_vehicles' issue[vehicle_id]).
%>
<script>
function handleSingleSelect(event, className) {
if (event.target.checked) {
// Uncheck all other checkboxes of the same type
document.querySelectorAll('.' + className).forEach(cb => {
if (cb !== event.target) {
cb.checked = false;
}
function newIssueParamBoxes(param) {
return Array.prototype.filter.call(document.querySelectorAll('input[data-new-issue-param]'), function (cb) {
return !param || cb.getAttribute('data-new-issue-param') === param;
});
}
// One box per field: ticking one unticks the others for the same field
document.addEventListener('change', function (event) {
var box = event.target;
if (!box.matches || !box.matches('input[data-new-issue-param]')) return;
if (box.checked) {
newIssueParamBoxes(box.getAttribute('data-new-issue-param')).forEach(function (cb) {
if (cb !== box) cb.checked = false;
});
}
updateLink();
}
// Bind single-select behavior to checkboxes once the DOM is loaded
document.addEventListener('DOMContentLoaded', function() {
document.querySelectorAll('.estimate-checkbox').forEach(cb => {
cb.addEventListener('change', (e) => handleSingleSelect(e, 'estimate-checkbox'));
});
document.querySelectorAll('.vehicle-checkbox').forEach(cb => {
cb.addEventListener('change', (e) => handleSingleSelect(e, 'vehicle-checkbox'));
});
});
function updateLink() {
const link = document.getElementById('dynamic-new-issue-link');
var link = document.getElementById('dynamic-new-issue-link');
if (!link) return;
// Cache the pristine base URL on first run
if (!link.dataset.baseUrl) {
link.dataset.baseUrl = link.getAttribute('href');
}
if (!link.dataset.baseUrl) link.dataset.baseUrl = link.getAttribute('href');
var url = new URL(link.dataset.baseUrl, window.location.origin);
// Build base URL object
let url = new URL(link.dataset.baseUrl, window.location.origin);
newIssueParamBoxes().forEach(function (cb) {
var param = cb.getAttribute('data-new-issue-param');
var ticked = newIssueParamBoxes(param).filter(function (b) { return b.checked; })[0];
if (ticked) {
url.searchParams.set(param, ticked.value);
} else {
url.searchParams.delete(param);
}
});
// 1. Handle Single Estimate
const checkedEstimate = document.querySelector('.estimate-checkbox:checked');
if (checkedEstimate) {
url.searchParams.set('issue[estimate_id]', checkedEstimate.value);
} else {
url.searchParams.delete('issue[estimate_id]');
}
// 2. Handle Single Vehicle
const checkedVehicle = document.querySelector('.vehicle-checkbox:checked');
if (checkedVehicle) {
url.searchParams.set('issue[vehicle_id]', checkedVehicle.value);
} else {
url.searchParams.delete('issue[vehicle_id]');
}
// Update the link's href attribute
link.setAttribute('href', url.toString());
}
</script>
+1 -1
View File
@@ -1,5 +1,5 @@
<div class="contextual">
<%# New issue for this customer; picks up the estimate / vehicle ticked on this page %>
<%# New issue for this customer; picks up what's ticked on this page (an estimate, other plugins' items) %>
<%= link_to sprite_icon('add', l(:label_new_issue)), new_issue_path(issue: { customer_id: @customer.id }),
id: 'dynamic-new-issue-link', class: 'icon icon-add', target: :_blank %>
<%# Plugins add their customer actions here (e.g. redmine_qbo_calendar's Add appointment) %>
+1 -1
View File
@@ -2,7 +2,7 @@
<% estimates.sort.reverse.each do |estimate| %>
<div class="row">
<%= check_box_tag "estimate_ids[]", estimate.id, false, onchange: "updateLink()", data: { text: "Estimate ##{estimate.doc_number}" }, class: "estimate-checkbox appointment" %>
<%= check_box_tag "estimate_ids[]", estimate.id, false, onchange: "updateLink()", data: { text: "Estimate ##{estimate.doc_number}", new_issue_param: 'issue[estimate_id]' }, class: "estimate-checkbox appointment" %>
<b><%= link_to "##{estimate.doc_number}", estimate_path(estimate), target: :_blank %></b> <%= estimate.txn_date %>
<%= render partial: 'qbo_estimates/estimate_actions', locals: { estimate: estimate } if RedmineQboBilling.writer? %>
</div>
+4 -3
View File
@@ -4,7 +4,8 @@
vin_field = company.vin_field
mileage_field = company.mileage_field
customer = Customer.find_by(id: form.customer_id) if form.customer_id.present?
vehicles = customer.respond_to?(:vehicles) ? customer.vehicles.select { |v| v.vin.present? } : []
# VINs other plugins suggest for the VIN box (e.g. redmine_qbo_vehicles: the customer's vehicles)
vin_options = RedmineQboBilling.vin_options(customer)
# When QuickBooks' settings couldn't be read at all, the page header says so instead of each field
settings_read = company.settings.present?
# The same editor writes estimates and invoices
@@ -121,8 +122,8 @@
<%= text_field_tag 'estimate[vin]', form.vin, id: 'qbo_estimate_vin', list: 'qbo-estimate-vins', maxlength: 31,
autocomplete: 'off', spellcheck: false, disabled: vin_field.nil? %>
<datalist id="qbo-estimate-vins">
<% vehicles.each do |vehicle| %>
<option value="<%= vehicle.vin %>"><%= vehicle.to_s %></option>
<% vin_options.each do |option| %>
<option value="<%= option[:vin] %>"><%= option[:name] %></option>
<% end %>
</datalist>
<% if settings_read && !vin_field %>
+7 -5
View File
@@ -283,12 +283,14 @@ $(function () {
loadedEmail = info.email || '';
$('#qbo-estimate-address').text(info.billing_address || '');
// VINs plugins suggest for this customer (e.g. their vehicles); one fills an empty VIN box
var options = info.vin_options || [];
var $list = $('#qbo-estimate-vins').empty();
(info.vehicles || []).forEach(function (vehicle) {
$('<option>').attr('value', vehicle.vin).text(vehicle.name).appendTo($list);
options.forEach(function (option) {
$('<option>').attr('value', option.vin).text(option.name).appendTo($list);
});
if ($vin.length && !$vin.prop('disabled') && !$vin.val().trim() && (info.vehicles || []).length === 1) {
$vin.val(info.vehicles[0].vin);
if ($vin.length && !$vin.prop('disabled') && !$vin.val().trim() && options.length === 1) {
$vin.val(options[0].vin);
}
});
});
@@ -381,7 +383,7 @@ $(function () {
return ids;
};
// VIN and mileage from the work orders (via plugins such as redmine_qbo_vehicles and redmine_mileage_cp):
// VIN and mileage from the work orders (via plugins such as redmine_qbo_vehicles):
// one vehicle fills the VIN box; the highest mileage fills the mileage box
var updateVehicleFields = function (issueIds) {
var values = (importData && importData.issue_values) || {};