mirror of
https://github.com/rickbarrette/redmine_qbo_vehicles.git
synced 2026-11-17 18:40:36 -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,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
|
||||
end
|
||||
{ vin: vins, mileage: mileage_by_issue(issues) }
|
||||
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