mirror of
https://github.com/rickbarrette/redmine_qbo_vehicles.git
synced 2026-11-18 02:40:37 -05:00
Own the QuickBooks VIN and Mileage fields
redmine_qbo now handles QuickBooks custom fields generically; this plugin claims the fields named like VIN and Mileage (Vehicles::QboFields) and supplies their behaviour: - qbo_billing_issue_values keyed by field id; qbo_billing_field_options (the customer's vehicles for the VIN box) and qbo_billing_field_defaults (the work orders' VIN and highest mileage) replace the VIN hooks. - Editor script (view_qbo_estimate_form_bottom): VINs in capitals; importing time or estimates fills the VIN from the work orders' vehicle (or an estimate's own VIN), warns about several vehicles, and fills the highest mileage. The several-vehicle and missing-VIN-field texts moved here. - qbo_calendar_appointment_params: an appointment added from an estimate gets the vehicle with its VIN. - The estimate's VIN is read through redmine_qbo's generic custom_field_value. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01BiwYMjhoQvL7XsKKfRz4pM
This commit is contained in:
7 files changed
+150
-19
No files matched your search
@@ -18,9 +18,11 @@ The goal of this project is to enable vehicle tracking for customer vehicles wit
|
||||
|
||||
* **Asset Tracking:** Adds vehicles owned by customers to the system.
|
||||
* **Issue Association:** Allows these vehicles to be attached directly to Redmine issues for better service tracking.
|
||||
* **QuickBooks VIN and Mileage fields (redmine_qbo 2026.11.0+):** redmine_qbo shows every QuickBooks text custom field on its estimate/invoice editor; this plugin fills the one named like **VIN** and, if there is one, the one named like **Mileage** or **Odometer**. VINs are shown in capitals.
|
||||
* **Invoices (redmine_qbo 2026.11.0+):** 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.0+):** the VIN box suggests the customer's vehicles, 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.
|
||||
* **Estimates (redmine_qbo 2026.11.0+):** 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.
|
||||
|
||||
## Installation
|
||||
|
||||
|
||||
@@ -0,0 +1,51 @@
|
||||
<%#
|
||||
redmine_qbo's estimate/invoice editor: this plugin's part of the VIN and Mileage custom fields (see
|
||||
Vehicles::QboFields). Uses redmine_qbo's window.qboEditor and its qbo:imported event:
|
||||
- VINs are shown in capitals
|
||||
- importing time or estimates fills the VIN from the work orders' vehicle (or an estimate's own VIN when
|
||||
its work orders give none), warns when they're for more than one vehicle, and fills the highest mileage
|
||||
%>
|
||||
<% vin_field = Vehicles::QboFields.vin_field %>
|
||||
<% mileage_field = Vehicles::QboFields.mileage_field %>
|
||||
<%= javascript_tag do %>
|
||||
$(function () {
|
||||
var ed = window.qboEditor;
|
||||
if (!ed) return;
|
||||
var vinId = <%= raw (vin_field && vin_field.id.to_s).to_json %>;
|
||||
var mileageId = <%= raw (mileage_field && mileage_field.id.to_s).to_json %>;
|
||||
var $vin = vinId ? ed.field(vinId) : $();
|
||||
|
||||
$vin.css({ fontFamily: 'monospace', textTransform: 'uppercase', width: '220px' })
|
||||
.on('blur', function () { this.value = this.value.trim().toUpperCase(); });
|
||||
<% if vin_field.nil? && User.current.admin? && RedmineQboBilling::Company.settings.present? %>
|
||||
$('<em class="info">').text(<%= raw l(:text_no_vin_field).to_json %>).appendTo(ed.form.find('.qbo-estimate-details').first());
|
||||
<% end %>
|
||||
|
||||
var number = function (value) {
|
||||
var n = parseFloat(String(value || '').replace(/[^0-9.\-]/g, ''));
|
||||
return isNaN(n) ? null : n;
|
||||
};
|
||||
|
||||
ed.form.on('qbo:imported', function (event, data) {
|
||||
var values = data.issue_values || {}, vins = [], mileage = null;
|
||||
var addVin = function (vin) {
|
||||
vin = String(vin || '').trim().toUpperCase();
|
||||
if (vin && vins.indexOf(vin) < 0) vins.push(vin);
|
||||
};
|
||||
(data.issue_ids || []).forEach(function (id) {
|
||||
var v = values[String(id)] || {};
|
||||
if (vinId) addVin(v[vinId]);
|
||||
var miles = mileageId ? number(v[mileageId]) : null;
|
||||
if (miles !== null && (mileage === null || miles > mileage)) mileage = miles;
|
||||
});
|
||||
// An imported estimate whose work orders give no VIN adds its own
|
||||
(data.estimates || []).forEach(function (estimate) {
|
||||
var fromIssue = vinId && estimate.issue_id && (values[String(estimate.issue_id)] || {})[vinId];
|
||||
if (vinId && !fromIssue) addVin((estimate.custom_fields || {})[vinId]);
|
||||
});
|
||||
if (vins.length === 1 && $vin.length && !$vin.prop('disabled')) $vin.val(vins[0]);
|
||||
if (mileage !== null) ed.field(mileageId).val(String(mileage));
|
||||
if (vins.length > 1) window.alert(<%= raw l(:text_invoice_many_vehicles).to_json %> + ' ' + vins.join(', '));
|
||||
});
|
||||
});
|
||||
<% end %>
|
||||
@@ -34,6 +34,8 @@ en:
|
||||
label_search_vehicles: "Search Vehicles"
|
||||
label_unknown: "Unknown"
|
||||
label_vehicle: "Vehicle"
|
||||
text_invoice_many_vehicles: "These work orders are for more than one vehicle:"
|
||||
text_no_vin_field: "QuickBooks isn't sharing a text custom field named VIN, so VINs can't be filled in."
|
||||
text_estimate_vehicle_matched: "Matches the estimate's VIN (%{vin})."
|
||||
text_estimate_vehicle_unmatched: "None of the customer's vehicles has the estimate's VIN (%{vin}). Pick the vehicle for these issues, or add it to the customer first."
|
||||
text_estimate_vehicle_no_vin: "The estimate has no VIN. Pick the vehicle for these issues."
|
||||
|
||||
@@ -18,7 +18,7 @@ module Vehicles
|
||||
# estimate's details leave them out, so a VIN field added recently in QuickBooks would never match.
|
||||
def vin(estimate)
|
||||
return nil unless estimate
|
||||
RedmineQboBilling::EstimateWriter.vin_for(estimate.id)
|
||||
QboFields.estimate_vin(estimate.id)
|
||||
rescue StandardError
|
||||
nil
|
||||
end
|
||||
@@ -35,5 +35,12 @@ module Vehicles
|
||||
|
||||
vehicles(estimate).detect { |v| v.vin.to_s.strip.casecmp?(estimate_vin) }
|
||||
end
|
||||
|
||||
# A customer's vehicle with the given VIN, or nil
|
||||
def for_vin(customer, vin)
|
||||
return nil if vin.blank? || !customer.respond_to?(:vehicles)
|
||||
|
||||
customer.vehicles.detect { |v| v.vin.to_s.strip.casecmp?(vin.to_s.strip) }
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -14,32 +14,56 @@ module Vehicles
|
||||
# Hooks for redmine_qbo's estimates and invoices
|
||||
class BillingHookListener < Redmine::Hook::Listener
|
||||
|
||||
# Each work order's VIN and mileage, so the invoice's VIN and mileage boxes are filled when its time or
|
||||
# estimates are imported (redmine_qbo uses the highest mileage). Mileage comes from Redmine's issue
|
||||
# custom field named "Mileage" (any capitalisation).
|
||||
# Each work order's VIN (its vehicle) and mileage (Redmine's issue custom field named "Mileage"), keyed
|
||||
# by the QuickBooks VIN and Mileage fields. The editor script (vehicles/_qbo_editor_script) uses them
|
||||
# when time or estimates are imported; redmine_qbo_lineitems puts them on its estimates.
|
||||
def qbo_billing_issue_values(context = {})
|
||||
issues = Array(context[:issues])
|
||||
vins = issues.each_with_object({}) do |issue, by_issue|
|
||||
vin = issue.respond_to?(:vehicle) ? issue.vehicle&.vin : nil
|
||||
by_issue[issue.id] = vin if vin.present?
|
||||
values = {}
|
||||
if (vin = QboFields.vin_field)
|
||||
values[vin.id.to_s] = issues.each_with_object({}) do |issue, by_issue|
|
||||
value = issue.respond_to?(:vehicle) ? issue.vehicle&.vin : nil
|
||||
by_issue[issue.id] = value if value.present?
|
||||
end
|
||||
{ vin: vins, mileage: mileage_by_issue(issues) }
|
||||
end
|
||||
if (mileage = QboFields.mileage_field)
|
||||
values[mileage.id.to_s] = mileage_by_issue(issues)
|
||||
end
|
||||
values
|
||||
end
|
||||
|
||||
# The customer's vehicles as VIN suggestions for the estimate/invoice VIN box
|
||||
def qbo_billing_vin_options(context = {})
|
||||
# The customer's vehicles as suggestions for the VIN box
|
||||
def qbo_billing_field_options(context = {})
|
||||
customer = context[:customer]
|
||||
return [] unless customer.respond_to?(:vehicles)
|
||||
vin = QboFields.vin_field
|
||||
return {} unless vin && customer.respond_to?(:vehicles)
|
||||
|
||||
customer.vehicles.select { |v| v.vin.present? }.map { |v| { vin: v.vin, name: v.to_s } }
|
||||
{ vin.id.to_s => customer.vehicles.select { |v| v.vin.present? }.map { |v| { value: v.vin, label: v.to_s } } }
|
||||
end
|
||||
|
||||
# The VIN for a document's work orders (e.g. an invoice made from an estimate without a VIN): their
|
||||
# vehicle when they share one, nothing otherwise
|
||||
def qbo_billing_default_vin(context = {})
|
||||
vins = Array(context[:issues]).filter_map { |issue| issue.respond_to?(:vehicle) ? issue.vehicle&.vin.presence : nil }
|
||||
.map(&:upcase).uniq
|
||||
vins.first if vins.size == 1
|
||||
# Values for a document's work orders (e.g. an invoice made from an estimate that leaves them blank):
|
||||
# their vehicle's VIN when they share one, and the highest mileage
|
||||
def qbo_billing_field_defaults(context = {})
|
||||
issues = Array(context[:issues])
|
||||
defaults = {}
|
||||
if (vin = QboFields.vin_field)
|
||||
vins = issues.filter_map { |issue| issue.respond_to?(:vehicle) ? issue.vehicle&.vin.presence : nil }.map(&:upcase).uniq
|
||||
defaults[vin.id.to_s] = vins.first if vins.size == 1
|
||||
end
|
||||
if (mileage = QboFields.mileage_field)
|
||||
highest = mileage_by_issue(issues).values.filter_map { |m| m.to_s.delete(',').strip.presence&.to_f }.max
|
||||
defaults[mileage.id.to_s] = (highest % 1).zero? ? highest.to_i.to_s : highest.to_s if highest
|
||||
end
|
||||
defaults
|
||||
end
|
||||
|
||||
# redmine_qbo_calendar's Add appointment link on an estimate: the customer's vehicle with the estimate's VIN
|
||||
def qbo_calendar_appointment_params(context = {})
|
||||
vin = QboFields.vin_field
|
||||
return {} unless vin
|
||||
|
||||
vehicle = EstimateVehicle.for_vin(context[:customer], (context[:custom_fields] || {})[vin.id.to_s])
|
||||
vehicle ? { vehicle_id: vehicle.id } : {}
|
||||
end
|
||||
|
||||
# Issues created from an estimate get a vehicle: the one picked on the Create issues page (or passed in,
|
||||
|
||||
@@ -20,6 +20,11 @@ module Vehicles
|
||||
])
|
||||
end
|
||||
|
||||
# redmine_qbo's estimate/invoice editor: VIN and Mileage behaviour (vehicles/_qbo_editor_script)
|
||||
def view_qbo_estimate_form_bottom(context = {})
|
||||
context[:hook_caller].render(partial: 'vehicles/qbo_editor_script')
|
||||
end
|
||||
|
||||
# redmine_qbo's Create issues from estimate page: a Vehicle for all the issues, preselected from the
|
||||
# estimate's VIN
|
||||
def view_qbo_estimate_issues_form(context = {})
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
#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.
|
||||
|
||||
|
||||
module Vehicles
|
||||
# The QuickBooks custom fields this plugin fills on redmine_qbo's estimates and invoices, found by name:
|
||||
# a text field named like "VIN" and, optionally, one named like "Mileage" or "Odometer". redmine_qbo shows
|
||||
# every enabled custom field; this plugin supplies their values (hooks) and editor behaviour (script).
|
||||
module QboFields
|
||||
VIN_NAME = /\bvin\b/i
|
||||
MILEAGE_NAME = /mileage|odometer|\bmiles\b/i
|
||||
|
||||
module_function
|
||||
|
||||
def vin_field
|
||||
company.custom_field_matching(VIN_NAME)
|
||||
end
|
||||
|
||||
def mileage_field
|
||||
company.custom_fields.detect { |f| f.name.to_s =~ MILEAGE_NAME && f.name.to_s !~ VIN_NAME }
|
||||
end
|
||||
|
||||
# The VIN on an estimate (read by redmine_qbo with QuickBooks' newer custom fields), or nil
|
||||
def estimate_vin(estimate_id)
|
||||
field = vin_field
|
||||
field ? RedmineQboBilling::EstimateWriter.custom_field_value(estimate_id, field) : nil
|
||||
end
|
||||
|
||||
def company
|
||||
RedmineQboBilling::Company
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in new issue
Block a user