Take over the vehicle parts of redmine_qbo's billing

- Mileage for invoices (the Mileage issue custom field) now comes from this plugin's
  qbo_billing_issue_values answer, alongside the VIN.
- qbo_billing_vin_options: the customer's vehicles as VIN suggestions in the estimate/invoice editor.
- qbo_billing_build_issue: issues created from an estimate get the appointment's vehicle, or the
  customer's vehicle whose VIN is on the estimate.
- Vehicle checkboxes on the customer page say which New issue field they fill (data-new-issue-param).

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 c8bd7b4290
commit 7ea286b288
3 files changed
+56 -5

No files matched your search

+3 -1
View File
@@ -18,7 +18,9 @@ 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.
* **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 an invoice whose work orders are for more than one vehicle isn't saved (one vehicle per invoice).
* **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 (one vehicle fills it in), and issues created from an estimate get the appointment's vehicle, or else the customer's vehicle whose VIN is on the estimate.
* **Customer page:** ticking a vehicle adds it to the New issue link.
## Installation
+1 -1
View File
@@ -6,7 +6,7 @@
<div class="container">
<% if show_checkbox %>
<%= check_box_tag "vehicle_ids[]", vehicle.id, false, onchange: "updateLink()", data: { url: vehicle_path(vehicle).html_safe, text: vehicle.to_s }, class: "vehicle-checkbox appointment checkbox" %>
<%= check_box_tag "vehicle_ids[]", vehicle.id, false, onchange: "updateLink()", data: { url: vehicle_path(vehicle).html_safe, text: vehicle.to_s, new_issue_param: 'issue[vehicle_id]' }, class: "vehicle-checkbox appointment checkbox" %>
<% else %>
<div class='checkbox'>
</div>
+52 -3
View File
@@ -14,13 +14,34 @@ module Vehicles
# Hooks for redmine_qbo's estimates and invoices
class BillingHookListener < Redmine::Hook::Listener
# Each work order's VIN, so the invoice's VIN box is filled when its time or estimates are imported
# 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).
def qbo_billing_issue_values(context = {})
vins = Array(context[:issues]).each_with_object({}) do |issue, by_issue|
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?
end
{ vin: vins }
{ vin: vins, mileage: mileage_by_issue(issues) }
end
# The customer's vehicles as VIN suggestions for the estimate/invoice VIN box
def qbo_billing_vin_options(context = {})
customer = context[:customer]
return [] unless customer.respond_to?(:vehicles)
customer.vehicles.select { |v| v.vin.present? }.map { |v| { vin: v.vin, name: v.to_s } }
end
# Issues created from an estimate get a vehicle: the one passed in (e.g. redmine_qbo_calendar's
# appointment vehicle), or the customer's vehicle whose VIN is on the estimate
def qbo_billing_build_issue(context = {})
issue = context[:issue]
return unless issue.respond_to?(:vehicle_id=)
vehicle_id = context.dig(:defaults, :vehicle_id).presence || vehicle_id_from_vin(context[:estimate])
issue.vehicle_id = vehicle_id if vehicle_id
end
# One vehicle per invoice (OSOR SOP): an invoice whose work orders are for more than one vehicle
@@ -38,6 +59,34 @@ module Vehicles
I18n.t(:error_one_vehicle_per_invoice, vehicles: details.join('; '))
end
private
# The customer's vehicle whose VIN is on the estimate's QuickBooks VIN field, or nil
def vehicle_id_from_vin(estimate)
customer = estimate&.customer
return nil unless customer.respond_to?(:vehicles)
company = RedmineQboBilling::Company
vin = RedmineQboBilling::EstimateWriter.custom_value(estimate.details, company.vin_field, company::VIN_NAME).to_s.strip
return nil if vin.empty?
customer.vehicles.detect { |v| v.vin.to_s.casecmp?(vin) }&.id
rescue StandardError
nil
end
# { issue_id => mileage } from the issue custom field named "Mileage"; empty without that field
def mileage_by_issue(issues)
issue_ids = issues.map(&:id)
field = IssueCustomField.all.detect { |f| f.name.to_s.strip.casecmp?('mileage') }
return {} if field.nil? || issue_ids.empty?
CustomValue.where(customized_type: 'Issue', customized_id: issue_ids, custom_field_id: field.id)
.where.not(value: [nil, ''])
.pluck(:customized_id, :value)
.to_h
end
end
end
end