Handle QuickBooks custom fields generically; no vehicle code left

redmine_qbo now works the same for any business; vehicle behaviour lives in redmine_qbo_vehicles.

- Every enabled QuickBooks text custom field gets a box on the estimate/invoice editor (the sales person
  is still filled in automatically). Forms keep custom_fields { field_id => value } instead of vin and
  mileage; Duplicate and Create invoice copy them all.
- Company: custom_field_matching(pattern) and editable_custom_fields; the VIN and Mileage name patterns
  are gone.
- Hooks are keyed by custom field id: qbo_billing_field_options (box suggestions) and
  qbo_billing_field_defaults (values for a document's work orders) replace the VIN hooks;
  qbo_billing_issue_values returns { field_id => { issue_id => value } }.
- Editor script API for plugins: window.qboEditor, the qbo:customer-changed and qbo:imported events, and
  the view_qbo_estimate_form_bottom hook to add a script. The VIN/mileage filling and the several-vehicle
  warning moved to redmine_qbo_vehicles.
- EstimateWriter.create_estimate(custom_fields:), custom_field_value and custom_field_values replace
  the vin/mileage arguments and vin_for.

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 11:34:10 -04:00
1 parent cb705d9525
commit 388ac915d7
14 files changed
+192 -164

No files matched your search

+4 -3
View File
@@ -106,8 +106,8 @@ class QboEstimatesController < ApplicationController
end
end
# 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)
# Customer details for the editor when a customer is picked: email, billing address and the suggestions
# plugins give for the custom field boxes (RedmineQboBilling.field_options)
def customer_info
customer = Customer.find_by(id: params[:customer_id])
return render(json: {}, status: :not_found) unless customer
@@ -117,7 +117,7 @@ class QboEstimatesController < ApplicationController
name: customer.name,
email: RedmineQboBilling::EstimateWriter.customer_email(customer),
billing_address: RedmineQboBilling::EstimateWriter.customer_address(customer),
vin_options: RedmineQboBilling.vin_options(customer)
field_options: RedmineQboBilling.field_options(customer)
}
end
@@ -205,6 +205,7 @@ class QboEstimatesController < ApplicationController
params.require(:estimate).permit(
:sync_token, :total_tax, :discount, :email_status, :txn_status,
*RedmineQboBilling::EstimateForm::HEADER_ATTRIBUTES,
custom_fields: {},
lines: RedmineQboBilling::EstimateForm::Line::PARAMS
)
end
@@ -94,6 +94,7 @@ class QboInvoicesController < ApplicationController
:sync_token, :total_tax, :discount, :email_status, :txn_status, :balance,
*RedmineQboBilling::EstimateForm::HEADER_ATTRIBUTES,
linked_estimate_ids: [],
custom_fields: {},
lines: RedmineQboBilling::EstimateForm::Line::PARAMS
)
end
+20 -26
View File
@@ -1,11 +1,11 @@
<%
company = RedmineQboBilling::Company
sales_field = company.sales_person_field
vin_field = company.vin_field
mileage_field = company.mileage_field
# A box for every other text custom field QuickBooks has enabled (e.g. VIN, Mileage, Crew #)
custom_fields = company.editable_custom_fields
customer = Customer.find_by(id: form.customer_id) if form.customer_id.present?
# VINs other plugins suggest for the VIN box (e.g. redmine_qbo_vehicles: the customer's vehicles)
vin_options = RedmineQboBilling.vin_options(customer)
# Suggestions other plugins give for those boxes (e.g. redmine_qbo_vehicles: the customer's vehicles' VINs)
field_options = RedmineQboBilling.field_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
@@ -117,28 +117,21 @@
<em class="info"><%= l(:text_no_sales_person_field) %></em>
<% end %>
</div>
<div class="qbo-field qbo-field-vin">
<label for="qbo_estimate_vin"><%= vin_field&.name || l(:label_vin) %></label>
<%= 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">
<% vin_options.each do |option| %>
<option value="<%= option[:vin] %>"><%= option[:name] %></option>
<% end %>
</datalist>
<% if settings_read && !vin_field %>
<em class="info"><%= l(:text_no_vin_field) %></em>
<% end %>
</div>
<%# Mileage only shows when QuickBooks has a text field named like "Mileage" %>
<% if mileage_field %>
<div class="qbo-field qbo-field-mileage">
<label for="qbo_estimate_mileage"><%= mileage_field.name %></label>
<%= text_field_tag 'estimate[mileage]', form.mileage, id: 'qbo_estimate_mileage', maxlength: 31,
inputmode: 'numeric', autocomplete: 'off' %>
<%# Plugins' editor scripts find a box by its field id or name (qboEditor.field / fieldNamed) %>
<% custom_fields.each do |field| %>
<div class="qbo-field qbo-field-custom" data-qbo-field-id="<%= field.id %>" data-qbo-field-name="<%= field.name %>">
<label for="qbo_custom_field_<%= field.id %>"><%= field.name %></label>
<%= text_field_tag "estimate[custom_fields][#{field.id}]", form.custom_field(field), id: "qbo_custom_field_#{field.id}",
list: "qbo-custom-field-options-#{field.id}", maxlength: 31, autocomplete: 'off', spellcheck: false,
class: 'qbo-custom-field' %>
<datalist id="qbo-custom-field-options-<%= field.id %>">
<% Array(field_options[field.id.to_s]).each do |option| %>
<option value="<%= option[:value] %>"><%= option[:label] %></option>
<% end %>
</datalist>
</div>
<% end %>
<% if settings_read && (sales_field.nil? || vin_field.nil?) && User.current.admin? %>
<% if settings_read && (sales_field.nil? || custom_fields.empty?) && User.current.admin? %>
<div class="qbo-field qbo-field-refresh">
<%= link_to sprite_icon('reload', l(:button_refresh_quickbooks_fields)), refresh_company_qbo_estimates_path,
method: :post, class: 'icon icon-reload' %>
@@ -197,8 +190,7 @@
<%# Import: billable QuickBooks time (replacing the lines ticked) and open estimates. Filled by the script. %>
<div class="qbo-import-panel" id="qbo-import-panel" hidden
data-loading-text="<%= l(:label_loading) %>" data-none-text="<%= l(:label_none) %>"
data-need-customer-text="<%= l(:error_estimate_needs_customer) %>"
data-many-vehicles-text="<%= l(:text_invoice_many_vehicles) %>">
data-need-customer-text="<%= l(:error_estimate_needs_customer) %>">
<div class="qbo-import-columns">
<section class="qbo-import-time">
<h4><%= l(:label_billable_time) %></h4>
@@ -303,3 +295,5 @@
<%= render partial: 'qbo_estimates/styles' %>
<%= render partial: 'qbo_estimates/script' %>
<%# Plugins add their editor scripts here (see window.qboEditor and the qbo:* events in qbo_estimates/_script) %>
<%= call_hook(:view_qbo_estimate_form_bottom, { form: form, document: form.document }) %>
+35 -37
View File
@@ -4,6 +4,32 @@ $(function () {
var $form = $('#qbo-estimate-form');
if (!$form.length) return;
// For other plugins' editor scripts (added through the view_qbo_estimate_form_bottom hook). The form also
// triggers these events (listen with $(document).on('qbo:imported', '#qbo-estimate-form', fn)):
// qbo:customer-changed (event, { customer_id, info }) info includes field_options
// qbo:imported (event, { issue_ids, estimates, issue_values }) after time/estimates are
// imported into an invoice; issue_values is { issue_id => { field_id => value } }
// and each estimate has its own custom_fields { field_id => value }
window.qboEditor = {
form: $form,
document: $form.data('document'),
// A QuickBooks custom field's box, by field id or by a name pattern (RegExp)
field: function (id) { return $('#qbo_custom_field_' + id); },
fieldNamed: function (pattern) {
var $box = $();
$form.find('.qbo-field-custom').each(function () {
if (!$box.length && pattern.test(String($(this).data('qboFieldName')))) $box = $(this).find('input.qbo-custom-field');
});
return $box;
},
fieldId: function ($box) { return $box.closest('.qbo-field-custom').data('qboFieldId'); },
// Replaces a box's suggestion list: [{ value, label }]
setFieldOptions: function (id, options) {
var $list = $('#qbo-custom-field-options-' + id).empty();
(options || []).forEach(function (o) { $('<option>').attr('value', o.value).text(o.label || o.value).appendTo($list); });
}
};
var $tbody = $('#qbo-estimate-lines tbody');
var template = document.getElementById('qbo-estimate-line-template').innerHTML;
var subtotalTemplate = document.getElementById('qbo-estimate-subtotal-template').innerHTML;
@@ -270,7 +296,6 @@ $(function () {
// ---- customer ----
var $email = $('#qbo_estimate_email');
var $vin = $('#qbo_estimate_vin');
var loadedEmail = $email.val();
$('#customer_id').on('change', function () {
@@ -283,12 +308,13 @@ $(function () {
loadedEmail = info.email || '';
$('#qbo-estimate-address').text(info.billing_address || '');
// VINs plugins suggest for this customer (e.g. their vehicles), offered in the VIN box's list; the box
// itself stays as it is (a new document starts blank)
var $list = $('#qbo-estimate-vins').empty();
(info.vin_options || []).forEach(function (option) {
$('<option>').attr('value', option.vin).text(option.name).appendTo($list);
// Plugins' suggestions for this customer go in the custom field boxes' lists; the boxes themselves
// stay as they are (a new document starts blank)
$form.find('.qbo-field-custom').each(function () {
var fieldId = $(this).data('qboFieldId');
window.qboEditor.setFieldOptions(fieldId, (info.field_options || {})[String(fieldId)]);
});
$form.trigger('qbo:customer-changed', [{ customer_id: id, info: info }]);
});
});
@@ -297,8 +323,6 @@ $(function () {
if (!this.value.trim()) $('#customer_id').val('');
});
$vin.on('blur', function () { this.value = this.value.trim().toUpperCase(); });
// New estimates expire a set number of days after the estimate date (plugin setting)
var expirationDays = parseInt($form.data('expirationDays'), 10) || 0;
var $txnDate = $('#qbo_estimate_txn_date');
@@ -380,33 +404,6 @@ $(function () {
return ids;
};
// 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. Imported estimates whose work orders give no VIN
// add their own.
var updateVehicleFields = function (issueIds, importedEstimates) {
var values = (importData && importData.issue_values) || {};
var vins = [], mileage = null;
var addVin = function (vin) {
vin = (vin || '').trim().toUpperCase();
if (vin && vins.indexOf(vin) < 0) vins.push(vin);
};
issueIds.forEach(function (id) {
var v = values[id] || {};
addVin(v.vin);
var miles = parseNumber(v.mileage);
if (miles !== null && !isNaN(miles) && (mileage === null || miles > mileage)) mileage = miles;
});
(importedEstimates || []).forEach(function (estimate) {
var fromIssue = estimate.issue_id && (values[String(estimate.issue_id)] || {}).vin;
if (!fromIssue) addVin(estimate.vin);
});
var $vinBox = $('#qbo_estimate_vin');
if (vins.length === 1 && $vinBox.length && !$vinBox.prop('disabled')) $vinBox.val(vins[0]);
var $mileageBox = $('#qbo_estimate_mileage');
if (mileage !== null && $mileageBox.length) $mileageBox.val(String(mileage));
return vins;
};
// Rows go after the last filled line, above the spare empty rows
var insertAfterFilled = function ($rows) {
var $filled = rows().filter(function () { return !rowIsBlank($(this)); });
@@ -546,9 +543,10 @@ $(function () {
estimates.forEach(function (estimate) {
if (estimate.issue_id && issueIds.indexOf(String(estimate.issue_id)) < 0) issueIds.push(String(estimate.issue_id));
});
var vins = updateVehicleFields(issueIds, estimates);
$panel.prop('hidden', true);
if (vins.length > 1) window.alert($panel.data('manyVehiclesText') + ' ' + vins.join(', '));
// Plugins fill their custom fields from the work orders (e.g. redmine_qbo_vehicles: VIN and mileage)
$form.trigger('qbo:imported', [{ issue_ids: issueIds, estimates: estimates,
issue_values: (importData && importData.issue_values) || {} }]);
$('#qbo-import-undo').prop('hidden', false);
});
+1 -2
View File
@@ -24,8 +24,7 @@
.qbo-field-date input { width: 150px; }
.qbo-field-number input { width: 160px; }
.qbo-field-sales-person input { width: 200px; }
.qbo-field-vin input { width: 220px; font-family: monospace; text-transform: uppercase; }
.qbo-field-mileage input { width: 120px; }
.qbo-field-custom input { width: 200px; }
.qbo-field-refresh { align-self: flex-end; padding-bottom: 6px; max-width: 420px; }
.qbo-address { min-width: 220px; min-height: 4.5em; white-space: pre-line; color: #393a3d; padding: 6px 0; line-height: 1.4; }
.qbo-estimate-amount { text-align: right; flex-shrink: 0; }
+1 -1
View File
@@ -2,7 +2,7 @@
<div class="contextual">
<%# Plugins add estimate actions here (e.g. redmine_qbo_calendar's Add appointment) %>
<%= call_hook :view_qbo_estimate_contextual, { estimate_id: @form.id, customer_id: @form.customer_id, vin: @form.vin, doc_number: @form.doc_number } %>
<%= call_hook :view_qbo_estimate_contextual, { estimate_id: @form.id, customer_id: @form.customer_id, custom_fields: @form.custom_fields, doc_number: @form.doc_number } %>
<%# An estimate is billed once: no Create invoice once it's on an invoice, closed or rejected %>
<% if RedmineQboBilling.invoice_writer? && @form.locked != :billed && @form.txn_status.to_s != 'Rejected' %>
<%= link_to sprite_icon('document', l(:button_create_invoice)), new_qbo_invoice_path(estimate_id: @form.id), class: 'icon icon-document' %>